Skip to content

Commit

Permalink
fix: added validation to check if index to fetch element is within li…
Browse files Browse the repository at this point in the history
…mits (#1270)
  • Loading branch information
Yashk767 authored Feb 28, 2025
1 parent 96f1e28 commit 486a93a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/propose.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func (*UtilsStruct) Propose(rpcParameters rpc.RPCParameters, config types.Config
return err
}
log.Debug("Propose: Sorted proposed blocks: ", sortedProposedBlocks)

if numOfProposedBlocks <= 0 || len(sortedProposedBlocks) < int(numOfProposedBlocks) {
log.Errorf("Invalid numOfProposedBlocks (%d) or mismatch with sortedProposedBlocks length (%d)", numOfProposedBlocks, len(sortedProposedBlocks))
return errors.New("proposed blocks count mismatch")
}

lastBlockIndex := sortedProposedBlocks[numOfProposedBlocks-1]
log.Debug("Propose: Last block index: ", lastBlockIndex)
lastProposedBlockStruct, err := razorUtils.GetProposedBlock(rpcParameters, epoch, lastBlockIndex)
Expand Down
15 changes: 15 additions & 0 deletions cmd/propose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,21 @@ func TestPropose(t *testing.T) {
},
wantErr: errors.New("txnOpts error"),
},
{
name: "Test 20: When there is a mismatch in number of proposed blocks and length of sorted proposed blocks array",
args: args{
state: 2,
staker: bindings.StructsStaker{},
numStakers: 5,
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakerId: 2,
salt: saltBytes32,
iteration: 1,
numOfProposedBlocks: 3,
sortedProposedBlockIds: []uint32{2, 1},
},
wantErr: errors.New("proposed blocks count mismatch"),
},
}
for _, tt := range tests {
SetUpMockInterfaces()
Expand Down

0 comments on commit 486a93a

Please sign in to comment.