Skip to content

Commit e7c488d

Browse files
authored
Coverage increased (#684)
* Removed unused functions * Covered commit rogue case * Fixed dispute tests * Covered and tested missing lines of code * minor change
1 parent 5dbdd5d commit e7c488d

18 files changed

+213
-478
lines changed

cmd/commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
6868
return types.CommitData{}, err
6969
}
7070
if rogueData.IsRogue && utils.Contains(rogueData.RogueMode, "commit") {
71-
collectionData = utils.GetRogueRandomValue(100000)
71+
collectionData = razorUtils.GetRogueRandomValue(100000)
7272
}
7373
log.Debugf("Data of collection %d:%s", collectionId, collectionData)
7474
leavesOfTree = append(leavesOfTree, collectionData)

cmd/commit_test.go

+29-6
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,12 @@ func TestCommit(t *testing.T) {
122122

123123
func TestHandleCommitState(t *testing.T) {
124124
var (
125-
client *ethclient.Client
126-
epoch uint32
127-
seed []byte
128-
rogueData types.Rogue
125+
client *ethclient.Client
126+
epoch uint32
127+
seed []byte
129128
)
130129

131-
//rogueValue := big.NewInt(int64(randMath.Intn(10000000)))
130+
rogueValue := utils.GetRogueRandomValue(100000)
132131

133132
type args struct {
134133
numActiveCollections uint16
@@ -140,6 +139,7 @@ func TestHandleCommitState(t *testing.T) {
140139
collectionIdErr error
141140
collectionData *big.Int
142141
collectionDataErr error
142+
rogueData types.Rogue
143143
}
144144
tests := []struct {
145145
name string
@@ -203,20 +203,43 @@ func TestHandleCommitState(t *testing.T) {
203203
want: types.CommitData{},
204204
wantErr: errors.New("error in getting collectionData"),
205205
},
206+
{
207+
name: "Test 6: When rogue mode is on for commit state",
208+
args: args{
209+
numActiveCollections: 3,
210+
assignedCollections: map[int]bool{1: true, 2: true},
211+
seqAllottedCollections: []*big.Int{big.NewInt(1), big.NewInt(2)},
212+
collectionId: 1,
213+
collectionData: big.NewInt(1),
214+
rogueData: types.Rogue{
215+
IsRogue: true,
216+
RogueMode: []string{"commit"},
217+
},
218+
},
219+
want: types.CommitData{
220+
AssignedCollections: map[int]bool{1: true, 2: true},
221+
SeqAllottedCollections: []*big.Int{big.NewInt(1), big.NewInt(2)},
222+
Leaves: []*big.Int{big.NewInt(0), rogueValue, rogueValue},
223+
},
224+
wantErr: nil,
225+
},
206226
}
207227
for _, tt := range tests {
208228
t.Run(tt.name, func(t *testing.T) {
209229
utilsPkgMock := new(mocks2.Utils)
230+
utilsMock := new(mocks.UtilsInterface)
210231

211232
utils.UtilsInterface = utilsPkgMock
233+
razorUtils = utilsMock
212234

213235
utilsPkgMock.On("GetNumActiveCollections", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.numActiveCollections, tt.args.numActiveCollectionsErr)
214236
utilsPkgMock.On("GetAssignedCollections", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(tt.args.assignedCollections, tt.args.seqAllottedCollections, tt.args.assignedCollectionsErr)
215237
utilsPkgMock.On("GetCollectionIdFromIndex", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.collectionId, tt.args.collectionIdErr)
216238
utilsPkgMock.On("GetAggregatedDataOfCollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(tt.args.collectionData, tt.args.collectionDataErr)
239+
utilsMock.On("GetRogueRandomValue", mock.Anything).Return(rogueValue)
217240

218241
utils := &UtilsStruct{}
219-
got, err := utils.HandleCommitState(client, epoch, seed, rogueData)
242+
got, err := utils.HandleCommitState(client, epoch, seed, tt.args.rogueData)
220243
if !reflect.DeepEqual(got, tt.want) {
221244
t.Errorf("Data from HandleCommitState function, got = %v, want = %v", got, tt.want)
222245
}

cmd/dispute.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
109109
}
110110

111111
// Median Value dispute
112-
isEqual, mismatchIndex := utils.UtilsInterface.IsEqualUint32(proposedBlock.Medians, medians)
112+
isEqual, mismatchIndex := utils.IsEqualUint32(proposedBlock.Medians, medians)
113113
if !isEqual {
114114
log.Warn("BLOCK NOT MATCHING WITH LOCAL CALCULATIONS.")
115115
log.Debug("Block Values: ", proposedBlock.Medians)
@@ -192,14 +192,14 @@ func (*UtilsStruct) CheckDisputeForIds(client *ethclient.Client, transactionOpts
192192
hashIdsInProposedBlock := solsha3.SoliditySHA3([]string{"uint16[]"}, []interface{}{idsInProposedBlock})
193193
hashRevealedCollectionIds := solsha3.SoliditySHA3([]string{"uint16[]"}, []interface{}{revealedCollectionIds})
194194

195-
isEqual, _ := utils.UtilsInterface.IsEqualByte(hashIdsInProposedBlock, hashRevealedCollectionIds)
195+
isEqual, _ := utils.IsEqualByte(hashIdsInProposedBlock, hashRevealedCollectionIds)
196196

197197
if isEqual {
198198
return nil, nil
199199
}
200200

201201
// Check if the error is in sorted ids
202-
isSorted, index0, index1 := utils.UtilsInterface.IsSorted(idsInProposedBlock)
202+
isSorted, index0, index1 := utils.IsSorted(idsInProposedBlock)
203203
if !isSorted {
204204
transactionOpts.ABI = bindings.BlockManagerABI
205205
transactionOpts.MethodName = "disputeOnOrderOfIds"
@@ -211,7 +211,7 @@ func (*UtilsStruct) CheckDisputeForIds(client *ethclient.Client, transactionOpts
211211
}
212212

213213
// Check if the error is collectionIdShouldBePresent
214-
isMissing, _, missingCollectionId := utils.UtilsInterface.IsMissing(revealedCollectionIds, idsInProposedBlock)
214+
isMissing, _, missingCollectionId := utils.IsMissing(revealedCollectionIds, idsInProposedBlock)
215215
if isMissing {
216216
transactionOpts.ABI = bindings.BlockManagerABI
217217
transactionOpts.MethodName = "disputeCollectionIdShouldBePresent"
@@ -229,7 +229,7 @@ func (*UtilsStruct) CheckDisputeForIds(client *ethclient.Client, transactionOpts
229229
}
230230

231231
// Check if the error is collectionIdShouldBeAbsent
232-
isPresent, positionOfPresentValue, presentCollectionId := utils.UtilsInterface.IsMissing(idsInProposedBlock, revealedCollectionIds)
232+
isPresent, positionOfPresentValue, presentCollectionId := utils.IsMissing(idsInProposedBlock, revealedCollectionIds)
233233
if isPresent {
234234
transactionOpts.ABI = bindings.BlockManagerABI
235235
transactionOpts.MethodName = "disputeCollectionIdShouldBeAbsent"

cmd/dispute_test.go

+26-39
Original file line numberDiff line numberDiff line change
@@ -704,25 +704,14 @@ func TestCheckDisputeForIds(t *testing.T) {
704704
txnOpts, _ := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(31337))
705705

706706
type args struct {
707-
isEqual bool
708-
isEqualInt int
709707
idsInProposedBlock []uint16
710708
revealedCollectionIds []uint16
711-
isSorted bool
712-
index0 int
713-
index1 int
714709
DisputeOnOrderOfIds *Types.Transaction
715710
DisputeOnOrderOfIdsErr error
716-
isMissing bool
717-
isMissingInt int
718711
incrementedGasLimit uint64
719712
incrementedGasLimitErr error
720-
missingCollectionId uint16
721713
DisputeCollectionIdShouldBePresent *Types.Transaction
722714
DisputeCollectionIdShouldBePresentErr error
723-
isPresent bool
724-
positionOfPresentValue int
725-
presentCollectionId uint16
726715
DisputeCollectionIdShouldBeAbsent *Types.Transaction
727716
DisputeCollectionIdShouldBeAbsentErr error
728717
}
@@ -733,59 +722,61 @@ func TestCheckDisputeForIds(t *testing.T) {
733722
wantErr bool
734723
}{
735724
{
736-
name: "Test 1: When CheckDisputeForIds executes successfully and check if the error is in sorted ids",
725+
name: "Test 1: When CheckDisputeForIds executes successfully with sorted Ids dispute case",
737726
args: args{
738-
isEqual: false,
739-
idsInProposedBlock: []uint16{1, 2, 3},
727+
idsInProposedBlock: []uint16{1, 3, 2},
740728
revealedCollectionIds: []uint16{1, 2, 3},
741-
isSorted: false,
742729
DisputeOnOrderOfIds: &Types.Transaction{},
743730
},
744731
want: &Types.Transaction{},
745732
wantErr: false,
746733
},
747734
{
748-
name: "Test 2: When CheckDisputeForIds executes successfully and check if the error is collectionIdShouldBePresent",
735+
name: "Test 2: When CheckDisputeForIds executes successfully with collectionIdShouldBePresent dispute case",
749736
args: args{
750-
isEqual: false,
751-
idsInProposedBlock: []uint16{1, 2, 3},
752-
isSorted: true,
753-
isMissing: true,
737+
idsInProposedBlock: []uint16{1, 2, 4},
738+
revealedCollectionIds: []uint16{1, 2, 3},
754739
DisputeCollectionIdShouldBePresent: &Types.Transaction{},
755740
},
756741
want: &Types.Transaction{},
757742
wantErr: false,
758743
},
759744
{
760-
name: "Test 3: When there is an error in incrementalGasLimit",
745+
name: "Test 3: When its a collectionIdShouldBePresent dispute case and their is an error in incrementalGasLimit",
761746
args: args{
762-
isEqual: false,
763-
idsInProposedBlock: []uint16{1, 2, 3},
764-
isSorted: true,
765-
isMissing: true,
766-
incrementedGasLimitErr: errors.New("error in incremented gas limit"),
767-
DisputeCollectionIdShouldBePresent: &Types.Transaction{},
747+
idsInProposedBlock: []uint16{1, 2, 4},
748+
revealedCollectionIds: []uint16{1, 2, 3},
749+
incrementedGasLimitErr: errors.New("error in incremented gas limit"),
768750
},
769751
want: nil,
770752
wantErr: true,
771753
},
772754
{
773-
name: "Test 4: When there is no error",
755+
name: "Test 4: When CheckDisputeForIds executes successfully with collectionIdShouldBeAbsent case",
774756
args: args{
775-
isEqual: false,
776-
idsInProposedBlock: []uint16{1, 2, 3},
777-
isSorted: true,
778-
isMissing: false,
779-
isPresent: false,
757+
idsInProposedBlock: []uint16{1, 2, 3, 4},
758+
revealedCollectionIds: []uint16{1, 2, 3},
780759
DisputeCollectionIdShouldBeAbsent: &Types.Transaction{},
781760
},
782-
want: nil,
761+
want: &Types.Transaction{},
783762
wantErr: false,
784763
},
764+
{
765+
name: "Test 5: When its a collectionIdShouldBeAbsent dispute case and their is an error in incrementalGasLimit",
766+
args: args{
767+
idsInProposedBlock: []uint16{1, 2, 3, 4},
768+
revealedCollectionIds: []uint16{1, 2, 3},
769+
DisputeCollectionIdShouldBeAbsent: &Types.Transaction{},
770+
incrementedGasLimitErr: errors.New("error in incremented gas limit"),
771+
},
772+
want: nil,
773+
wantErr: true,
774+
},
785775
{
786776
name: "Test 5: When there is no dispute",
787777
args: args{
788-
isEqual: true,
778+
idsInProposedBlock: []uint16{1, 2, 3},
779+
revealedCollectionIds: []uint16{1, 2, 3},
789780
},
790781
want: nil,
791782
wantErr: false,
@@ -802,14 +793,10 @@ func TestCheckDisputeForIds(t *testing.T) {
802793
blockManagerUtils = blockManagerUtilsMock
803794
utilsInterface = utilsPkgMock
804795

805-
utilsPkgMock.On("IsEqualByte", mock.Anything, mock.Anything).Return(tt.args.isEqual, tt.args.isEqualInt)
806-
utilsPkgMock.On("IsSorted", mock.Anything).Return(tt.args.isSorted, tt.args.index0, tt.args.index1)
807796
utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts)
808797
blockManagerUtilsMock.On("DisputeOnOrderOfIds", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.DisputeOnOrderOfIds, tt.args.DisputeOnOrderOfIdsErr)
809-
utilsPkgMock.On("IsMissing", mock.Anything, mock.Anything).Return(tt.args.isMissing, tt.args.isMissingInt, tt.args.missingCollectionId)
810798
utilsPkgMock.On("IncreaseGasLimitValue", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.incrementedGasLimit, tt.args.incrementedGasLimitErr)
811799
blockManagerUtilsMock.On("DisputeCollectionIdShouldBePresent", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.DisputeCollectionIdShouldBePresent, tt.args.DisputeCollectionIdShouldBePresentErr)
812-
utilsPkgMock.On("IsMissing", mock.Anything, mock.Anything).Return(tt.args.isPresent, tt.args.positionOfPresentValue, tt.args.presentCollectionId)
813800
blockManagerUtilsMock.On("DisputeCollectionIdShouldBeAbsent", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.DisputeCollectionIdShouldBeAbsent, tt.args.DisputeCollectionIdShouldBeAbsentErr)
814801
utilsPkgMock.On("IncreaseGasLimitValue", mock.Anything, mock.Anything, mock.Anything).Return(uint64(2000), nil)
815802
ut := &UtilsStruct{}

cmd/interface.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type UtilsInterface interface {
6565
WaitForBlockCompletion(*ethclient.Client, string) int
6666
GetNumActiveCollections(*ethclient.Client) (uint16, error)
6767
GetRogueRandomValue(int) *big.Int
68+
GetRogueRandomMedianValue() uint32
6869
GetAggregatedDataOfCollection(client *ethclient.Client, collectionId uint16, epoch uint32) (*big.Int, error)
6970
GetDelayedState(*ethclient.Client, int32) (int64, error)
7071
GetDefaultPath() (string, error)
@@ -112,8 +113,6 @@ type UtilsInterface interface {
112113
GetStake(*ethclient.Client, uint32) (*big.Int, error)
113114
ConvertWeiToEth(*big.Int) (*big.Float, error)
114115
WaitTillNextNSecs(int32)
115-
SaveDataToFile(string, uint32, []*big.Int) error
116-
ReadDataFromFile(string) (uint32, []*big.Int, error)
117116
DeleteJobFromJSON(string, string) error
118117
AddJobToJSON(string, *types.StructsJob) error
119118
GetStakerSRZRBalance(*ethclient.Client, bindings.StructsStaker) (*big.Int, error)

cmd/mocks/utils_interface.go

+14-44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/propose.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ func (*UtilsStruct) Propose(client *ethclient.Client, config types.Configuration
144144
log.Debugf("Iteration: %d Biggest Staker Id: %d", iteration, biggestStakerId)
145145
log.Info("Proposing block...")
146146

147-
if err != nil {
148-
return core.NilHash, err
149-
}
150147
txnOpts := razorUtils.GetTxnOpts(types.TransactionOptions{
151148
Client: client,
152149
Password: account.Password,
@@ -326,7 +323,7 @@ func (*UtilsStruct) MakeBlock(client *ethclient.Client, blockNumber *big.Int, ep
326323
if influenceSum != nil && influenceSum.Cmp(big.NewInt(0)) != 0 {
327324
idsRevealedInThisEpoch = append(idsRevealedInThisEpoch, activeCollections[leafId])
328325
if rogueData.IsRogue && utils.Contains(rogueData.RogueMode, "medians") {
329-
medians = append(medians, rand.Uint32())
326+
medians = append(medians, razorUtils.GetRogueRandomMedianValue())
330327
continue
331328
}
332329
accWeight := big.NewInt(0)
@@ -346,7 +343,7 @@ func (*UtilsStruct) MakeBlock(client *ethclient.Client, blockNumber *big.Int, ep
346343
}
347344
if rogueData.IsRogue && utils.Contains(rogueData.RogueMode, "extraIds") {
348345
//Adding a dummy median and appending extra id to idsRevealed array if rogueMode == extraIds
349-
medians = append(medians, rand.Uint32())
346+
medians = append(medians, razorUtils.GetRogueRandomMedianValue())
350347
idsRevealedInThisEpoch = append(idsRevealedInThisEpoch, idsRevealedInThisEpoch[len(idsRevealedInThisEpoch)-1]+1)
351348
}
352349
if rogueData.IsRogue && utils.Contains(rogueData.RogueMode, "unsortedIds") && len(idsRevealedInThisEpoch) > 1 {

0 commit comments

Comments
 (0)