Skip to content

Commit

Permalink
Merge pull request #505 from Yashk767/ClaimBlockRewardFix
Browse files Browse the repository at this point in the history
Claim block reward hotfix
  • Loading branch information
SkandaBhat authored Jan 21, 2022
2 parents 9859ee4 + 18c05e3 commit 6782f86
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 30 deletions.
41 changes: 35 additions & 6 deletions cmd/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
113 changes: 99 additions & 14 deletions cmd/confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"
"razor/core"
"razor/core/types"
"razor/pkg/bindings"
"testing"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmd/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func init() {
transactionUtils = TransactionUtils{}
proposeUtils = ProposeUtils{}
cmdUtils = UtilsCmd{}
utils.Options = &utils.OptionsStruct{}
utils.UtilsInterface = &utils.UtilsStruct{}

rootCmd.AddCommand(stakeCmd)
var (
Expand Down
28 changes: 18 additions & 10 deletions cmd/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 6782f86

Please sign in to comment.