Skip to content

Commit

Permalink
Coverage increased (#684)
Browse files Browse the repository at this point in the history
* Removed unused functions

* Covered commit rogue case

* Fixed dispute tests

* Covered and tested missing lines of code

* minor change
  • Loading branch information
Yashk767 authored Apr 26, 2022
1 parent 5dbdd5d commit e7c488d
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 478 deletions.
2 changes: 1 addition & 1 deletion cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (*UtilsStruct) HandleCommitState(client *ethclient.Client, epoch uint32, se
return types.CommitData{}, err
}
if rogueData.IsRogue && utils.Contains(rogueData.RogueMode, "commit") {
collectionData = utils.GetRogueRandomValue(100000)
collectionData = razorUtils.GetRogueRandomValue(100000)
}
log.Debugf("Data of collection %d:%s", collectionId, collectionData)
leavesOfTree = append(leavesOfTree, collectionData)
Expand Down
35 changes: 29 additions & 6 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,12 @@ func TestCommit(t *testing.T) {

func TestHandleCommitState(t *testing.T) {
var (
client *ethclient.Client
epoch uint32
seed []byte
rogueData types.Rogue
client *ethclient.Client
epoch uint32
seed []byte
)

//rogueValue := big.NewInt(int64(randMath.Intn(10000000)))
rogueValue := utils.GetRogueRandomValue(100000)

type args struct {
numActiveCollections uint16
Expand All @@ -140,6 +139,7 @@ func TestHandleCommitState(t *testing.T) {
collectionIdErr error
collectionData *big.Int
collectionDataErr error
rogueData types.Rogue
}
tests := []struct {
name string
Expand Down Expand Up @@ -203,20 +203,43 @@ func TestHandleCommitState(t *testing.T) {
want: types.CommitData{},
wantErr: errors.New("error in getting collectionData"),
},
{
name: "Test 6: When rogue mode is on for commit state",
args: args{
numActiveCollections: 3,
assignedCollections: map[int]bool{1: true, 2: true},
seqAllottedCollections: []*big.Int{big.NewInt(1), big.NewInt(2)},
collectionId: 1,
collectionData: big.NewInt(1),
rogueData: types.Rogue{
IsRogue: true,
RogueMode: []string{"commit"},
},
},
want: types.CommitData{
AssignedCollections: map[int]bool{1: true, 2: true},
SeqAllottedCollections: []*big.Int{big.NewInt(1), big.NewInt(2)},
Leaves: []*big.Int{big.NewInt(0), rogueValue, rogueValue},
},
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
utilsPkgMock := new(mocks2.Utils)
utilsMock := new(mocks.UtilsInterface)

utils.UtilsInterface = utilsPkgMock
razorUtils = utilsMock

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

utils := &UtilsStruct{}
got, err := utils.HandleCommitState(client, epoch, seed, rogueData)
got, err := utils.HandleCommitState(client, epoch, seed, tt.args.rogueData)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Data from HandleCommitState function, got = %v, want = %v", got, tt.want)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (*UtilsStruct) HandleDispute(client *ethclient.Client, config types.Configu
}

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

isEqual, _ := utils.UtilsInterface.IsEqualByte(hashIdsInProposedBlock, hashRevealedCollectionIds)
isEqual, _ := utils.IsEqualByte(hashIdsInProposedBlock, hashRevealedCollectionIds)

if isEqual {
return nil, nil
}

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

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

// Check if the error is collectionIdShouldBeAbsent
isPresent, positionOfPresentValue, presentCollectionId := utils.UtilsInterface.IsMissing(idsInProposedBlock, revealedCollectionIds)
isPresent, positionOfPresentValue, presentCollectionId := utils.IsMissing(idsInProposedBlock, revealedCollectionIds)
if isPresent {
transactionOpts.ABI = bindings.BlockManagerABI
transactionOpts.MethodName = "disputeCollectionIdShouldBeAbsent"
Expand Down
65 changes: 26 additions & 39 deletions cmd/dispute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,25 +704,14 @@ func TestCheckDisputeForIds(t *testing.T) {
txnOpts, _ := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(31337))

type args struct {
isEqual bool
isEqualInt int
idsInProposedBlock []uint16
revealedCollectionIds []uint16
isSorted bool
index0 int
index1 int
DisputeOnOrderOfIds *Types.Transaction
DisputeOnOrderOfIdsErr error
isMissing bool
isMissingInt int
incrementedGasLimit uint64
incrementedGasLimitErr error
missingCollectionId uint16
DisputeCollectionIdShouldBePresent *Types.Transaction
DisputeCollectionIdShouldBePresentErr error
isPresent bool
positionOfPresentValue int
presentCollectionId uint16
DisputeCollectionIdShouldBeAbsent *Types.Transaction
DisputeCollectionIdShouldBeAbsentErr error
}
Expand All @@ -733,59 +722,61 @@ func TestCheckDisputeForIds(t *testing.T) {
wantErr bool
}{
{
name: "Test 1: When CheckDisputeForIds executes successfully and check if the error is in sorted ids",
name: "Test 1: When CheckDisputeForIds executes successfully with sorted Ids dispute case",
args: args{
isEqual: false,
idsInProposedBlock: []uint16{1, 2, 3},
idsInProposedBlock: []uint16{1, 3, 2},
revealedCollectionIds: []uint16{1, 2, 3},
isSorted: false,
DisputeOnOrderOfIds: &Types.Transaction{},
},
want: &Types.Transaction{},
wantErr: false,
},
{
name: "Test 2: When CheckDisputeForIds executes successfully and check if the error is collectionIdShouldBePresent",
name: "Test 2: When CheckDisputeForIds executes successfully with collectionIdShouldBePresent dispute case",
args: args{
isEqual: false,
idsInProposedBlock: []uint16{1, 2, 3},
isSorted: true,
isMissing: true,
idsInProposedBlock: []uint16{1, 2, 4},
revealedCollectionIds: []uint16{1, 2, 3},
DisputeCollectionIdShouldBePresent: &Types.Transaction{},
},
want: &Types.Transaction{},
wantErr: false,
},
{
name: "Test 3: When there is an error in incrementalGasLimit",
name: "Test 3: When its a collectionIdShouldBePresent dispute case and their is an error in incrementalGasLimit",
args: args{
isEqual: false,
idsInProposedBlock: []uint16{1, 2, 3},
isSorted: true,
isMissing: true,
incrementedGasLimitErr: errors.New("error in incremented gas limit"),
DisputeCollectionIdShouldBePresent: &Types.Transaction{},
idsInProposedBlock: []uint16{1, 2, 4},
revealedCollectionIds: []uint16{1, 2, 3},
incrementedGasLimitErr: errors.New("error in incremented gas limit"),
},
want: nil,
wantErr: true,
},
{
name: "Test 4: When there is no error",
name: "Test 4: When CheckDisputeForIds executes successfully with collectionIdShouldBeAbsent case",
args: args{
isEqual: false,
idsInProposedBlock: []uint16{1, 2, 3},
isSorted: true,
isMissing: false,
isPresent: false,
idsInProposedBlock: []uint16{1, 2, 3, 4},
revealedCollectionIds: []uint16{1, 2, 3},
DisputeCollectionIdShouldBeAbsent: &Types.Transaction{},
},
want: nil,
want: &Types.Transaction{},
wantErr: false,
},
{
name: "Test 5: When its a collectionIdShouldBeAbsent dispute case and their is an error in incrementalGasLimit",
args: args{
idsInProposedBlock: []uint16{1, 2, 3, 4},
revealedCollectionIds: []uint16{1, 2, 3},
DisputeCollectionIdShouldBeAbsent: &Types.Transaction{},
incrementedGasLimitErr: errors.New("error in incremented gas limit"),
},
want: nil,
wantErr: true,
},
{
name: "Test 5: When there is no dispute",
args: args{
isEqual: true,
idsInProposedBlock: []uint16{1, 2, 3},
revealedCollectionIds: []uint16{1, 2, 3},
},
want: nil,
wantErr: false,
Expand All @@ -802,14 +793,10 @@ func TestCheckDisputeForIds(t *testing.T) {
blockManagerUtils = blockManagerUtilsMock
utilsInterface = utilsPkgMock

utilsPkgMock.On("IsEqualByte", mock.Anything, mock.Anything).Return(tt.args.isEqual, tt.args.isEqualInt)
utilsPkgMock.On("IsSorted", mock.Anything).Return(tt.args.isSorted, tt.args.index0, tt.args.index1)
utilsMock.On("GetTxnOpts", mock.AnythingOfType("types.TransactionOptions")).Return(txnOpts)
blockManagerUtilsMock.On("DisputeOnOrderOfIds", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.DisputeOnOrderOfIds, tt.args.DisputeOnOrderOfIdsErr)
utilsPkgMock.On("IsMissing", mock.Anything, mock.Anything).Return(tt.args.isMissing, tt.args.isMissingInt, tt.args.missingCollectionId)
utilsPkgMock.On("IncreaseGasLimitValue", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.incrementedGasLimit, tt.args.incrementedGasLimitErr)
blockManagerUtilsMock.On("DisputeCollectionIdShouldBePresent", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.DisputeCollectionIdShouldBePresent, tt.args.DisputeCollectionIdShouldBePresentErr)
utilsPkgMock.On("IsMissing", mock.Anything, mock.Anything).Return(tt.args.isPresent, tt.args.positionOfPresentValue, tt.args.presentCollectionId)
blockManagerUtilsMock.On("DisputeCollectionIdShouldBeAbsent", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.DisputeCollectionIdShouldBeAbsent, tt.args.DisputeCollectionIdShouldBeAbsentErr)
utilsPkgMock.On("IncreaseGasLimitValue", mock.Anything, mock.Anything, mock.Anything).Return(uint64(2000), nil)
ut := &UtilsStruct{}
Expand Down
3 changes: 1 addition & 2 deletions cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type UtilsInterface interface {
WaitForBlockCompletion(*ethclient.Client, string) int
GetNumActiveCollections(*ethclient.Client) (uint16, error)
GetRogueRandomValue(int) *big.Int
GetRogueRandomMedianValue() uint32
GetAggregatedDataOfCollection(client *ethclient.Client, collectionId uint16, epoch uint32) (*big.Int, error)
GetDelayedState(*ethclient.Client, int32) (int64, error)
GetDefaultPath() (string, error)
Expand Down Expand Up @@ -112,8 +113,6 @@ type UtilsInterface interface {
GetStake(*ethclient.Client, uint32) (*big.Int, error)
ConvertWeiToEth(*big.Int) (*big.Float, error)
WaitTillNextNSecs(int32)
SaveDataToFile(string, uint32, []*big.Int) error
ReadDataFromFile(string) (uint32, []*big.Int, error)
DeleteJobFromJSON(string, string) error
AddJobToJSON(string, *types.StructsJob) error
GetStakerSRZRBalance(*ethclient.Client, bindings.StructsStaker) (*big.Int, error)
Expand Down
58 changes: 14 additions & 44 deletions cmd/mocks/utils_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions cmd/propose.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ func (*UtilsStruct) Propose(client *ethclient.Client, config types.Configuration
log.Debugf("Iteration: %d Biggest Staker Id: %d", iteration, biggestStakerId)
log.Info("Proposing block...")

if err != nil {
return core.NilHash, err
}
txnOpts := razorUtils.GetTxnOpts(types.TransactionOptions{
Client: client,
Password: account.Password,
Expand Down Expand Up @@ -326,7 +323,7 @@ func (*UtilsStruct) MakeBlock(client *ethclient.Client, blockNumber *big.Int, ep
if influenceSum != nil && influenceSum.Cmp(big.NewInt(0)) != 0 {
idsRevealedInThisEpoch = append(idsRevealedInThisEpoch, activeCollections[leafId])
if rogueData.IsRogue && utils.Contains(rogueData.RogueMode, "medians") {
medians = append(medians, rand.Uint32())
medians = append(medians, razorUtils.GetRogueRandomMedianValue())
continue
}
accWeight := big.NewInt(0)
Expand All @@ -346,7 +343,7 @@ func (*UtilsStruct) MakeBlock(client *ethclient.Client, blockNumber *big.Int, ep
}
if rogueData.IsRogue && utils.Contains(rogueData.RogueMode, "extraIds") {
//Adding a dummy median and appending extra id to idsRevealed array if rogueMode == extraIds
medians = append(medians, rand.Uint32())
medians = append(medians, razorUtils.GetRogueRandomMedianValue())
idsRevealedInThisEpoch = append(idsRevealedInThisEpoch, idsRevealedInThisEpoch[len(idsRevealedInThisEpoch)-1]+1)
}
if rogueData.IsRogue && utils.Contains(rogueData.RogueMode, "unsortedIds") && len(idsRevealedInThisEpoch) > 1 {
Expand Down
Loading

0 comments on commit e7c488d

Please sign in to comment.