diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml index e9f4a9baf..1eb64b326 100644 --- a/.github/workflows/develop.yml +++ b/.github/workflows/develop.yml @@ -61,7 +61,7 @@ jobs: golangci-lint run -v --timeout 5m - name: Execute test case run: | - go-acc ./... --ignore razor/accounts/mocks --ignore razor/cmd/mocks --ignore razor/cmd/eventListeners.go --ignore razor/utils/mocks --ignore pkg --ignore razor/path/mocks --output coverage.txt + go-acc ./... --ignore razor/accounts/mocks --ignore razor/cmd/mocks --ignore razor/utils/mocks --ignore pkg --ignore razor/path/mocks --output coverage.txt - name: Run benchmarks run: | go test ./... -bench=. -run=^# diff --git a/cache/collectionCache.go b/cache/collectionCache.go deleted file mode 100644 index 3875c0c02..000000000 --- a/cache/collectionCache.go +++ /dev/null @@ -1,34 +0,0 @@ -package cache - -import ( - "razor/pkg/bindings" - "sync" -) - -// CollectionsCacheStruct struct to hold collection cache and associated mutex -type CollectionsCacheStruct struct { - Collections map[uint16]bindings.StructsCollection - Mu sync.RWMutex -} - -// CollectionsCache Global instances of CollectionsCacheStruct directly initialized -var CollectionsCache = CollectionsCacheStruct{ - Collections: make(map[uint16]bindings.StructsCollection), - Mu: sync.RWMutex{}, -} - -func GetCollectionFromCache(collectionId uint16) (bindings.StructsCollection, bool) { - CollectionsCache.Mu.RLock() // Use read lock for concurrency safety - defer CollectionsCache.Mu.RUnlock() - - collection, exists := CollectionsCache.Collections[collectionId] - return collection, exists -} - -func UpdateCollectionCache(collectionId uint16, updatedCollection bindings.StructsCollection) { - CollectionsCache.Mu.Lock() - defer CollectionsCache.Mu.Unlock() - - // Update or add the collection in the cache with the new data - CollectionsCache.Collections[collectionId] = updatedCollection -} diff --git a/cache/jobCache.go b/cache/jobCache.go deleted file mode 100644 index bc2822a12..000000000 --- a/cache/jobCache.go +++ /dev/null @@ -1,34 +0,0 @@ -package cache - -import ( - "razor/pkg/bindings" - "sync" -) - -// JobsCacheStruct struct to hold job cache and associated mutex -type JobsCacheStruct struct { - Jobs map[uint16]bindings.StructsJob - Mu sync.RWMutex -} - -// JobsCache Global instance of JobsCacheStruct directly initialized -var JobsCache = JobsCacheStruct{ - Jobs: make(map[uint16]bindings.StructsJob), - Mu: sync.RWMutex{}, -} - -func GetJobFromCache(jobId uint16) (bindings.StructsJob, bool) { - JobsCache.Mu.RLock() // Use read lock for concurrency safety - defer JobsCache.Mu.RUnlock() - - job, exists := JobsCache.Jobs[jobId] - return job, exists -} - -func UpdateJobCache(jobId uint16, updatedJob bindings.StructsJob) { - JobsCache.Mu.Lock() - defer JobsCache.Mu.Unlock() - - // Update or add the job in the cache with the new data - JobsCache.Jobs[jobId] = updatedJob -} diff --git a/cmd/eventListeners.go b/cmd/eventListeners.go deleted file mode 100644 index 4a5f897a1..000000000 --- a/cmd/eventListeners.go +++ /dev/null @@ -1,204 +0,0 @@ -package cmd - -import ( - "context" - "github.com/ethereum/go-ethereum" - "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" - Types "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/ethclient" - "math/big" - "razor/cache" - "razor/core" - "razor/pkg/bindings" - "razor/utils" - "strings" - "time" -) - -func (*UtilsStruct) InitAssetCache(client *ethclient.Client) error { - log.Info("INITIALIZING JOBS AND COLLECTIONS CACHE...") - if err := utils.InitJobsCache(client); err != nil { - log.Error("Error in initializing jobs cache: ", err) - return err - } - if err := utils.InitCollectionsCache(client); err != nil { - log.Error("Error in initializing collections cache: ", err) - return err - } - - latestHeader, err := clientUtils.GetLatestBlockWithRetry(client) - if err != nil { - log.Error("Error in fetching block: ", err) - return err - } - log.Debugf("initAssetCache: Latest header value: %d", latestHeader.Number) - - fromBlock, err := razorUtils.EstimateBlockNumberAtEpochBeginning(client, latestHeader.Number) - if err != nil { - log.Error("Error in estimating block number at epoch beginning: ", err) - return err - } - - // Start listeners for job and collection updates - go startListener(client, fromBlock, time.Second*time.Duration(core.AssetUpdateListenerInterval), listenForJobUpdates) - go startListener(client, fromBlock, time.Second*time.Duration(core.AssetUpdateListenerInterval), listenForCollectionUpdates) - go startListener(client, fromBlock, time.Second*time.Duration(core.AssetUpdateListenerInterval), listenForAssetCreation) - - return nil -} - -// startListener starts a generic listener for blockchain events. -func startListener(client *ethclient.Client, fromBlock *big.Int, interval time.Duration, listenerFunc func(*ethclient.Client, abi.ABI, *big.Int, *big.Int)) { - collectionManagerContractABI, err := abi.JSON(strings.NewReader(bindings.CollectionManagerMetaData.ABI)) - if err != nil { - log.Errorf("Failed to parse contract ABI: %v", err) - return - } - - ticker := time.NewTicker(interval) - defer ticker.Stop() - - for range ticker.C { - toBlock, err := clientUtils.GetLatestBlockWithRetry(client) - if err != nil { - log.Error("Error in getting latest block to start event listener: ", err) - continue - } - - listenerFunc(client, collectionManagerContractABI, fromBlock, toBlock.Number) - // Update fromBlock for the next interval - fromBlock = new(big.Int).Add(toBlock.Number, big.NewInt(1)) - } -} - -// listenForJobUpdates listens and processes job update events. -func listenForJobUpdates(client *ethclient.Client, collectionManagerContractABI abi.ABI, fromBlock, toBlock *big.Int) { - err := processEventLogs(client, collectionManagerContractABI, fromBlock, toBlock, "JobUpdated", func(topics []common.Hash, vLog Types.Log) { - jobId := utils.ConvertHashToUint16(topics[1]) - updatedJob, err := utils.UtilsInterface.GetActiveJob(client, jobId) - if err != nil { - log.Errorf("Error in getting job with job Id %v: %v", jobId, err) - return - } - log.Debugf("RECEIVED ASSET UPDATE: Updating the job with Id %v with details %+v...", jobId, updatedJob) - cache.UpdateJobCache(jobId, updatedJob) - }) - - if err != nil { - log.Errorf("Error processing JobUpdated events: %v", err) - return - } -} - -// listenForCollectionUpdates listens and processes collection update and collection activity status events. -func listenForCollectionUpdates(client *ethclient.Client, collectionManagerContractABI abi.ABI, fromBlock, toBlock *big.Int) { - // Process CollectionCreated event - err := processEventLogs(client, collectionManagerContractABI, fromBlock, toBlock, "CollectionUpdated", func(topics []common.Hash, vLog Types.Log) { - collectionId := utils.ConvertHashToUint16(topics[1]) - newCollection, err := utils.UtilsInterface.GetCollection(client, collectionId) - if err != nil { - log.Errorf("Error in getting collection with collection Id %v: %v", collectionId, err) - return - } - log.Debugf("RECEIVED ASSET UPDATE: Updating the collection with ID %v with details %+v", collectionId, newCollection) - cache.UpdateCollectionCache(collectionId, newCollection) - }) - - if err != nil { - log.Errorf("Error processing CollectionCreated events: %v", err) - return - } - - // Process CollectionActivityStatus event - err = processEventLogs(client, collectionManagerContractABI, fromBlock, toBlock, "CollectionActivityStatus", func(topics []common.Hash, vLog Types.Log) { - collectionId := utils.ConvertHashToUint16(topics[1]) - updatedCollection, err := utils.UtilsInterface.GetCollection(client, collectionId) - if err != nil { - log.Errorf("Error in getting updated collection with collection Id %v: %v", collectionId, err) - return - } - log.Debugf("RECEIVED ASSET UPDATE: Updating the activity status for collection with ID %v with details %+v", collectionId, updatedCollection) - cache.UpdateCollectionCache(collectionId, updatedCollection) - }) - - if err != nil { - log.Errorf("Error processing CollectionActivityStatus events: %v", err) - } -} - -// listenForAssetCreation listens and processes asset creation events. -func listenForAssetCreation(client *ethclient.Client, collectionManagerContractABI abi.ABI, fromBlock, toBlock *big.Int) { - // Process JobCreated events - err := processEventLogs(client, collectionManagerContractABI, fromBlock, toBlock, "JobCreated", func(topics []common.Hash, vLog Types.Log) { - jobId := utils.ConvertHashToUint16(topics[1]) - newJob, err := utils.UtilsInterface.GetActiveJob(client, jobId) - if err != nil { - log.Errorf("Error in getting job with job Id %v: %v", jobId, err) - return - } - log.Debugf("RECEIVED ASSET UPDATE: New JobCreated event detected for job ID %v with details %+v", jobId, newJob) - cache.UpdateJobCache(jobId, newJob) - }) - - if err != nil { - log.Errorf("Error processing JobCreated events: %v", err) - return - } - - // Process CollectionCreated events - err = processEventLogs(client, collectionManagerContractABI, fromBlock, toBlock, "CollectionCreated", func(topics []common.Hash, vLog Types.Log) { - collectionId := utils.ConvertHashToUint16(topics[1]) - newCollection, err := utils.UtilsInterface.GetCollection(client, collectionId) - if err != nil { - log.Errorf("Error in getting collection with collection Id %v: %v", collectionId, err) - return - } - log.Debugf("RECEIVED ASSET UPDATE: New CollectionCreated event detected for collection ID %v with details %+v", collectionId, newCollection) - cache.UpdateCollectionCache(collectionId, newCollection) - }) - - if err != nil { - log.Errorf("Error processing CollectionCreated events: %v", err) - return - } -} - -// getEventLogs is a utility function to fetch the event logs -func getEventLogs(client *ethclient.Client, fromBlock *big.Int, toBlock *big.Int) ([]Types.Log, error) { - // Set up the query for filtering logs - query := ethereum.FilterQuery{ - FromBlock: fromBlock, - ToBlock: toBlock, - Addresses: []common.Address{ - common.HexToAddress(core.CollectionManagerAddress), - }, - } - - // Retrieve the logs - logs, err := client.FilterLogs(context.Background(), query) - if err != nil { - log.Errorf("Error in filter logs: %v", err) - return []Types.Log{}, nil - } - - return logs, nil -} - -// processEventLogs is a utility function to process the event logs using a provided handler function. -func processEventLogs(client *ethclient.Client, collectionManagerContractABI abi.ABI, fromBlock, toBlock *big.Int, eventName string, handler func(topics []common.Hash, vLog Types.Log)) error { - logs, err := getEventLogs(client, fromBlock, toBlock) - if err != nil { - log.Errorf("Failed to fetch logs for %s event: %v", eventName, err) - return err - } - - eventID := collectionManagerContractABI.Events[eventName].ID.Hex() - for _, vLog := range logs { - if len(vLog.Topics) > 0 && vLog.Topics[0].Hex() == eventID { - handler(vLog.Topics, vLog) - } - } - - return nil -} diff --git a/cmd/interface.go b/cmd/interface.go index f04d02905..3c2392540 100644 --- a/cmd/interface.go +++ b/cmd/interface.go @@ -249,7 +249,6 @@ type UtilsCmdInterface interface { ResetDispute(client *ethclient.Client, blockManager *bindings.BlockManager, txnOpts *bind.TransactOpts, epoch uint32) StoreBountyId(client *ethclient.Client, account types.Account) error CheckToDoResetDispute(client *ethclient.Client, blockManager *bindings.BlockManager, txnOpts *bind.TransactOpts, epoch uint32, sortedValues []*big.Int) - InitAssetCache(client *ethclient.Client) error } type TransactionInterface interface { diff --git a/cmd/mocks/utils_cmd_interface.go b/cmd/mocks/utils_cmd_interface.go index cdf4be167..9b8b794a0 100644 --- a/cmd/mocks/utils_cmd_interface.go +++ b/cmd/mocks/utils_cmd_interface.go @@ -1353,20 +1353,6 @@ func (_m *UtilsCmdInterface) IndexRevealEventsOfCurrentEpoch(client *ethclient.C return r0, r1 } -// InitAssetCache provides a mock function with given fields: client -func (_m *UtilsCmdInterface) InitAssetCache(client *ethclient.Client) error { - ret := _m.Called(client) - - var r0 error - if rf, ok := ret.Get(0).(func(*ethclient.Client) error); ok { - r0 = rf(client) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // InitiateCommit provides a mock function with given fields: client, config, account, epoch, stakerId, rogueData func (_m *UtilsCmdInterface) InitiateCommit(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, stakerId uint32, rogueData types.Rogue) error { ret := _m.Called(client, config, account, epoch, stakerId, rogueData) diff --git a/cmd/vote.go b/cmd/vote.go index 682390769..2e3739718 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -89,12 +89,6 @@ func (*UtilsStruct) ExecuteVote(flagSet *pflag.FlagSet) { account := types.Account{Address: address, Password: password} cmdUtils.HandleExit() - - err = cmdUtils.InitAssetCache(client) - utils.CheckError("Error in initializing asset cache: ", err) - - go utils.HandleResetCache(client, config.BufferPercent) - log.Debugf("Calling Vote() with arguments rogueData = %+v, account address = %s, backup node actions to ignore = %s", rogueData, account.Address, backupNodeActionsToIgnore) if err := cmdUtils.Vote(context.Background(), config, client, rogueData, account, backupNodeActionsToIgnore); err != nil { log.Errorf("%v\n", err) diff --git a/cmd/vote_test.go b/cmd/vote_test.go index 87e31ea72..4dadc4683 100644 --- a/cmd/vote_test.go +++ b/cmd/vote_test.go @@ -25,17 +25,16 @@ func TestExecuteVote(t *testing.T) { var config types.Configurations type args struct { - config types.Configurations - configErr error - password string - rogueStatus bool - rogueErr error - rogueMode []string - rogueModeErr error - address string - addressErr error - initAssetCacheErr error - voteErr error + config types.Configurations + configErr error + password string + rogueStatus bool + rogueErr error + rogueMode []string + rogueModeErr error + address string + addressErr error + voteErr error } tests := []struct { name string @@ -116,18 +115,6 @@ func TestExecuteVote(t *testing.T) { }, expectedFatal: false, }, - { - name: "Test 7: When there is an error in initializing cache", - args: args{ - config: config, - password: "test", - address: "0x000000000000000000000000000000000000dea1", - rogueStatus: true, - rogueMode: []string{}, - initAssetCacheErr: errors.New("initAssetCache error"), - }, - expectedFatal: true, - }, } defer func() { log.ExitFunc = nil }() @@ -147,7 +134,6 @@ func TestExecuteVote(t *testing.T) { utilsMock.On("ConnectToClient", mock.AnythingOfType("string")).Return(client) flagSetMock.On("GetBoolRogue", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.rogueStatus, tt.args.rogueErr) flagSetMock.On("GetStringSliceRogueMode", mock.AnythingOfType("*pflag.FlagSet")).Return(tt.args.rogueMode, tt.args.rogueModeErr) - cmdUtilsMock.On("InitAssetCache", mock.Anything).Return(tt.args.initAssetCacheErr) cmdUtilsMock.On("HandleExit").Return() cmdUtilsMock.On("Vote", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.voteErr) osMock.On("Exit", mock.AnythingOfType("int")).Return() diff --git a/core/constants.go b/core/constants.go index c182bf5d2..c8952f864 100644 --- a/core/constants.go +++ b/core/constants.go @@ -81,8 +81,3 @@ var HexArrayExtractIndexRegex = `^hexArray\[(\d+)\]$` var BatchSize = 1000 var NumRoutines = 10 var MaxIterations = 10000000 - -// Following are the constants which determine storing jobs and collections value for time being in cache - -var AssetUpdateListenerInterval = 10 -var AssetCacheExpiry = 5 * EpochLength diff --git a/utils/asset.go b/utils/asset.go index 383ebe9c9..03d9bbfa0 100644 --- a/utils/asset.go +++ b/utils/asset.go @@ -199,9 +199,9 @@ func (*UtilsStruct) Aggregate(client *ethclient.Client, previousEpoch uint32, co for _, id := range collection.JobIDs { // Ignoring the Jobs which are already overriden and added to jobs array if !Contains(overriddenJobIds, id) { - job, isPresent := cache.GetJobFromCache(id) - if !isPresent { - log.Errorf("Job with id %v is not present in cache", id) + job, err := UtilsInterface.GetActiveJob(client, id) + if err != nil { + log.Errorf("Error in fetching job %d: %v", id, err) continue } jobs = append(jobs, job) @@ -243,9 +243,9 @@ func (*UtilsStruct) GetActiveJob(client *ethclient.Client, jobId uint16) (bindin } func (*UtilsStruct) GetActiveCollection(client *ethclient.Client, collectionId uint16) (bindings.StructsCollection, error) { - collection, isPresent := cache.GetCollectionFromCache(collectionId) - if !isPresent { - return bindings.StructsCollection{}, errors.New("collection not present in cache") + collection, err := UtilsInterface.GetCollection(client, collectionId) + if err != nil { + return bindings.StructsCollection{}, err } if !collection.Active { return bindings.StructsCollection{}, errors.New("collection inactive") @@ -531,95 +531,6 @@ func (*UtilsStruct) HandleOfficialJobsFromJSONFile(client *ethclient.Client, col return overrideJobs, overriddenJobIds } -func HandleResetCache(client *ethclient.Client, bufferPercent int32) { - assetCacheTicker := time.NewTicker(time.Second * time.Duration(core.AssetCacheExpiry)) - defer assetCacheTicker.Stop() - - for { - <-assetCacheTicker.C // Wait for the next tick - log.Info("ASSET CACHE EXPIRED! INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...") - if err := UtilsInterface.ResetAssetCache(client, bufferPercent); err != nil { - log.Errorf("Error resetting asset cache: %v", err) - } - } -} - -func (*UtilsStruct) ResetAssetCache(client *ethclient.Client, bufferPercent int32) error { - state, err := UtilsInterface.GetBufferedState(client, bufferPercent) - if err != nil { - log.Error("Error in getting buffered state: ", err) - return err - } - // Avoiding resetting jobs/collections cache in commit state - if state == 0 { - log.Info("ResetAssetCache: Cannot reset Jobs/Collections cache in commit state!") - stateRemainingTime, err := UtilsInterface.GetRemainingTimeOfCurrentState(client, bufferPercent) - if err != nil { - log.Error("Error in getting remaining time of current state: ", err) - return err - } - log.Infof("ResetAssetCache: Waiting for commit state to complete, sleeping for %v seconds...", stateRemainingTime) - time.Sleep(time.Second * time.Duration(stateRemainingTime)) - log.Infof("ResetAssetCache: INITIALIZING JOBS AND COLLECTIONS CACHE NOW!") - } - - if err := InitJobsCache(client); err != nil { - log.Error("Error in initializing jobs cache: ", err) - return err - } - if err := InitCollectionsCache(client); err != nil { - log.Error("Error in initializing collections cache: ", err) - return err - } - return nil -} - -func InitJobsCache(client *ethclient.Client) error { - cache.JobsCache.Mu.Lock() - defer cache.JobsCache.Mu.Unlock() - - // Flush the JobsCache before initialization - for k := range cache.JobsCache.Jobs { - delete(cache.JobsCache.Jobs, k) - } - - numJobs, err := AssetManagerInterface.GetNumJobs(client) - if err != nil { - return err - } - for i := 1; i <= int(numJobs); i++ { - job, err := UtilsInterface.GetActiveJob(client, uint16(i)) - if err != nil { - return err - } - cache.JobsCache.Jobs[job.Id] = job - } - return nil -} - -func InitCollectionsCache(client *ethclient.Client) error { - cache.CollectionsCache.Mu.Lock() - defer cache.CollectionsCache.Mu.Unlock() - - // Flush the CollectionsCacheStruct before initialization - for k := range cache.CollectionsCache.Collections { - delete(cache.CollectionsCache.Collections, k) - } - - numCollections, err := UtilsInterface.GetNumCollections(client) - if err != nil { - return err - } - for i := 1; i <= int(numCollections); i++ { - collection, err := AssetManagerInterface.GetCollection(client, uint16(i)) - if err != nil { - return err - } - cache.CollectionsCache.Collections[collection.Id] = collection - } - return nil -} - func ReplaceValueWithDataFromENVFile(re *regexp.Regexp, value string) string { // substrings denotes all the occurrences of substring which satisfies APIKeyRegex substrings := re.FindAllString(value, -1) diff --git a/utils/asset_test.go b/utils/asset_test.go index 6d214e6fd..4df204b48 100644 --- a/utils/asset_test.go +++ b/utils/asset_test.go @@ -29,12 +29,7 @@ func TestAggregate(t *testing.T) { var previousEpoch uint32 var fileInfo fs.FileInfo - job1 := bindings.StructsJob{Id: 1, SelectorType: 1, Weight: 100, - Power: 2, Name: "ethusd_gemini", Selector: "last", - Url: "https://api.gemini.com/v1/pubticker/ethusd", - } - - job2 := bindings.StructsJob{Id: 2, SelectorType: 1, Weight: 100, + job := bindings.StructsJob{Id: 1, SelectorType: 1, Weight: 100, Power: 2, Name: "ethusd_gemini", Selector: "last", Url: "https://api.gemini.com/v1/pubticker/ethusd", } @@ -44,13 +39,14 @@ func TestAggregate(t *testing.T) { Id: 4, Power: 2, AggregationMethod: 2, - JobIDs: []uint16{1, 2}, + JobIDs: []uint16{1, 2, 3}, Name: "ethCollectionMean", } type args struct { collection bindings.StructsCollection - jobCacheError bool + activeJob bindings.StructsJob + activeJobErr error dataToCommit []*big.Int dataToCommitErr error weight []uint8 @@ -76,20 +72,21 @@ func TestAggregate(t *testing.T) { name: "Test 1: When Aggregate() executes successfully", args: args{ collection: collection, - dataToCommit: []*big.Int{big.NewInt(3827200), big.NewInt(3828474)}, - weight: []uint8{1, 1}, + activeJob: job, + dataToCommit: []*big.Int{big.NewInt(3827200), big.NewInt(3828474), big.NewInt(3826440), big.NewInt(3824616), big.NewInt(3823852)}, + weight: []uint8{1, 1, 1, 1, 1}, prevCommitmentData: big.NewInt(1), assetFilePath: "", statErr: nil, }, - want: big.NewInt(3827837), + want: big.NewInt(3826116), wantErr: false, }, { - name: "Test 2: When the job is not present in cache", + name: "Test 2: When there is an error in getting activeJob", args: args{ collection: collection, - jobCacheError: true, + activeJobErr: errors.New("activeJob error"), dataToCommit: []*big.Int{big.NewInt(2)}, weight: []uint8{100}, prevCommitmentData: big.NewInt(1), @@ -103,6 +100,7 @@ func TestAggregate(t *testing.T) { name: "Test 3: When there is an error in getting dataToCommit", args: args{ collection: collection, + activeJob: job, dataToCommitErr: errors.New("dataToCommit error"), weight: []uint8{100}, prevCommitmentData: big.NewInt(1), @@ -114,6 +112,7 @@ func TestAggregate(t *testing.T) { name: "Test 4: When there is an error in getting prevCommitmentData", args: args{ collection: collection, + activeJob: job, dataToCommit: []*big.Int{big.NewInt(2)}, weight: []uint8{100}, prevCommitmentDataErr: errors.New("prevCommitmentData error"), @@ -125,6 +124,7 @@ func TestAggregate(t *testing.T) { name: "Test 5: When there is an error in getting prevCommitmentData", args: args{ collection: collection, + activeJob: job, dataToCommitErr: errors.New("dataToCommit error"), weight: []uint8{100}, prevCommitmentDataErr: errors.New("prevCommitmentData error"), @@ -171,12 +171,6 @@ func TestAggregate(t *testing.T) { }, } for _, tt := range tests { - if !tt.args.jobCacheError { - cache.JobsCache.Jobs[job1.Id] = job1 - cache.JobsCache.Jobs[job2.Id] = job2 - cache.CollectionsCache.Collections[collection.Id] = collection - } - t.Run(tt.name, func(t *testing.T) { utilsMock := new(mocks.Utils) pathUtilsMock := new(pathMocks.PathInterface) @@ -191,7 +185,7 @@ func TestAggregate(t *testing.T) { path.OSUtilsInterface = osUtilsMock utils := StartRazor(optionsPackageStruct) - //utilsMock.On("GetActiveJob", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint16")).Return(tt.args.activeJob, tt.args.activeJobErr) + utilsMock.On("GetActiveJob", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint16")).Return(tt.args.activeJob, tt.args.activeJobErr) utilsMock.On("GetDataToCommitFromJobs", mock.Anything, mock.Anything).Return(tt.args.dataToCommit, tt.args.weight, tt.args.dataToCommitErr) utilsMock.On("FetchPreviousValue", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint32"), mock.AnythingOfType("uint16")).Return(tt.args.prevCommitmentData, tt.args.prevCommitmentDataErr) pathUtilsMock.On("GetJobFilePath").Return(tt.args.assetFilePath, tt.args.assetFilePathErr) @@ -209,9 +203,6 @@ func TestAggregate(t *testing.T) { t.Errorf("Aggregate() got = %v, want %v", got, tt.want) } }) - delete(cache.JobsCache.Jobs, job1.Id) - delete(cache.JobsCache.Jobs, job2.Id) - delete(cache.CollectionsCache.Collections, collection.Id) } } @@ -277,28 +268,23 @@ func TestGetActiveCollectionIds(t *testing.T) { func TestGetActiveCollection(t *testing.T) { var client *ethclient.Client + var collectionId uint16 - collectionEth := bindings.StructsCollection{ - Active: true, - Id: 1, + collectionEth := bindings.StructsCollection{Active: true, + Id: 2, Power: 2, AggregationMethod: 2, JobIDs: []uint16{1, 2}, Name: "ethCollectionMean", } - collectionEthInactive := bindings.StructsCollection{ - Active: false, - Id: 2, - Power: 2, - AggregationMethod: 2, - JobIDs: []uint16{1, 2}, - Name: "ethCollectionMean", + collectionEthInactive := bindings.StructsCollection{Active: false, Id: 2, Power: 2, + AggregationMethod: 2, JobIDs: []uint16{1, 2}, Name: "ethCollectionMean", } type args struct { - collectionId uint16 - collectionCacheErr bool + collection bindings.StructsCollection + collectionErr error } tests := []struct { name string @@ -309,15 +295,15 @@ func TestGetActiveCollection(t *testing.T) { { name: "Test 1: When GetActiveCollection() executes successfully", args: args{ - collectionId: 1, + collection: collectionEth, }, want: collectionEth, wantErr: false, }, { - name: "Test 2: When the collection is not present in cache", + name: "Test 2: When there is an error in getting collection", args: args{ - collectionCacheErr: true, + collectionErr: errors.New("collection error"), }, want: bindings.StructsCollection{}, wantErr: true, @@ -325,15 +311,13 @@ func TestGetActiveCollection(t *testing.T) { { name: "Test 3: When there is an inactive collection", args: args{ - collectionId: 2, + collection: collectionEthInactive, }, want: bindings.StructsCollection{}, wantErr: true, }, } for _, tt := range tests { - cache.CollectionsCache.Collections[collectionEth.Id] = collectionEth - cache.CollectionsCache.Collections[collectionEthInactive.Id] = collectionEthInactive t.Run(tt.name, func(t *testing.T) { utilsMock := new(mocks.Utils) @@ -341,7 +325,10 @@ func TestGetActiveCollection(t *testing.T) { UtilsInterface: utilsMock, } utils := StartRazor(optionsPackageStruct) - got, err := utils.GetActiveCollection(client, tt.args.collectionId) + + utilsMock.On("GetCollection", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint16")).Return(tt.args.collection, tt.args.collectionErr) + + got, err := utils.GetActiveCollection(client, collectionId) if (err != nil) != tt.wantErr { t.Errorf("GetActiveCollection() error = %v, wantErr %v", err, tt.wantErr) return @@ -350,8 +337,6 @@ func TestGetActiveCollection(t *testing.T) { t.Errorf("GetActiveCollection() got = %v, want %v", got, tt.want) } }) - delete(cache.CollectionsCache.Collections, collectionEth.Id) - delete(cache.CollectionsCache.Collections, collectionEthInactive.Id) } } @@ -1638,130 +1623,3 @@ func TestIsJSONCompatible(t *testing.T) { }) } } - -func TestUtilsStruct_ResetAssetCache(t *testing.T) { - var ( - client *ethclient.Client - bufferPercent int32 - ) - - type args struct { - state int64 - stateErr error - stateRemainingTime int64 - stateRemainingTimeErr error - numOfJobs uint16 - numOfJobsErr error - job bindings.StructsJob - jobErr error - numOfCollections uint16 - numOfCollectionsErr error - collection bindings.StructsCollection - collectionErr error - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "Successful Reset", - args: args{ - state: 1, // Not in commit state - numOfJobs: 2, - job: bindings.StructsJob{Id: 1}, - numOfCollections: 2, - collection: bindings.StructsCollection{Id: 1}, - }, - wantErr: false, - }, - { - name: "Successful Reset after waiting in commit state", - args: args{ - state: 0, // Not in commit state - stateRemainingTime: 1, - numOfJobs: 1, - job: bindings.StructsJob{Id: 1}, - numOfCollections: 1, - collection: bindings.StructsCollection{Id: 1}, - }, - wantErr: false, - }, - { - name: "Error in getting buffered state", - args: args{ - stateErr: errors.New("state error"), - }, - wantErr: true, - }, - { - name: "Error in getting state remaining time when resetting in commit state", - args: args{ - state: 0, // In commit state - stateRemainingTimeErr: errors.New("stateRemainingTime error"), - }, - wantErr: true, - }, - { - name: "Error in getting num Of jobs", - args: args{ - state: 1, - numOfJobsErr: errors.New("numOfJobs error"), - }, - wantErr: true, - }, - { - name: "Error in getting job", - args: args{ - state: 1, - numOfJobs: 1, - jobErr: errors.New("job error"), - }, - wantErr: true, - }, - { - name: "Error in getting num of collections", - args: args{ - state: 1, - numOfJobs: 2, - job: bindings.StructsJob{Id: 1}, - numOfCollectionsErr: errors.New("numOfCollections error"), - }, - wantErr: true, - }, - { - name: "Error in getting collection", - args: args{ - state: 1, - numOfJobs: 2, - job: bindings.StructsJob{Id: 1}, - numOfCollections: 1, - collectionErr: errors.New("collection error"), - }, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - utilsMock := new(mocks.Utils) - assetManagerMock := new(mocks.AssetManagerUtils) - - optionsPackageStruct := OptionsPackageStruct{ - UtilsInterface: utilsMock, - AssetManagerInterface: assetManagerMock, - } - utils := StartRazor(optionsPackageStruct) - - utilsMock.On("GetBufferedState", mock.Anything, mock.Anything).Return(tt.args.state, tt.args.stateErr) - utilsMock.On("GetRemainingTimeOfCurrentState", mock.Anything, mock.Anything).Return(tt.args.stateRemainingTime, tt.args.stateRemainingTimeErr) - assetManagerMock.On("GetNumJobs", mock.Anything).Return(tt.args.numOfJobs, tt.args.numOfJobsErr) - utilsMock.On("GetNumCollections", mock.Anything).Return(tt.args.numOfCollections, tt.args.numOfCollectionsErr) - utilsMock.On("GetActiveJob", mock.Anything, mock.Anything).Return(tt.args.job, tt.args.jobErr) - assetManagerMock.On("GetCollection", mock.Anything, mock.Anything).Return(tt.args.collection, tt.args.collectionErr) - - if err := utils.ResetAssetCache(client, bufferPercent); (err != nil) != tt.wantErr { - t.Errorf("ResetAssetCache() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} diff --git a/utils/interface.go b/utils/interface.go index 56164f63a..77304e2d8 100644 --- a/utils/interface.go +++ b/utils/interface.go @@ -158,7 +158,6 @@ type Utils interface { GetStakedTokenManagerWithOpts(client *ethclient.Client, tokenAddress common.Address) (*bindings.StakedToken, bind.CallOpts) GetStakerSRZRBalance(client *ethclient.Client, staker bindings.StructsStaker) (*big.Int, error) CheckPassword(address string, password string) error - ResetAssetCache(client *ethclient.Client, bufferPercent int32) error } type EthClientUtils interface { diff --git a/utils/math.go b/utils/math.go index 5312b64a6..976ec9202 100644 --- a/utils/math.go +++ b/utils/math.go @@ -3,7 +3,6 @@ package utils import ( "crypto/rand" "errors" - "github.com/ethereum/go-ethereum/common" "math" "math/big" mathRand "math/rand" @@ -295,11 +294,3 @@ func isHexArrayPattern(s string) bool { re := regexp.MustCompile(pattern) return re.MatchString(s) } - -func ConvertHashToUint16(hash common.Hash) uint16 { - // Convert the hash to a big integer to handle the numeric value - bigIntValue := hash.Big() - - // Convert the big integer to uint64 first (safe for down casting to uint16) and then downcast to uint16 - return uint16(bigIntValue.Uint64()) -} diff --git a/utils/mocks/utils.go b/utils/mocks/utils.go index c17df5990..a30978eaf 100644 --- a/utils/mocks/utils.go +++ b/utils/mocks/utils.go @@ -1838,20 +1838,6 @@ func (_m *Utils) ReadJSONData(fileName string) (map[string]*types.StructsJob, er return r0, r1 } -// ResetAssetCache provides a mock function with given fields: client, bufferPercent -func (_m *Utils) ResetAssetCache(client *ethclient.Client, bufferPercent int32) error { - ret := _m.Called(client, bufferPercent) - - var r0 error - if rf, ok := ret.Get(0).(func(*ethclient.Client, int32) error); ok { - r0 = rf(client, bufferPercent) - } else { - r0 = ret.Error(0) - } - - return r0 -} - // SecondsToReadableTime provides a mock function with given fields: input func (_m *Utils) SecondsToReadableTime(input int) string { ret := _m.Called(input)