From 2a341b295b939f8e51056e8cbea31ee5d43452ae Mon Sep 17 00:00:00 2001 From: Yashk767 <76935991+Yashk767@users.noreply.github.com> Date: Tue, 30 Apr 2024 11:57:41 +0530 Subject: [PATCH] refactor: Optimised number of `GetBlockNumber` and `GetStakerId` eth calls (#1204) * refactor: Used getStakerId() only once * refactor: used exisiting blockNumber from Vote() in dependent functions * refactor: fixed tests after header being passed as a parameter * fix: removed continous retries if error in fetching block number in logger * refactor: added tests when context is cancelled for Vote() * refactor: fixed vote tests --- block/block.go | 4 +- cmd/cmd-utils.go | 7 +- cmd/cmd-utils_test.go | 23 +++- cmd/commit.go | 5 +- cmd/commit_test.go | 15 +-- cmd/interface.go | 16 +-- cmd/mocks/utils_cmd_interface.go | 92 +++++++-------- cmd/propose.go | 9 +- cmd/propose_test.go | 18 +-- cmd/reveal.go | 4 +- cmd/reveal_test.go | 19 +-- cmd/vote.go | 50 ++++---- cmd/vote_test.go | 191 ++++++++++++++++++++----------- utils/common.go | 11 +- utils/common_test.go | 21 +--- utils/interface.go | 2 +- utils/mocks/utils.go | 20 ++-- 17 files changed, 295 insertions(+), 212 deletions(-) diff --git a/block/block.go b/block/block.go index bd12e70d2..ea896d420 100644 --- a/block/block.go +++ b/block/block.go @@ -32,9 +32,9 @@ func CalculateLatestBlock(client *ethclient.Client) { latestHeader, err := client.HeaderByNumber(context.Background(), nil) if err != nil { logrus.Error("CalculateBlockNumber: Error in fetching block: ", err) - continue + } else { + SetLatestBlock(latestHeader) } - SetLatestBlock(latestHeader) } time.Sleep(time.Second * time.Duration(core.BlockNumberInterval)) } diff --git a/cmd/cmd-utils.go b/cmd/cmd-utils.go index 30a84e1cd..2583c3586 100644 --- a/cmd/cmd-utils.go +++ b/cmd/cmd-utils.go @@ -23,7 +23,12 @@ func (*UtilsStruct) GetEpochAndState(client *ethclient.Client) (uint32, int64, e if err != nil { return 0, 0, err } - state, err := razorUtils.GetBufferedState(client, bufferPercent) + latestHeader, err := clientUtils.GetLatestBlockWithRetry(client) + if err != nil { + log.Error("Error in fetching block: ", err) + return 0, 0, err + } + state, err := razorUtils.GetBufferedState(client, latestHeader, bufferPercent) if err != nil { return 0, 0, err } diff --git a/cmd/cmd-utils_test.go b/cmd/cmd-utils_test.go index 63f543611..0da68b1e5 100644 --- a/cmd/cmd-utils_test.go +++ b/cmd/cmd-utils_test.go @@ -2,6 +2,7 @@ package cmd import ( "errors" + Types "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" "github.com/spf13/pflag" "github.com/stretchr/testify/mock" @@ -15,6 +16,8 @@ func TestGetEpochAndState(t *testing.T) { type args struct { epoch uint32 epochErr error + latestHeader *Types.Header + latestHeaderErr error bufferPercent int32 bufferPercentErr error state int64 @@ -32,6 +35,7 @@ func TestGetEpochAndState(t *testing.T) { name: "Test 1: When GetEpochAndState function executes successfully", args: args{ epoch: 4, + latestHeader: &Types.Header{}, bufferPercent: 20, state: 0, stateName: "commit", @@ -44,6 +48,7 @@ func TestGetEpochAndState(t *testing.T) { name: "Test 2: When there is an error in getting epoch", args: args{ epochErr: errors.New("epoch error"), + latestHeader: &Types.Header{}, bufferPercent: 20, state: 0, stateName: "commit", @@ -56,6 +61,7 @@ func TestGetEpochAndState(t *testing.T) { name: "Test 3: When there is an error in getting bufferPercent", args: args{ epoch: 4, + latestHeader: &Types.Header{}, bufferPercentErr: errors.New("bufferPercent error"), state: 0, stateName: "commit", @@ -68,6 +74,7 @@ func TestGetEpochAndState(t *testing.T) { name: "Test 4: When there is an error in getting state", args: args{ epoch: 4, + latestHeader: &Types.Header{}, bufferPercent: 20, stateErr: errors.New("state error"), }, @@ -75,6 +82,19 @@ func TestGetEpochAndState(t *testing.T) { wantState: 0, wantErr: errors.New("state error"), }, + { + name: "Test 5: When there is an error in getting latest header", + args: args{ + epoch: 4, + latestHeaderErr: errors.New("header error"), + bufferPercent: 20, + state: 0, + stateName: "commit", + }, + wantEpoch: 0, + wantState: 0, + wantErr: errors.New("header error"), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -82,7 +102,8 @@ func TestGetEpochAndState(t *testing.T) { utilsMock.On("GetEpoch", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.epoch, tt.args.epochErr) cmdUtilsMock.On("GetBufferPercent").Return(tt.args.bufferPercent, tt.args.bufferPercentErr) - utilsMock.On("GetBufferedState", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("int32")).Return(tt.args.state, tt.args.stateErr) + clientUtilsMock.On("GetLatestBlockWithRetry", mock.Anything).Return(tt.args.latestHeader, tt.args.latestHeaderErr) + utilsMock.On("GetBufferedState", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr) utils := &UtilsStruct{} gotEpoch, gotState, err := utils.GetEpochAndState(client) diff --git a/cmd/commit.go b/cmd/commit.go index c40959f36..b66382928 100644 --- a/cmd/commit.go +++ b/cmd/commit.go @@ -4,6 +4,7 @@ package cmd import ( "encoding/hex" "errors" + Types "github.com/ethereum/go-ethereum/core/types" "math/big" "razor/cache" "razor/client" @@ -150,8 +151,8 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se /* Commit finally commits the data to the smart contract. It calculates the commitment to send using the merkle tree root and the seed. */ -func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, values []*big.Int) (common.Hash, error) { - if state, err := razorUtils.GetBufferedState(client, config.BufferPercent); err != nil || state != 0 { +func (*UtilsStruct) Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, latestHeader *Types.Header, seed []byte, values []*big.Int) (common.Hash, error) { + if state, err := razorUtils.GetBufferedState(client, latestHeader, config.BufferPercent); err != nil || state != 0 { log.Error("Not commit state") return core.NilHash, err } diff --git a/cmd/commit_test.go b/cmd/commit_test.go index 739179cf2..e5f95cc6f 100644 --- a/cmd/commit_test.go +++ b/cmd/commit_test.go @@ -20,11 +20,12 @@ import ( func TestCommit(t *testing.T) { var ( - client *ethclient.Client - account types.Account - config types.Configurations - seed []byte - epoch uint32 + client *ethclient.Client + account types.Account + config types.Configurations + latestHeader *Types.Header + seed []byte + epoch uint32 ) type args struct { @@ -95,13 +96,13 @@ func TestCommit(t *testing.T) { utils.MerkleInterface = &utils.MerkleTreeStruct{} merkleUtils = utils.MerkleInterface - utilsMock.On("GetBufferedState", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("int32")).Return(tt.args.state, tt.args.stateErr) + utilsMock.On("GetBufferedState", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr) utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(TxnOpts) voteManagerMock.On("Commit", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*bind.TransactOpts"), mock.AnythingOfType("uint32"), mock.Anything).Return(tt.args.commitTxn, tt.args.commitErr) transactionMock.On("Hash", mock.AnythingOfType("*types.Transaction")).Return(tt.args.hash) utils := &UtilsStruct{} - got, err := utils.Commit(client, config, account, epoch, seed, tt.args.values) + got, err := utils.Commit(client, config, account, epoch, latestHeader, seed, tt.args.values) if got != tt.want { t.Errorf("Txn hash for Commit function, got = %v, want = %v", got, tt.want) } diff --git a/cmd/interface.go b/cmd/interface.go index a2d03f230..3f8fd528b 100644 --- a/cmd/interface.go +++ b/cmd/interface.go @@ -166,13 +166,13 @@ type UtilsCmdInterface interface { ClaimBlockReward(options types.TransactionOptions) (common.Hash, error) GetSalt(client *ethclient.Client, epoch uint32) ([32]byte, error) HandleCommitState(client *ethclient.Client, epoch uint32, seed []byte, httpClient *client.HttpClient, rogueData types.Rogue) (types.CommitData, error) - Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, values []*big.Int) (common.Hash, error) + Commit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, latestHeader *Types.Header, seed []byte, values []*big.Int) (common.Hash, error) ListAccounts() ([]accounts.Account, error) AssignAmountInWei(flagSet *pflag.FlagSet) (*big.Int, error) ExecuteTransfer(flagSet *pflag.FlagSet) Transfer(client *ethclient.Client, config types.Configurations, transferInput types.TransferInput) (common.Hash, error) CheckForLastCommitted(client *ethclient.Client, staker bindings.StructsStaker, epoch uint32) error - Reveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, commitData types.CommitData, signature []byte) (common.Hash, error) + Reveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, latestHeader *Types.Header, commitData types.CommitData, signature []byte) (common.Hash, error) GenerateTreeRevealData(merkleTree [][][]byte, commitData types.CommitData) bindings.StructsMerkleTree IndexRevealEventsOfCurrentEpoch(client *ethclient.Client, blockNumber *big.Int, epoch uint32) ([]types.RevealedStruct, error) ExecuteCreateJob(flagSet *pflag.FlagSet) @@ -207,7 +207,7 @@ type UtilsCmdInterface interface { IsElectedProposer(proposer types.ElectedProposer, currentStakerStake *big.Int) bool GetSortedRevealedValues(client *ethclient.Client, blockNumber *big.Int, epoch uint32) (*types.RevealedDataMaps, error) GetIteration(client *ethclient.Client, proposer types.ElectedProposer, bufferPercent int32) int - Propose(client *ethclient.Client, config types.Configurations, account types.Account, staker bindings.StructsStaker, epoch uint32, blockNumber *big.Int, rogueData types.Rogue) error + Propose(client *ethclient.Client, config types.Configurations, account types.Account, staker bindings.StructsStaker, epoch uint32, latestHeader *Types.Header, rogueData types.Rogue) error GiveSorted(client *ethclient.Client, blockManager *bindings.BlockManager, txnArgs types.TransactionOptions, epoch uint32, assetId uint16, sortedStakers []*big.Int) error GetLocalMediansData(client *ethclient.Client, account types.Account, epoch uint32, blockNumber *big.Int, rogueData types.Rogue) (types.ProposeFileData, error) CheckDisputeForIds(client *ethclient.Client, transactionOpts types.TransactionOptions, epoch uint32, blockIndex uint8, idsInProposedBlock []uint16, revealedCollectionIds []uint16) (*Types.Transaction, error) @@ -232,16 +232,16 @@ type UtilsCmdInterface interface { GetSmallestStakeAndId(client *ethclient.Client, epoch uint32) (*big.Int, uint32, error) StakeCoins(txnArgs types.TransactionOptions) (common.Hash, error) CalculateSecret(account types.Account, epoch uint32, keystorePath string, chainId *big.Int) ([]byte, []byte, error) - HandleBlock(client *ethclient.Client, account types.Account, blockNumber *big.Int, config types.Configurations, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) + HandleBlock(client *ethclient.Client, account types.Account, stakerId uint32, header *Types.Header, config types.Configurations, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) ExecuteVote(flagSet *pflag.FlagSet) - Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, account types.Account, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) error + Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, account types.Account, stakerId uint32, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) error HandleExit() ExecuteListAccounts(flagSet *pflag.FlagSet) ClaimCommission(flagSet *pflag.FlagSet) ExecuteStake(flagSet *pflag.FlagSet) - InitiateCommit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, httpClient *client.HttpClient, rogueData types.Rogue) error - InitiateReveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, rogueData types.Rogue) error - InitiatePropose(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, blockNumber *big.Int, rogueData types.Rogue) error + InitiateCommit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, latestHeader *Types.Header, httpClient *client.HttpClient, rogueData types.Rogue) error + InitiateReveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, latestHeader *Types.Header, rogueData types.Rogue) error + InitiatePropose(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, latestHeader *Types.Header, rogueData types.Rogue) error GetBountyIdFromEvents(client *ethclient.Client, blockNumber *big.Int, bountyHunter string) (uint32, error) HandleClaimBounty(client *ethclient.Client, config types.Configurations, account types.Account) error ExecuteContractAddresses(flagSet *pflag.FlagSet) diff --git a/cmd/mocks/utils_cmd_interface.go b/cmd/mocks/utils_cmd_interface.go index 72a01e841..42dd75656 100644 --- a/cmd/mocks/utils_cmd_interface.go +++ b/cmd/mocks/utils_cmd_interface.go @@ -272,25 +272,25 @@ func (_m *UtilsCmdInterface) ClaimCommission(flagSet *pflag.FlagSet) { _m.Called(flagSet) } -// Commit provides a mock function with given fields: _a0, config, account, epoch, seed, values -func (_m *UtilsCmdInterface) Commit(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, seed []byte, values []*big.Int) (common.Hash, error) { - ret := _m.Called(_a0, config, account, epoch, seed, values) +// Commit provides a mock function with given fields: _a0, config, account, epoch, latestHeader, seed, values +func (_m *UtilsCmdInterface) Commit(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, latestHeader *coretypes.Header, seed []byte, values []*big.Int) (common.Hash, error) { + ret := _m.Called(_a0, config, account, epoch, latestHeader, seed, values) var r0 common.Hash var r1 error - if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, []byte, []*big.Int) (common.Hash, error)); ok { - return rf(_a0, config, account, epoch, seed, values) + if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, *coretypes.Header, []byte, []*big.Int) (common.Hash, error)); ok { + return rf(_a0, config, account, epoch, latestHeader, seed, values) } - if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, []byte, []*big.Int) common.Hash); ok { - r0 = rf(_a0, config, account, epoch, seed, values) + if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, *coretypes.Header, []byte, []*big.Int) common.Hash); ok { + r0 = rf(_a0, config, account, epoch, latestHeader, seed, values) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(common.Hash) } } - if rf, ok := ret.Get(1).(func(*ethclient.Client, types.Configurations, types.Account, uint32, []byte, []*big.Int) error); ok { - r1 = rf(_a0, config, account, epoch, seed, values) + if rf, ok := ret.Get(1).(func(*ethclient.Client, types.Configurations, types.Account, uint32, *coretypes.Header, []byte, []*big.Int) error); ok { + r1 = rf(_a0, config, account, epoch, latestHeader, seed, values) } else { r1 = ret.Error(1) } @@ -1191,9 +1191,9 @@ func (_m *UtilsCmdInterface) GiveSorted(_a0 *ethclient.Client, blockManager *bin return r0 } -// HandleBlock provides a mock function with given fields: _a0, account, blockNumber, config, httpClient, rogueData, backupNodeActionsToIgnore -func (_m *UtilsCmdInterface) HandleBlock(_a0 *ethclient.Client, account types.Account, blockNumber *big.Int, config types.Configurations, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) { - _m.Called(_a0, account, blockNumber, config, httpClient, rogueData, backupNodeActionsToIgnore) +// HandleBlock provides a mock function with given fields: _a0, account, stakerId, header, config, httpClient, rogueData, backupNodeActionsToIgnore +func (_m *UtilsCmdInterface) HandleBlock(_a0 *ethclient.Client, account types.Account, stakerId uint32, header *coretypes.Header, config types.Configurations, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) { + _m.Called(_a0, account, stakerId, header, config, httpClient, rogueData, backupNodeActionsToIgnore) } // HandleClaimBounty provides a mock function with given fields: _a0, config, account @@ -1355,13 +1355,13 @@ func (_m *UtilsCmdInterface) IndexRevealEventsOfCurrentEpoch(_a0 *ethclient.Clie return r0, r1 } -// InitiateCommit provides a mock function with given fields: _a0, config, account, epoch, stakerId, httpClient, rogueData -func (_m *UtilsCmdInterface) InitiateCommit(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, httpClient *client.HttpClient, rogueData types.Rogue) error { - ret := _m.Called(_a0, config, account, epoch, stakerId, httpClient, rogueData) +// InitiateCommit provides a mock function with given fields: _a0, config, account, epoch, stakerId, latestHeader, httpClient, rogueData +func (_m *UtilsCmdInterface) InitiateCommit(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, latestHeader *coretypes.Header, httpClient *client.HttpClient, rogueData types.Rogue) error { + ret := _m.Called(_a0, config, account, epoch, stakerId, latestHeader, httpClient, rogueData) var r0 error - if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, uint32, *client.HttpClient, types.Rogue) error); ok { - r0 = rf(_a0, config, account, epoch, stakerId, httpClient, rogueData) + if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, uint32, *coretypes.Header, *client.HttpClient, types.Rogue) error); ok { + r0 = rf(_a0, config, account, epoch, stakerId, latestHeader, httpClient, rogueData) } else { r0 = ret.Error(0) } @@ -1369,13 +1369,13 @@ func (_m *UtilsCmdInterface) InitiateCommit(_a0 *ethclient.Client, config types. return r0 } -// InitiatePropose provides a mock function with given fields: _a0, config, account, epoch, staker, blockNumber, rogueData -func (_m *UtilsCmdInterface) InitiatePropose(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, blockNumber *big.Int, rogueData types.Rogue) error { - ret := _m.Called(_a0, config, account, epoch, staker, blockNumber, rogueData) +// InitiatePropose provides a mock function with given fields: _a0, config, account, epoch, staker, latestHeader, rogueData +func (_m *UtilsCmdInterface) InitiatePropose(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, latestHeader *coretypes.Header, rogueData types.Rogue) error { + ret := _m.Called(_a0, config, account, epoch, staker, latestHeader, rogueData) var r0 error - if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, bindings.StructsStaker, *big.Int, types.Rogue) error); ok { - r0 = rf(_a0, config, account, epoch, staker, blockNumber, rogueData) + if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, bindings.StructsStaker, *coretypes.Header, types.Rogue) error); ok { + r0 = rf(_a0, config, account, epoch, staker, latestHeader, rogueData) } else { r0 = ret.Error(0) } @@ -1383,13 +1383,13 @@ func (_m *UtilsCmdInterface) InitiatePropose(_a0 *ethclient.Client, config types return r0 } -// InitiateReveal provides a mock function with given fields: _a0, config, account, epoch, staker, rogueData -func (_m *UtilsCmdInterface) InitiateReveal(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, rogueData types.Rogue) error { - ret := _m.Called(_a0, config, account, epoch, staker, rogueData) +// InitiateReveal provides a mock function with given fields: _a0, config, account, epoch, staker, latestHeader, rogueData +func (_m *UtilsCmdInterface) InitiateReveal(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, latestHeader *coretypes.Header, rogueData types.Rogue) error { + ret := _m.Called(_a0, config, account, epoch, staker, latestHeader, rogueData) var r0 error - if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, bindings.StructsStaker, types.Rogue) error); ok { - r0 = rf(_a0, config, account, epoch, staker, rogueData) + if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, bindings.StructsStaker, *coretypes.Header, types.Rogue) error); ok { + r0 = rf(_a0, config, account, epoch, staker, latestHeader, rogueData) } else { r0 = ret.Error(0) } @@ -1533,13 +1533,13 @@ func (_m *UtilsCmdInterface) ModifyCollectionStatus(_a0 *ethclient.Client, confi return r0, r1 } -// Propose provides a mock function with given fields: _a0, config, account, staker, epoch, blockNumber, rogueData -func (_m *UtilsCmdInterface) Propose(_a0 *ethclient.Client, config types.Configurations, account types.Account, staker bindings.StructsStaker, epoch uint32, blockNumber *big.Int, rogueData types.Rogue) error { - ret := _m.Called(_a0, config, account, staker, epoch, blockNumber, rogueData) +// Propose provides a mock function with given fields: _a0, config, account, staker, epoch, latestHeader, rogueData +func (_m *UtilsCmdInterface) Propose(_a0 *ethclient.Client, config types.Configurations, account types.Account, staker bindings.StructsStaker, epoch uint32, latestHeader *coretypes.Header, rogueData types.Rogue) error { + ret := _m.Called(_a0, config, account, staker, epoch, latestHeader, rogueData) var r0 error - if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, bindings.StructsStaker, uint32, *big.Int, types.Rogue) error); ok { - r0 = rf(_a0, config, account, staker, epoch, blockNumber, rogueData) + if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, bindings.StructsStaker, uint32, *coretypes.Header, types.Rogue) error); ok { + r0 = rf(_a0, config, account, staker, epoch, latestHeader, rogueData) } else { r0 = ret.Error(0) } @@ -1578,25 +1578,25 @@ func (_m *UtilsCmdInterface) ResetUnstakeLock(_a0 *ethclient.Client, config type return r0, r1 } -// Reveal provides a mock function with given fields: _a0, config, account, epoch, commitData, signature -func (_m *UtilsCmdInterface) Reveal(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, commitData types.CommitData, signature []byte) (common.Hash, error) { - ret := _m.Called(_a0, config, account, epoch, commitData, signature) +// Reveal provides a mock function with given fields: _a0, config, account, epoch, latestHeader, commitData, signature +func (_m *UtilsCmdInterface) Reveal(_a0 *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, latestHeader *coretypes.Header, commitData types.CommitData, signature []byte) (common.Hash, error) { + ret := _m.Called(_a0, config, account, epoch, latestHeader, commitData, signature) var r0 common.Hash var r1 error - if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, types.CommitData, []byte) (common.Hash, error)); ok { - return rf(_a0, config, account, epoch, commitData, signature) + if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, *coretypes.Header, types.CommitData, []byte) (common.Hash, error)); ok { + return rf(_a0, config, account, epoch, latestHeader, commitData, signature) } - if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, types.CommitData, []byte) common.Hash); ok { - r0 = rf(_a0, config, account, epoch, commitData, signature) + if rf, ok := ret.Get(0).(func(*ethclient.Client, types.Configurations, types.Account, uint32, *coretypes.Header, types.CommitData, []byte) common.Hash); ok { + r0 = rf(_a0, config, account, epoch, latestHeader, commitData, signature) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(common.Hash) } } - if rf, ok := ret.Get(1).(func(*ethclient.Client, types.Configurations, types.Account, uint32, types.CommitData, []byte) error); ok { - r1 = rf(_a0, config, account, epoch, commitData, signature) + if rf, ok := ret.Get(1).(func(*ethclient.Client, types.Configurations, types.Account, uint32, *coretypes.Header, types.CommitData, []byte) error); ok { + r1 = rf(_a0, config, account, epoch, latestHeader, commitData, signature) } else { r1 = ret.Error(1) } @@ -1828,13 +1828,13 @@ func (_m *UtilsCmdInterface) UpdateJob(_a0 *ethclient.Client, config types.Confi return r0, r1 } -// Vote provides a mock function with given fields: ctx, config, _a2, account, httpClient, rogueData, backupNodeActionsToIgnore -func (_m *UtilsCmdInterface) Vote(ctx context.Context, config types.Configurations, _a2 *ethclient.Client, account types.Account, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) error { - ret := _m.Called(ctx, config, _a2, account, httpClient, rogueData, backupNodeActionsToIgnore) +// Vote provides a mock function with given fields: ctx, config, _a2, account, stakerId, httpClient, rogueData, backupNodeActionsToIgnore +func (_m *UtilsCmdInterface) Vote(ctx context.Context, config types.Configurations, _a2 *ethclient.Client, account types.Account, stakerId uint32, httpClient *client.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) error { + ret := _m.Called(ctx, config, _a2, account, stakerId, httpClient, rogueData, backupNodeActionsToIgnore) var r0 error - if rf, ok := ret.Get(0).(func(context.Context, types.Configurations, *ethclient.Client, types.Account, *client.HttpClient, types.Rogue, []string) error); ok { - r0 = rf(ctx, config, _a2, account, httpClient, rogueData, backupNodeActionsToIgnore) + if rf, ok := ret.Get(0).(func(context.Context, types.Configurations, *ethclient.Client, types.Account, uint32, *client.HttpClient, types.Rogue, []string) error); ok { + r0 = rf(ctx, config, _a2, account, stakerId, httpClient, rogueData, backupNodeActionsToIgnore) } else { r0 = ret.Error(0) } diff --git a/cmd/propose.go b/cmd/propose.go index 4cf8e6f0d..973fe0fb6 100644 --- a/cmd/propose.go +++ b/cmd/propose.go @@ -4,6 +4,7 @@ package cmd import ( "encoding/hex" "errors" + Types "github.com/ethereum/go-ethereum/core/types" "math" "math/big" "razor/core" @@ -29,8 +30,8 @@ var globalProposedDataStruct types.ProposeFileData // Find iteration using salt as seed //This functions handles the propose state -func (*UtilsStruct) Propose(client *ethclient.Client, config types.Configurations, account types.Account, staker bindings.StructsStaker, epoch uint32, blockNumber *big.Int, rogueData types.Rogue) error { - if state, err := razorUtils.GetBufferedState(client, config.BufferPercent); err != nil || state != 2 { +func (*UtilsStruct) Propose(client *ethclient.Client, config types.Configurations, account types.Account, staker bindings.StructsStaker, epoch uint32, latestHeader *Types.Header, rogueData types.Rogue) error { + if state, err := razorUtils.GetBufferedState(client, latestHeader, config.BufferPercent); err != nil || state != 2 { log.Error("Not propose state") return err } @@ -132,8 +133,8 @@ func (*UtilsStruct) Propose(client *ethclient.Client, config types.Configuration } log.Info("Current iteration is less than iteration of last proposed block, can propose") } - log.Debugf("Propose: Calling MakeBlock() with arguments blockNumber = %s, epoch = %d, rogueData = %+v", blockNumber, epoch, rogueData) - medians, ids, revealedDataMaps, err := cmdUtils.MakeBlock(client, blockNumber, epoch, rogueData) + log.Debugf("Propose: Calling MakeBlock() with arguments blockNumber = %s, epoch = %d, rogueData = %+v", latestHeader.Number, epoch, rogueData) + medians, ids, revealedDataMaps, err := cmdUtils.MakeBlock(client, latestHeader.Number, epoch, rogueData) if err != nil { log.Error(err) return err diff --git a/cmd/propose_test.go b/cmd/propose_test.go index 9eed60a02..3612f7b05 100644 --- a/cmd/propose_test.go +++ b/cmd/propose_test.go @@ -20,18 +20,20 @@ import ( func TestPropose(t *testing.T) { var ( - client *ethclient.Client - account types.Account - config types.Configurations - staker bindings.StructsStaker - epoch uint32 - blockNumber *big.Int + client *ethclient.Client + account types.Account + config types.Configurations + staker bindings.StructsStaker + epoch uint32 ) salt := []byte{142, 170, 157, 83, 109, 43, 34, 152, 21, 154, 159, 12, 195, 119, 50, 186, 218, 57, 39, 173, 228, 135, 20, 100, 149, 27, 169, 158, 34, 113, 66, 64} saltBytes32 := [32]byte{} copy(saltBytes32[:], salt) + latestHeader := &Types.Header{ + Number: big.NewInt(1001), + } type args struct { rogueData types.Rogue state int64 @@ -514,7 +516,7 @@ func TestPropose(t *testing.T) { for _, tt := range tests { SetUpMockInterfaces() - utilsMock.On("GetBufferedState", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("int32")).Return(tt.args.state, tt.args.stateErr) + utilsMock.On("GetBufferedState", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr) utilsMock.On("GetNumberOfStakers", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(tt.args.numStakers, tt.args.numStakerErr) cmdUtilsMock.On("GetBiggestStakeAndId", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string"), mock.AnythingOfType("uint32")).Return(tt.args.biggestStake, tt.args.biggestStakerId, tt.args.biggestStakerIdErr) cmdUtilsMock.On("GetSmallestStakeAndId", mock.Anything, mock.Anything).Return(tt.args.smallestStake, tt.args.smallestStakerId, tt.args.smallestStakerIdErr) @@ -539,7 +541,7 @@ func TestPropose(t *testing.T) { utils := &UtilsStruct{} t.Run(tt.name, func(t *testing.T) { - err := utils.Propose(client, config, account, staker, epoch, blockNumber, tt.args.rogueData) + err := utils.Propose(client, config, account, staker, epoch, latestHeader, tt.args.rogueData) if err == nil || tt.wantErr == nil { if err != tt.wantErr { t.Errorf("Error for Propose function, got = %v, want %v", err, tt.wantErr) diff --git a/cmd/reveal.go b/cmd/reveal.go index bf9b3efb2..0470948d5 100644 --- a/cmd/reveal.go +++ b/cmd/reveal.go @@ -31,8 +31,8 @@ func (*UtilsStruct) CheckForLastCommitted(client *ethclient.Client, staker bindi } //This function checks if the state is reveal or not and then reveals the votes -func (*UtilsStruct) Reveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, commitData types.CommitData, signature []byte) (common.Hash, error) { - if state, err := razorUtils.GetBufferedState(client, config.BufferPercent); err != nil || state != 1 { +func (*UtilsStruct) Reveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, latestHeader *Types.Header, commitData types.CommitData, signature []byte) (common.Hash, error) { + if state, err := razorUtils.GetBufferedState(client, latestHeader, config.BufferPercent); err != nil || state != 1 { log.Error("Not reveal state") return core.NilHash, err } diff --git a/cmd/reveal_test.go b/cmd/reveal_test.go index a1333696a..c276ade07 100644 --- a/cmd/reveal_test.go +++ b/cmd/reveal_test.go @@ -85,12 +85,15 @@ func TestCheckForLastCommitted(t *testing.T) { } func TestReveal(t *testing.T) { - var client *ethclient.Client - var commitData types.CommitData - var signature []byte - var account types.Account - var config types.Configurations - var epoch uint32 + var ( + client *ethclient.Client + commitData types.CommitData + signature []byte + account types.Account + config types.Configurations + epoch uint32 + latestHeader *Types.Header + ) type args struct { state int64 @@ -157,7 +160,7 @@ func TestReveal(t *testing.T) { t.Run(tt.name, func(t *testing.T) { SetUpMockInterfaces() - utilsMock.On("GetBufferedState", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("int32")).Return(tt.args.state, tt.args.stateErr) + utilsMock.On("GetBufferedState", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr) merkleUtilsMock.On("CreateMerkle", mock.Anything).Return(tt.args.merkleTree, tt.args.merkleTreeErr) cmdUtilsMock.On("GenerateTreeRevealData", mock.Anything, mock.Anything).Return(tt.args.treeRevealData) utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(TxnOpts) @@ -166,7 +169,7 @@ func TestReveal(t *testing.T) { utils := &UtilsStruct{} - got, err := utils.Reveal(client, config, account, epoch, commitData, signature) + got, err := utils.Reveal(client, config, account, epoch, latestHeader, commitData, signature) if got != tt.want { t.Errorf("Txn hash for Reveal function, got = %v, want = %v", got, tt.want) } diff --git a/cmd/vote.go b/cmd/vote.go index 6a2e6e25c..5bdfeba2e 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -18,6 +18,8 @@ import ( "razor/utils" "time" + Types "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" "github.com/spf13/pflag" @@ -98,9 +100,16 @@ func (*UtilsStruct) ExecuteVote(flagSet *pflag.FlagSet) { MaxIdleConnectionsPerHost: core.HTTPClientMaxIdleConnsPerHost, }) + stakerId, err := razorUtils.GetStakerId(client, address) + utils.CheckError("Error in getting staker id: ", err) + + if stakerId == 0 { + log.Fatal("Staker doesn't exist") + } + cmdUtils.HandleExit() log.Debugf("Calling Vote() with arguments rogueData = %+v, account address = %s, backup node actions to ignore = %s", rogueData, account.Address, backupNodeActionsToIgnore) - if err := cmdUtils.Vote(context.Background(), config, client, account, httpClient, rogueData, backupNodeActionsToIgnore); err != nil { + if err := cmdUtils.Vote(context.Background(), config, client, account, stakerId, httpClient, rogueData, backupNodeActionsToIgnore); err != nil { log.Errorf("%v\n", err) osUtils.Exit(1) } @@ -131,7 +140,7 @@ func (*UtilsStruct) HandleExit() { } //This function handles all the states of voting -func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, account types.Account, httpClient *clientPkg.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) error { +func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, account types.Account, stakerId uint32, httpClient *clientPkg.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) error { header, err := clientUtils.GetLatestBlockWithRetry(client) utils.CheckError("Error in getting block: ", err) for { @@ -148,7 +157,7 @@ func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, clien log.Debugf("Vote: Latest header value: %d", latestHeader.Number) if latestHeader.Number.Cmp(header.Number) != 0 { header = latestHeader - cmdUtils.HandleBlock(client, account, latestHeader.Number, config, httpClient, rogueData, backupNodeActionsToIgnore) + cmdUtils.HandleBlock(client, account, stakerId, latestHeader, config, httpClient, rogueData, backupNodeActionsToIgnore) } time.Sleep(time.Second * time.Duration(core.BlockNumberInterval)) } @@ -163,8 +172,8 @@ var ( ) //This function handles the block -func (*UtilsStruct) HandleBlock(client *ethclient.Client, account types.Account, blockNumber *big.Int, config types.Configurations, httpClient *clientPkg.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) { - state, err := razorUtils.GetBufferedState(client, config.BufferPercent) +func (*UtilsStruct) HandleBlock(client *ethclient.Client, account types.Account, stakerId uint32, latestHeader *Types.Header, config types.Configurations, httpClient *clientPkg.HttpClient, rogueData types.Rogue, backupNodeActionsToIgnore []string) { + state, err := razorUtils.GetBufferedState(client, latestHeader, config.BufferPercent) if err != nil { log.Error("Error in getting state: ", err) return @@ -175,15 +184,6 @@ func (*UtilsStruct) HandleBlock(client *ethclient.Client, account types.Account, return } - stakerId, err := razorUtils.GetStakerId(client, account.Address) - if err != nil { - log.Error("Error in getting staker id: ", err) - return - } - if stakerId == 0 { - log.Error("Staker doesn't exist") - return - } staker, err := razorUtils.GetStaker(client, stakerId) if err != nil { log.Error(err) @@ -240,21 +240,21 @@ func (*UtilsStruct) HandleBlock(client *ethclient.Client, account types.Account, switch state { case 0: log.Debugf("Starting commit...") - err := cmdUtils.InitiateCommit(client, config, account, epoch, stakerId, httpClient, rogueData) + err := cmdUtils.InitiateCommit(client, config, account, epoch, stakerId, latestHeader, httpClient, rogueData) if err != nil { log.Error(err) break } case 1: log.Debugf("Starting reveal...") - err := cmdUtils.InitiateReveal(client, config, account, epoch, staker, rogueData) + err := cmdUtils.InitiateReveal(client, config, account, epoch, staker, latestHeader, rogueData) if err != nil { log.Error(err) break } case 2: log.Debugf("Starting propose...") - err := cmdUtils.InitiatePropose(client, config, account, epoch, staker, blockNumber, rogueData) + err := cmdUtils.InitiatePropose(client, config, account, epoch, staker, latestHeader, rogueData) if err != nil { log.Error(err) break @@ -267,7 +267,7 @@ func (*UtilsStruct) HandleBlock(client *ethclient.Client, account types.Account, break } - err := cmdUtils.HandleDispute(client, config, account, epoch, blockNumber, rogueData, backupNodeActionsToIgnore) + err := cmdUtils.HandleDispute(client, config, account, epoch, latestHeader.Number, rogueData, backupNodeActionsToIgnore) if err != nil { log.Error(err) break @@ -322,7 +322,7 @@ func (*UtilsStruct) HandleBlock(client *ethclient.Client, account types.Account, } //This function initiates the commit -func (*UtilsStruct) InitiateCommit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, httpClient *clientPkg.HttpClient, rogueData types.Rogue) error { +func (*UtilsStruct) InitiateCommit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, latestHeader *Types.Header, httpClient *clientPkg.HttpClient, rogueData types.Rogue) error { staker, err := razorUtils.GetStaker(client, stakerId) if err != nil { log.Error(err) @@ -371,7 +371,7 @@ func (*UtilsStruct) InitiateCommit(client *ethclient.Client, config types.Config } log.Debug("InitiateCommit: Commit Data: ", commitData) - commitTxn, err := cmdUtils.Commit(client, config, account, epoch, seed, commitData.Leaves) + commitTxn, err := cmdUtils.Commit(client, config, account, epoch, latestHeader, seed, commitData.Leaves) if err != nil { return errors.New("Error in committing data: " + err.Error()) } @@ -404,7 +404,7 @@ func (*UtilsStruct) InitiateCommit(client *ethclient.Client, config types.Config } //This function initiates the reveal -func (*UtilsStruct) InitiateReveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, rogueData types.Rogue) error { +func (*UtilsStruct) InitiateReveal(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, latestHeader *Types.Header, rogueData types.Rogue) error { stakedAmount := staker.Stake log.Debug("InitiateReveal: Staked Amount: ", stakedAmount) minStakeAmount, err := razorUtils.GetMinStakeAmount(client) @@ -515,7 +515,7 @@ func (*UtilsStruct) InitiateReveal(client *ethclient.Client, config types.Config SeqAllottedCollections: globalCommitDataStruct.SeqAllottedCollections, } log.Debugf("InitiateReveal: Calling Reveal() with arguments epoch = %d, commitDataToSend = %+v, signature = %v", epoch, commitDataToSend, signature) - revealTxn, err := cmdUtils.Reveal(client, config, account, epoch, commitDataToSend, signature) + revealTxn, err := cmdUtils.Reveal(client, config, account, epoch, latestHeader, commitDataToSend, signature) if err != nil { return errors.New("Reveal error: " + err.Error()) } @@ -534,7 +534,7 @@ func (*UtilsStruct) InitiateReveal(client *ethclient.Client, config types.Config } //This function initiates the propose -func (*UtilsStruct) InitiatePropose(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, blockNumber *big.Int, rogueData types.Rogue) error { +func (*UtilsStruct) InitiatePropose(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, staker bindings.StructsStaker, latestHeader *Types.Header, rogueData types.Rogue) error { stakedAmount := staker.Stake log.Debug("InitiatePropose: Staked Amount: ", stakedAmount) minStakeAmount, err := razorUtils.GetMinStakeAmount(client) @@ -566,8 +566,8 @@ func (*UtilsStruct) InitiatePropose(client *ethclient.Client, config types.Confi return nil } - log.Debugf("InitiatePropose: Calling Propose() with arguments staker = %+v, epoch = %d, blockNumber = %s, rogueData = %+v", staker, epoch, blockNumber, rogueData) - err = cmdUtils.Propose(client, config, account, staker, epoch, blockNumber, rogueData) + log.Debugf("InitiatePropose: Calling Propose() with arguments staker = %+v, epoch = %d, blockNumber = %s, rogueData = %+v", staker, epoch, latestHeader.Number, rogueData) + err = cmdUtils.Propose(client, config, account, staker, epoch, latestHeader, rogueData) if err != nil { return errors.New("Propose error: " + err.Error()) } diff --git a/cmd/vote_test.go b/cmd/vote_test.go index d53677a1a..dc058b3ce 100644 --- a/cmd/vote_test.go +++ b/cmd/vote_test.go @@ -1,6 +1,7 @@ package cmd import ( + "context" "encoding/hex" "errors" "math/big" @@ -14,6 +15,9 @@ import ( "razor/utils" "reflect" "testing" + "time" + + Types "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient" @@ -36,6 +40,8 @@ func TestExecuteVote(t *testing.T) { rogueModeErr error address string addressErr error + stakerId uint32 + stakerIdErr error voteErr error } tests := []struct { @@ -49,6 +55,7 @@ func TestExecuteVote(t *testing.T) { config: config, password: "test", address: "0x000000000000000000000000000000000000dea1", + stakerId: 1, rogueStatus: true, rogueMode: []string{"propose", "commit"}, voteErr: nil, @@ -111,12 +118,37 @@ func TestExecuteVote(t *testing.T) { config: config, password: "test", address: "0x000000000000000000000000000000000000dea1", + stakerId: 1, rogueStatus: true, rogueMode: []string{"propose", "commit"}, voteErr: errors.New("vote error"), }, expectedFatal: false, }, + { + name: "Test 7: When there is an error in getting stakerId", + args: args{ + config: config, + password: "test", + address: "0x000000000000000000000000000000000000dea1", + stakerIdErr: errors.New("stakerId error"), + rogueStatus: true, + rogueMode: []string{"propose", "commit"}, + }, + expectedFatal: true, + }, + { + name: "Test 8: When stakerId is 0", + args: args{ + config: config, + password: "test", + address: "0x000000000000000000000000000000000000dea1", + stakerId: 0, + rogueStatus: true, + rogueMode: []string{"propose", "commit"}, + }, + expectedFatal: true, + }, } defer func() { log.ExitFunc = nil }() @@ -136,9 +168,10 @@ func TestExecuteVote(t *testing.T) { flagSetMock.On("GetStringSliceBackupNode", mock.Anything).Return([]string{}, nil) utilsMock.On("ConnectToClient", mock.AnythingOfType("string")).Return(client) flagSetMock.On("GetBoolRogue", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.rogueStatus, tt.args.rogueErr) + utilsMock.On("GetStakerId", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(tt.args.stakerId, tt.args.stakerIdErr) flagSetMock.On("GetStringSliceRogueMode", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.rogueMode, tt.args.rogueModeErr) cmdUtilsMock.On("HandleExit").Return() - cmdUtilsMock.On("Vote", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.voteErr) + cmdUtilsMock.On("Vote", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.voteErr) osMock.On("Exit", mock.AnythingOfType("int")).Return() utils := &UtilsStruct{} @@ -301,11 +334,12 @@ func TestCalculateSecret(t *testing.T) { func TestInitiateCommit(t *testing.T) { var ( - client *ethclient.Client - config types.Configurations - account types.Account - stakerId uint32 - rogueData types.Rogue + client *ethclient.Client + config types.Configurations + latestHeader *Types.Header + account types.Account + stakerId uint32 + rogueData types.Rogue ) type args struct { staker bindings.StructsStaker @@ -508,12 +542,12 @@ func TestInitiateCommit(t *testing.T) { cmdUtilsMock.On("GetSalt", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.salt, tt.args.saltErr) cmdUtilsMock.On("HandleCommitState", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.commitData, tt.args.commitDataErr) pathMock.On("GetDefaultPath").Return(tt.args.path, tt.args.pathErr) - cmdUtilsMock.On("Commit", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.commitTxn, tt.args.commitTxnErr) + cmdUtilsMock.On("Commit", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.commitTxn, tt.args.commitTxnErr) utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(tt.args.waitForBlockCompletionErr) pathMock.On("GetCommitDataFileName", mock.AnythingOfType("string")).Return(tt.args.fileName, tt.args.fileNameErr) fileUtilsMock.On("SaveDataToCommitJsonFile", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.saveErr) ut := &UtilsStruct{} - if err := ut.InitiateCommit(client, config, account, tt.args.epoch, stakerId, &clientPkg.HttpClient{}, rogueData); (err != nil) != tt.wantErr { + if err := ut.InitiateCommit(client, config, account, tt.args.epoch, stakerId, latestHeader, &clientPkg.HttpClient{}, rogueData); (err != nil) != tt.wantErr { t.Errorf("InitiateCommit() error = %v, wantErr %v", err, tt.wantErr) } }) @@ -522,9 +556,10 @@ func TestInitiateCommit(t *testing.T) { func TestInitiateReveal(t *testing.T) { var ( - client *ethclient.Client - config types.Configurations - account types.Account + client *ethclient.Client + config types.Configurations + account types.Account + latestHeader *Types.Header ) randomNum := big.NewInt(1111) @@ -730,10 +765,10 @@ func TestInitiateReveal(t *testing.T) { cmdUtilsMock.On("CalculateSecret", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.signature, tt.args.secret, tt.args.secretErr) cmdUtilsMock.On("GetSalt", mock.Anything, mock.Anything).Return([32]byte{}, nil) utilsMock.On("GetCommitment", mock.Anything, mock.Anything).Return(types.Commitment{}, nil) - cmdUtilsMock.On("Reveal", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.revealTxn, tt.args.revealTxnErr) + cmdUtilsMock.On("Reveal", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.revealTxn, tt.args.revealTxnErr) utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(nil) ut := &UtilsStruct{} - if err := ut.InitiateReveal(client, config, account, tt.args.epoch, tt.args.staker, tt.args.rogueData); (err != nil) != tt.wantErr { + if err := ut.InitiateReveal(client, config, account, tt.args.epoch, tt.args.staker, latestHeader, tt.args.rogueData); (err != nil) != tt.wantErr { t.Errorf("InitiateReveal() error = %v, wantErr %v", err, tt.wantErr) } }) @@ -742,11 +777,10 @@ func TestInitiateReveal(t *testing.T) { func TestInitiatePropose(t *testing.T) { var ( - client *ethclient.Client - config types.Configurations - account types.Account - blockNumber *big.Int - rogueData types.Rogue + client *ethclient.Client + config types.Configurations + account types.Account + rogueData types.Rogue ) type args struct { staker bindings.StructsStaker @@ -759,6 +793,10 @@ func TestInitiatePropose(t *testing.T) { lastRevealErr error proposeTxnErr error } + + latestHeader := &Types.Header{ + Number: big.NewInt(1), + } tests := []struct { name string args args @@ -846,10 +884,10 @@ func TestInitiatePropose(t *testing.T) { utilsMock.On("GetMinStakeAmount", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.minStakeAmount, tt.args.minStakeAmountErr) utilsMock.On("GetEpochLastProposed", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32")).Return(tt.args.lastProposal, tt.args.lastProposalErr) utilsMock.On("GetEpochLastRevealed", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32")).Return(tt.args.lastReveal, tt.args.lastRevealErr) - cmdUtilsMock.On("Propose", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.proposeTxnErr) + cmdUtilsMock.On("Propose", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.proposeTxnErr) utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(nil) ut := &UtilsStruct{} - if err := ut.InitiatePropose(client, config, account, tt.args.epoch, tt.args.staker, blockNumber, rogueData); (err != nil) != tt.wantErr { + if err := ut.InitiatePropose(client, config, account, tt.args.epoch, tt.args.staker, latestHeader, rogueData); (err != nil) != tt.wantErr { t.Errorf("InitiatePropose() error = %v, wantErr %v", err, tt.wantErr) } }) @@ -860,11 +898,14 @@ func TestHandleBlock(t *testing.T) { var ( client *ethclient.Client account types.Account - blockNumber *big.Int + stakerId uint32 rogueData types.Rogue backupNodeActionsToIgnore []string ) + latestHeader := &Types.Header{ + Number: big.NewInt(1001), + } type args struct { config types.Configurations state int64 @@ -872,8 +913,6 @@ func TestHandleBlock(t *testing.T) { epoch uint32 epochErr error stateName string - stakerId uint32 - stakerIdErr error staker bindings.StructsStaker stakerErr error ethBalance *big.Int @@ -904,7 +943,6 @@ func TestHandleBlock(t *testing.T) { state: 0, epoch: 1, stateName: "commit", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -926,30 +964,12 @@ func TestHandleBlock(t *testing.T) { epochErr: errors.New("error in getting epoch"), }, }, - { - name: "Test 4: When there is an error in getting stakerId", - args: args{ - state: 0, - epoch: 1, - stakerIdErr: errors.New("error in getting stakerId"), - }, - }, - { - name: "Test 5: When stakerId is 0", - args: args{ - state: 0, - epoch: 1, - stateName: "commit", - stakerId: 0, - }, - }, { name: "Test 6: When there is an error in getting staker", args: args{ state: 0, epoch: 1, stateName: "commit", - stakerId: 1, stakerErr: errors.New("error in getting staker"), }, }, @@ -959,7 +979,6 @@ func TestHandleBlock(t *testing.T) { state: 0, epoch: 1, stateName: "commit", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalanceErr: errors.New("error in getting ethBalance"), }, @@ -970,7 +989,6 @@ func TestHandleBlock(t *testing.T) { state: 0, epoch: 1, stateName: "commit", - stakerId: 1, sRZRBalance: big.NewInt(1000), staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), @@ -984,7 +1002,6 @@ func TestHandleBlock(t *testing.T) { state: 0, epoch: 1, stateName: "commit", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -998,7 +1015,6 @@ func TestHandleBlock(t *testing.T) { state: 0, epoch: 1, stateName: "commit", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(100)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(100), @@ -1013,7 +1029,6 @@ func TestHandleBlock(t *testing.T) { state: 0, epoch: 1, stateName: "commit", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(0)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(0), @@ -1028,7 +1043,6 @@ func TestHandleBlock(t *testing.T) { state: 0, epoch: 1, stateName: "commit", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000), IsSlashed: true}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1043,7 +1057,6 @@ func TestHandleBlock(t *testing.T) { state: 0, epoch: 1, stateName: "commit", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1059,7 +1072,6 @@ func TestHandleBlock(t *testing.T) { state: 1, epoch: 1, stateName: "reveal", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1075,7 +1087,6 @@ func TestHandleBlock(t *testing.T) { state: 2, epoch: 1, stateName: "propose", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1091,7 +1102,6 @@ func TestHandleBlock(t *testing.T) { state: 3, epoch: 1, stateName: "dispute", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1107,7 +1117,6 @@ func TestHandleBlock(t *testing.T) { state: 3, epoch: 1, stateName: "dispute", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1123,7 +1132,6 @@ func TestHandleBlock(t *testing.T) { state: 3, epoch: 1, stateName: "dispute", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1141,7 +1149,6 @@ func TestHandleBlock(t *testing.T) { epoch: 1, stateName: "confirm", lastVerification: 1, - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1158,7 +1165,6 @@ func TestHandleBlock(t *testing.T) { epoch: 2, stateName: "confirm", lastVerification: 1, - stakerId: 2, staker: bindings.StructsStaker{Id: 2, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1175,7 +1181,6 @@ func TestHandleBlock(t *testing.T) { epoch: 1, lastVerification: 4, stateName: "dispute", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1191,7 +1196,6 @@ func TestHandleBlock(t *testing.T) { epoch: 1, lastVerification: 4, stateName: "", - stakerId: 1, staker: bindings.StructsStaker{Id: 1, Stake: big.NewInt(10000)}, ethBalance: big.NewInt(1000), actualStake: big.NewFloat(10000), @@ -1206,16 +1210,15 @@ func TestHandleBlock(t *testing.T) { t.Run(tt.name, func(t *testing.T) { SetUpMockInterfaces() - utilsMock.On("GetBufferedState", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("int32")).Return(tt.args.state, tt.args.stateErr) + utilsMock.On("GetBufferedState", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr) utilsMock.On("GetEpoch", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.epoch, tt.args.epochErr) - utilsMock.On("GetStakerId", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(tt.args.stakerId, tt.args.stakerIdErr) utilsMock.On("GetStaker", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32")).Return(tt.args.staker, tt.args.stakerErr) clientUtilsMock.On("BalanceAtWithRetry", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.ethBalance, tt.args.ethBalanceErr) utilsMock.On("GetStakerSRZRBalance", mock.Anything, mock.Anything).Return(tt.args.sRZRBalance, tt.args.sRZRBalanceErr) osMock.On("Exit", mock.AnythingOfType("int")).Return() - cmdUtilsMock.On("InitiateCommit", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateCommitErr) - cmdUtilsMock.On("InitiateReveal", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateRevealErr) - cmdUtilsMock.On("InitiatePropose", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateProposeErr) + cmdUtilsMock.On("InitiateCommit", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateCommitErr) + cmdUtilsMock.On("InitiateReveal", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateRevealErr) + cmdUtilsMock.On("InitiatePropose", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.initiateProposeErr) cmdUtilsMock.On("HandleDispute", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.handleDisputeErr) utilsMock.On("IsFlagPassed", mock.AnythingOfType("string")).Return(tt.args.isFlagPassed) cmdUtilsMock.On("HandleClaimBounty", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.handleClaimBountyErr) @@ -1225,7 +1228,67 @@ func TestHandleBlock(t *testing.T) { utilsMock.On("WaitTillNextNSecs", mock.AnythingOfType("int32")).Return() lastVerification = tt.args.lastVerification ut := &UtilsStruct{} - ut.HandleBlock(client, account, blockNumber, tt.args.config, &clientPkg.HttpClient{}, rogueData, backupNodeActionsToIgnore) + ut.HandleBlock(client, account, stakerId, latestHeader, tt.args.config, &clientPkg.HttpClient{}, rogueData, backupNodeActionsToIgnore) + }) + } +} + +func TestVote(t *testing.T) { + var ( + config types.Configurations + client *ethclient.Client + rogueData types.Rogue + account types.Account + stakerId uint32 + httpClient *clientPkg.HttpClient + backupNodeActionsToIgnore []string + ) + type args struct { + header *Types.Header + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Test when context is cancelled", + args: args{ + header: &Types.Header{ + Number: big.NewInt(101), + }, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + SetUpMockInterfaces() + + clientUtilsMock.On("GetLatestBlockWithRetry", mock.Anything).Return(tt.args.header, nil) + cmdUtilsMock.On("HandleBlock", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + + ut := &UtilsStruct{} + errChan := make(chan error) + // Run Vote function in a goroutine + go func() { + errChan <- ut.Vote(ctx, config, client, account, stakerId, httpClient, rogueData, backupNodeActionsToIgnore) + }() + + // Wait for some time to allow Vote function to execute + time.Sleep(time.Second * 2) + + // Cancel the context to simulate its done + cancel() + + // Check the error returned from the function + err := <-errChan + if (err != nil) != tt.wantErr { + t.Errorf("Vote() error = %v, wantErr %v", err, tt.wantErr) + } }) } } diff --git a/utils/common.go b/utils/common.go index 9148d5cee..3167e77ce 100644 --- a/utils/common.go +++ b/utils/common.go @@ -3,6 +3,7 @@ package utils import ( "context" "errors" + Types "github.com/ethereum/go-ethereum/core/types" "math/big" "os" "path/filepath" @@ -52,21 +53,17 @@ func (*UtilsStruct) FetchBalance(client *ethclient.Client, accountAddress string return balance, nil } -func (*UtilsStruct) GetBufferedState(client *ethclient.Client, buffer int32) (int64, error) { - block, err := ClientInterface.GetLatestBlockWithRetry(client) - if err != nil { - return -1, err - } +func (*UtilsStruct) GetBufferedState(client *ethclient.Client, header *Types.Header, buffer int32) (int64, error) { stateBuffer, err := UtilsInterface.GetStateBuffer(client) if err != nil { return -1, err } lowerLimit := (core.StateLength * uint64(buffer)) / 100 upperLimit := core.StateLength - (core.StateLength*uint64(buffer))/100 - if block.Time%(core.StateLength) > upperLimit-stateBuffer || block.Time%(core.StateLength) < lowerLimit+stateBuffer { + if header.Time%(core.StateLength) > upperLimit-stateBuffer || header.Time%(core.StateLength) < lowerLimit+stateBuffer { return -1, nil } - state := block.Time / core.StateLength + state := header.Time / core.StateLength return int64(state % core.NumberOfStates), nil } diff --git a/utils/common_test.go b/utils/common_test.go index bac674473..cea9ca0fb 100644 --- a/utils/common_test.go +++ b/utils/common_test.go @@ -366,7 +366,6 @@ func TestGetBufferedState(t *testing.T) { type args struct { block *types.Header - blockErr error buffer int32 stateBuffer uint64 stateBufferErr error @@ -391,18 +390,7 @@ func TestGetBufferedState(t *testing.T) { wantErr: false, }, { - name: "Test 2: When there is an error in getting block", - args: args{ - block: &types.Header{ - Number: big.NewInt(100), - }, - blockErr: errors.New("block error"), - }, - want: -1, - wantErr: true, - }, - { - name: "Test 3: When blockNumber%(core.StateLength) is greater than lowerLimit", + name: "Test 2: When blockNumber%(core.StateLength) is greater than lowerLimit", args: args{ block: &types.Header{ Time: 1080, @@ -414,7 +402,7 @@ func TestGetBufferedState(t *testing.T) { wantErr: false, }, { - name: "Test 4: When GetBufferedState() executes successfully and state we get is other than 0", + name: "Test 3: When GetBufferedState() executes successfully and state we get is other than 0", args: args{ block: &types.Header{ Time: 900, @@ -427,7 +415,7 @@ func TestGetBufferedState(t *testing.T) { wantErr: false, }, { - name: "Test 5: When there is an error in getting stateBuffer", + name: "Test 4: When there is an error in getting stateBuffer", args: args{ block: &types.Header{ Time: 100, @@ -454,9 +442,8 @@ func TestGetBufferedState(t *testing.T) { utils := StartRazor(optionsPackageStruct) utilsMock.On("GetStateBuffer", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.stateBuffer, tt.args.stateBufferErr) - clientUtilsMock.On("GetLatestBlockWithRetry", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.block, tt.args.blockErr) - got, err := utils.GetBufferedState(client, tt.args.buffer) + got, err := utils.GetBufferedState(client, tt.args.block, tt.args.buffer) if (err != nil) != tt.wantErr { t.Errorf("GetBufferedState() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/utils/interface.go b/utils/interface.go index ce19a0c21..077868238 100644 --- a/utils/interface.go +++ b/utils/interface.go @@ -126,7 +126,7 @@ type Utils interface { HandleOfficialJobsFromJSONFile(client *ethclient.Client, collection bindings.StructsCollection, dataString string) ([]bindings.StructsJob, []uint16) ConnectToClient(provider string) *ethclient.Client FetchBalance(client *ethclient.Client, accountAddress string) (*big.Int, error) - GetBufferedState(client *ethclient.Client, buffer int32) (int64, error) + GetBufferedState(client *ethclient.Client, header *Types.Header, buffer int32) (int64, error) WaitForBlockCompletion(client *ethclient.Client, hashToRead string) error CheckEthBalanceIsZero(client *ethclient.Client, address string) AssignStakerId(flagSet *pflag.FlagSet, client *ethclient.Client, address string) (uint32, error) diff --git a/utils/mocks/utils.go b/utils/mocks/utils.go index 24fe1c5bb..5c513f965 100644 --- a/utils/mocks/utils.go +++ b/utils/mocks/utils.go @@ -14,6 +14,8 @@ import ( common "github.com/ethereum/go-ethereum/common" + coretypes "github.com/ethereum/go-ethereum/core/types" + ethclient "github.com/ethereum/go-ethereum/ethclient" mock "github.com/stretchr/testify/mock" @@ -570,23 +572,23 @@ func (_m *Utils) GetBlockManagerWithOpts(_a0 *ethclient.Client) (*bindings.Block return r0, r1 } -// GetBufferedState provides a mock function with given fields: _a0, buffer -func (_m *Utils) GetBufferedState(_a0 *ethclient.Client, buffer int32) (int64, error) { - ret := _m.Called(_a0, buffer) +// GetBufferedState provides a mock function with given fields: _a0, header, buffer +func (_m *Utils) GetBufferedState(_a0 *ethclient.Client, header *coretypes.Header, buffer int32) (int64, error) { + ret := _m.Called(_a0, header, buffer) var r0 int64 var r1 error - if rf, ok := ret.Get(0).(func(*ethclient.Client, int32) (int64, error)); ok { - return rf(_a0, buffer) + if rf, ok := ret.Get(0).(func(*ethclient.Client, *coretypes.Header, int32) (int64, error)); ok { + return rf(_a0, header, buffer) } - if rf, ok := ret.Get(0).(func(*ethclient.Client, int32) int64); ok { - r0 = rf(_a0, buffer) + if rf, ok := ret.Get(0).(func(*ethclient.Client, *coretypes.Header, int32) int64); ok { + r0 = rf(_a0, header, buffer) } else { r0 = ret.Get(0).(int64) } - if rf, ok := ret.Get(1).(func(*ethclient.Client, int32) error); ok { - r1 = rf(_a0, buffer) + if rf, ok := ret.Get(1).(func(*ethclient.Client, *coretypes.Header, int32) error); ok { + r1 = rf(_a0, header, buffer) } else { r1 = ret.Error(1) }