diff --git a/cmd/dispute.go b/cmd/dispute.go index 0535cb65e..9cd548bc3 100644 --- a/cmd/dispute.go +++ b/cmd/dispute.go @@ -173,7 +173,21 @@ func (*UtilsStruct) GetLocalMediansData(client *ethclient.Client, account types. _revealedCollectionIds = proposedata.RevealedCollectionIds } CalculateMedian: - if _mediansData == nil || _revealedCollectionIds == nil || _revealedDataMaps == nil || rogueData.IsRogue { + stakerId, err := razorUtils.GetStakerId(client, account.Address) + if err != nil { + log.Error("Error in getting stakerId: ", err) + return nil, nil, nil, err + } + lastProposedEpoch, err := cmdUtils.GetLastProposedEpoch(client, blockNumber, stakerId) + if err != nil { + log.Error("Error in getting last proposed epoch: ", err) + return nil, nil, nil, err + } + + nilProposedData := _mediansData == nil || _revealedCollectionIds == nil || _revealedDataMaps == nil + epochCheck := epoch != lastProposedEpoch + + if nilProposedData || rogueData.IsRogue || epochCheck { medians, revealedCollectionIds, revealedDataMaps, err := cmdUtils.MakeBlock(client, blockNumber, epoch, types.Rogue{IsRogue: false}) if err != nil { log.Error("Error in calculating block medians") diff --git a/cmd/dispute_test.go b/cmd/dispute_test.go index 716f817e0..bcce179ac 100644 --- a/cmd/dispute_test.go +++ b/cmd/dispute_test.go @@ -533,6 +533,10 @@ func TestGetLocalMediansData(t *testing.T) { revealedCollectionIds []uint16 revealedDataMaps *types.RevealedDataMaps mediansErr error + stakerId uint32 + stakerIdErr error + lastProposedEpoch uint32 + lastProposedEpochErr error } tests := []struct { name string @@ -597,6 +601,34 @@ func TestGetLocalMediansData(t *testing.T) { want2: &types.RevealedDataMaps{}, wantErr: false, }, + { + name: "Test 6: When there is an error in getting stakerId", + args: args{ + medians: []*big.Int{big.NewInt(100), big.NewInt(200), big.NewInt(300)}, + revealedCollectionIds: []uint16{1, 2, 3}, + revealedDataMaps: &types.RevealedDataMaps{}, + stakerIdErr: errors.New("stakerId error"), + }, + want: nil, + want1: nil, + want2: nil, + wantErr: true, + }, + { + name: "Test 7: When there is an error in getting last proposed epoch", + args: args{ + medians: []*big.Int{big.NewInt(100), big.NewInt(200), big.NewInt(300)}, + revealedCollectionIds: []uint16{1, 2, 3}, + revealedDataMaps: &types.RevealedDataMaps{}, + stakerId: 2, + epoch: 5, + lastProposedEpochErr: errors.New("lastProposedEpoch error"), + }, + want: nil, + want1: nil, + want2: nil, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -609,6 +641,9 @@ func TestGetLocalMediansData(t *testing.T) { utilsMock.On("GetProposeDataFileName", mock.AnythingOfType("string")).Return(tt.args.fileName, tt.args.fileNameErr) utilsMock.On("ReadFromProposeJsonFile", mock.Anything).Return(tt.args.proposedData, tt.args.proposeDataErr) cmdUtilsMock.On("MakeBlock", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything, mock.Anything).Return(tt.args.medians, tt.args.revealedCollectionIds, tt.args.revealedDataMaps, tt.args.mediansErr) + utilsMock.On("GetStakerId", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(tt.args.stakerId, tt.args.stakerIdErr) + cmdUtilsMock.On("GetLastProposedEpoch", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("*big.Int"), mock.AnythingOfType("uint32")).Return(tt.args.lastProposedEpoch, tt.args.lastProposedEpochErr) + ut := &UtilsStruct{} got, got1, got2, err := ut.GetLocalMediansData(client, account, tt.args.epoch, blockNumber, rogueData) if (err != nil) != tt.wantErr {