Skip to content

Commit

Permalink
Merge pull request #45 from icon-project/bgkwon-refactoring-relay
Browse files Browse the repository at this point in the history
Refactoring relay and fix bugs in relay
  • Loading branch information
kwon528 authored Jul 31, 2023
2 parents be64600 + 6227ddb commit c95ce0e
Show file tree
Hide file tree
Showing 29 changed files with 1,152 additions and 797 deletions.
32 changes: 14 additions & 18 deletions chain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,24 @@
package chain

import (
"encoding/json"

"github.com/icon-project/btp2/common/config"
"github.com/icon-project/btp2/common/types"
)

type BaseConfig struct {
Address types.BtpAddress `json:"address"`
Endpoint string `json:"endpoint"`
KeyStoreData json.RawMessage `json:"key_store"`
KeyStorePass string `json:"key_password,omitempty"`
KeySecret string `json:"key_secret,omitempty"`
BridgeMode bool `json:"bridge_mode"`
LatestResult bool `json:"latest_result"`
FilledBlockUpdate bool `json:"filled_block_update"`
Options map[string]interface{} `json:"options,omitempty"`
Address types.BtpAddress `json:"address"`
Endpoint string `json:"endpoint"`
KeyStore string `json:"key_store"`
Type string `json:"type"`
KeyStorePass string `json:"key_password,omitempty"`
KeySecret string `json:"key_secret,omitempty"`

Options map[string]interface{} `json:"options,omitempty"`
}

func (b BaseConfig) GetAddress() types.BtpAddress {
return b.Address
}

type Config struct {
config.FileConfig `json:",squash"` //instead of `mapstructure:",squash"`
Src BaseConfig `json:"src"`
Dst BaseConfig `json:"dst"`
Direction string `json:"direction"`
Offset int64 `json:"offset"`
func (b BaseConfig) GetType() string {
return b.Type
}
33 changes: 13 additions & 20 deletions chain/ethbr/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,13 @@ var (
BlockRetryLimit = 5
)

type Wallet interface {
Sign(data []byte) ([]byte, error)
Address() string
}

type Client struct {
uri string
log log.Logger
subscription *rpc.ClientSubscription
ethClient *ethclient.Client
rpcClient *rpc.Client
chainID *big.Int
stop <-chan bool
uri string
log log.Logger
ethClient *ethclient.Client
rpcClient *rpc.Client
chainID *big.Int
stop <-chan bool
}

func toBlockNumArg(number *big.Int) string {
Expand Down Expand Up @@ -203,7 +197,7 @@ func (c *Client) GetBlockNumber() (uint64, error) {
}

// Poll deprecated
func (c *Client) Poll(cb func(bh *types.Header) error) error {
func (c *Client) Poll(cb func(bh *types.Header) error, errCb func(int64, error)) error {
n, err := c.GetBlockNumber()
if err != nil {
return err
Expand All @@ -227,15 +221,15 @@ func (c *Client) Poll(cb func(bh *types.Header) error) error {

if err = cb(bh); err != nil {
c.log.Errorf("Poll callback return err:%+v", err)
return err
errCb(bh.Number.Int64()-1, err)
}

current.Add(current, big.NewInt(1))
}
}
}

func (c *Client) MonitorBlock(br *BlockRequest, cb func(b *BlockNotification) error) error {
func (c *Client) MonitorBlock(br *BlockRequest, cb func(b *BlockNotification) error, errCb func(int64, error)) error {
onBlockHeader := func(bh *types.Header) error {
bn := &BlockNotification{
Hash: bh.Hash(),
Expand Down Expand Up @@ -277,12 +271,12 @@ func (c *Client) MonitorBlock(br *BlockRequest, cb func(b *BlockNotification) er
}
}
return onBlockHeader(bh)
})
}, errCb)
}

func (c *Client) Monitor(cb func(bh *types.Header) error) error {
func (c *Client) Monitor(cb func(bh *types.Header) error, errCb func(int64, error)) error {
if strings.HasPrefix(c.uri, "http") {
return c.Poll(cb)
return c.Poll(cb, errCb)
}
var (
s ethereum.Subscription
Expand All @@ -292,7 +286,7 @@ func (c *Client) Monitor(cb func(bh *types.Header) error) error {
if s, err = c.ethClient.SubscribeNewHead(context.Background(), ch); err != nil {
if rpc.ErrNotificationsUnsupported == err {
c.log.Infoln("%v, try polling", err)
return c.Poll(cb)
return c.Poll(cb, errCb)
}
return err
}
Expand All @@ -312,7 +306,6 @@ func (c *Client) Monitor(cb func(bh *types.Header) error) error {

func (c *Client) CloseMonitor() {
c.log.Debugf("CloseMonitor %s", c.rpcClient)
c.subscription.Unsubscribe()
c.ethClient.Close()
c.rpcClient.Close()
}
Expand Down
74 changes: 74 additions & 0 deletions chain/ethbr/ethbrfactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ethbr

import (
"encoding/json"
"fmt"
"os"

"github.com/icon-project/btp2/chain"
"github.com/icon-project/btp2/common/link"
"github.com/icon-project/btp2/common/log"
"github.com/icon-project/btp2/common/types"
"github.com/icon-project/btp2/common/wallet"
)

const TYPE = "eth-bridge"

func RegisterEthBridge() {
link.RegisterFactory(&link.Factory{
Type: TYPE,
ParseChainConfig: ParseChainConfig,
NewReceiver: NewReceiver,
NewSender: NewSender,
})
}

func ParseChainConfig(raw json.RawMessage) (link.ChainConfig, error) {
cfg := chain.BaseConfig{}
if err := json.Unmarshal(raw, &cfg); err != nil {
return nil, err
}
if cfg.Type != TYPE {
return nil, fmt.Errorf("invalid type (type:%s)", cfg.Type)
}
return cfg, nil
}

func NewReceiver(srcCfg link.ChainConfig, dstAddr types.BtpAddress, baseDir string, l log.Logger) (link.Receiver, error) {
src := srcCfg.(chain.BaseConfig)

return newEthBridge(srcCfg, dstAddr, src.Endpoint, l, baseDir, src.Options)
}

func NewSender(srcAddr types.BtpAddress, dstCfg link.ChainConfig, l log.Logger) (types.Sender, error) {
dst := dstCfg.(chain.BaseConfig)
w, err := newWallet(dst.KeyStorePass, dst.KeySecret, dst.KeyStore)
if err != nil {
return nil, err
}

return newSender(srcAddr, dst, w, dst.Endpoint, dst.Options, l), nil
}

func newWallet(passwd, secret string, keyStorePath string) (types.Wallet, error) {
if keyStore, err := os.ReadFile(keyStorePath); err != nil {
return nil, fmt.Errorf("fail to open KeyStore file path=%s", keyStorePath)
} else {
pw, err := resolvePassword(secret, passwd)
if err != nil {
return nil, err
}
return wallet.DecryptKeyStore(keyStore, pw)
}
}

func resolvePassword(keySecret, keyStorePass string) ([]byte, error) {
if keySecret != "" {
return os.ReadFile(keySecret)
} else {
if keyStorePass != "" {
return []byte(keyStorePass), nil
}
}
return nil, nil
}
4 changes: 0 additions & 4 deletions chain/ethbr/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ func NewMessageProof(bs *types.BMCLinkStatus, startSeq, lastSeq int64, rps []*cl
continue
}
numOfEvents += len(rp.Events)
//if b, err = codec.RLP.MarshalToBytes(rp.Events); err != nil {
// return nil, err
//}
//TODO refactoring
if b, err = rlp.EncodeToBytes(rp.Events); err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit c95ce0e

Please sign in to comment.