diff --git a/.gitignore b/.gitignore index b49e3161..84d08386 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,9 @@ __debug_* build/ bin/ +# contracts development config +contracts/config/anvil_localnet/ + # Test binary, built with `go test -c` *.test diff --git a/contracts/config/anvil_localnet/GroupMessages.json b/contracts/config/anvil_localnet/GroupMessages.json index 5b9760fa..e680ab8a 100644 --- a/contracts/config/anvil_localnet/GroupMessages.json +++ b/contracts/config/anvil_localnet/GroupMessages.json @@ -1,10 +1,10 @@ { "addresses": { "groupMessagesDeployer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", - "groupMessagesImpl": "0xc5a5C42992dECbae36851359345FE25997F5C42d", - "groupMessagesProxy": "0x67d269191c92Caf3cD7723F116c85e6E9bf55933", + "groupMessagesImpl": "0x5FbDB2315678afecb367f032d93F642f64180aa3", + "groupMessagesProxy": "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512", "groupMessagesProxyAdmin": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" }, - "deploymentBlock": 28, - "latestUpgradeBlock": 28 + "deploymentBlock": 0, + "latestUpgradeBlock": 0 } \ No newline at end of file diff --git a/contracts/config/anvil_localnet/IdentityUpdates.json b/contracts/config/anvil_localnet/IdentityUpdates.json index 79d70dbb..cf24edfd 100644 --- a/contracts/config/anvil_localnet/IdentityUpdates.json +++ b/contracts/config/anvil_localnet/IdentityUpdates.json @@ -1,10 +1,10 @@ { "addresses": { "identityUpdatesDeployer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", - "identityUpdatesImpl": "0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E", - "identityUpdatesProxy": "0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690", + "identityUpdatesImpl": "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0", + "identityUpdatesProxy": "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9", "identityUpdatesProxyAdmin": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" }, - "deploymentBlock": 30, - "latestUpgradeBlock": 30 + "deploymentBlock": 2, + "latestUpgradeBlock": 2 } \ No newline at end of file diff --git a/pkg/blockchain/registryAdmin.go b/pkg/blockchain/registryAdmin.go index d99b1f51..922ecb05 100644 --- a/pkg/blockchain/registryAdmin.go +++ b/pkg/blockchain/registryAdmin.go @@ -57,14 +57,14 @@ func (n *NodeRegistryAdmin) AddNode( httpAddress string, ) error { if !common.IsHexAddress(owner) { - return fmt.Errorf("Invalid owner address provided %s", owner) + return fmt.Errorf("invalid owner address provided %s", owner) } ownerAddress := common.HexToAddress(owner) signingKey := crypto.FromECDSAPub(signingKeyPub) if n.signer == nil { - return fmt.Errorf("No signer provided") + return fmt.Errorf("no signer provided") } tx, err := n.contract.AddNode(&bind.TransactOpts{ Context: ctx, diff --git a/pkg/blockchain/signer.go b/pkg/blockchain/signer.go index bb309e8e..aa4fc568 100644 --- a/pkg/blockchain/signer.go +++ b/pkg/blockchain/signer.go @@ -32,7 +32,7 @@ func NewPrivateKeySigner(privateKeyString string, chainID int) (*PrivateKeySigne publicKey := privateKey.Public() publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) if !ok { - return nil, fmt.Errorf("Failed to cast to ECDSA public key %v", err) + return nil, fmt.Errorf("failed to cast to ECDSA public key %v", err) } fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) @@ -42,7 +42,7 @@ func NewPrivateKeySigner(privateKeyString string, chainID int) (*PrivateKeySigne big.NewInt(int64(chainID)), ) if err != nil { - return nil, fmt.Errorf("Failed to create transactor: %v", err) + return nil, fmt.Errorf("failed to create transactor: %v", err) } return &PrivateKeySigner{ diff --git a/pkg/db/queries.sql b/pkg/db/queries.sql index b6431cd6..bde45949 100644 --- a/pkg/db/queries.sql +++ b/pkg/db/queries.sql @@ -104,17 +104,18 @@ WHERE originator_node_id = @originator_node_id; -- name: SetLatestBlock :exec -INSERT INTO latest_block(contract_address, block_number) - VALUES (@contract_address, @block_number) +INSERT INTO latest_block(contract_address, block_number, block_hash) + VALUES (@contract_address, @block_number, @block_hash) ON CONFLICT (contract_address) DO UPDATE SET - block_number = @block_number + block_number = @block_number, block_hash = @block_hash WHERE @block_number > latest_block.block_number; -- name: GetLatestBlock :one SELECT - block_number + block_number, + block_hash FROM latest_block WHERE diff --git a/pkg/db/queries/models.go b/pkg/db/queries/models.go index eee1890c..6579fe5a 100644 --- a/pkg/db/queries/models.go +++ b/pkg/db/queries/models.go @@ -27,6 +27,7 @@ type GatewayEnvelope struct { type LatestBlock struct { ContractAddress string BlockNumber int64 + BlockHash []byte } type NodeInfo struct { diff --git a/pkg/db/queries/queries.sql.go b/pkg/db/queries/queries.sql.go index e9c2c443..f842c0c7 100644 --- a/pkg/db/queries/queries.sql.go +++ b/pkg/db/queries/queries.sql.go @@ -77,18 +77,24 @@ func (q *Queries) GetAddressLogs(ctx context.Context, addresses []string) ([]Get const getLatestBlock = `-- name: GetLatestBlock :one SELECT - block_number + block_number, + block_hash FROM latest_block WHERE contract_address = $1 ` -func (q *Queries) GetLatestBlock(ctx context.Context, contractAddress string) (int64, error) { +type GetLatestBlockRow struct { + BlockNumber int64 + BlockHash []byte +} + +func (q *Queries) GetLatestBlock(ctx context.Context, contractAddress string) (GetLatestBlockRow, error) { row := q.db.QueryRowContext(ctx, getLatestBlock, contractAddress) - var block_number int64 - err := row.Scan(&block_number) - return block_number, err + var i GetLatestBlockRow + err := row.Scan(&i.BlockNumber, &i.BlockHash) + return i, err } const getLatestSequenceId = `-- name: GetLatestSequenceId :one @@ -380,11 +386,11 @@ func (q *Queries) SelectVectorClock(ctx context.Context) ([]SelectVectorClockRow } const setLatestBlock = `-- name: SetLatestBlock :exec -INSERT INTO latest_block(contract_address, block_number) - VALUES ($1, $2) +INSERT INTO latest_block(contract_address, block_number, block_hash) + VALUES ($1, $2, $3) ON CONFLICT (contract_address) DO UPDATE SET - block_number = $2 + block_number = $2, block_hash = $3 WHERE $2 > latest_block.block_number ` @@ -392,9 +398,10 @@ ON CONFLICT (contract_address) type SetLatestBlockParams struct { ContractAddress string BlockNumber int64 + BlockHash []byte } func (q *Queries) SetLatestBlock(ctx context.Context, arg SetLatestBlockParams) error { - _, err := q.db.ExecContext(ctx, setLatestBlock, arg.ContractAddress, arg.BlockNumber) + _, err := q.db.ExecContext(ctx, setLatestBlock, arg.ContractAddress, arg.BlockNumber, arg.BlockHash) return err } diff --git a/pkg/indexer/blockTracker.go b/pkg/indexer/blockTracker.go index 859a05de..5e61273a 100644 --- a/pkg/indexer/blockTracker.go +++ b/pkg/indexer/blockTracker.go @@ -8,6 +8,7 @@ import ( "sync" "sync/atomic" + "github.com/ethereum/go-ethereum/common" "github.com/xmtp/xmtpd/pkg/db/queries" ) @@ -18,12 +19,17 @@ and allows the user to increase the value. * */ type BlockTracker struct { - latestBlock atomic.Uint64 + latestBlock *Block contractAddress string queries *queries.Queries mu sync.Mutex } +type Block struct { + number atomic.Uint64 + hash common.Hash +} + // Return a new BlockTracker initialized to the latest block from the DB func NewBlockTracker( ctx context.Context, @@ -39,18 +45,28 @@ func NewBlockTracker( if err != nil { return nil, err } - bt.latestBlock.Store(latestBlock) + bt.latestBlock = latestBlock return bt, nil } -func (bt *BlockTracker) GetLatestBlock() uint64 { - return bt.latestBlock.Load() +func (bt *BlockTracker) GetLatestBlockNumber() uint64 { + return bt.latestBlock.number.Load() } -func (bt *BlockTracker) UpdateLatestBlock(ctx context.Context, block uint64) error { +func (bt *BlockTracker) GetLatestBlockHash() common.Hash { + bt.mu.Lock() + defer bt.mu.Unlock() + return bt.latestBlock.hash +} + +func (bt *BlockTracker) UpdateLatestBlock( + ctx context.Context, + block uint64, + hash common.Hash, +) error { // Quick check without lock - if block <= bt.latestBlock.Load() { + if block <= bt.latestBlock.number.Load() { return nil } @@ -58,22 +74,29 @@ func (bt *BlockTracker) UpdateLatestBlock(ctx context.Context, block uint64) err defer bt.mu.Unlock() // Re-check after acquiring lock - if block <= bt.latestBlock.Load() { + if block <= bt.latestBlock.number.Load() { return nil } - if err := bt.updateDB(ctx, block); err != nil { + if err := bt.updateDB(ctx, block, hash); err != nil { return err } - bt.latestBlock.Store(block) + if hash == (common.Hash{}) { + return fmt.Errorf("invalid block hash %s", hash.String()) + } + + bt.latestBlock.number.Store(block) + bt.latestBlock.hash = hash + return nil } -func (bt *BlockTracker) updateDB(ctx context.Context, block uint64) error { +func (bt *BlockTracker) updateDB(ctx context.Context, block uint64, hash common.Hash) error { return bt.queries.SetLatestBlock(ctx, queries.SetLatestBlockParams{ ContractAddress: bt.contractAddress, BlockNumber: int64(block), + BlockHash: hash.Bytes(), }) } @@ -81,22 +104,30 @@ func loadLatestBlock( ctx context.Context, contractAddress string, querier *queries.Queries, -) (uint64, error) { +) (*Block, error) { + block := &Block{ + number: atomic.Uint64{}, + hash: common.Hash{}, + } + latestBlock, err := querier.GetLatestBlock(ctx, contractAddress) if err != nil { if errors.Is(err, sql.ErrNoRows) { - return 0, nil + return block, nil } - return 0, err + return block, err } - if latestBlock < 0 { - return 0, fmt.Errorf( + if latestBlock.BlockNumber < 0 { + return block, fmt.Errorf( "invalid block number %d for contract %s", - latestBlock, + latestBlock.BlockNumber, contractAddress, ) } - return uint64(latestBlock), nil + block.number.Store(uint64(latestBlock.BlockNumber)) + block.hash = common.BytesToHash(latestBlock.BlockHash) + + return block, nil } diff --git a/pkg/indexer/blockTracker_test.go b/pkg/indexer/blockTracker_test.go index 7ee9e9be..5e2d771c 100644 --- a/pkg/indexer/blockTracker_test.go +++ b/pkg/indexer/blockTracker_test.go @@ -22,7 +22,7 @@ func TestInitialize(t *testing.T) { tracker, err := NewBlockTracker(ctx, CONTRACT_ADDRESS, querier) require.NoError(t, err) require.NotNil(t, tracker) - require.Equal(t, uint64(0), tracker.GetLatestBlock()) + require.Equal(t, uint64(0), tracker.GetLatestBlockNumber()) } func TestUpdateLatestBlock(t *testing.T) { @@ -35,25 +35,32 @@ func TestUpdateLatestBlock(t *testing.T) { tracker, err := NewBlockTracker(ctx, CONTRACT_ADDRESS, querier) require.NoError(t, err) + blockHigh := testutils.IntToHash(100) + blockLow := testutils.IntToHash(50) + // Test updating to a higher block - err = tracker.UpdateLatestBlock(ctx, 100) + err = tracker.UpdateLatestBlock(ctx, 100, blockHigh) require.NoError(t, err) - require.Equal(t, uint64(100), tracker.GetLatestBlock()) + require.Equal(t, uint64(100), tracker.GetLatestBlockNumber()) + require.Equal(t, blockHigh, tracker.GetLatestBlockHash()) // Test updating to a lower block (should not update) - err = tracker.UpdateLatestBlock(ctx, 50) + err = tracker.UpdateLatestBlock(ctx, 50, blockLow) require.NoError(t, err) - require.Equal(t, uint64(100), tracker.GetLatestBlock()) + require.Equal(t, uint64(100), tracker.GetLatestBlockNumber()) + require.Equal(t, blockHigh, tracker.GetLatestBlockHash()) // Test updating to the same block (should not update) - err = tracker.UpdateLatestBlock(ctx, 100) + err = tracker.UpdateLatestBlock(ctx, 100, blockHigh) require.NoError(t, err) - require.Equal(t, uint64(100), tracker.GetLatestBlock()) + require.Equal(t, uint64(100), tracker.GetLatestBlockNumber()) + require.Equal(t, blockHigh, tracker.GetLatestBlockHash()) // Verify persistence newTracker, err := NewBlockTracker(ctx, CONTRACT_ADDRESS, querier) require.NoError(t, err) - require.Equal(t, uint64(100), newTracker.GetLatestBlock()) + require.Equal(t, uint64(100), newTracker.GetLatestBlockNumber()) + require.Equal(t, blockHigh, newTracker.GetLatestBlockHash()) } func TestConcurrentUpdates(t *testing.T) { @@ -77,7 +84,11 @@ func TestConcurrentUpdates(t *testing.T) { defer wg.Done() for j := 0; j < updatesPerGoroutine; j++ { blockNum := uint64(startBlock + j) - err := tracker.UpdateLatestBlock(ctx, blockNum) + err := tracker.UpdateLatestBlock( + ctx, + blockNum, + testutils.IntToHash(int64(blockNum)), + ) require.NoError(t, err) } }(i * updatesPerGoroutine) @@ -87,12 +98,16 @@ func TestConcurrentUpdates(t *testing.T) { // The final block number should be the highest one attempted expectedFinalBlock := uint64((numGoroutines-1)*updatesPerGoroutine + (updatesPerGoroutine - 1)) - require.Equal(t, expectedFinalBlock, tracker.GetLatestBlock()) + require.Equal(t, expectedFinalBlock, tracker.GetLatestBlockNumber()) + + expectedFinalHash := testutils.IntToHash(int64(expectedFinalBlock)) + require.Equal(t, expectedFinalHash, tracker.GetLatestBlockHash()) // Verify persistence newTracker, err := NewBlockTracker(ctx, CONTRACT_ADDRESS, querier) require.NoError(t, err) - require.Equal(t, expectedFinalBlock, newTracker.GetLatestBlock()) + require.Equal(t, expectedFinalBlock, newTracker.GetLatestBlockNumber()) + require.Equal(t, expectedFinalHash, newTracker.GetLatestBlockHash()) } func TestMultipleContractAddresses(t *testing.T) { @@ -110,15 +125,20 @@ func TestMultipleContractAddresses(t *testing.T) { tracker2, err := NewBlockTracker(ctx, address2, querier) require.NoError(t, err) + blockHash1 := testutils.IntToHash(100) + blockHash2 := testutils.IntToHash(200) + // Update trackers independently - err = tracker1.UpdateLatestBlock(ctx, 100) + err = tracker1.UpdateLatestBlock(ctx, 100, blockHash1) require.NoError(t, err) - err = tracker2.UpdateLatestBlock(ctx, 200) + err = tracker2.UpdateLatestBlock(ctx, 200, blockHash2) require.NoError(t, err) // Verify different addresses maintain separate block numbers - require.Equal(t, uint64(100), tracker1.GetLatestBlock()) - require.Equal(t, uint64(200), tracker2.GetLatestBlock()) + require.Equal(t, uint64(100), tracker1.GetLatestBlockNumber()) + require.Equal(t, blockHash1, tracker1.GetLatestBlockHash()) + require.Equal(t, uint64(200), tracker2.GetLatestBlockNumber()) + require.Equal(t, blockHash2, tracker2.GetLatestBlockHash()) // Verify persistence for both addresses newTracker1, err := NewBlockTracker(ctx, address1, querier) @@ -126,6 +146,8 @@ func TestMultipleContractAddresses(t *testing.T) { newTracker2, err := NewBlockTracker(ctx, address2, querier) require.NoError(t, err) - require.Equal(t, uint64(100), newTracker1.GetLatestBlock()) - require.Equal(t, uint64(200), newTracker2.GetLatestBlock()) + require.Equal(t, uint64(100), newTracker1.GetLatestBlockNumber()) + require.Equal(t, blockHash1, newTracker1.GetLatestBlockHash()) + require.Equal(t, uint64(200), newTracker2.GetLatestBlockNumber()) + require.Equal(t, blockHash2, newTracker2.GetLatestBlockHash()) } diff --git a/pkg/indexer/indexer.go b/pkg/indexer/indexer.go index c98e2859..641ecdad 100644 --- a/pkg/indexer/indexer.go +++ b/pkg/indexer/indexer.go @@ -33,7 +33,6 @@ type Indexer struct { func NewIndexer( ctx context.Context, log *zap.Logger, - ) *Indexer { ctx, cancel := context.WithCancel(ctx) return &Indexer{ @@ -57,7 +56,8 @@ func (s *Indexer) Close() { func (i *Indexer) StartIndexer( db *sql.DB, cfg config.ContractsOptions, - validationService mlsvalidate.MLSValidationService) error { + validationService mlsvalidate.MLSValidationService, +) error { client, err := blockchain.NewClient(i.ctx, cfg.RpcUrl) if err != nil { return err @@ -147,7 +147,7 @@ func configureLogStream( } messagesChannel := builder.ListenForContractEvent( - messagesTracker.GetLatestBlock(), + messagesTracker.GetLatestBlockNumber(), common.HexToAddress(cfg.MessagesContractAddress), []common.Hash{messagesTopic}, cfg.MaxChainDisconnectTime, @@ -164,7 +164,7 @@ func configureLogStream( } identityUpdatesChannel := builder.ListenForContractEvent( - identityUpdatesTracker.GetLatestBlock(), + identityUpdatesTracker.GetLatestBlockNumber(), common.HexToAddress(cfg.IdentityUpdatesContractAddress), []common.Hash{identityUpdatesTopic}, cfg.MaxChainDisconnectTime, @@ -212,7 +212,7 @@ func indexLogs( } } else { logger.Info("Stored log", zap.Uint64("blockNumber", event.BlockNumber)) - if trackerErr := blockTracker.UpdateLatestBlock(ctx, event.BlockNumber); trackerErr != nil { + if trackerErr := blockTracker.UpdateLatestBlock(ctx, event.BlockNumber, event.BlockHash); trackerErr != nil { logger.Error("error updating block tracker", zap.Error(trackerErr)) } } diff --git a/pkg/indexer/indexer_test.go b/pkg/indexer/indexer_test.go index 609da982..f28f733d 100644 --- a/pkg/indexer/indexer_test.go +++ b/pkg/indexer/indexer_test.go @@ -19,10 +19,13 @@ func TestIndexLogsSuccess(t *testing.T) { channel := make(chan types.Log, 10) defer close(channel) newBlockNumber := uint64(10) + newBlockHash := common.HexToHash( + "0x0000000000000000000000000000000000000000000000000000000000000000", + ) logStorer := storerMocks.NewMockLogStorer(t) blockTracker := indexerMocks.NewMockIBlockTracker(t) - blockTracker.EXPECT().UpdateLatestBlock(mock.Anything, newBlockNumber).Return(nil) + blockTracker.EXPECT().UpdateLatestBlock(mock.Anything, newBlockNumber, newBlockHash).Return(nil) event := types.Log{ Address: common.HexToAddress("0x123"), diff --git a/pkg/indexer/interface.go b/pkg/indexer/interface.go index e6bc088c..a20f388b 100644 --- a/pkg/indexer/interface.go +++ b/pkg/indexer/interface.go @@ -1,8 +1,13 @@ package indexer -import "context" +import ( + "context" + + "github.com/ethereum/go-ethereum/common" +) type IBlockTracker interface { - GetLatestBlock() uint64 - UpdateLatestBlock(ctx context.Context, block uint64) error + GetLatestBlockNumber() uint64 + GetLatestBlockHash() common.Hash + UpdateLatestBlock(ctx context.Context, block uint64, hash common.Hash) error } diff --git a/pkg/migrations/00003_add-latest-block.up.sql b/pkg/migrations/00003_add-latest-block.up.sql index 855d9a3b..07a8589f 100644 --- a/pkg/migrations/00003_add-latest-block.up.sql +++ b/pkg/migrations/00003_add-latest-block.up.sql @@ -1,5 +1,6 @@ CREATE TABLE latest_block( contract_address TEXT NOT NULL PRIMARY KEY, - block_number BIGINT NOT NULL + block_number BIGINT NOT NULL, + block_hash BYTEA NOT NULL ); diff --git a/pkg/mocks/indexer/mock_IBlockTracker.go b/pkg/mocks/indexer/mock_IBlockTracker.go index 82fd0bde..8c93e90e 100644 --- a/pkg/mocks/indexer/mock_IBlockTracker.go +++ b/pkg/mocks/indexer/mock_IBlockTracker.go @@ -5,6 +5,8 @@ package indexer import ( context "context" + common "github.com/ethereum/go-ethereum/common" + mock "github.com/stretchr/testify/mock" ) @@ -21,12 +23,59 @@ func (_m *MockIBlockTracker) EXPECT() *MockIBlockTracker_Expecter { return &MockIBlockTracker_Expecter{mock: &_m.Mock} } -// GetLatestBlock provides a mock function with given fields: -func (_m *MockIBlockTracker) GetLatestBlock() uint64 { +// GetLatestBlockHash provides a mock function with given fields: +func (_m *MockIBlockTracker) GetLatestBlockHash() common.Hash { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetLatestBlockHash") + } + + var r0 common.Hash + if rf, ok := ret.Get(0).(func() common.Hash); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(common.Hash) + } + } + + return r0 +} + +// MockIBlockTracker_GetLatestBlockHash_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetLatestBlockHash' +type MockIBlockTracker_GetLatestBlockHash_Call struct { + *mock.Call +} + +// GetLatestBlockHash is a helper method to define mock.On call +func (_e *MockIBlockTracker_Expecter) GetLatestBlockHash() *MockIBlockTracker_GetLatestBlockHash_Call { + return &MockIBlockTracker_GetLatestBlockHash_Call{Call: _e.mock.On("GetLatestBlockHash")} +} + +func (_c *MockIBlockTracker_GetLatestBlockHash_Call) Run(run func()) *MockIBlockTracker_GetLatestBlockHash_Call { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *MockIBlockTracker_GetLatestBlockHash_Call) Return(_a0 common.Hash) *MockIBlockTracker_GetLatestBlockHash_Call { + _c.Call.Return(_a0) + return _c +} + +func (_c *MockIBlockTracker_GetLatestBlockHash_Call) RunAndReturn(run func() common.Hash) *MockIBlockTracker_GetLatestBlockHash_Call { + _c.Call.Return(run) + return _c +} + +// GetLatestBlockNumber provides a mock function with given fields: +func (_m *MockIBlockTracker) GetLatestBlockNumber() uint64 { ret := _m.Called() if len(ret) == 0 { - panic("no return value specified for GetLatestBlock") + panic("no return value specified for GetLatestBlockNumber") } var r0 uint64 @@ -39,44 +88,44 @@ func (_m *MockIBlockTracker) GetLatestBlock() uint64 { return r0 } -// MockIBlockTracker_GetLatestBlock_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetLatestBlock' -type MockIBlockTracker_GetLatestBlock_Call struct { +// MockIBlockTracker_GetLatestBlockNumber_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetLatestBlockNumber' +type MockIBlockTracker_GetLatestBlockNumber_Call struct { *mock.Call } -// GetLatestBlock is a helper method to define mock.On call -func (_e *MockIBlockTracker_Expecter) GetLatestBlock() *MockIBlockTracker_GetLatestBlock_Call { - return &MockIBlockTracker_GetLatestBlock_Call{Call: _e.mock.On("GetLatestBlock")} +// GetLatestBlockNumber is a helper method to define mock.On call +func (_e *MockIBlockTracker_Expecter) GetLatestBlockNumber() *MockIBlockTracker_GetLatestBlockNumber_Call { + return &MockIBlockTracker_GetLatestBlockNumber_Call{Call: _e.mock.On("GetLatestBlockNumber")} } -func (_c *MockIBlockTracker_GetLatestBlock_Call) Run(run func()) *MockIBlockTracker_GetLatestBlock_Call { +func (_c *MockIBlockTracker_GetLatestBlockNumber_Call) Run(run func()) *MockIBlockTracker_GetLatestBlockNumber_Call { _c.Call.Run(func(args mock.Arguments) { run() }) return _c } -func (_c *MockIBlockTracker_GetLatestBlock_Call) Return(_a0 uint64) *MockIBlockTracker_GetLatestBlock_Call { +func (_c *MockIBlockTracker_GetLatestBlockNumber_Call) Return(_a0 uint64) *MockIBlockTracker_GetLatestBlockNumber_Call { _c.Call.Return(_a0) return _c } -func (_c *MockIBlockTracker_GetLatestBlock_Call) RunAndReturn(run func() uint64) *MockIBlockTracker_GetLatestBlock_Call { +func (_c *MockIBlockTracker_GetLatestBlockNumber_Call) RunAndReturn(run func() uint64) *MockIBlockTracker_GetLatestBlockNumber_Call { _c.Call.Return(run) return _c } -// UpdateLatestBlock provides a mock function with given fields: ctx, block -func (_m *MockIBlockTracker) UpdateLatestBlock(ctx context.Context, block uint64) error { - ret := _m.Called(ctx, block) +// UpdateLatestBlock provides a mock function with given fields: ctx, block, hash +func (_m *MockIBlockTracker) UpdateLatestBlock(ctx context.Context, block uint64, hash common.Hash) error { + ret := _m.Called(ctx, block, hash) if len(ret) == 0 { panic("no return value specified for UpdateLatestBlock") } var r0 error - if rf, ok := ret.Get(0).(func(context.Context, uint64) error); ok { - r0 = rf(ctx, block) + if rf, ok := ret.Get(0).(func(context.Context, uint64, common.Hash) error); ok { + r0 = rf(ctx, block, hash) } else { r0 = ret.Error(0) } @@ -92,13 +141,14 @@ type MockIBlockTracker_UpdateLatestBlock_Call struct { // UpdateLatestBlock is a helper method to define mock.On call // - ctx context.Context // - block uint64 -func (_e *MockIBlockTracker_Expecter) UpdateLatestBlock(ctx interface{}, block interface{}) *MockIBlockTracker_UpdateLatestBlock_Call { - return &MockIBlockTracker_UpdateLatestBlock_Call{Call: _e.mock.On("UpdateLatestBlock", ctx, block)} +// - hash common.Hash +func (_e *MockIBlockTracker_Expecter) UpdateLatestBlock(ctx interface{}, block interface{}, hash interface{}) *MockIBlockTracker_UpdateLatestBlock_Call { + return &MockIBlockTracker_UpdateLatestBlock_Call{Call: _e.mock.On("UpdateLatestBlock", ctx, block, hash)} } -func (_c *MockIBlockTracker_UpdateLatestBlock_Call) Run(run func(ctx context.Context, block uint64)) *MockIBlockTracker_UpdateLatestBlock_Call { +func (_c *MockIBlockTracker_UpdateLatestBlock_Call) Run(run func(ctx context.Context, block uint64, hash common.Hash)) *MockIBlockTracker_UpdateLatestBlock_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(uint64)) + run(args[0].(context.Context), args[1].(uint64), args[2].(common.Hash)) }) return _c } @@ -108,7 +158,7 @@ func (_c *MockIBlockTracker_UpdateLatestBlock_Call) Return(_a0 error) *MockIBloc return _c } -func (_c *MockIBlockTracker_UpdateLatestBlock_Call) RunAndReturn(run func(context.Context, uint64) error) *MockIBlockTracker_UpdateLatestBlock_Call { +func (_c *MockIBlockTracker_UpdateLatestBlock_Call) RunAndReturn(run func(context.Context, uint64, common.Hash) error) *MockIBlockTracker_UpdateLatestBlock_Call { _c.Call.Return(run) return _c } diff --git a/pkg/sync/syncWorker.go b/pkg/sync/syncWorker.go index c1d623bc..68e14360 100644 --- a/pkg/sync/syncWorker.go +++ b/pkg/sync/syncWorker.go @@ -265,7 +265,7 @@ func (s *syncWorker) connectToNode(node registry.Node) (*grpc.ClientConn, error) zap.String("peer", node.HttpAddress), zap.Error(err), ) - return nil, fmt.Errorf("Failed to connect to peer at %s: %v", node.HttpAddress, err) + return nil, fmt.Errorf("failed to connect to peer at %s: %v", node.HttpAddress, err) } s.log.Debug(fmt.Sprintf("Successfully connected to peer at %s", node.HttpAddress)) @@ -302,7 +302,7 @@ func (s *syncWorker) setupStream( ) if err != nil { return nil, fmt.Errorf( - "Failed to batch subscribe to peer: %v", + "failed to batch subscribe to peer: %v", err, ) } @@ -326,11 +326,11 @@ func (s *syncWorker) listenToStream( // Recv() is a blocking operation that can only be interrupted by cancelling ctx envs, err := originatorStream.stream.Recv() if err == io.EOF { - return fmt.Errorf("Stream closed with EOF") + return fmt.Errorf("stream closed with EOF") } if err != nil { return fmt.Errorf( - "Stream closed with error: %v", + "stream closed with error: %v", err) } s.log.Debug("Received envelopes", zap.Any("numEnvelopes", len(envs.Envelopes))) diff --git a/pkg/testutils/blockchain.go b/pkg/testutils/blockchain.go new file mode 100644 index 00000000..41352f13 --- /dev/null +++ b/pkg/testutils/blockchain.go @@ -0,0 +1,11 @@ +package testutils + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" +) + +func IntToHash(x int64) common.Hash { + return common.BigToHash(big.NewInt(x)) +} diff --git a/pkg/testutils/random.go b/pkg/testutils/random.go index 8654f92b..e12dbbb8 100644 --- a/pkg/testutils/random.go +++ b/pkg/testutils/random.go @@ -62,3 +62,8 @@ func RandomPrivateKey(t *testing.T) *ecdsa.PrivateKey { return key } + +func RandomBlockHash() common.Hash { + bytes := RandomBytes(32) + return common.BytesToHash(bytes) +}