Skip to content

Commit

Permalink
Fetched updated data for dispute (#745)
Browse files Browse the repository at this point in the history
* Added lastProposedEpoch check in local median caluclations

* Added tests

* Made OR conditions readable
  • Loading branch information
Yashk767 authored Jun 2, 2022
1 parent 16c31c4 commit a5be2fb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
16 changes: 15 additions & 1 deletion cmd/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
35 changes: 35 additions & 0 deletions cmd/dispute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit a5be2fb

Please sign in to comment.