From 85fc96eac88544954e80102944e7c84c560d5f94 Mon Sep 17 00:00:00 2001 From: Yashk767 Date: Fri, 21 Jan 2022 13:11:52 +0530 Subject: [PATCH 1/3] Added check that ony proposer can claim block reward --- cmd/confirm.go | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/cmd/confirm.go b/cmd/confirm.go index 415fd0513..688074fab 100644 --- a/cmd/confirm.go +++ b/cmd/confirm.go @@ -10,13 +10,42 @@ import ( var blockManagerUtils blockManagerInterface func (utilsStruct UtilsStruct) ClaimBlockReward(options types.TransactionOptions) (common.Hash, error) { - log.Info("Claiming block reward...") - txnOpts := utilsStruct.razorUtils.GetTxnOpts(options) - txn, err := utilsStruct.blockManagerUtils.ClaimBlockReward(options.Client, txnOpts) + epoch, err := utilsStruct.razorUtils.GetEpoch(options.Client) if err != nil { - log.Error("Error in claiming block reward: ", err) + log.Error("Error in getting epoch: ", err) return core.NilHash, err } - log.Info("Txn Hash: ", utilsStruct.transactionUtils.Hash(txn).Hex()) - return utilsStruct.transactionUtils.Hash(txn), nil + + sortedProposedBlockIds, err := utilsStruct.razorUtils.GetSortedProposedBlockIds(options.Client, options.AccountAddress, epoch) + if err != nil { + log.Error("Error in getting sortedProposedBlockIds: ", err) + return core.NilHash, err + } + + stakerID, err := utilsStruct.razorUtils.GetStakerId(options.Client, options.AccountAddress) + if err != nil { + log.Error("Error in getting stakerId: ", err) + return core.NilHash, err + } + + selectedProposedBlock, err := utilsStruct.razorUtils.GetProposedBlock(options.Client, options.AccountAddress, epoch, sortedProposedBlockIds[0]) + if err != nil { + log.Error("Error in getting selectedProposedBlock: ", err) + return core.NilHash, err + } + + if selectedProposedBlock.ProposerId == stakerID { + log.Info("Claiming block reward...") + txnOpts := utilsStruct.razorUtils.GetTxnOpts(options) + txn, err := utilsStruct.blockManagerUtils.ClaimBlockReward(options.Client, txnOpts) + if err != nil { + log.Error("Error in claiming block reward: ", err) + return core.NilHash, err + } + log.Info("Txn Hash: ", utilsStruct.transactionUtils.Hash(txn).Hex()) + return utilsStruct.transactionUtils.Hash(txn), nil + } + + log.Debug("Only selected block proposer can claim block reward") + return core.NilHash, nil } From d95f0d8fb700958c1ab699fd5a7c0710e80310c8 Mon Sep 17 00:00:00 2001 From: Yashk767 Date: Fri, 21 Jan 2022 13:13:11 +0530 Subject: [PATCH 2/3] Moved nil txn condition --- cmd/stake.go | 2 ++ cmd/vote.go | 28 ++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd/stake.go b/cmd/stake.go index 03970ac4c..58e6cf088 100644 --- a/cmd/stake.go +++ b/cmd/stake.go @@ -127,6 +127,8 @@ func init() { transactionUtils = TransactionUtils{} proposeUtils = ProposeUtils{} cmdUtils = UtilsCmd{} + utils.Options = &utils.OptionsStruct{} + utils.UtilsInterface = &utils.UtilsStruct{} rootCmd.AddCommand(stakeCmd) var ( diff --git a/cmd/vote.go b/cmd/vote.go index 7b733cb93..b57afa2a7 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -204,11 +204,13 @@ func handleBlock(client *ethclient.Client, account types.Account, blockNumber *b break } commitTxn, err := utilsStruct.Commit(client, data, secret, account, config) - if err != nil || commitTxn == core.NilHash { + if err != nil { log.Error("Error in committing data: ", err) break } - utils.WaitForBlockCompletion(client, commitTxn.String()) + if commitTxn != core.NilHash { + utils.WaitForBlockCompletion(client, commitTxn.String()) + } _committedData = data log.Debug("Saving committed data for recovery") fileName, err := getCommitDataFileName(account.Address, utilsStruct) @@ -270,12 +272,13 @@ func handleBlock(client *ethclient.Client, account types.Account, blockNumber *b } revealTxn, err := utilsStruct.Reveal(client, _committedData, secret, account, account.Address, config) - if err != nil || revealTxn == core.NilHash { + if err != nil { log.Error("Reveal error: ", err) break } - utils.WaitForBlockCompletion(client, revealTxn.String()) - + if revealTxn != core.NilHash { + utils.WaitForBlockCompletion(client, revealTxn.String()) + } case 2: lastProposal, err := getLastProposedEpoch(client, blockNumber, stakerId) if err != nil { @@ -296,11 +299,13 @@ func handleBlock(client *ethclient.Client, account types.Account, blockNumber *b break } proposeTxn, err := utilsStruct.Propose(client, account, config, stakerId, epoch, rogueData) - if err != nil || proposeTxn == core.NilHash { + if err != nil { log.Error("Propose error: ", err) break } - utils.WaitForBlockCompletion(client, proposeTxn.String()) + if proposeTxn != core.NilHash { + utils.WaitForBlockCompletion(client, proposeTxn.String()) + } case 3: if lastVerification >= epoch { break @@ -324,12 +329,15 @@ func handleBlock(client *ethclient.Client, account types.Account, blockNumber *b ABI: jobManager.BlockManagerABI, }) - if err != nil || txn == core.NilHash { + if err != nil { log.Error("ClaimBlockReward error: ", err) break } - utils.WaitForBlockCompletion(client, txn.Hex()) - blockConfirmed = epoch + if txn != core.NilHash { + utils.WaitForBlockCompletion(client, txn.Hex()) + blockConfirmed = epoch + } + } case -1: if config.WaitTime > 5 { From 18c05e32d9c8970ddc27885c21c893cd2a78cecf Mon Sep 17 00:00:00 2001 From: Yashk767 Date: Fri, 21 Jan 2022 13:13:35 +0530 Subject: [PATCH 3/3] Additional tests --- cmd/confirm_test.go | 113 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 99 insertions(+), 14 deletions(-) diff --git a/cmd/confirm_test.go b/cmd/confirm_test.go index 2b47a0c2b..37234a89c 100644 --- a/cmd/confirm_test.go +++ b/cmd/confirm_test.go @@ -8,6 +8,7 @@ import ( "math/big" "razor/core" "razor/core/types" + "razor/pkg/bindings" "testing" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -30,10 +31,18 @@ func TestClaimBlockReward(t *testing.T) { } type args struct { - txnOpts *bind.TransactOpts - ClaimBlockRewardTxn *Types.Transaction - ClaimBlockRewardErr error - hash common.Hash + epoch uint32 + epochErr error + stakerId uint32 + stakerIdErr error + sortedProposedBlockIds []uint32 + sortedProposedBlockIdsErr error + selectedBlock bindings.StructsBlock + selectedBlockErr error + txnOpts *bind.TransactOpts + ClaimBlockRewardTxn *Types.Transaction + ClaimBlockRewardErr error + hash common.Hash } tests := []struct { name string @@ -42,30 +51,106 @@ func TestClaimBlockReward(t *testing.T) { wantErr error }{ { - name: "Test1: When ClaimBlockReward function executes successfully", + name: "Test 1: When ClaimBlockReward function executes successfully", args: args{ - txnOpts: txnOpts, - ClaimBlockRewardTxn: &Types.Transaction{}, - ClaimBlockRewardErr: nil, - hash: common.BigToHash(big.NewInt(1)), + epoch: 5, + stakerId: 2, + sortedProposedBlockIds: []uint32{2, 1, 3}, + selectedBlock: bindings.StructsBlock{ProposerId: 2}, + txnOpts: txnOpts, + ClaimBlockRewardTxn: &Types.Transaction{}, + ClaimBlockRewardErr: nil, + hash: common.BigToHash(big.NewInt(1)), }, want: common.BigToHash(big.NewInt(1)), wantErr: nil, }, { - name: "Test2: When ClaimBlockReward transaction fails", + name: "Test 2: When ClaimBlockReward transaction fails", args: args{ - txnOpts: txnOpts, - ClaimBlockRewardTxn: &Types.Transaction{}, - ClaimBlockRewardErr: errors.New("claimBlockReward error"), - hash: common.BigToHash(big.NewInt(1)), + epoch: 5, + stakerId: 2, + sortedProposedBlockIds: []uint32{2, 1, 3}, + selectedBlock: bindings.StructsBlock{ProposerId: 2}, + txnOpts: txnOpts, + ClaimBlockRewardTxn: &Types.Transaction{}, + ClaimBlockRewardErr: errors.New("claimBlockReward error"), + hash: common.BigToHash(big.NewInt(1)), }, want: core.NilHash, wantErr: errors.New("claimBlockReward error"), }, + { + name: "Test 3: When there is an error in getting epoch", + args: args{ + epochErr: errors.New("epoch error"), + }, + want: core.NilHash, + wantErr: errors.New("epoch error"), + }, + { + name: "Test 4: When there is an error in getting stakerId", + args: args{ + epoch: 5, + stakerIdErr: errors.New("stakerId error"), + }, + want: core.NilHash, + wantErr: errors.New("stakerId error"), + }, + { + name: "Test 5: When there is an error in getting sortedProposedBlockIds", + args: args{ + epoch: 5, + stakerId: 2, + sortedProposedBlockIdsErr: errors.New("sortedProposedBlockIds error"), + }, + want: core.NilHash, + wantErr: errors.New("sortedProposedBlockIds error"), + }, + { + name: "Test 6: When there is an error in getting proposedBlock", + args: args{ + epoch: 5, + stakerId: 2, + sortedProposedBlockIds: []uint32{2, 1, 3}, + selectedBlockErr: errors.New("block error"), + }, + want: core.NilHash, + wantErr: errors.New("block error"), + }, + { + name: "Test 7: When stakerId != proposerId and ClaimBlockReward function executes successfully", + args: args{ + epoch: 5, + stakerId: 3, + sortedProposedBlockIds: []uint32{2, 1, 3}, + selectedBlock: bindings.StructsBlock{ProposerId: 2}, + txnOpts: txnOpts, + ClaimBlockRewardTxn: &Types.Transaction{}, + ClaimBlockRewardErr: nil, + hash: common.BigToHash(big.NewInt(1)), + }, + want: core.NilHash, + wantErr: nil, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + GetEpochMock = func(*ethclient.Client) (uint32, error) { + return tt.args.epoch, tt.args.epochErr + } + + GetStakerIdMock = func(*ethclient.Client, string) (uint32, error) { + return tt.args.stakerId, tt.args.stakerIdErr + } + + GetSortedProposedBlockIdsMock = func(*ethclient.Client, string, uint32) ([]uint32, error) { + return tt.args.sortedProposedBlockIds, tt.args.sortedProposedBlockIdsErr + } + + GetProposedBlockMock = func(*ethclient.Client, string, uint32, uint32) (bindings.StructsBlock, error) { + return tt.args.selectedBlock, tt.args.selectedBlockErr + } GetTxnOptsMock = func(types.TransactionOptions) *bind.TransactOpts { return tt.args.txnOpts