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

Commit 587da11

Browse files
authored
Decouple EVM config from from EVM chain and listener constructors (#326)
1 parent a5840c2 commit 587da11

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

chains/evm/chain.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"math/big"
1010

11-
"github.com/ChainSafe/chainbridge-core/config/chain"
1211
"github.com/ChainSafe/chainbridge-core/relayer/message"
1312
"github.com/ChainSafe/chainbridge-core/store"
1413
"github.com/rs/zerolog/log"
@@ -27,11 +26,23 @@ type EVMChain struct {
2726
listener EventListener
2827
writer ProposalExecutor
2928
blockstore *store.BlockStore
30-
config *chain.EVMConfig
29+
30+
domainID uint8
31+
startBlock *big.Int
32+
freshStart bool
33+
latestBlock bool
3134
}
3235

33-
func NewEVMChain(listener EventListener, writer ProposalExecutor, blockstore *store.BlockStore, config *chain.EVMConfig) *EVMChain {
34-
return &EVMChain{listener: listener, writer: writer, blockstore: blockstore, config: config}
36+
func NewEVMChain(listener EventListener, writer ProposalExecutor, blockstore *store.BlockStore, domainID uint8, startBlock *big.Int, latestBlock bool, freshStart bool) *EVMChain {
37+
return &EVMChain{
38+
listener: listener,
39+
writer: writer,
40+
blockstore: blockstore,
41+
domainID: domainID,
42+
startBlock: startBlock,
43+
latestBlock: latestBlock,
44+
freshStart: freshStart,
45+
}
3546
}
3647

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

4253
startBlock, err := c.blockstore.GetStartBlock(
43-
*c.config.GeneralChainConfig.Id,
44-
c.config.StartBlock,
45-
c.config.GeneralChainConfig.LatestBlock,
46-
c.config.GeneralChainConfig.FreshStart,
54+
c.domainID,
55+
c.startBlock,
56+
c.latestBlock,
57+
c.freshStart,
4758
)
4859
if err != nil {
4960
sysErr <- fmt.Errorf("error %w on getting last stored block", err)
@@ -65,5 +76,5 @@ func (c *EVMChain) Write(msg []*message.Message) {
6576
}
6677

6778
func (c *EVMChain) DomainID() uint8 {
68-
return *c.config.GeneralChainConfig.Id
79+
return c.domainID
6980
}

chains/evm/listener/listener.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"math/big"
99
"time"
1010

11-
"github.com/ChainSafe/chainbridge-core/config/chain"
1211
"github.com/ChainSafe/chainbridge-core/relayer/message"
1312
"github.com/ChainSafe/chainbridge-core/store"
1413

@@ -36,15 +35,22 @@ type EVMListener struct {
3635

3736
// NewEVMListener creates an EVMListener that listens to deposit events on chain
3837
// and calls event handler when one occurs
39-
func NewEVMListener(client ChainClient, eventHandlers []EventHandler, blockstore *store.BlockStore, config *chain.EVMConfig) *EVMListener {
38+
func NewEVMListener(
39+
client ChainClient,
40+
eventHandlers []EventHandler,
41+
blockstore *store.BlockStore,
42+
domainID uint8,
43+
blockRetryInterval time.Duration,
44+
blockConfirmations *big.Int,
45+
blockInterval *big.Int) *EVMListener {
4046
return &EVMListener{
4147
client: client,
4248
eventHandlers: eventHandlers,
4349
blockstore: blockstore,
44-
domainID: *config.GeneralChainConfig.Id,
45-
blockRetryInterval: config.BlockRetryInterval,
46-
blockConfirmations: config.BlockConfirmations,
47-
blockInterval: config.BlockInterval,
50+
domainID: domainID,
51+
blockRetryInterval: blockRetryInterval,
52+
blockConfirmations: blockConfirmations,
53+
blockInterval: blockInterval,
4854
}
4955
}
5056

example/app/app.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func Run() error {
7676
eventListener := events.NewListener(client)
7777
eventHandlers := make([]listener.EventHandler, 0)
7878
eventHandlers = append(eventHandlers, listener.NewDepositEventHandler(eventListener, depositHandler, common.HexToAddress(config.Bridge), *config.GeneralChainConfig.Id))
79-
evmListener := listener.NewEVMListener(client, eventHandlers, blockstore, config)
79+
evmListener := listener.NewEVMListener(client, eventHandlers, blockstore, *config.GeneralChainConfig.Id, config.BlockRetryInterval, config.BlockConfirmations, config.BlockInterval)
8080

8181
mh := executor.NewEVMMessageHandler(bridgeContract)
8282
mh.RegisterMessageHandler(config.Erc20Handler, executor.ERC20MessageHandler)
@@ -90,7 +90,7 @@ func Run() error {
9090
evmVoter = executor.NewVoter(mh, client, bridgeContract)
9191
}
9292

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

9595
chains = append(chains, chain)
9696
}

0 commit comments

Comments
 (0)