diff --git a/Makefile b/Makefile index d5da9d03..79eea798 100644 --- a/Makefile +++ b/Makefile @@ -40,3 +40,5 @@ genmocks: mockgen -source=chains/evm/transactor/signAndSend/signAndSend.go -destination=./mock/signAndSend.go -package mock mockgen -source=./store/store.go -destination=./mock/store.go -package mock mockgen -source=./relayer/message/handler.go -destination=./mock/message.go -package mock + mockgen -source=./chains/evm/listener/listener.go -destination=./mock/evmListener.go -package mock + mockgen -destination=./mock/substrateListener.go -package mock github.com/sygmaprotocol/sygma-core/chains/substrate/listener ChainConnection diff --git a/chains/evm/listener/listener.go b/chains/evm/listener/listener.go index 9c1d07d1..df695267 100644 --- a/chains/evm/listener/listener.go +++ b/chains/evm/listener/listener.go @@ -8,8 +8,6 @@ import ( "math/big" "time" - "github.com/sygmaprotocol/sygma-core/store" - "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) @@ -26,13 +24,17 @@ type BlockDeltaMeter interface { TrackBlockDelta(domainID uint8, head *big.Int, current *big.Int) } +type BlockStorer interface { + StoreBlock(block *big.Int, domainID uint8) error +} + type EVMListener struct { client ChainClient eventHandlers []EventHandler metrics BlockDeltaMeter + blockstore BlockStorer domainID uint8 - blockstore *store.BlockStore blockRetryInterval time.Duration blockConfirmations *big.Int blockInterval *big.Int @@ -45,7 +47,7 @@ type EVMListener struct { func NewEVMListener( client ChainClient, eventHandlers []EventHandler, - blockstore *store.BlockStore, + blockstore BlockStorer, metrics BlockDeltaMeter, domainID uint8, blockRetryInterval time.Duration, @@ -69,6 +71,7 @@ func NewEVMListener( // configured for the listener. func (l *EVMListener) ListenToEvents(ctx context.Context, startBlock *big.Int) { endBlock := big.NewInt(0) +loop: for { select { case <-ctx.Done(): @@ -76,7 +79,7 @@ func (l *EVMListener) ListenToEvents(ctx context.Context, startBlock *big.Int) { default: head, err := l.client.LatestBlock() if err != nil { - l.log.Error().Err(err).Msg("Unable to get latest block") + l.log.Warn().Err(err).Msg("Unable to get latest block") time.Sleep(l.blockRetryInterval) continue } @@ -97,8 +100,8 @@ func (l *EVMListener) ListenToEvents(ctx context.Context, startBlock *big.Int) { for _, handler := range l.eventHandlers { err := handler.HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))) if err != nil { - l.log.Error().Err(err).Msgf("Unable to handle events") - continue + l.log.Warn().Err(err).Msgf("Unable to handle events") + continue loop } } diff --git a/chains/evm/listener/listener_test.go b/chains/evm/listener/listener_test.go new file mode 100644 index 00000000..745e67d8 --- /dev/null +++ b/chains/evm/listener/listener_test.go @@ -0,0 +1,159 @@ +package listener_test + +import ( + "context" + "fmt" + "math/big" + "testing" + "time" + + "github.com/stretchr/testify/suite" + "github.com/sygmaprotocol/sygma-core/chains/evm/listener" + "github.com/sygmaprotocol/sygma-core/mock" + "go.uber.org/mock/gomock" +) + +type ListenerTestSuite struct { + suite.Suite + listener *listener.EVMListener + mockClient *mock.MockChainClient + mockEventHandler *mock.MockEventHandler + mockBlockStorer *mock.MockBlockStorer + mockBlockDeltaMeter *mock.MockBlockDeltaMeter + domainID uint8 +} + +func TestRunTestSuite(t *testing.T) { + suite.Run(t, new(ListenerTestSuite)) +} + +func (s *ListenerTestSuite) SetupTest() { + ctrl := gomock.NewController(s.T()) + s.domainID = 1 + s.mockClient = mock.NewMockChainClient(ctrl) + s.mockEventHandler = mock.NewMockEventHandler(ctrl) + s.mockBlockStorer = mock.NewMockBlockStorer(ctrl) + s.mockBlockDeltaMeter = mock.NewMockBlockDeltaMeter(ctrl) + s.listener = listener.NewEVMListener( + s.mockClient, + []listener.EventHandler{s.mockEventHandler, s.mockEventHandler}, + s.mockBlockStorer, + s.mockBlockDeltaMeter, + s.domainID, + time.Millisecond*75, + big.NewInt(5), + big.NewInt(5)) +} + +func (s *ListenerTestSuite) Test_ListenToEvents_RetriesIfBlockUnavailable() { + s.mockClient.EXPECT().LatestBlock().Return(big.NewInt(0), fmt.Errorf("error")) + + ctx, cancel := context.WithCancel(context.Background()) + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_SleepsIfBlockTooNew() { + s.mockClient.EXPECT().LatestBlock().Return(big.NewInt(109), nil) + + ctx, cancel := context.WithCancel(context.Background()) + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_RetriesInCaseOfHandlerFailure() { + startBlock := big.NewInt(100) + endBlock := big.NewInt(105) + head := big.NewInt(110) + + // First pass + s.mockClient.EXPECT().LatestBlock().Return(head, nil) + s.mockBlockDeltaMeter.EXPECT().TrackBlockDelta(uint8(1), head, endBlock) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(fmt.Errorf("error")) + // Second pass + s.mockClient.EXPECT().LatestBlock().Return(head, nil) + s.mockBlockDeltaMeter.EXPECT().TrackBlockDelta(uint8(1), head, endBlock) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockBlockStorer.EXPECT().StoreBlock(endBlock, s.domainID).Return(nil) + // third pass + s.mockClient.EXPECT().LatestBlock().Return(head, nil) + + ctx, cancel := context.WithCancel(context.Background()) + + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_StoresBlockIfEventHandlingSuccessful() { + startBlock := big.NewInt(100) + endBlock := big.NewInt(105) + head := big.NewInt(110) + + s.mockClient.EXPECT().LatestBlock().Return(head, nil) + // prevent infinite runs + s.mockClient.EXPECT().LatestBlock().Return(big.NewInt(95), nil) + s.mockBlockDeltaMeter.EXPECT().TrackBlockDelta(uint8(1), head, endBlock) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockBlockStorer.EXPECT().StoreBlock(endBlock, s.domainID).Return(nil) + + ctx, cancel := context.WithCancel(context.Background()) + + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_IgnoresBlockStorerError() { + startBlock := big.NewInt(100) + endBlock := big.NewInt(105) + head := big.NewInt(110) + + s.mockClient.EXPECT().LatestBlock().Return(head, nil) + s.mockBlockDeltaMeter.EXPECT().TrackBlockDelta(uint8(1), head, endBlock) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockBlockStorer.EXPECT().StoreBlock(endBlock, s.domainID).Return(fmt.Errorf("error")) + + // prevent infinite runs + s.mockClient.EXPECT().LatestBlock().Return(big.NewInt(95), nil) + + ctx, cancel := context.WithCancel(context.Background()) + + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_UsesHeadAsStartBlockIfNilPassed() { + startBlock := big.NewInt(110) + endBlock := big.NewInt(115) + oldHead := big.NewInt(110) + newHead := big.NewInt(120) + + s.mockClient.EXPECT().LatestBlock().Return(oldHead, nil) + s.mockClient.EXPECT().LatestBlock().Return(newHead, nil) + s.mockClient.EXPECT().LatestBlock().Return(big.NewInt(65), nil) + + s.mockBlockDeltaMeter.EXPECT().TrackBlockDelta(uint8(1), big.NewInt(120), endBlock) + + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockBlockStorer.EXPECT().StoreBlock(endBlock, s.domainID).Return(nil) + + ctx, cancel := context.WithCancel(context.Background()) + + go s.listener.ListenToEvents(ctx, nil) + + time.Sleep(time.Millisecond * 100) + cancel() +} diff --git a/chains/substrate/listener/listener.go b/chains/substrate/listener/listener.go index c1065b29..18d66ab9 100644 --- a/chains/substrate/listener/listener.go +++ b/chains/substrate/listener/listener.go @@ -8,31 +8,33 @@ import ( "math/big" "time" - "github.com/centrifuge/go-substrate-rpc-client/v4/registry/parser" "github.com/centrifuge/go-substrate-rpc-client/v4/types" "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "github.com/sygmaprotocol/sygma-core/store" ) type EventHandler interface { - HandleEvents(evts []*parser.Event) error + HandleEvents(startBlock *big.Int, endBlock *big.Int) error } + type ChainConnection interface { - UpdateMetatdata() error - GetHeaderLatest() (*types.Header, error) - GetBlockHash(blockNumber uint64) (types.Hash, error) - GetBlockEvents(hash types.Hash) ([]*parser.Event, error) GetFinalizedHead() (types.Hash, error) GetBlock(blockHash types.Hash) (*types.SignedBlock, error) } -type SubstrateListener struct { - conn ChainConnection +type BlockStorer interface { + StoreBlock(block *big.Int, domainID uint8) error +} - blockstore store.BlockStore +type BlockDeltaMeter interface { + TrackBlockDelta(domainID uint8, head *big.Int, current *big.Int) +} +type SubstrateListener struct { + conn ChainConnection + blockstore BlockStorer eventHandlers []EventHandler + metrics BlockDeltaMeter blockRetryInterval time.Duration blockInterval *big.Int @@ -41,7 +43,7 @@ type SubstrateListener struct { log zerolog.Logger } -func NewSubstrateListener(connection ChainConnection, blockstore store.BlockStore, eventHandlers []EventHandler, domainID uint8, blockRetryInterval time.Duration, blockInterval *big.Int) *SubstrateListener { +func NewSubstrateListener(connection ChainConnection, eventHandlers []EventHandler, blockstore BlockStorer, metrics BlockDeltaMeter, domainID uint8, blockRetryInterval time.Duration, blockInterval *big.Int) *SubstrateListener { return &SubstrateListener{ log: log.With().Uint8("domainID", domainID).Logger(), domainID: domainID, @@ -50,6 +52,7 @@ func NewSubstrateListener(connection ChainConnection, blockstore store.BlockStor eventHandlers: eventHandlers, blockRetryInterval: blockRetryInterval, blockInterval: blockInterval, + metrics: metrics, } } @@ -57,6 +60,7 @@ func (l *SubstrateListener) ListenToEvents(ctx context.Context, startBlock *big. endBlock := big.NewInt(0) go func() { + loop: for { select { case <-ctx.Done(): @@ -64,13 +68,13 @@ func (l *SubstrateListener) ListenToEvents(ctx context.Context, startBlock *big. default: hash, err := l.conn.GetFinalizedHead() if err != nil { - l.log.Error().Err(err).Msg("Failed to fetch finalized header") + l.log.Warn().Err(err).Msg("Failed to fetch finalized header") time.Sleep(l.blockRetryInterval) continue } head, err := l.conn.GetBlock(hash) if err != nil { - l.log.Error().Err(err).Msg("Failed to fetch block") + l.log.Warn().Err(err).Msg("Failed to fetch block") time.Sleep(l.blockRetryInterval) continue } @@ -86,21 +90,18 @@ func (l *SubstrateListener) ListenToEvents(ctx context.Context, startBlock *big. continue } - evts, err := l.fetchEvents(startBlock, endBlock) - if err != nil { - l.log.Err(err).Msgf("Failed fetching events for block range %s-%s", startBlock, endBlock) - time.Sleep(l.blockRetryInterval) - continue - } + l.metrics.TrackBlockDelta(l.domainID, big.NewInt(int64(head.Block.Header.Number)), endBlock) + l.log.Debug().Msgf("Fetching substrate events for block range %s-%s", startBlock, endBlock) for _, handler := range l.eventHandlers { - err := handler.HandleEvents(evts) + err := handler.HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))) if err != nil { - l.log.Error().Err(err).Msg("Error handling substrate events") - continue + l.log.Warn().Err(err).Msg("Error handling substrate events") + continue loop } } - err = l.blockstore.StoreBlock(startBlock, l.domainID) + + err = l.blockstore.StoreBlock(endBlock, l.domainID) if err != nil { l.log.Error().Str("block", startBlock.String()).Err(err).Msg("Failed to write latest block to blockstore") } @@ -109,24 +110,3 @@ func (l *SubstrateListener) ListenToEvents(ctx context.Context, startBlock *big. } }() } - -func (l *SubstrateListener) fetchEvents(startBlock *big.Int, endBlock *big.Int) ([]*parser.Event, error) { - l.log.Debug().Msgf("Fetching substrate events for block range %s-%s", startBlock, endBlock) - - evts := make([]*parser.Event, 0) - for i := new(big.Int).Set(startBlock); i.Cmp(endBlock) == -1; i.Add(i, big.NewInt(1)) { - hash, err := l.conn.GetBlockHash(i.Uint64()) - if err != nil { - return nil, err - } - - evt, err := l.conn.GetBlockEvents(hash) - if err != nil { - return nil, err - } - evts = append(evts, evt...) - - } - - return evts, nil -} diff --git a/chains/substrate/listener/listener_test.go b/chains/substrate/listener/listener_test.go new file mode 100644 index 00000000..d9154ecd --- /dev/null +++ b/chains/substrate/listener/listener_test.go @@ -0,0 +1,212 @@ +package listener_test + +import ( + "context" + "fmt" + "math/big" + "testing" + "time" + + "github.com/centrifuge/go-substrate-rpc-client/v4/types" + "github.com/stretchr/testify/suite" + "github.com/sygmaprotocol/sygma-core/chains/substrate/listener" + "github.com/sygmaprotocol/sygma-core/mock" + "go.uber.org/mock/gomock" +) + +type ListenerTestSuite struct { + suite.Suite + listener *listener.SubstrateListener + mockClient *mock.MockChainConnection + mockEventHandler *mock.MockEventHandler + mockBlockStorer *mock.MockBlockStorer + mockBlockDeltaMeter *mock.MockBlockDeltaMeter + domainID uint8 +} + +func TestRunTestSuite(t *testing.T) { + suite.Run(t, new(ListenerTestSuite)) +} + +func (s *ListenerTestSuite) SetupTest() { + ctrl := gomock.NewController(s.T()) + s.domainID = 1 + s.mockClient = mock.NewMockChainConnection(ctrl) + s.mockEventHandler = mock.NewMockEventHandler(ctrl) + s.mockBlockStorer = mock.NewMockBlockStorer(ctrl) + s.mockBlockDeltaMeter = mock.NewMockBlockDeltaMeter(ctrl) + s.listener = listener.NewSubstrateListener( + s.mockClient, + []listener.EventHandler{s.mockEventHandler, s.mockEventHandler}, + s.mockBlockStorer, + s.mockBlockDeltaMeter, + s.domainID, + time.Millisecond*75, + big.NewInt(5), + ) +} + +func (s *ListenerTestSuite) Test_ListenToEvents_RetriesIfFinalizedHeadUnavailable() { + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, fmt.Errorf("error")) + + ctx, cancel := context.WithCancel(context.Background()) + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_RetriesIfBlockUnavailable() { + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(nil, fmt.Errorf("error")) + + ctx, cancel := context.WithCancel(context.Background()) + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_SleepsIfBlockTooNew() { + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{ + Block: types.Block{ + Header: types.Header{ + Number: 104, + }, + }, + }, nil) + + ctx, cancel := context.WithCancel(context.Background()) + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_RetriesInCaseOfHandlerFailure() { + startBlock := big.NewInt(100) + endBlock := big.NewInt(105) + head := big.NewInt(110) + + // First pass + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{ + Block: types.Block{ + Header: types.Header{ + Number: types.BlockNumber(head.Int64()), + }, + }, + }, nil) + s.mockBlockDeltaMeter.EXPECT().TrackBlockDelta(uint8(1), head, endBlock) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(fmt.Errorf("error")) + // Second pass + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{ + Block: types.Block{ + Header: types.Header{ + Number: types.BlockNumber(head.Int64()), + }, + }, + }, nil) + s.mockBlockDeltaMeter.EXPECT().TrackBlockDelta(uint8(1), head, endBlock) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockBlockStorer.EXPECT().StoreBlock(endBlock, s.domainID).Return(nil) + // third pass + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{ + Block: types.Block{ + Header: types.Header{ + Number: 100, + }, + }, + }, nil) + + ctx, cancel := context.WithCancel(context.Background()) + + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_IgnoresBlockStorerError() { + startBlock := big.NewInt(100) + endBlock := big.NewInt(105) + head := big.NewInt(110) + + // First pass + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{ + Block: types.Block{ + Header: types.Header{ + Number: types.BlockNumber(head.Int64()), + }, + }, + }, nil) + s.mockBlockDeltaMeter.EXPECT().TrackBlockDelta(uint8(1), head, endBlock) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockBlockStorer.EXPECT().StoreBlock(endBlock, s.domainID).Return(fmt.Errorf("error")) + // second pass + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{ + Block: types.Block{ + Header: types.Header{ + Number: 95, + }, + }, + }, nil) + + ctx, cancel := context.WithCancel(context.Background()) + go s.listener.ListenToEvents(ctx, big.NewInt(100)) + + time.Sleep(time.Millisecond * 50) + cancel() +} + +func (s *ListenerTestSuite) Test_ListenToEvents_UsesHeadAsStartBlockIfNilPassed() { + startBlock := big.NewInt(110) + endBlock := big.NewInt(115) + oldHead := big.NewInt(110) + newHead := big.NewInt(120) + + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{ + Block: types.Block{ + Header: types.Header{ + Number: types.BlockNumber(oldHead.Int64()), + }, + }, + }, nil) + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{ + Block: types.Block{ + Header: types.Header{ + Number: types.BlockNumber(newHead.Int64()), + }, + }, + }, nil) + s.mockClient.EXPECT().GetFinalizedHead().Return(types.Hash{}, nil) + s.mockClient.EXPECT().GetBlock(gomock.Any()).Return(&types.SignedBlock{ + Block: types.Block{ + Header: types.Header{ + Number: types.BlockNumber(95), + }, + }, + }, nil) + + s.mockBlockDeltaMeter.EXPECT().TrackBlockDelta(uint8(1), big.NewInt(120), endBlock) + + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockEventHandler.EXPECT().HandleEvents(startBlock, new(big.Int).Sub(endBlock, big.NewInt(1))).Return(nil) + s.mockBlockStorer.EXPECT().StoreBlock(endBlock, s.domainID).Return(nil) + + ctx, cancel := context.WithCancel(context.Background()) + + go s.listener.ListenToEvents(ctx, nil) + + time.Sleep(time.Millisecond * 100) + cancel() +} diff --git a/mock/evmListener.go b/mock/evmListener.go new file mode 100644 index 00000000..ac0a197d --- /dev/null +++ b/mock/evmListener.go @@ -0,0 +1,163 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ./chains/evm/listener/listener.go +// +// Generated by this command: +// +// mockgen -source=./chains/evm/listener/listener.go -destination=./mock/evmListener.go -package mock +// +// Package mock is a generated GoMock package. +package mock + +import ( + big "math/big" + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockEventHandler is a mock of EventHandler interface. +type MockEventHandler struct { + ctrl *gomock.Controller + recorder *MockEventHandlerMockRecorder +} + +// MockEventHandlerMockRecorder is the mock recorder for MockEventHandler. +type MockEventHandlerMockRecorder struct { + mock *MockEventHandler +} + +// NewMockEventHandler creates a new mock instance. +func NewMockEventHandler(ctrl *gomock.Controller) *MockEventHandler { + mock := &MockEventHandler{ctrl: ctrl} + mock.recorder = &MockEventHandlerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockEventHandler) EXPECT() *MockEventHandlerMockRecorder { + return m.recorder +} + +// HandleEvents mocks base method. +func (m *MockEventHandler) HandleEvents(startBlock, endBlock *big.Int) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HandleEvents", startBlock, endBlock) + ret0, _ := ret[0].(error) + return ret0 +} + +// HandleEvents indicates an expected call of HandleEvents. +func (mr *MockEventHandlerMockRecorder) HandleEvents(startBlock, endBlock any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HandleEvents", reflect.TypeOf((*MockEventHandler)(nil).HandleEvents), startBlock, endBlock) +} + +// MockChainClient is a mock of ChainClient interface. +type MockChainClient struct { + ctrl *gomock.Controller + recorder *MockChainClientMockRecorder +} + +// MockChainClientMockRecorder is the mock recorder for MockChainClient. +type MockChainClientMockRecorder struct { + mock *MockChainClient +} + +// NewMockChainClient creates a new mock instance. +func NewMockChainClient(ctrl *gomock.Controller) *MockChainClient { + mock := &MockChainClient{ctrl: ctrl} + mock.recorder = &MockChainClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockChainClient) EXPECT() *MockChainClientMockRecorder { + return m.recorder +} + +// LatestBlock mocks base method. +func (m *MockChainClient) LatestBlock() (*big.Int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "LatestBlock") + ret0, _ := ret[0].(*big.Int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// LatestBlock indicates an expected call of LatestBlock. +func (mr *MockChainClientMockRecorder) LatestBlock() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "LatestBlock", reflect.TypeOf((*MockChainClient)(nil).LatestBlock)) +} + +// MockBlockDeltaMeter is a mock of BlockDeltaMeter interface. +type MockBlockDeltaMeter struct { + ctrl *gomock.Controller + recorder *MockBlockDeltaMeterMockRecorder +} + +// MockBlockDeltaMeterMockRecorder is the mock recorder for MockBlockDeltaMeter. +type MockBlockDeltaMeterMockRecorder struct { + mock *MockBlockDeltaMeter +} + +// NewMockBlockDeltaMeter creates a new mock instance. +func NewMockBlockDeltaMeter(ctrl *gomock.Controller) *MockBlockDeltaMeter { + mock := &MockBlockDeltaMeter{ctrl: ctrl} + mock.recorder = &MockBlockDeltaMeterMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockBlockDeltaMeter) EXPECT() *MockBlockDeltaMeterMockRecorder { + return m.recorder +} + +// TrackBlockDelta mocks base method. +func (m *MockBlockDeltaMeter) TrackBlockDelta(domainID uint8, head, current *big.Int) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "TrackBlockDelta", domainID, head, current) +} + +// TrackBlockDelta indicates an expected call of TrackBlockDelta. +func (mr *MockBlockDeltaMeterMockRecorder) TrackBlockDelta(domainID, head, current any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TrackBlockDelta", reflect.TypeOf((*MockBlockDeltaMeter)(nil).TrackBlockDelta), domainID, head, current) +} + +// MockBlockStorer is a mock of BlockStorer interface. +type MockBlockStorer struct { + ctrl *gomock.Controller + recorder *MockBlockStorerMockRecorder +} + +// MockBlockStorerMockRecorder is the mock recorder for MockBlockStorer. +type MockBlockStorerMockRecorder struct { + mock *MockBlockStorer +} + +// NewMockBlockStorer creates a new mock instance. +func NewMockBlockStorer(ctrl *gomock.Controller) *MockBlockStorer { + mock := &MockBlockStorer{ctrl: ctrl} + mock.recorder = &MockBlockStorerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockBlockStorer) EXPECT() *MockBlockStorerMockRecorder { + return m.recorder +} + +// StoreBlock mocks base method. +func (m *MockBlockStorer) StoreBlock(block *big.Int, domainID uint8) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StoreBlock", block, domainID) + ret0, _ := ret[0].(error) + return ret0 +} + +// StoreBlock indicates an expected call of StoreBlock. +func (mr *MockBlockStorerMockRecorder) StoreBlock(block, domainID any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StoreBlock", reflect.TypeOf((*MockBlockStorer)(nil).StoreBlock), block, domainID) +} diff --git a/mock/substrateListener.go b/mock/substrateListener.go new file mode 100644 index 00000000..676cd181 --- /dev/null +++ b/mock/substrateListener.go @@ -0,0 +1,69 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/sygmaprotocol/sygma-core/chains/substrate/listener (interfaces: ChainConnection) +// +// Generated by this command: +// +// mockgen -destination=./mock/substrateListener.go -package mock github.com/sygmaprotocol/sygma-core/chains/substrate/listener ChainConnection +// +// Package mock is a generated GoMock package. +package mock + +import ( + reflect "reflect" + + types "github.com/centrifuge/go-substrate-rpc-client/v4/types" + gomock "go.uber.org/mock/gomock" +) + +// MockChainConnection is a mock of ChainConnection interface. +type MockChainConnection struct { + ctrl *gomock.Controller + recorder *MockChainConnectionMockRecorder +} + +// MockChainConnectionMockRecorder is the mock recorder for MockChainConnection. +type MockChainConnectionMockRecorder struct { + mock *MockChainConnection +} + +// NewMockChainConnection creates a new mock instance. +func NewMockChainConnection(ctrl *gomock.Controller) *MockChainConnection { + mock := &MockChainConnection{ctrl: ctrl} + mock.recorder = &MockChainConnectionMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockChainConnection) EXPECT() *MockChainConnectionMockRecorder { + return m.recorder +} + +// GetBlock mocks base method. +func (m *MockChainConnection) GetBlock(arg0 types.Hash) (*types.SignedBlock, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBlock", arg0) + ret0, _ := ret[0].(*types.SignedBlock) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetBlock indicates an expected call of GetBlock. +func (mr *MockChainConnectionMockRecorder) GetBlock(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBlock", reflect.TypeOf((*MockChainConnection)(nil).GetBlock), arg0) +} + +// GetFinalizedHead mocks base method. +func (m *MockChainConnection) GetFinalizedHead() (types.Hash, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetFinalizedHead") + ret0, _ := ret[0].(types.Hash) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetFinalizedHead indicates an expected call of GetFinalizedHead. +func (mr *MockChainConnectionMockRecorder) GetFinalizedHead() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetFinalizedHead", reflect.TypeOf((*MockChainConnection)(nil).GetFinalizedHead)) +}