Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Phase 1]: Tunnel offchain relayer #1

Merged
merged 20 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
falcon structure
  • Loading branch information
nkitlabs committed Oct 14, 2024
commit c824ff776b15512c3c759ecbda9ae5daeb81451d
38 changes: 38 additions & 0 deletions falcon/band/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package band

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

var _ Client = &client{}

// Client is the interface to interact with the BandChain.
type Client interface {
// GetTunnelPacket returns the packet with the given tunnelID and sequence.
GetTunnelPacket(tunnelID uint64, sequence uint64) (*types.Packet, error)

// GetTunnel returns the tunnel with the given tunnelID.
GetTunnel(tunnelID uint64) (*types.Tunnel, error)

// GetSigning returns the signing with the given signingID.
GetSigning(signingID uint64) (*types.Signing, error)
}

type client struct {
RpcEndpoints []string
}

// NewClient creates a new BandChain client instance.
func NewClient(rpcEndpoints []string) Client {
return &client{RpcEndpoints: rpcEndpoints}
}

func (c *client) GetTunnelPacket(tunnelID uint64, sequence uint64) (*types.Packet, error) {
return nil, nil
}

func (c *client) GetTunnel(tunnelID uint64) (*types.Tunnel, error) {
return nil, nil
}

func (c *client) GetSigning(signingID uint64) (*types.Signing, error) {
return nil, nil
}
7 changes: 7 additions & 0 deletions falcon/band/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package band

// Config defines the configuration for the BandChain client.
type Config struct {
RpcEndpoints []string `toml:"rpc_endpoints"`
Timeout int `toml:"timeout"`
}
21 changes: 21 additions & 0 deletions falcon/band/types/packet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package types

// Packet stores an information of the packet that is generated from the tunnel.
type Packet struct {
TunnelID uint64
Sequence uint64
SigningID uint64
}

// NewPacket creates a new packet instance.
func NewPacket(
tunnelID uint64,
sequence uint64,
signingID uint64,
) *Packet {
return &Packet{
TunnelID: tunnelID,
Sequence: sequence,
SigningID: signingID,
}
}
46 changes: 46 additions & 0 deletions falcon/band/types/signing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package types

import (
"time"

tmbytes "github.com/cometbft/cometbft/libs/bytes"
)

// EVMSignature defines a signature in the EVM format.
type EVMSignature struct {
RAddress tmbytes.HexBytes
Signature tmbytes.HexBytes
}

// NewEVMSignature creates a new EVMSignature instance.
func NewEVMSignature(
rAddress tmbytes.HexBytes,
signature tmbytes.HexBytes,
) *EVMSignature {
return &EVMSignature{
RAddress: rAddress,
Signature: signature,
}
}

// Signing contains information of a requested message and group signature.
type Signing struct {
ID uint64
Message tmbytes.HexBytes
Signature []byte
EVMSignature *EVMSignature
CreatedAt time.Time
}

// NewSigning creates a new Signing instance.
func NewSigning(
id uint64,
message tmbytes.HexBytes,
evmSignature *EVMSignature,
) *Signing {
return &Signing{
ID: id,
Message: message,
EVMSignature: evmSignature,
}
}
24 changes: 24 additions & 0 deletions falcon/band/types/tunnel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package types

// Tunnel stores an information of the tunnel.
type Tunnel struct {
ID uint64
LatestSequence uint64
TargetAddress string
TargetChainID string
}

// NewTunnel creates a new tunnel instance.
func NewTunnel(
id uint64,
latestSequence uint64,
targetAddress string,
targetChainID string,
) *Tunnel {
return &Tunnel{
ID: id,
LatestSequence: latestSequence,
TargetAddress: targetAddress,
TargetChainID: targetChainID,
}
}
18 changes: 18 additions & 0 deletions falcon/chains/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package chains

import "math/big"

// Client defines the interface for the target chain client
type Client interface {
// GetNonce returns the nonce of the given address
GetNonce(address string) (uint64, error)

// BroadcastTx broadcasts the given raw transaction
BroadcastTx(rawTx string) (string, error)

// GetBalances returns the balances of the given accounts
GetBalances(accounts []string) ([]*big.Int, error)

// GetTunnelNonce returns the nonce of the given tunnel
GetTunnelNonce(targetAddress string, tunnelID uint64) (uint64, error)
}
4 changes: 4 additions & 0 deletions falcon/chains/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package chains

// Config defines the common configuration for the target chain client.
type Config struct{}
38 changes: 38 additions & 0 deletions falcon/chains/evm/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package evm

import (
"math/big"

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

var _ chains.Client = &Client{}

type Client struct {
config *Config
keys []keys.Key
}

func NewClient(cfg *Config, relayers []keys.Key) *Client {
return &Client{
config: cfg,
keys: relayers,
}
}

func (c *Client) GetNonce(address string) (uint64, error) {
return 0, nil
}

func (c *Client) BroadcastTx(rawTx string) (string, error) {
return "", nil
}

func (c *Client) GetBalances(accounts []string) ([]*big.Int, error) {
return nil, nil
}

func (c *Client) GetTunnelNonce(targetAddress string, tunnelID uint64) (uint64, error) {
return 0, nil
}
7 changes: 7 additions & 0 deletions falcon/chains/evm/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package evm

import "github.com/bandprotocol/falcon/falcon/chains"

type Config struct {
chains.Config
}
25 changes: 25 additions & 0 deletions falcon/chains/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package chains

import "fmt"

// Registry is a collection of chain clients.
type Registry struct {
Chains map[string]Client
}

// NewRegistry creates a new chain registry.
func NewRegistry() *Registry {
return &Registry{
Chains: make(map[string]Client),
}
}

// Register registers a chain client to the registry.
func (r *Registry) Register(chainID string, client Client) error {
if _, ok := r.Chains[chainID]; !ok {
return fmt.Errorf("chain %s already registered", chainID)
}

r.Chains[chainID] = client
return nil
}
19 changes: 19 additions & 0 deletions falcon/keys/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package keys

// 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,
}
}
25 changes: 25 additions & 0 deletions falcon/scheduler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package falcon

// Scheduler is a struct to manage all tunnel relayers
type Scheduler struct {
TunnelRelayers []TunnelRelayer
}

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

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

// Stop stops all tunnel relayers
func (s *Scheduler) Stop() error {
return nil
}
36 changes: 36 additions & 0 deletions falcon/tunnel_relayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package falcon

import (
"time"

"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 {
TunnelID uint64
ContractAddress string
CheckingPacketInterval time.Duration

BandClient band.Client
TargetChainClient chains.Client
}

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

BandClient: bandClient,
TargetChainClient: targetChainClient,
}
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/bandprotocol/falcon
go 1.22.3

require (
github.com/cometbft/cometbft v0.38.12
github.com/jsternberg/zap-logfmt v1.3.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand All @@ -25,8 +26,8 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/cometbft/cometbft v0.38.12 h1:OWsLZN2KcSSFe8bet9xCn07VwhBnavPea3VyPnNq1bg=
github.com/cometbft/cometbft v0.38.12/go.mod h1:GPHp3/pehPqgX1930HmK1BpBLZPxB75v/dZg8Viwy+o=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -7,8 +9,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand All @@ -28,8 +30,8 @@ github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
Expand Down Expand Up @@ -66,10 +68,10 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down