Skip to content

Commit

Permalink
add logic run and restructure folders
Browse files Browse the repository at this point in the history
  • Loading branch information
nkitlabs committed Oct 14, 2024
1 parent c824ff7 commit b58a5b7
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 52 deletions.
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"runtime/debug"
"strings"
"syscall"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -55,7 +56,8 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {

rootCmd.PersistentPostRun = func(cmd *cobra.Command, _ []string) {
// Force syncing the logs before exit, if anything is buffered.
if err := app.Log.Sync(); err != nil {
// check error of log.Sync() https://github.com/uber-go/zap/issues/991#issuecomment-962098428
if err := app.Log.Sync(); err != nil && !errors.Is(err, syscall.ENOTTY) {
fmt.Fprintf(os.Stderr, "failed to sync logs: %v\n", err)
}
}
Expand Down Expand Up @@ -101,7 +103,7 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
func Execute() error {
cobra.EnableCommandSorting = false

rootCmd := NewRootCmd(nil)
Expand Down Expand Up @@ -143,9 +145,7 @@ func Execute() {
}
}()

if err := rootCmd.ExecuteContext(ctx); err != nil {
panic(err)
}
return rootCmd.ExecuteContext(ctx)
}

// lineBreakCommand returns a new instance of the lineBreakCommand every time to avoid
Expand Down
15 changes: 13 additions & 2 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand All @@ -20,8 +21,18 @@ func startCmd(app *falcon.App) *cobra.Command {
$ %s start # start relaying data from every tunnel being registered on source chain.
$ %s start 1 12 # start relaying data from specific tunnelIDs.`, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
_ = app
return nil
tunnelIDs := []uint64{}
for _, arg := range args {
tunnelID, err := strconv.ParseUint(arg, 10, 64)
if err != nil {
return err
}

tunnelIDs = append(tunnelIDs, tunnelID)
}

// TODO: add context to the function so that it
return app.Start(cmd.Context(), tunnelIDs)
},
}

Expand Down
43 changes: 43 additions & 0 deletions falcon/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import (

"github.com/spf13/viper"
"go.uber.org/zap"

"github.com/bandprotocol/falcon/falcon/band"
bandtypes "github.com/bandprotocol/falcon/falcon/band/types"
"github.com/bandprotocol/falcon/falcon/chains"
)

// App is the main application struct.
type App struct {
Log *zap.Logger
Viper *viper.Viper
Expand Down Expand Up @@ -54,3 +59,41 @@ func (a *App) InitLogger(configLogLevel string) error {
func (a *App) LoadConfigFile(ctx context.Context) error {
return nil
}

// InitConfigFile initializes the configuration to the given path.
func (a *App) InitConfigFile(homePath string) error {
return nil
}

// Start starts the tunnel relayer program.
func (a *App) Start(ctx context.Context, tunnelIDs []uint64) error {
// initialize band client
bandClient := band.NewClient(a.Log, a.Config.BandChainConfig.RpcEndpoints)

// TODO: initialize target chain clients
chainClients := make(map[string]chains.Client)

// TODO: load the tunnel information from the bandchain.
// If len(tunnelIDs == 0), load all tunnels info.
tunnels := []*bandtypes.Tunnel{}

// initialize the tunnel relayer
tunnelRelayers := []TunnelRelayer{}
for _, tunnel := range tunnels {
chainClient := chainClients[tunnel.TargetChainID]

tr := NewTunnelRelayer(
a.Log,
tunnel.ID,
"",
a.Config.CheckingPacketInterval,
bandClient,
chainClient,
)
tunnelRelayers = append(tunnelRelayers, tr)
}

// start the tunnel relayers
scheduler := NewScheduler(a.Log, tunnelRelayers)
return scheduler.Start(ctx)
}
15 changes: 12 additions & 3 deletions falcon/band/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package band

import "github.com/bandprotocol/falcon/falcon/band/types"
import (
"go.uber.org/zap"

"github.com/bandprotocol/falcon/falcon/band/types"
)

var _ Client = &client{}

Expand All @@ -16,13 +20,18 @@ type Client interface {
GetSigning(signingID uint64) (*types.Signing, error)
}

// client is the BandChain client struct.
type client struct {
Log *zap.Logger
RpcEndpoints []string
}

// NewClient creates a new BandChain client instance.
func NewClient(rpcEndpoints []string) Client {
return &client{RpcEndpoints: rpcEndpoints}
func NewClient(log *zap.Logger, rpcEndpoints []string) Client {
return &client{
Log: log,
RpcEndpoints: rpcEndpoints,
}
}

func (c *client) GetTunnelPacket(tunnelID uint64, sequence uint64) (*types.Packet, error) {
Expand Down
9 changes: 9 additions & 0 deletions falcon/chains/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
package chains

// ChainType defines the type of the target chain.
type ChainType int

const (
ChainTypeUndefined ChainType = iota
ChainTypeEVM
Cosmwasm
)

// Config defines the common configuration for the target chain client.
type Config struct{}
19 changes: 13 additions & 6 deletions falcon/chains/evm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ package evm
import (
"math/big"

"go.uber.org/zap"

"github.com/bandprotocol/falcon/falcon/chains"
"github.com/bandprotocol/falcon/falcon/keys"
)

var _ chains.Client = &Client{}

// Client is the struct that handles interactions with the EVM chain.
type Client struct {
config *Config
keys []keys.Key
Log *zap.Logger
Config *Config
KeyStore *KeyStore
Keys []Key
}

func NewClient(cfg *Config, relayers []keys.Key) *Client {
// NewClient creates a new EVM client from config file and load keys.
func NewClient(log *zap.Logger, cfgPath string) *Client {
// TODO: implement this
_ = cfgPath

return &Client{
config: cfg,
keys: relayers,
Log: log,
}
}

Expand Down
50 changes: 50 additions & 0 deletions falcon/chains/evm/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package evm

import "go.uber.org/zap"

// Key represents a key in the keystore.
type Key struct {
Name string
ChainID string
PrivKey string
PubKey string
}

// NewKey creates a new key instance.
func NewKey(name, chainID, privKey, pubKey string) Key {
return Key{
Name: name,
ChainID: chainID,
PrivKey: privKey,
PubKey: pubKey,
}
}

// KeyStore stores information for interacting with the key store.
type KeyStore struct {
Log *zap.Logger
Path string
}

// NewKeyStore creates a new key store instance.
func NewKeyStore(log *zap.Logger, cfgPath string) KeyStore {
return KeyStore{
Log: log,
Path: cfgPath,
}
}

// SaveKeys saves keys to the store.
func (ks *KeyStore) SaveKeys(keys []Key, cfgPath string) error {
return nil
}

// GetKeys gets keys from the store.
func (ks *KeyStore) GetKeys() ([]Key, error) {
return nil, nil
}

// GetKey gets a key by name from the store.
func (ks *KeyStore) GetKey(name string) (Key, error) {
return Key{}, nil
}
21 changes: 20 additions & 1 deletion falcon/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
package falcon

type Config struct{}
import (
"time"

"github.com/bandprotocol/falcon/falcon/band"
"github.com/bandprotocol/falcon/falcon/chains"
)

// TargetChainConfig is the metadata of the configured target chain.
type TargetChainConfig struct {
Name string `toml:"id"`
Type chains.ChainType `toml:"type"`
ConfigPath string `toml:"path"`
}

// Config defines the configuration for the falcon tunnel relayer.
type Config struct {
BandChainConfig band.Config `toml:"bandchain"`
TargetChains []TargetChainConfig `toml:"target_chains"`
CheckingPacketInterval time.Duration `toml:"checking_packet_interval"`
}
19 changes: 0 additions & 19 deletions falcon/keys/key.go

This file was deleted.

16 changes: 10 additions & 6 deletions falcon/scheduler.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package falcon

import (
"context"

"go.uber.org/zap"
)

// Scheduler is a struct to manage all tunnel relayers
type Scheduler struct {
Log *zap.Logger
TunnelRelayers []TunnelRelayer
}

// NewScheduler creates a new Scheduler
func NewScheduler(
log *zap.Logger,
tunnelRelayers []TunnelRelayer,
) *Scheduler {
return &Scheduler{
Log: log,
TunnelRelayers: tunnelRelayers,
}
}

// Start starts all tunnel relayers
func (s *Scheduler) Start() error {
return nil
}

// Stop stops all tunnel relayers
func (s *Scheduler) Stop() error {
func (s *Scheduler) Start(ctx context.Context) error {
return nil
}
19 changes: 11 additions & 8 deletions falcon/tunnel_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,37 @@ package falcon
import (
"time"

"go.uber.org/zap"

"github.com/bandprotocol/falcon/falcon/band"
"github.com/bandprotocol/falcon/falcon/chains"
)

// TunnelRelayer is a relayer that listens to the tunnel and relays the packet
type TunnelRelayer struct {
Log *zap.Logger
TunnelID uint64
ContractAddress string
CheckingPacketInterval time.Duration

BandClient band.Client
TargetChainClient chains.Client
BandClient band.Client
TargetChainClient chains.Client
}

// NewTunnelRelayer creates a new TunnelRelayer
func NewTunnelRelayer(
log *zap.Logger,
tunnelID uint64,
contractAddress string,
checkingPacketInterval time.Duration,
bandClient band.Client,
targetChainClient chains.Client,
) *TunnelRelayer {
return &TunnelRelayer{
) TunnelRelayer {
return TunnelRelayer{
Log: log,
TunnelID: tunnelID,
ContractAddress: contractAddress,
CheckingPacketInterval: checkingPacketInterval,

BandClient: bandClient,
TargetChainClient: targetChainClient,
BandClient: bandClient,
TargetChainClient: targetChainClient,
}
}
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package main

import "github.com/bandprotocol/falcon/cmd"
import (
"os"

"github.com/bandprotocol/falcon/cmd"
)

func main() {
cmd.Execute()
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}

0 comments on commit b58a5b7

Please sign in to comment.