Skip to content

Commit 9979d65

Browse files
authored
Increased coverage In CMD (#655)
* tests added for cmd * coverage increased in CMD * modifyCollectionStatus test fix * added tests for AutoClaimBounty
1 parent 0f233d0 commit 9979d65

23 files changed

+2803
-1194
lines changed

cmd/addStake_test.go

+35-30
Original file line numberDiff line numberDiff line change
@@ -134,31 +134,29 @@ func TestExecuteStake(t *testing.T) {
134134
var config types.Configurations
135135

136136
type args struct {
137-
config types.Configurations
138-
configErr error
139-
password string
140-
address string
141-
addressErr error
142-
balance *big.Int
143-
balanceErr error
144-
amount *big.Int
145-
amountErr error
146-
approveTxn common.Hash
147-
approveErr error
148-
minSafeRazor *big.Int
149-
minSafeRazorErr error
150-
stakeTxn common.Hash
151-
stakeErr error
152-
isFlagPassed bool
153-
autoVote bool
154-
autoVoteErr error
155-
isRogue bool
156-
isRogueErr error
157-
rogueMode []string
158-
rogueModeErr error
159-
voteErr error
160-
revealedDataMaps types.RevealedDataMaps
161-
//revealedDataErr error
137+
config types.Configurations
138+
configErr error
139+
password string
140+
address string
141+
addressErr error
142+
balance *big.Int
143+
balanceErr error
144+
amount *big.Int
145+
amountErr error
146+
approveTxn common.Hash
147+
approveErr error
148+
minSafeRazor *big.Int
149+
minSafeRazorErr error
150+
stakeTxn common.Hash
151+
stakeErr error
152+
isFlagPassed bool
153+
autoVote bool
154+
autoVoteErr error
155+
isRogue bool
156+
isRogueErr error
157+
rogueMode []string
158+
rogueModeErr error
159+
voteErr error
162160
}
163161
tests := []struct {
164162
name string
@@ -177,11 +175,6 @@ func TestExecuteStake(t *testing.T) {
177175
approveTxn: common.BigToHash(big.NewInt(1)),
178176
stakeTxn: common.BigToHash(big.NewInt(2)),
179177
isFlagPassed: false,
180-
revealedDataMaps: types.RevealedDataMaps{
181-
SortedRevealedValues: nil,
182-
VoteWeights: nil,
183-
InfluenceSum: nil,
184-
},
185178
},
186179
expectedFatal: false,
187180
},
@@ -354,6 +347,18 @@ func TestExecuteStake(t *testing.T) {
354347
},
355348
expectedFatal: true,
356349
},
350+
{
351+
name: "Test 12: When stake value is less than minSafeRazor",
352+
args: args{
353+
config: config,
354+
password: "test",
355+
address: "0x000000000000000000000000000000000000dead",
356+
amount: big.NewInt(20),
357+
balance: big.NewInt(10000),
358+
minSafeRazor: big.NewInt(100),
359+
},
360+
expectedFatal: true,
361+
},
357362
}
358363

359364
defer func() { log.ExitFunc = nil }()

cmd/cmd-utils.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (*UtilsStruct) GetEpochAndState(client *ethclient.Client) (uint32, int64, e
2626
return 0, 0, err
2727
}
2828
log.Debug("Epoch ", epoch)
29-
log.Debug("State ", utils.GetStateName(state))
29+
log.Debug("State ", utils.UtilsInterface.GetStateName(state))
3030
return epoch, state, nil
3131
}
3232

@@ -94,9 +94,9 @@ func GetStatesAllowed(states []int) string {
9494
var statesAllowed string
9595
for i := 0; i < len(states); i++ {
9696
if i == len(states)-1 {
97-
statesAllowed = statesAllowed + strconv.Itoa(states[i]) + ":" + utils.GetStateName(int64(states[i]))
97+
statesAllowed = statesAllowed + strconv.Itoa(states[i]) + ":" + utils.UtilsInterface.GetStateName(int64(states[i]))
9898
} else {
99-
statesAllowed = statesAllowed + strconv.Itoa(states[i]) + ":" + utils.GetStateName(int64(states[i])) + ", "
99+
statesAllowed = statesAllowed + strconv.Itoa(states[i]) + ":" + utils.UtilsInterface.GetStateName(int64(states[i])) + ", "
100100
}
101101
}
102102
return statesAllowed

cmd/cmd-utils_test.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/stretchr/testify/mock"
88
"math/big"
99
"razor/cmd/mocks"
10+
"razor/utils"
11+
mocks2 "razor/utils/mocks"
1012
"testing"
1113
)
1214

@@ -82,14 +84,16 @@ func TestGetEpochAndState(t *testing.T) {
8284

8385
utilsMock := new(mocks.UtilsInterface)
8486
cmdUtilsMock := new(mocks.UtilsCmdInterface)
87+
utilsPkgMock := new(mocks2.Utils)
8588

8689
razorUtils = utilsMock
8790
cmdUtils = cmdUtilsMock
91+
utils.UtilsInterface = utilsPkgMock
8892

8993
utilsMock.On("GetEpoch", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.epoch, tt.args.epochErr)
9094
cmdUtilsMock.On("GetBufferPercent").Return(tt.args.bufferPercent, tt.args.bufferPercentErr)
9195
utilsMock.On("GetDelayedState", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("int32")).Return(tt.args.state, tt.args.stateErr)
92-
utilsMock.On("GetStateName", mock.AnythingOfType("int64")).Return(tt.args.stateName)
96+
utilsPkgMock.On("GetStateName", mock.AnythingOfType("int64")).Return(tt.args.stateName)
9397

9498
utils := &UtilsStruct{}
9599
gotEpoch, gotState, err := utils.GetEpochAndState(client)
@@ -387,7 +391,8 @@ func TestAssignAmountInWei1(t *testing.T) {
387391

388392
func TestGetStatesAllowed(t *testing.T) {
389393
type args struct {
390-
states []int
394+
states []int
395+
stateName string
391396
}
392397
tests := []struct {
393398
name string
@@ -397,9 +402,10 @@ func TestGetStatesAllowed(t *testing.T) {
397402
{
398403
name: "Test 1: When states has multiple elements",
399404
args: args{
400-
states: []int{1, 2, 4},
405+
states: []int{1},
406+
stateName: "Reveal",
401407
},
402-
want: "1:Reveal, 2:Propose, 4:Confirm",
408+
want: "1:Reveal",
403409
},
404410
{
405411
name: "Test 2: When states array is nil",
@@ -411,6 +417,11 @@ func TestGetStatesAllowed(t *testing.T) {
411417
}
412418
for _, tt := range tests {
413419
t.Run(tt.name, func(t *testing.T) {
420+
utilsPkgMock := new(mocks2.Utils)
421+
422+
utils.UtilsInterface = utilsPkgMock
423+
424+
utilsPkgMock.On("GetStateName", mock.AnythingOfType("int64")).Return(tt.args.stateName)
414425
if got := GetStatesAllowed(tt.args.states); got != tt.want {
415426
t.Errorf("GetStatesAllowed() = %v, want %v", got, tt.want)
416427
}

cmd/commit_test.go

+163
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"razor/cmd/mocks"
1515
"razor/core"
1616
"razor/core/types"
17+
"razor/pkg/bindings"
1718
"razor/utils"
1819
mocks2 "razor/utils/mocks"
1920
"reflect"
@@ -162,6 +163,46 @@ func TestHandleCommitState(t *testing.T) {
162163
},
163164
wantErr: nil,
164165
},
166+
{
167+
name: "Test 2: When there is an error in getting numActiveCollections",
168+
args: args{
169+
numActiveCollectionsErr: errors.New("error in getting numActiveCollections"),
170+
},
171+
want: types.CommitData{},
172+
wantErr: errors.New("error in getting numActiveCollections"),
173+
},
174+
{
175+
name: "Test 3: When there is an error in getting assignedCollections",
176+
args: args{
177+
numActiveCollections: 1,
178+
assignedCollectionsErr: errors.New("error in getting assignedCollections"),
179+
},
180+
want: types.CommitData{},
181+
wantErr: errors.New("error in getting assignedCollections"),
182+
},
183+
{
184+
name: "Test 4: When there is an error in getting collectionId",
185+
args: args{
186+
numActiveCollections: 3,
187+
assignedCollections: map[int]bool{1: true, 2: true},
188+
seqAllottedCollections: []*big.Int{big.NewInt(1), big.NewInt(2)},
189+
collectionIdErr: errors.New("error in getting collectionId"),
190+
},
191+
want: types.CommitData{},
192+
wantErr: errors.New("error in getting collectionId"),
193+
},
194+
{
195+
name: "Test 5: When there is an error in getting collectionData",
196+
args: args{
197+
numActiveCollections: 3,
198+
assignedCollections: map[int]bool{1: true, 2: true},
199+
seqAllottedCollections: []*big.Int{big.NewInt(1), big.NewInt(2)},
200+
collectionId: 1,
201+
collectionDataErr: errors.New("error in getting collectionData"),
202+
},
203+
want: types.CommitData{},
204+
wantErr: errors.New("error in getting collectionData"),
205+
},
165206
}
166207
for _, tt := range tests {
167208
t.Run(tt.name, func(t *testing.T) {
@@ -192,3 +233,125 @@ func TestHandleCommitState(t *testing.T) {
192233
})
193234
}
194235
}
236+
237+
func TestGetSalt(t *testing.T) {
238+
var client *ethclient.Client
239+
240+
type args struct {
241+
epoch uint32
242+
numProposedBlocks uint8
243+
numProposedBlocksErr error
244+
blockIndexedToBeConfirmed int8
245+
blockIndexedToBeConfirmedErr error
246+
saltFromBlockChain [32]byte
247+
saltFromBlockChainErr error
248+
blockId uint32
249+
blockIdErr error
250+
previousBlock bindings.StructsBlock
251+
previousBlockErr error
252+
salt [32]byte
253+
}
254+
tests := []struct {
255+
name string
256+
args args
257+
want [32]byte
258+
wantErr error
259+
}{
260+
{
261+
name: "Test 1: When GetSalt() function executes successfully",
262+
args: args{
263+
epoch: 2,
264+
numProposedBlocks: 1,
265+
blockIndexedToBeConfirmed: 1,
266+
blockId: 1,
267+
previousBlock: bindings.StructsBlock{},
268+
salt: [32]byte{},
269+
},
270+
want: [32]byte{},
271+
wantErr: nil,
272+
},
273+
{
274+
name: "Test 2: When there is an error in getting numProposedBlocks",
275+
args: args{
276+
epoch: 2,
277+
numProposedBlocksErr: errors.New("error in getting numProposedBlocks"),
278+
},
279+
want: [32]byte{},
280+
wantErr: errors.New("error in getting numProposedBlocks"),
281+
},
282+
{
283+
name: "Test 3: When there is an error in getting blockIndexedToBeConfirmed",
284+
args: args{
285+
epoch: 2,
286+
numProposedBlocks: 1,
287+
blockIndexedToBeConfirmedErr: errors.New("error in getting blockIndexedToBeConfirmed"),
288+
},
289+
want: [32]byte{},
290+
wantErr: errors.New("error in getting blockIndexedToBeConfirmed"),
291+
},
292+
{
293+
name: "Test 4: When numProposedBlock is zero",
294+
args: args{
295+
epoch: 2,
296+
numProposedBlocks: 0,
297+
saltFromBlockChain: [32]byte{},
298+
},
299+
want: [32]byte{},
300+
wantErr: nil,
301+
},
302+
{
303+
name: "Test 5: When there is an error in getting blockId",
304+
args: args{
305+
epoch: 2,
306+
numProposedBlocks: 1,
307+
blockIndexedToBeConfirmed: 1,
308+
blockIdErr: errors.New("error"),
309+
},
310+
want: [32]byte{},
311+
wantErr: errors.New("Error in getting blockId: error"),
312+
},
313+
{
314+
name: "Test 6: When there is an error in getting previousBlock",
315+
args: args{
316+
epoch: 2,
317+
numProposedBlocks: 1,
318+
blockIndexedToBeConfirmed: 1,
319+
blockId: 1,
320+
previousBlockErr: errors.New("error"),
321+
},
322+
want: [32]byte{},
323+
wantErr: errors.New("Error in getting previous block: error"),
324+
},
325+
}
326+
for _, tt := range tests {
327+
t.Run(tt.name, func(t *testing.T) {
328+
utilsPkgMock := new(mocks2.Utils)
329+
utilsVoteManagerMock := new(mocks2.VoteManagerUtils)
330+
331+
utils.UtilsInterface = utilsPkgMock
332+
utils.VoteManagerInterface = utilsVoteManagerMock
333+
334+
utilsPkgMock.On("GetNumberOfProposedBlocks", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.numProposedBlocks, tt.args.numProposedBlocksErr)
335+
utilsPkgMock.On("GetBlockIndexToBeConfirmed", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.blockIndexedToBeConfirmed, tt.args.blockIndexedToBeConfirmedErr)
336+
utilsVoteManagerMock.On("GetSaltFromBlockchain", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.saltFromBlockChain, tt.args.saltFromBlockChainErr)
337+
utilsPkgMock.On("GetSortedProposedBlockId", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(tt.args.blockId, tt.args.blockIdErr)
338+
utilsPkgMock.On("GetProposedBlock", mock.AnythingOfType("*ethclient.Client"), mock.Anything, mock.Anything).Return(tt.args.previousBlock, tt.args.previousBlockErr)
339+
utilsPkgMock.On("CalculateSalt", mock.Anything, mock.Anything).Return(tt.args.salt)
340+
341+
ut := &UtilsStruct{}
342+
got, err := ut.GetSalt(client, tt.args.epoch)
343+
if !reflect.DeepEqual(got, tt.want) {
344+
t.Errorf("Data from GetSalt function, got = %v, want = %v", got, tt.want)
345+
}
346+
if err == nil || tt.wantErr == nil {
347+
if err != tt.wantErr {
348+
t.Errorf("Error from GetSalt function, got = %v, want = %v", err, tt.wantErr)
349+
}
350+
} else {
351+
if err.Error() != tt.wantErr.Error() {
352+
t.Errorf("Error from GetSalt function, got = %v, want = %v", err, tt.wantErr)
353+
}
354+
}
355+
})
356+
}
357+
}

cmd/dispute.go

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

110110
// Median Value dispute
111-
isEqual, mismatchIndex := utils.IsEqualUint32(proposedBlock.Medians, medians)
111+
isEqual, mismatchIndex := utils.UtilsInterface.IsEqualUint32(proposedBlock.Medians, medians)
112112
if !isEqual {
113113
log.Warn("BLOCK NOT MATCHING WITH LOCAL CALCULATIONS.")
114114
log.Debug("Block Values: ", proposedBlock.Medians)
@@ -188,7 +188,7 @@ CalculateMedian:
188188

189189
func (*UtilsStruct) CheckDisputeForIds(client *ethclient.Client, transactionOpts types.TransactionOptions, epoch uint32, blockIndex uint8, idsInProposedBlock []uint16, revealedCollectionIds []uint16) (*types2.Transaction, error) {
190190
// Check if the error is in sorted ids
191-
isSorted, index0, index1 := utils.IsSorted(idsInProposedBlock)
191+
isSorted, index0, index1 := utils.UtilsInterface.IsSorted(idsInProposedBlock)
192192
if !isSorted {
193193
transactionOpts.ABI = bindings.BlockManagerABI
194194
transactionOpts.MethodName = "disputeOnOrderOfIds"
@@ -200,7 +200,7 @@ func (*UtilsStruct) CheckDisputeForIds(client *ethclient.Client, transactionOpts
200200
}
201201

202202
// Check if the error is collectionIdShouldBePresent
203-
isMissing, _, missingCollectionId := utils.IsMissing(revealedCollectionIds, idsInProposedBlock)
203+
isMissing, _, missingCollectionId := utils.UtilsInterface.IsMissing(revealedCollectionIds, idsInProposedBlock)
204204
if isMissing {
205205
transactionOpts.ABI = bindings.BlockManagerABI
206206
transactionOpts.MethodName = "disputeCollectionIdShouldBePresent"
@@ -212,7 +212,7 @@ func (*UtilsStruct) CheckDisputeForIds(client *ethclient.Client, transactionOpts
212212
}
213213

214214
// Check if the error is collectionIdShouldBeAbsent
215-
isPresent, positionOfPresentValue, presentCollectionId := utils.IsMissing(idsInProposedBlock, revealedCollectionIds)
215+
isPresent, positionOfPresentValue, presentCollectionId := utils.UtilsInterface.IsMissing(idsInProposedBlock, revealedCollectionIds)
216216
if isPresent {
217217
transactionOpts.ABI = bindings.BlockManagerABI
218218
transactionOpts.MethodName = "disputeCollectionIdShouldBeAbsent"
@@ -299,7 +299,7 @@ func (*UtilsStruct) GetCollectionIdPositionInBlock(client *ethclient.Client, lea
299299
}
300300

301301
func (*UtilsStruct) GetBountyIdFromEvents(client *ethclient.Client, blockNumber *big.Int, bountyHunter string) (uint32, error) {
302-
fromBlock, err := utils.CalculateBlockNumberAtEpochBeginning(client, core.EpochLength, blockNumber)
302+
fromBlock, err := utils.UtilsInterface.CalculateBlockNumberAtEpochBeginning(client, core.EpochLength, blockNumber)
303303
if err != nil {
304304
log.Error(err)
305305
return 0, err

0 commit comments

Comments
 (0)