Skip to content

Commit 982502c

Browse files
authored
randomSortedProposedBlockIds fix (#823)
* randomSortedProposedBlockIds fix * tests added
1 parent 34a5207 commit 982502c

File tree

6 files changed

+225
-87
lines changed

6 files changed

+225
-87
lines changed

cmd/dispute.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/ethereum/go-ethereum/ethclient"
1111
solsha3 "github.com/miguelmota/go-solidity-sha3"
1212
"math/big"
13-
"math/rand"
1413
"os"
1514
"razor/core"
1615
"razor/core/types"
@@ -49,7 +48,7 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
4948
return err
5049
}
5150

52-
randomSortedProposedBlockIds := rand.Perm(len(sortedProposedBlockIds)) //returns random permutation of integers from 0 to n-1
51+
randomSortedProposedBlockIds := utils.UtilsInterface.Shuffle(sortedProposedBlockIds) //shuffles the sortedProposedBlockIds array
5352
transactionOptions := types.TransactionOptions{
5453
Client: client,
5554
Password: account.Password,

cmd/dispute_test.go

+128-85
Original file line numberDiff line numberDiff line change
@@ -142,27 +142,28 @@ func TestHandleDispute(t *testing.T) {
142142
var blockManager *bindings.BlockManager
143143

144144
type args struct {
145-
sortedProposedBlockIds []uint32
146-
sortedProposedBlockIdsErr error
147-
biggestStake *big.Int
148-
biggestStakeId uint32
149-
biggestStakeErr error
150-
medians []*big.Int
151-
revealedCollectionIds []uint16
152-
revealedDataMaps *types.RevealedDataMaps
153-
mediansErr error
154-
proposedBlock bindings.StructsBlock
155-
proposedBlockErr error
156-
disputeBiggestStakeTxn *Types.Transaction
157-
disputeBiggestStakeErr error
158-
Hash common.Hash
159-
idDisputeTxn *Types.Transaction
160-
idDisputeTxnErr error
161-
misMatchIndex int
162-
leafId uint16
163-
leafIdErr error
164-
disputeErr error
165-
storeBountyIdErr error
145+
sortedProposedBlockIds []uint32
146+
sortedProposedBlockIdsErr error
147+
biggestStake *big.Int
148+
biggestStakeId uint32
149+
biggestStakeErr error
150+
randomSortedProposedBlockIds []uint32
151+
medians []*big.Int
152+
revealedCollectionIds []uint16
153+
revealedDataMaps *types.RevealedDataMaps
154+
mediansErr error
155+
proposedBlock bindings.StructsBlock
156+
proposedBlockErr error
157+
disputeBiggestStakeTxn *Types.Transaction
158+
disputeBiggestStakeErr error
159+
Hash common.Hash
160+
idDisputeTxn *Types.Transaction
161+
idDisputeTxnErr error
162+
misMatchIndex int
163+
leafId uint16
164+
leafIdErr error
165+
disputeErr error
166+
storeBountyIdErr error
166167
}
167168
tests := []struct {
168169
name string
@@ -172,11 +173,12 @@ func TestHandleDispute(t *testing.T) {
172173
{
173174
name: "Test 1: When HandleDispute function executes successfully",
174175
args: args{
175-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
176-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
177-
biggestStakeId: 2,
178-
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
179-
revealedCollectionIds: []uint16{1},
176+
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
177+
randomSortedProposedBlockIds: []uint32{2, 3, 1, 4, 0},
178+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
179+
biggestStakeId: 2,
180+
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
181+
revealedCollectionIds: []uint16{1},
180182
revealedDataMaps: &types.RevealedDataMaps{
181183
SortedRevealedValues: nil,
182184
VoteWeights: nil,
@@ -194,9 +196,10 @@ func TestHandleDispute(t *testing.T) {
194196
{
195197
name: "Test 2: When HandleDispute function executes successfully when there is no dispute case",
196198
args: args{
197-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
198-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
199-
biggestStakeId: 2,
199+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
200+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
201+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
202+
biggestStakeId: 2,
200203
proposedBlock: bindings.StructsBlock{
201204
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
202205
BiggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
@@ -219,18 +222,20 @@ func TestHandleDispute(t *testing.T) {
219222
{
220223
name: "Test 4: When there is an error in getting proposedBlock",
221224
args: args{
222-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
223-
proposedBlockErr: errors.New("proposedBlock error"),
224-
disputeErr: nil,
225+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
226+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
227+
proposedBlockErr: errors.New("proposedBlock error"),
228+
disputeErr: nil,
225229
},
226230
want: nil,
227231
},
228232
{
229233
name: "Test 5: When there is a case of Dispute but block is already disputed",
230234
args: args{
231-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
232-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
233-
biggestStakeId: 2,
235+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
236+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
237+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
238+
biggestStakeId: 2,
234239
proposedBlock: bindings.StructsBlock{
235240
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
236241
BiggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
@@ -241,9 +246,10 @@ func TestHandleDispute(t *testing.T) {
241246
{
242247
name: "Test 6: When HandleDispute function executes successfully when there is a biggest influence dispute case",
243248
args: args{
244-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
245-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
246-
biggestStakeId: 2,
249+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
250+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
251+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
252+
biggestStakeId: 2,
247253
proposedBlock: bindings.StructsBlock{
248254
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
249255
Valid: true,
@@ -258,8 +264,9 @@ func TestHandleDispute(t *testing.T) {
258264
{
259265
name: "Test 7: When there is an error in getting biggestInfluenceAndId",
260266
args: args{
261-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
262-
biggestStakeErr: errors.New("biggestInfluenceAndIdErr"),
267+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
268+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
269+
biggestStakeErr: errors.New("biggestInfluenceAndIdErr"),
263270
proposedBlock: bindings.StructsBlock{
264271
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
265272
Valid: true,
@@ -275,9 +282,10 @@ func TestHandleDispute(t *testing.T) {
275282
{
276283
name: "Test 8: When DisputeBiggestStakeProposed transaction fails",
277284
args: args{
278-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
279-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
280-
biggestStakeId: 2,
285+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
286+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
287+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
288+
biggestStakeId: 2,
281289
proposedBlock: bindings.StructsBlock{
282290
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
283291
Valid: true,
@@ -292,21 +300,23 @@ func TestHandleDispute(t *testing.T) {
292300
{
293301
name: "Test 9: When there is an error in getting medians",
294302
args: args{
295-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
296-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
297-
biggestStakeId: 2,
298-
mediansErr: errors.New("error in getting medians"),
303+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
304+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
305+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
306+
biggestStakeId: 2,
307+
mediansErr: errors.New("error in getting medians"),
299308
},
300309
want: errors.New("error in getting medians"),
301310
},
302311
{
303312
name: "Test 10: When there is an error in fetching Ids from CheckDisputeForIds",
304313
args: args{
305-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
306-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
307-
biggestStakeId: 2,
308-
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
309-
revealedCollectionIds: []uint16{1},
314+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
315+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
316+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
317+
biggestStakeId: 2,
318+
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
319+
revealedCollectionIds: []uint16{1},
310320
revealedDataMaps: &types.RevealedDataMaps{
311321
SortedRevealedValues: nil,
312322
VoteWeights: nil,
@@ -325,11 +335,12 @@ func TestHandleDispute(t *testing.T) {
325335
{
326336
name: "Test 11: When idDisputeTxn is not nil",
327337
args: args{
328-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
329-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
330-
biggestStakeId: 2,
331-
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
332-
revealedCollectionIds: []uint16{1},
338+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
339+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
340+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
341+
biggestStakeId: 2,
342+
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
343+
revealedCollectionIds: []uint16{1},
333344
revealedDataMaps: &types.RevealedDataMaps{
334345
SortedRevealedValues: nil,
335346
VoteWeights: nil,
@@ -348,11 +359,12 @@ func TestHandleDispute(t *testing.T) {
348359
{
349360
name: "Test 12: When it is a median dispute case and error in getting leafId",
350361
args: args{
351-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
352-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
353-
biggestStakeId: 2,
354-
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
355-
revealedCollectionIds: []uint16{1},
362+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
363+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
364+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
365+
biggestStakeId: 2,
366+
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
367+
revealedCollectionIds: []uint16{1},
356368
revealedDataMaps: &types.RevealedDataMaps{
357369
SortedRevealedValues: nil,
358370
VoteWeights: nil,
@@ -373,11 +385,12 @@ func TestHandleDispute(t *testing.T) {
373385
{
374386
name: "Test 13: When there is an error in dispute",
375387
args: args{
376-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
377-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
378-
biggestStakeId: 2,
379-
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
380-
revealedCollectionIds: []uint16{1},
388+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
389+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
390+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
391+
biggestStakeId: 2,
392+
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
393+
revealedCollectionIds: []uint16{1},
381394
revealedDataMaps: &types.RevealedDataMaps{
382395
SortedRevealedValues: nil,
383396
VoteWeights: nil,
@@ -398,9 +411,10 @@ func TestHandleDispute(t *testing.T) {
398411
{
399412
name: "Test 14: When there is a biggest influence dispute case but there is an error in storing bountyId",
400413
args: args{
401-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
402-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
403-
biggestStakeId: 2,
414+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
415+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
416+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
417+
biggestStakeId: 2,
404418
proposedBlock: bindings.StructsBlock{
405419
Medians: []*big.Int{big.NewInt(6701548), big.NewInt(478307)},
406420
Valid: true,
@@ -416,11 +430,12 @@ func TestHandleDispute(t *testing.T) {
416430
{
417431
name: "Test 15: When there is a idsDispute case but there is an error in storing bountyId",
418432
args: args{
419-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
420-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
421-
biggestStakeId: 2,
422-
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
423-
revealedCollectionIds: []uint16{1},
433+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
434+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
435+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
436+
biggestStakeId: 2,
437+
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
438+
revealedCollectionIds: []uint16{1},
424439
revealedDataMaps: &types.RevealedDataMaps{
425440
SortedRevealedValues: nil,
426441
VoteWeights: nil,
@@ -440,11 +455,12 @@ func TestHandleDispute(t *testing.T) {
440455
{
441456
name: "Test 16: When HandleDispute function executes successfully and medians proposed are empty",
442457
args: args{
443-
sortedProposedBlockIds: []uint32{3, 1, 2, 0, 4},
444-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
445-
biggestStakeId: 2,
446-
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
447-
revealedCollectionIds: []uint16{1},
458+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
459+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
460+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
461+
biggestStakeId: 2,
462+
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
463+
revealedCollectionIds: []uint16{1},
448464
revealedDataMaps: &types.RevealedDataMaps{
449465
SortedRevealedValues: nil,
450466
VoteWeights: nil,
@@ -462,11 +478,35 @@ func TestHandleDispute(t *testing.T) {
462478
{
463479
name: "Test 17: When there is a case of blockIndex = -1",
464480
args: args{
465-
sortedProposedBlockIds: []uint32{3, 1, 2, 5, 4},
466-
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
467-
biggestStakeId: 2,
468-
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
469-
revealedCollectionIds: []uint16{1},
481+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
482+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
483+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
484+
biggestStakeId: 2,
485+
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
486+
revealedCollectionIds: []uint16{1},
487+
revealedDataMaps: &types.RevealedDataMaps{
488+
SortedRevealedValues: nil,
489+
VoteWeights: nil,
490+
InfluenceSum: nil,
491+
},
492+
proposedBlock: bindings.StructsBlock{
493+
Medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
494+
Valid: true,
495+
BiggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
496+
},
497+
disputeErr: nil,
498+
},
499+
want: nil,
500+
},
501+
{
502+
name: "Test 18: When HandleDispute function executes successfully and contains different values in sortedProposedBlockIds",
503+
args: args{
504+
sortedProposedBlockIds: []uint32{45, 65, 23, 64, 12},
505+
randomSortedProposedBlockIds: []uint32{23, 64, 12, 65, 23},
506+
biggestStake: big.NewInt(1).Mul(big.NewInt(5356), big.NewInt(1e18)),
507+
biggestStakeId: 2,
508+
medians: []*big.Int{big.NewInt(6901548), big.NewInt(498307)},
509+
revealedCollectionIds: []uint16{1},
470510
revealedDataMaps: &types.RevealedDataMaps{
471511
SortedRevealedValues: nil,
472512
VoteWeights: nil,
@@ -502,6 +542,7 @@ func TestHandleDispute(t *testing.T) {
502542
utilsMock.On("GetSortedProposedBlockIds", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32")).Return(tt.args.sortedProposedBlockIds, tt.args.sortedProposedBlockIdsErr)
503543
cmdUtilsMock.On("GetBiggestStakeAndId", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string"), mock.AnythingOfType("uint32")).Return(tt.args.biggestStake, tt.args.biggestStakeId, tt.args.biggestStakeErr)
504544
cmdUtilsMock.On("GetLocalMediansData", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.medians, tt.args.revealedCollectionIds, tt.args.revealedDataMaps, tt.args.mediansErr)
545+
utilsPkgMock.On("Shuffle", mock.Anything).Return(tt.args.randomSortedProposedBlockIds)
505546
utilsMock.On("GetProposedBlock", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32"), mock.AnythingOfType("uint32")).Return(tt.args.proposedBlock, tt.args.proposedBlockErr)
506547
utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts)
507548
blockManagerUtilsMock.On("DisputeBiggestStakeProposed", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.disputeBiggestStakeTxn, tt.args.disputeBiggestStakeErr)
@@ -1132,6 +1173,7 @@ func BenchmarkHandleDispute(b *testing.B) {
11321173
var blockNumber *big.Int
11331174
var rogueData types.Rogue
11341175
var blockManager *bindings.BlockManager
1176+
var randomSortedPorposedBlockIds []uint32
11351177

11361178
table := []struct {
11371179
numOfSortedBlocks uint32
@@ -1171,6 +1213,7 @@ func BenchmarkHandleDispute(b *testing.B) {
11711213
utilsMock.On("GetSortedProposedBlockIds", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32")).Return(getUint32DummyIds(v.numOfSortedBlocks), nil)
11721214
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)
11731215
cmdUtilsMock.On("GetLocalMediansData", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(medians, revealedCollectionIds, revealedDataMaps, nil)
1216+
utilsPkgMock.On("Shuffle", mock.Anything).Return(randomSortedPorposedBlockIds)
11741217
utilsMock.On("GetProposedBlock", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32"), mock.AnythingOfType("uint32")).Return(proposedBlock, nil)
11751218
utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts)
11761219
blockManagerUtilsMock.On("DisputeBiggestStakeProposed", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&Types.Transaction{}, nil)

utils/interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ type Utils interface {
170170
AssignLogFile(flagSet *pflag.FlagSet)
171171
CalculateBlockNumberAtEpochBeginning(client *ethclient.Client, epochLength int64, currentBlockNumber *big.Int) (*big.Int, error)
172172
GetStateName(stateNumber int64) string
173+
Shuffle(slice []uint32) []uint32
173174
}
174175

175176
type EthClientUtils interface {

0 commit comments

Comments
 (0)