From b4b6389be6389f6f605f5d52f3ea0ac1dd4d0f62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Petruni=C4=87?= Date: Tue, 3 Sep 2024 13:23:24 +0200 Subject: [PATCH] feat: allow for listen or execute only networks (#33) --- chains/evm/chain.go | 13 +++++++++++++ chains/substrate/chain.go | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/chains/evm/chain.go b/chains/evm/chain.go index a09ea060..18862e12 100644 --- a/chains/evm/chain.go +++ b/chains/evm/chain.go @@ -5,6 +5,7 @@ package evm import ( "context" + "fmt" "math/big" "github.com/rs/zerolog" @@ -51,15 +52,27 @@ func NewEVMChain(listener EventListener, messageHandler MessageHandler, executor // PollEvents is the goroutine that polls blocks and searches Deposit events in them. // Events are then sent to eventsChan. func (c *EVMChain) PollEvents(ctx context.Context) { + if c.listener == nil { + return + } + c.logger.Info().Str("startBlock", c.startBlock.String()).Msg("Polling Blocks...") go c.listener.ListenToEvents(ctx, c.startBlock) } func (c *EVMChain) ReceiveMessage(m *message.Message) (*proposal.Proposal, error) { + if c.messageHandler == nil { + return nil, fmt.Errorf("message handler not configured") + } + return c.messageHandler.HandleMessage(m) } func (c *EVMChain) Write(props []*proposal.Proposal) error { + if c.executor == nil { + return fmt.Errorf("executor not configured") + } + err := c.executor.Execute(props) if err != nil { c.logger.Err(err).Str("messageID", props[0].MessageID).Msgf("error writing proposals %+v on network %d", props, c.DomainID()) diff --git a/chains/substrate/chain.go b/chains/substrate/chain.go index 9c72f922..c0989a52 100644 --- a/chains/substrate/chain.go +++ b/chains/substrate/chain.go @@ -2,6 +2,7 @@ package substrate import ( "context" + "fmt" "math/big" "github.com/rs/zerolog" @@ -46,15 +47,27 @@ func NewSubstrateChain(listener EventListener, messageHandler MessageHandler, ex // PollEvents is the goroutine that polls blocks and searches Deposit events in them. // Events are then sent to eventsChan. func (c *SubstrateChain) PollEvents(ctx context.Context) { + if c.listener == nil { + return + } + c.logger.Info().Str("startBlock", c.startBlock.String()).Msg("Polling Blocks...") go c.listener.ListenToEvents(ctx, c.startBlock) } func (c *SubstrateChain) ReceiveMessage(m *message.Message) (*proposal.Proposal, error) { + if c.messageHandler == nil { + return nil, fmt.Errorf("message handler not configured") + } + return c.messageHandler.HandleMessage(m) } func (c *SubstrateChain) Write(props []*proposal.Proposal) error { + if c.executor == nil { + return fmt.Errorf("executor not configured") + } + err := c.executor.Execute(props) if err != nil { c.logger.Err(err).Str("messageID", props[0].MessageID).Msgf("error writing proposals %+v on network %d", props, c.DomainID())