Skip to content

Commit

Permalink
fix: added context parameter to functions to sync with updated generi…
Browse files Browse the repository at this point in the history
…c retry function to make build successful (#1256)

* refactor: used generic retry function instead of individual retry implementation

* refactor: reverted nil with exisiting 0 big integer when error occurs in fetchBalance

* fix: added context to function parameters foe successfull build

* refactor: fixed tests

* refactor: fixed benchmarks
  • Loading branch information
Yashk767 authored Oct 10, 2024
1 parent c4e3e3b commit 0df6525
Show file tree
Hide file tree
Showing 24 changed files with 211 additions and 210 deletions.
3 changes: 2 additions & 1 deletion cmd/collectionList.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package cmd

import (
"context"
"encoding/json"
"os"
"razor/logger"
Expand Down Expand Up @@ -46,7 +47,7 @@ func (*UtilsStruct) ExecuteCollectionList(flagSet *pflag.FlagSet) {

//This function provides the list of all collections with their name, power, ID etc.
func (*UtilsStruct) GetCollectionList(client *ethclient.Client) error {
collections, err := razorUtils.GetAllCollections(client)
collections, err := razorUtils.GetAllCollections(context.Background(), client)
log.Debugf("GetCollectionList: Collections: %+v", collections)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/collectionList_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestGetCollectionList(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetAllCollections", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.collectionList, tt.args.collectionListErr)
utilsMock.On("GetAllCollections", mock.Anything, mock.Anything).Return(tt.args.collectionList, tt.args.collectionListErr)
utils := &UtilsStruct{}

err := utils.GetCollectionList(tt.args.client)
Expand Down
4 changes: 2 additions & 2 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ HandleCommitState fetches the collections assigned to the staker and creates the
Values for only the collections assigned to the staker is fetched for others, 0 is added to the leaves of tree.
*/
func (*UtilsStruct) HandleCommitState(ctx context.Context, client *ethclient.Client, epoch uint32, seed []byte, commitParams *types.CommitParams, rogueData types.Rogue) (types.CommitData, error) {
numActiveCollections, err := razorUtils.GetNumActiveCollections(client)
numActiveCollections, err := razorUtils.GetNumActiveCollections(ctx, client)
if err != nil {
return types.CommitData{}, err
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (*UtilsStruct) HandleCommitState(ctx context.Context, client *ethclient.Cli

log.Debugf("HandleCommitState: Is the collection at iterating index %v assigned: %v ", i, assignedCollections[i])
if assignedCollections[i] {
collectionId, err := razorUtils.GetCollectionIdFromIndex(client, uint16(i))
collectionId, err := razorUtils.GetCollectionIdFromIndex(ctx, client, uint16(i))
if err != nil {
log.Error("Error in getting collection ID: ", err)
errChan <- err
Expand Down
8 changes: 4 additions & 4 deletions cmd/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ func TestHandleCommitState(t *testing.T) {

SetUpMockInterfaces()

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

Expand Down Expand Up @@ -404,9 +404,9 @@ func BenchmarkHandleCommitState(b *testing.B) {

SetUpMockInterfaces()

utilsMock.On("GetNumActiveCollections", mock.AnythingOfType("*ethclient.Client")).Return(v.numActiveCollections, nil)
utilsMock.On("GetNumActiveCollections", mock.Anything, mock.Anything).Return(v.numActiveCollections, nil)
utilsMock.On("GetAssignedCollections", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(v.assignedCollections, nil, nil)
utilsMock.On("GetCollectionIdFromIndex", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(uint16(1), nil)
utilsMock.On("GetCollectionIdFromIndex", mock.Anything, mock.Anything, mock.Anything).Return(uint16(1), nil)
utilsMock.On("GetAggregatedDataOfCollection", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(big.NewInt(1000), nil)
utilsMock.On("GetRogueRandomValue", mock.Anything).Return(rogueValue)

Expand Down
8 changes: 4 additions & 4 deletions cmd/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (*UtilsStruct) HandleDispute(ctx context.Context, client *ethclient.Client,

sortedValues := revealedDataMaps.SortedRevealedValues[collectionIdOfWrongMedian-1]
log.Debug("HandleDispute: Sorted values: ", sortedValues)
leafId, err := razorUtils.GetLeafIdOfACollection(client, collectionIdOfWrongMedian)
leafId, err := razorUtils.GetLeafIdOfACollection(ctx, client, collectionIdOfWrongMedian)
if err != nil {
log.Error("Error in leaf id: ", err)
continue
Expand Down Expand Up @@ -369,7 +369,7 @@ func (*UtilsStruct) Dispute(ctx context.Context, client *ethclient.Client, confi
giveSortedLeafIds = append(giveSortedLeafIds, int(leafId))
}
log.Debugf("Dispute: Calling GetCollectionIdPositionInBlock with arguments leafId = %d, proposed block = %+v", leafId, proposedBlock)
positionOfCollectionInBlock := cmdUtils.GetCollectionIdPositionInBlock(client, leafId, proposedBlock)
positionOfCollectionInBlock := cmdUtils.GetCollectionIdPositionInBlock(ctx, client, leafId, proposedBlock)
log.Debug("Dispute: Position of collection id in block: ", positionOfCollectionInBlock)

log.Info("Finalizing dispute...")
Expand Down Expand Up @@ -464,9 +464,9 @@ func GiveSorted(ctx context.Context, client *ethclient.Client, blockManager *bin
}

//This function returns the collection Id position in block
func (*UtilsStruct) GetCollectionIdPositionInBlock(client *ethclient.Client, leafId uint16, proposedBlock bindings.StructsBlock) *big.Int {
func (*UtilsStruct) GetCollectionIdPositionInBlock(ctx context.Context, client *ethclient.Client, leafId uint16, proposedBlock bindings.StructsBlock) *big.Int {
ids := proposedBlock.Ids
idToBeDisputed, err := razorUtils.GetCollectionIdFromLeafId(client, leafId)
idToBeDisputed, err := razorUtils.GetCollectionIdFromLeafId(ctx, client, leafId)
if err != nil {
log.Error("Error in fetching collection id from leaf id")
return nil
Expand Down
12 changes: 6 additions & 6 deletions cmd/dispute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestDispute(t *testing.T) {
utilsMock.On("GetBlockManager", mock.AnythingOfType("*ethclient.Client")).Return(blockManager)
utilsMock.On("GetTxnOpts", mock.Anything, mock.AnythingOfType("types.TransactionOptions")).Return(TxnOpts)
cmdUtilsMock.On("GiveSorted", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
cmdUtilsMock.On("GetCollectionIdPositionInBlock", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.positionOfCollectionInBlock)
cmdUtilsMock.On("GetCollectionIdPositionInBlock", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.positionOfCollectionInBlock)
blockManagerMock.On("FinalizeDispute", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.finalizeDisputeTxn, tt.args.finalizeDisputeErr)
transactionMock.On("Hash", mock.Anything).Return(tt.args.hash)
cmdUtilsMock.On("StoreBountyId", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.storeBountyIdErr)
Expand Down Expand Up @@ -538,7 +538,7 @@ func TestHandleDispute(t *testing.T) {
transactionMock.On("Hash", mock.Anything).Return(tt.args.Hash)
utilsMock.On("WaitForBlockCompletion", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("string")).Return(nil)
cmdUtilsMock.On("CheckDisputeForIds", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.idDisputeTxn, tt.args.idDisputeTxnErr)
utilsMock.On("GetLeafIdOfACollection", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.leafId, tt.args.leafIdErr)
utilsMock.On("GetLeafIdOfACollection", mock.Anything, mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.leafId, tt.args.leafIdErr)
cmdUtilsMock.On("Dispute", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.disputeErr)
utilsMock.On("GetBlockManager", mock.AnythingOfType("*ethclient.Client")).Return(blockManager)
cmdUtilsMock.On("StoreBountyId", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.storeBountyIdErr)
Expand Down Expand Up @@ -864,9 +864,9 @@ func TestGetCollectionIdPositionInBlock(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetCollectionIdFromLeafId", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(tt.args.idToBeDisputed, tt.args.idToBeDisputedErr)
utilsMock.On("GetCollectionIdFromLeafId", mock.Anything, mock.Anything, mock.Anything).Return(tt.args.idToBeDisputed, tt.args.idToBeDisputedErr)
ut := &UtilsStruct{}
if got := ut.GetCollectionIdPositionInBlock(client, leafId, tt.args.proposedBlock); !reflect.DeepEqual(got, tt.want) {
if got := ut.GetCollectionIdPositionInBlock(context.Background(), client, leafId, tt.args.proposedBlock); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetCollectionIdPositionInBlock() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -1153,9 +1153,9 @@ func BenchmarkGetCollectionIdPositionInBlock(b *testing.B) {
for i := 0; i < b.N; i++ {
SetUpMockInterfaces()

utilsMock.On("GetCollectionIdFromLeafId", mock.AnythingOfType("*ethclient.Client"), mock.Anything).Return(v.idToBeDisputed, nil)
utilsMock.On("GetCollectionIdFromLeafId", mock.Anything, mock.Anything, mock.Anything).Return(v.idToBeDisputed, nil)
ut := &UtilsStruct{}
ut.GetCollectionIdPositionInBlock(client, leafId, bindings.StructsBlock{Ids: getDummyIds(v.numOfIds)})
ut.GetCollectionIdPositionInBlock(context.Background(), client, leafId, bindings.StructsBlock{Ids: getDummyIds(v.numOfIds)})
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/eventListeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (*UtilsStruct) InitJobAndCollectionCache(ctx context.Context, client *ethcl
collectionsCache := cache.NewCollectionsCache()

// Initialize caches
if err := utils.InitJobsCache(client, jobsCache); err != nil {
if err := utils.InitJobsCache(ctx, client, jobsCache); err != nil {
log.Error("Error in initializing jobs cache: ", err)
return nil, nil, nil, err
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func processEvents(ctx context.Context, client *ethclient.Client, contractABI ab
switch eventName {
case core.JobUpdatedEvent, core.JobCreatedEvent:
jobId := utils.ConvertHashToUint16(vLog.Topics[1])
updatedJob, err := utils.UtilsInterface.GetActiveJob(client, jobId)
updatedJob, err := utils.UtilsInterface.GetActiveJob(ctx, client, jobId)
if err != nil {
log.Errorf("Error in getting job with job Id %v: %v", jobId, err)
continue
Expand All @@ -96,7 +96,7 @@ func processEvents(ctx context.Context, client *ethclient.Client, contractABI ab
jobsCache.UpdateJob(jobId, updatedJob)
case core.CollectionUpdatedEvent, core.CollectionCreatedEvent, core.CollectionActivityStatusEvent:
collectionId := utils.ConvertHashToUint16(vLog.Topics[1])
newCollection, err := utils.UtilsInterface.GetCollection(client, collectionId)
newCollection, err := utils.UtilsInterface.GetCollection(ctx, client, collectionId)
if err != nil {
log.Errorf("Error in getting collection with collection Id %v: %v", collectionId, err)
continue
Expand Down
2 changes: 1 addition & 1 deletion cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ type UtilsCmdInterface interface {
GetLocalMediansData(ctx context.Context, client *ethclient.Client, account types.Account, epoch uint32, blockNumber *big.Int, rogueData types.Rogue) (types.ProposeFileData, error)
CheckDisputeForIds(ctx context.Context, client *ethclient.Client, transactionOpts types.TransactionOptions, epoch uint32, blockIndex uint8, idsInProposedBlock []uint16, revealedCollectionIds []uint16) (*Types.Transaction, error)
Dispute(ctx context.Context, client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, blockIndex uint8, proposedBlock bindings.StructsBlock, leafId uint16, sortedValues []*big.Int) error
GetCollectionIdPositionInBlock(client *ethclient.Client, leafId uint16, proposedBlock bindings.StructsBlock) *big.Int
GetCollectionIdPositionInBlock(ctx context.Context, client *ethclient.Client, leafId uint16, proposedBlock bindings.StructsBlock) *big.Int
HandleDispute(ctx context.Context, client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, blockNumber *big.Int, rogueData types.Rogue, backupNodeActionsToIgnore []string) error
ExecuteExtendLock(flagSet *pflag.FlagSet)
ResetUnstakeLock(client *ethclient.Client, config types.Configurations, extendLockInput types.ExtendLockInput) (common.Hash, error)
Expand Down
3 changes: 2 additions & 1 deletion cmd/jobList.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package cmd

import (
"context"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -44,7 +45,7 @@ func (*UtilsStruct) ExecuteJobList(flagSet *pflag.FlagSet) {

//This function provides the list of all jobs
func (*UtilsStruct) GetJobList(client *ethclient.Client) error {
jobs, err := razorUtils.GetJobs(client)
jobs, err := razorUtils.GetJobs(context.Background(), client)
log.Debugf("JobList: Jobs: %+v", jobs)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/jobList_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestGetJobList(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
SetUpMockInterfaces()

utilsMock.On("GetJobs", mock.AnythingOfType("*ethclient.Client")).Return(tt.args.jobList, tt.args.jobListErr)
utilsMock.On("GetJobs", mock.Anything, mock.Anything).Return(tt.args.jobList, tt.args.jobListErr)
utils := &UtilsStruct{}

err := utils.GetJobList(tt.args.client)
Expand Down
10 changes: 5 additions & 5 deletions cmd/mocks/utils_cmd_interface.go

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

2 changes: 1 addition & 1 deletion cmd/propose.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (*UtilsStruct) MakeBlock(ctx context.Context, client *ethclient.Client, blo
}
log.Debugf("MakeBlock: Revealed data map: %+v", revealedDataMaps)

activeCollections, err := razorUtils.GetActiveCollectionIds(client)
activeCollections, err := razorUtils.GetActiveCollectionIds(ctx, client)
if err != nil {
return nil, nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/propose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ func TestMakeBlock(t *testing.T) {
SetUpMockInterfaces()

cmdUtilsMock.On("GetSortedRevealedValues", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.revealedDataMaps, tt.args.revealedDataMapsErr)
utilsMock.On("GetActiveCollectionIds", mock.Anything).Return(tt.args.activeCollections, tt.args.activeCollectionsErr)
utilsMock.On("GetActiveCollectionIds", mock.Anything, mock.Anything).Return(tt.args.activeCollections, tt.args.activeCollectionsErr)
utilsMock.On("GetRogueRandomValue", mock.Anything).Return(randomValue)
ut := &UtilsStruct{}
got, got1, got2, err := ut.MakeBlock(context.Background(), client, blockNumber, epoch, tt.args.rogueData)
Expand Down Expand Up @@ -1536,7 +1536,7 @@ func BenchmarkMakeBlock(b *testing.B) {
VoteWeights: map[string]*big.Int{(big.NewInt(1).Mul(big.NewInt(697718000), big.NewInt(1e18))).String(): big.NewInt(100)},
InfluenceSum: map[uint16]*big.Int{0: big.NewInt(100)},
}, nil)
utilsMock.On("GetActiveCollectionIds", mock.Anything).Return([]uint16{1}, nil)
utilsMock.On("GetActiveCollectionIds", mock.Anything, mock.Anything).Return([]uint16{1}, nil)
ut := &UtilsStruct{}
_, _, _, err := ut.MakeBlock(context.Background(), client, blockNumber, epoch, types.Rogue{IsRogue: false})
if err != nil {
Expand Down
Loading

0 comments on commit 0df6525

Please sign in to comment.