Skip to content

Commit

Permalink
Fixed dispute tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 committed Sep 30, 2022
1 parent 3b549c0 commit 2d50f49
Showing 1 changed file with 91 additions and 112 deletions.
203 changes: 91 additions & 112 deletions cmd/dispute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,27 @@ func TestHandleDispute(t *testing.T) {
var backupNodeActionsToIgnore []string

type args struct {
sortedProposedBlockIds []uint32
sortedProposedBlockIdsErr error
biggestStake *big.Int
biggestStakeId uint32
biggestStakeErr error
randomSortedProposedBlockIds []uint32
medians []*big.Int
revealedCollectionIds []uint16
revealedDataMaps *types.RevealedDataMaps
mediansErr error
proposedBlock bindings.StructsBlock
proposedBlockErr error
disputeBiggestStakeTxn *Types.Transaction
disputeBiggestStakeErr error
Hash common.Hash
idDisputeTxn *Types.Transaction
idDisputeTxnErr error
misMatchIndex int
leafId uint16
leafIdErr error
disputeErr error
storeBountyIdErr error
sortedProposedBlockIds []uint32
sortedProposedBlockIdsErr error
biggestStake *big.Int
biggestStakeId uint32
biggestStakeErr error
medians []*big.Int
revealedCollectionIds []uint16
revealedDataMaps *types.RevealedDataMaps
mediansErr error
proposedBlock bindings.StructsBlock
proposedBlockErr error
disputeBiggestStakeTxn *Types.Transaction
disputeBiggestStakeErr error
Hash common.Hash
idDisputeTxn *Types.Transaction
idDisputeTxnErr error
misMatchIndex int
leafId uint16
leafIdErr error
disputeErr error
storeBountyIdErr error
}
tests := []struct {
name string
Expand All @@ -180,12 +179,11 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 1: When HandleDispute function executes successfully",
args: args{
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
randomSortedProposedBlockIds: []uint32{2, 3, 1, 4, 0},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
revealedDataMaps: &types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
Expand All @@ -203,10 +201,9 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 2: When HandleDispute function executes successfully when there is no dispute case",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
proposedBlock: bindings.StructsBlock{
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
BiggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
Expand All @@ -229,20 +226,18 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 4: When there is an error in getting proposedBlock",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
proposedBlockErr: errors.New("proposedBlock error"),
disputeErr: nil,
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
proposedBlockErr: errors.New("proposedBlock error"),
disputeErr: nil,
},
want: nil,
},
{
name: "Test 5: When there is a case of Dispute but block is already disputed",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
proposedBlock: bindings.StructsBlock{
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
BiggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
Expand All @@ -253,10 +248,9 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 6: When HandleDispute function executes successfully when there is a biggest influence dispute case",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
proposedBlock: bindings.StructsBlock{
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
Valid: true,
Expand All @@ -271,9 +265,8 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 7: When there is an error in getting biggestInfluenceAndId",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStakeErr: errors.New("biggestInfluenceAndIdErr"),
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStakeErr: errors.New("biggestInfluenceAndIdErr"),
proposedBlock: bindings.StructsBlock{
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
Valid: true,
Expand All @@ -289,10 +282,9 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 8: When DisputeBiggestStakeProposed transaction fails",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
proposedBlock: bindings.StructsBlock{
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
Valid: true,
Expand All @@ -307,23 +299,21 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 9: When there is an error in getting medians",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
mediansErr: errors.New("error in getting medians"),
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
mediansErr: errors.New("error in getting medians"),
},
want: errors.New("error in getting medians"),
},
{
name: "Test 10: When there is an error in fetching Ids from CheckDisputeForIds",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
revealedDataMaps: &types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
Expand All @@ -342,12 +332,11 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 11: When idDisputeTxn is not nil",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
revealedDataMaps: &types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
Expand All @@ -366,12 +355,11 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 12: When it is a median dispute case and error in getting leafId",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
revealedDataMaps: &types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
Expand All @@ -392,12 +380,11 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 13: When there is an error in dispute",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
revealedDataMaps: &types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
Expand All @@ -418,10 +405,9 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 14: When there is a biggest influence dispute case but there is an error in storing bountyId",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
proposedBlock: bindings.StructsBlock{
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
Valid: true,
Expand All @@ -437,12 +423,11 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 15: When there is a idsDispute case but there is an error in storing bountyId",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
revealedDataMaps: &types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
Expand All @@ -462,12 +447,11 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 16: When HandleDispute function executes successfully and medians proposed are empty",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
revealedDataMaps: &types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
Expand All @@ -485,12 +469,11 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 17: When there is a case of blockIndex = -1",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
revealedDataMaps: &types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
Expand All @@ -508,12 +491,11 @@ func TestHandleDispute(t *testing.T) {
{
name: "Test 18: When HandleDispute function executes successfully and contains different values in sortedProposedBlockIds",
args: args{
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
biggestStakeId: 2,
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
revealedCollectionIds: []uint16{1},
revealedDataMaps: &types.RevealedDataMaps{
SortedRevealedValues: nil,
VoteWeights: nil,
Expand Down Expand Up @@ -553,7 +535,6 @@ func TestHandleDispute(t *testing.T) {
RevealedCollectionIds: tt.args.revealedCollectionIds,
RevealedDataMaps: tt.args.revealedDataMaps,
}, tt.args.mediansErr)
utilsPkgMock.On("Shuffle", mock.Anything).Return(tt.args.randomSortedProposedBlockIds)
utilsMock.On("GetProposedBlock", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32"), mock.AnythingOfType("uint32")).Return(tt.args.proposedBlock, tt.args.proposedBlockErr)
utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts)
blockManagerUtilsMock.On("DisputeBiggestStakeProposed", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.disputeBiggestStakeTxn, tt.args.disputeBiggestStakeErr)
Expand Down Expand Up @@ -639,7 +620,7 @@ func TestGiveSorted(t *testing.T) {
leafId: 0,
sortedValues: nil,
},
wantErr: false,
wantErr: true,
},
{
name: "Test 4: When there is an error in getting disputesMapping",
Expand Down Expand Up @@ -1233,7 +1214,6 @@ func BenchmarkHandleDispute(b *testing.B) {
var blockNumber *big.Int
var rogueData types.Rogue
var blockManager *bindings.BlockManager
var randomSortedPorposedBlockIds []uint32
var backupNodeActionsToIgnore []string

table := []struct {
Expand Down Expand Up @@ -1279,7 +1259,6 @@ func BenchmarkHandleDispute(b *testing.B) {
utilsMock.On("GetSortedProposedBlockIds", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32")).Return(getUint32DummyIds(v.numOfSortedBlocks), nil)
cmdUtilsMock.On("GetBiggestStakeAndId", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string"), mock.AnythingOfType("uint32")).Return(big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)), uint32(2), nil)
cmdUtilsMock.On("GetLocalMediansData", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(proposedData, nil)
utilsPkgMock.On("Shuffle", mock.Anything).Return(randomSortedPorposedBlockIds)
utilsMock.On("GetProposedBlock", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32"), mock.AnythingOfType("uint32")).Return(proposedBlock, nil)
utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts)
blockManagerUtilsMock.On("DisputeBiggestStakeProposed", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&Types.Transaction{}, nil)
Expand Down

0 comments on commit 2d50f49

Please sign in to comment.