Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Decouple EVM config from from EVM chain and listener constructors (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 authored Jan 19, 2023
1 parent a5840c2 commit 587da11
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
29 changes: 20 additions & 9 deletions chains/evm/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"math/big"

"github.com/ChainSafe/chainbridge-core/config/chain"
"github.com/ChainSafe/chainbridge-core/relayer/message"
"github.com/ChainSafe/chainbridge-core/store"
"github.com/rs/zerolog/log"
Expand All @@ -27,11 +26,23 @@ type EVMChain struct {
listener EventListener
writer ProposalExecutor
blockstore *store.BlockStore
config *chain.EVMConfig

domainID uint8
startBlock *big.Int
freshStart bool
latestBlock bool
}

func NewEVMChain(listener EventListener, writer ProposalExecutor, blockstore *store.BlockStore, config *chain.EVMConfig) *EVMChain {
return &EVMChain{listener: listener, writer: writer, blockstore: blockstore, config: config}
func NewEVMChain(listener EventListener, writer ProposalExecutor, blockstore *store.BlockStore, domainID uint8, startBlock *big.Int, latestBlock bool, freshStart bool) *EVMChain {
return &EVMChain{
listener: listener,
writer: writer,
blockstore: blockstore,
domainID: domainID,
startBlock: startBlock,
latestBlock: latestBlock,
freshStart: freshStart,
}
}

// PollEvents is the goroutine that polls blocks and searches Deposit events in them.
Expand All @@ -40,10 +51,10 @@ func (c *EVMChain) PollEvents(ctx context.Context, sysErr chan<- error, msgChan
log.Info().Msg("Polling Blocks...")

startBlock, err := c.blockstore.GetStartBlock(
*c.config.GeneralChainConfig.Id,
c.config.StartBlock,
c.config.GeneralChainConfig.LatestBlock,
c.config.GeneralChainConfig.FreshStart,
c.domainID,
c.startBlock,
c.latestBlock,
c.freshStart,
)
if err != nil {
sysErr <- fmt.Errorf("error %w on getting last stored block", err)
Expand All @@ -65,5 +76,5 @@ func (c *EVMChain) Write(msg []*message.Message) {
}

func (c *EVMChain) DomainID() uint8 {
return *c.config.GeneralChainConfig.Id
return c.domainID
}
18 changes: 12 additions & 6 deletions chains/evm/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"math/big"
"time"

"github.com/ChainSafe/chainbridge-core/config/chain"
"github.com/ChainSafe/chainbridge-core/relayer/message"
"github.com/ChainSafe/chainbridge-core/store"

Expand Down Expand Up @@ -36,15 +35,22 @@ type EVMListener struct {

// NewEVMListener creates an EVMListener that listens to deposit events on chain
// and calls event handler when one occurs
func NewEVMListener(client ChainClient, eventHandlers []EventHandler, blockstore *store.BlockStore, config *chain.EVMConfig) *EVMListener {
func NewEVMListener(
client ChainClient,
eventHandlers []EventHandler,
blockstore *store.BlockStore,
domainID uint8,
blockRetryInterval time.Duration,
blockConfirmations *big.Int,
blockInterval *big.Int) *EVMListener {
return &EVMListener{
client: client,
eventHandlers: eventHandlers,
blockstore: blockstore,
domainID: *config.GeneralChainConfig.Id,
blockRetryInterval: config.BlockRetryInterval,
blockConfirmations: config.BlockConfirmations,
blockInterval: config.BlockInterval,
domainID: domainID,
blockRetryInterval: blockRetryInterval,
blockConfirmations: blockConfirmations,
blockInterval: blockInterval,
}
}

Expand Down
4 changes: 2 additions & 2 deletions example/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Run() error {
eventListener := events.NewListener(client)
eventHandlers := make([]listener.EventHandler, 0)
eventHandlers = append(eventHandlers, listener.NewDepositEventHandler(eventListener, depositHandler, common.HexToAddress(config.Bridge), *config.GeneralChainConfig.Id))
evmListener := listener.NewEVMListener(client, eventHandlers, blockstore, config)
evmListener := listener.NewEVMListener(client, eventHandlers, blockstore, *config.GeneralChainConfig.Id, config.BlockRetryInterval, config.BlockConfirmations, config.BlockInterval)

mh := executor.NewEVMMessageHandler(bridgeContract)
mh.RegisterMessageHandler(config.Erc20Handler, executor.ERC20MessageHandler)
Expand All @@ -90,7 +90,7 @@ func Run() error {
evmVoter = executor.NewVoter(mh, client, bridgeContract)
}

chain := evm.NewEVMChain(evmListener, evmVoter, blockstore, config)
chain := evm.NewEVMChain(evmListener, evmVoter, blockstore, *config.GeneralChainConfig.Id, config.StartBlock, config.GeneralChainConfig.LatestBlock, config.GeneralChainConfig.FreshStart)

chains = append(chains, chain)
}
Expand Down

0 comments on commit 587da11

Please sign in to comment.