From 22f28f0b89e0b2617d86ff48c124f4f50677d4e2 Mon Sep 17 00:00:00 2001 From: YashK Date: Mon, 26 Feb 2024 15:04:14 +0530 Subject: [PATCH 01/10] refactor: moved resetting cache in a separate go routine --- cmd/vote.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/cmd/vote.go b/cmd/vote.go index 865c3b0c..8dfa232d 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -132,16 +132,8 @@ func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, clien for { select { case <-assetCacheTicker.C: - log.Info("ASSET CACHE EXPIRED!") - log.Info("INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...") - if err := utils.InitJobsCache(client); err != nil { - log.Error("Error in initializing jobs cache: ", err) - continue - } - if err := utils.InitCollectionsCache(client); err != nil { - log.Error("Error in initializing collections cache: ", err) - continue - } + log.Info("ASSET CACHE EXPIRED! INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...") + go utils.ResetAssetCache(client) case <-ctx.Done(): return nil default: @@ -529,7 +521,7 @@ func (*UtilsStruct) InitiateReveal(client *ethclient.Client, config types.Config if revealTxn != core.NilHash { waitForBlockCompletionErr := razorUtils.WaitForBlockCompletion(client, revealTxn.Hex()) if waitForBlockCompletionErr != nil { - log.Error("Error in WaitForBlockCompletionErr for reveal: ", err) + log.Error("Error in WaitForBlockCompletionErr for reveal: ", waitForBlockCompletionErr) return err } } From aecfe15f6ee4b976ea84734c272c9f9cd11dec55 Mon Sep 17 00:00:00 2001 From: YashK Date: Mon, 26 Feb 2024 15:05:57 +0530 Subject: [PATCH 02/10] fix: fixed default and max/min values for wait and buffer from config --- cmd/config-utils.go | 12 ++++++------ cmd/config-utils_test.go | 16 ++++++++-------- config.sh | 8 ++++---- core/constants.go | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/cmd/config-utils.go b/cmd/config-utils.go index d4e856cd..c788631f 100644 --- a/cmd/config-utils.go +++ b/cmd/config-utils.go @@ -209,8 +209,8 @@ func (*UtilsStruct) GetMultiplier() (float32, error) { //This function returns the buffer percent func (*UtilsStruct) GetBufferPercent() (int32, error) { const ( - MinBufferPercent = 10 - MaxBufferPercent = 30 + MinBufferPercent = 0 + MaxBufferPercent = 5 ) bufferPercent, err := getConfigValue("buffer", "int32", core.DefaultBufferPercent, "buffer") @@ -238,8 +238,8 @@ func (*UtilsStruct) GetBufferPercent() (int32, error) { //This function returns the wait time func (*UtilsStruct) GetWaitTime() (int32, error) { const ( - MinWaitTime = 1 // Minimum wait time in seconds - MaxWaitTime = 30 // Maximum wait time in seconds + MinWaitTime = 1 // Minimum wait time in seconds + MaxWaitTime = 5 // Maximum wait time in seconds ) waitTime, err := getConfigValue("wait", "int32", core.DefaultWaitTime, "wait") @@ -335,7 +335,7 @@ func (*UtilsStruct) GetGasLimitOverride() (uint64, error) { func (*UtilsStruct) GetRPCTimeout() (int64, error) { const ( MinRPCTimeout = 10 // Minimum RPC timeout in seconds - MaxRPCTimeout = 60 // Maximum RPC timeout in seconds + MaxRPCTimeout = 20 // Maximum RPC timeout in seconds ) rpcTimeout, err := getConfigValue("rpcTimeout", "int64", core.DefaultRPCTimeout, "rpcTimeout") @@ -357,7 +357,7 @@ func (*UtilsStruct) GetRPCTimeout() (int64, error) { func (*UtilsStruct) GetHTTPTimeout() (int64, error) { const ( MinHTTPTimeout = 10 // Minimum HTTP timeout in seconds - MaxHTTPTimeout = 60 // Maximum HTTP timeout in seconds + MaxHTTPTimeout = 20 // Maximum HTTP timeout in seconds ) httpTimeout, err := getConfigValue("httpTimeout", "int64", core.DefaultHTTPTimeout, "httpTimeout") diff --git a/cmd/config-utils_test.go b/cmd/config-utils_test.go index 736c3b9a..da9e80f4 100644 --- a/cmd/config-utils_test.go +++ b/cmd/config-utils_test.go @@ -260,9 +260,9 @@ func TestGetBufferPercent(t *testing.T) { name: "Test 1: When buffer percent is fetched from root flag", args: args{ isFlagSet: true, - bufferPercent: 15, + bufferPercent: 5, }, - want: 15, + want: 5, wantErr: nil, }, { @@ -278,9 +278,9 @@ func TestGetBufferPercent(t *testing.T) { name: "Test 3: When buffer value is fetched from config", useDummyConfigFile: true, args: args{ - bufferInTestConfig: 30, + bufferInTestConfig: 1, }, - want: 30, + want: 1, wantErr: nil, }, { @@ -1130,9 +1130,9 @@ func TestGetWaitTime(t *testing.T) { name: "Test 1: When wait time is fetched from root flag", args: args{ isFlagSet: true, - waitTime: 10, + waitTime: 2, }, - want: 10, + want: 2, wantErr: nil, }, { @@ -1148,9 +1148,9 @@ func TestGetWaitTime(t *testing.T) { name: "Test 3: When wait time value is fetched from config", useDummyConfigFile: true, args: args{ - waitInTestConfig: 20, + waitInTestConfig: 3, }, - want: 20, + want: 3, wantErr: nil, }, { diff --git a/config.sh b/config.sh index c5b6e40a..4d8c884f 100644 --- a/config.sh +++ b/config.sh @@ -19,10 +19,10 @@ then GAS_MULTIPLIER=1.0 fi -read -rp "Buffer Percent: (20) " BUFFER +read -rp "Buffer Percent: (0) " BUFFER if [ -z "$BUFFER" ]; then - BUFFER=20 + BUFFER=0 fi read -rp "Wait Time: (1) " WAIT_TIME @@ -30,9 +30,9 @@ if [ -z "$WAIT_TIME" ]; then WAIT_TIME=1 fi -read -rp "Gas Price: (1) " GAS_PRICE +read -rp "Gas Price: (0) " GAS_PRICE if [ -z "$GAS_PRICE" ]; then - GAS_PRICE=1 + GAS_PRICE=0 fi read -rp "Gas Limit Increment : (2) " GAS_LIMIT diff --git a/core/constants.go b/core/constants.go index f8a2051a..493a563b 100644 --- a/core/constants.go +++ b/core/constants.go @@ -21,7 +21,7 @@ var BlockCompletionTimeout = 30 //Following are the default config values for all the config parameters var DefaultGasMultiplier float32 = 1.0 -var DefaultBufferPercent int32 = 20 +var DefaultBufferPercent int32 = 0 var DefaultGasPrice int32 = 0 var DefaultWaitTime int32 = 1 var DefaultGasLimit float32 = 2 From 74d8b765c32620aa3eb8bb8a894c147d859d03e7 Mon Sep 17 00:00:00 2001 From: YashK Date: Mon, 26 Feb 2024 15:07:19 +0530 Subject: [PATCH 03/10] refactor: separated out resetting job/collection cache in a function --- utils/asset.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/utils/asset.go b/utils/asset.go index 4eb8cc89..cfac5e35 100644 --- a/utils/asset.go +++ b/utils/asset.go @@ -531,6 +531,17 @@ func (*UtilsStruct) HandleOfficialJobsFromJSONFile(client *ethclient.Client, col return overrideJobs, overriddenJobIds } +func ResetAssetCache(client *ethclient.Client) { + if err := InitJobsCache(client); err != nil { + log.Error("Error in initializing jobs cache: ", err) + return + } + if err := InitCollectionsCache(client); err != nil { + log.Error("Error in initializing collections cache: ", err) + return + } +} + func InitJobsCache(client *ethclient.Client) error { cache.JobsCache.Mu.Lock() defer cache.JobsCache.Mu.Unlock() From 7d397c8eb4988df35bfec31806716efd017d70a0 Mon Sep 17 00:00:00 2001 From: YashK Date: Mon, 26 Feb 2024 15:28:43 +0530 Subject: [PATCH 04/10] refactor: recorrected 0 value for buffer log --- cmd/config-utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/config-utils.go b/cmd/config-utils.go index c788631f..6fde8983 100644 --- a/cmd/config-utils.go +++ b/cmd/config-utils.go @@ -228,7 +228,7 @@ func (*UtilsStruct) GetBufferPercent() (int32, error) { // If bufferPercent is 0, use the default value. if bufferPercentInt32 == 0 { - log.Debugf("BufferPercent is unset, using default value %d", core.DefaultBufferPercent) + log.Debugf("BufferPercent is unset or set to 0, using its default %d value", core.DefaultBufferPercent) return core.DefaultBufferPercent, nil } From d9eaa5f45799d5aa7f1abdd15ca24064a907e3fa Mon Sep 17 00:00:00 2001 From: YashK Date: Tue, 27 Feb 2024 17:59:26 +0530 Subject: [PATCH 05/10] fix: cannot reset cache in commit state --- cmd/vote.go | 12 +++++++++++- utils/asset.go | 25 ++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/cmd/vote.go b/cmd/vote.go index 8dfa232d..d55003ba 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -127,13 +127,23 @@ func (*UtilsStruct) HandleExit() { //This function handles all the states of voting func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error { assetCacheTicker := time.NewTicker(time.Second * time.Duration(core.AssetCacheExpiry)) + errChan := make(chan error, 1) + header, err := clientUtils.GetLatestBlockWithRetry(client) utils.CheckError("Error in getting block: ", err) for { select { case <-assetCacheTicker.C: log.Info("ASSET CACHE EXPIRED! INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...") - go utils.ResetAssetCache(client) + go func() { + err := utils.ResetAssetCache(client, config.BufferPercent) + errChan <- err + }() + case err := <-errChan: // Handling the error from ResetAssetCache + if err != nil { + log.Errorf("Error resetting asset cache: %v", err) + continue + } case <-ctx.Done(): return nil default: diff --git a/utils/asset.go b/utils/asset.go index cfac5e35..d294ae3c 100644 --- a/utils/asset.go +++ b/utils/asset.go @@ -531,15 +531,34 @@ func (*UtilsStruct) HandleOfficialJobsFromJSONFile(client *ethclient.Client, col return overrideJobs, overriddenJobIds } -func ResetAssetCache(client *ethclient.Client) { +func 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 + return err } if err := InitCollectionsCache(client); err != nil { log.Error("Error in initializing collections cache: ", err) - return + return err } + return nil } func InitJobsCache(client *ethclient.Client) error { From 3e8d47ac2d4e5e07fc59304ac782d81a5a54805d Mon Sep 17 00:00:00 2001 From: YashK Date: Tue, 27 Feb 2024 18:01:16 +0530 Subject: [PATCH 06/10] refactor: defined buffer state sleep time in constants --- cmd/vote.go | 4 ++-- core/constants.go | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/vote.go b/cmd/vote.go index d55003ba..04c6ea3e 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -321,8 +321,8 @@ func (*UtilsStruct) HandleBlock(client *ethclient.Client, account types.Account, } } case -1: - if config.WaitTime > 5 { - timeUtils.Sleep(5 * time.Second) + if config.WaitTime >= core.BufferStateSleepTime { + timeUtils.Sleep(time.Second * time.Duration(core.BufferStateSleepTime)) return } } diff --git a/core/constants.go b/core/constants.go index 493a563b..c182bf5d 100644 --- a/core/constants.go +++ b/core/constants.go @@ -30,6 +30,9 @@ var DefaultRPCTimeout int64 = 10 var DefaultHTTPTimeout int64 = 10 var DefaultLogLevel = "" +//BufferStateSleepTime is the sleeping time whenever buffer state hits +var BufferStateSleepTime int32 = 2 + //Following are the default logFile parameters in config var DefaultLogFileMaxSize = 200 From 991623e8f4208e99d436c97c1ce8d9fd9b637b48 Mon Sep 17 00:00:00 2001 From: YashK Date: Wed, 28 Feb 2024 11:07:52 +0530 Subject: [PATCH 07/10] refactor: added method called for ResetAssetCache and added tests --- cmd/vote.go | 2 +- utils/asset.go | 2 +- utils/asset_test.go | 127 +++++++++++++++++++++++++++++++++++++++++++ utils/interface.go | 1 + utils/mocks/utils.go | 14 +++++ 5 files changed, 144 insertions(+), 2 deletions(-) diff --git a/cmd/vote.go b/cmd/vote.go index 04c6ea3e..bca33e57 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -136,7 +136,7 @@ func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, clien case <-assetCacheTicker.C: log.Info("ASSET CACHE EXPIRED! INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...") go func() { - err := utils.ResetAssetCache(client, config.BufferPercent) + err := razorUtils.ResetAssetCache(client, config.BufferPercent) errChan <- err }() case err := <-errChan: // Handling the error from ResetAssetCache diff --git a/utils/asset.go b/utils/asset.go index d294ae3c..8b2f0d23 100644 --- a/utils/asset.go +++ b/utils/asset.go @@ -531,7 +531,7 @@ func (*UtilsStruct) HandleOfficialJobsFromJSONFile(client *ethclient.Client, col return overrideJobs, overriddenJobIds } -func ResetAssetCache(client *ethclient.Client, bufferPercent int32) error { +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) diff --git a/utils/asset_test.go b/utils/asset_test.go index dac1786d..6d214e6f 100644 --- a/utils/asset_test.go +++ b/utils/asset_test.go @@ -1638,3 +1638,130 @@ 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 77304e2d..56164f63 100644 --- a/utils/interface.go +++ b/utils/interface.go @@ -158,6 +158,7 @@ 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/mocks/utils.go b/utils/mocks/utils.go index a30978ea..c17df599 100644 --- a/utils/mocks/utils.go +++ b/utils/mocks/utils.go @@ -1838,6 +1838,20 @@ 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) From a87b16f5858e6d321fb292a7485ff5becfdb2dc4 Mon Sep 17 00:00:00 2001 From: YashK Date: Thu, 29 Feb 2024 14:25:40 +0530 Subject: [PATCH 08/10] refactor: introduced resetting asset cache in a separate goroutine at the very start --- cmd/interface.go | 2 +- cmd/mocks/utils_cmd_interface.go | 10 +++++----- cmd/vote.go | 14 +++++++------- cmd/vote_test.go | 2 +- utils/asset.go | 13 +++++++++++++ 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/cmd/interface.go b/cmd/interface.go index f04d0290..2e0b74cf 100644 --- a/cmd/interface.go +++ b/cmd/interface.go @@ -234,7 +234,7 @@ type UtilsCmdInterface interface { CalculateSecret(account types.Account, epoch uint32, keystorePath string, chainId *big.Int) ([]byte, []byte, error) HandleBlock(client *ethclient.Client, account types.Account, blockNumber *big.Int, config types.Configurations, rogueData types.Rogue, backupNodeActionsToIgnore []string) ExecuteVote(flagSet *pflag.FlagSet) - Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error + Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string, resetAssetCacheChan chan bool) error HandleExit() ExecuteListAccounts(flagSet *pflag.FlagSet) ClaimCommission(flagSet *pflag.FlagSet) diff --git a/cmd/mocks/utils_cmd_interface.go b/cmd/mocks/utils_cmd_interface.go index cdf4be16..a36f0b95 100644 --- a/cmd/mocks/utils_cmd_interface.go +++ b/cmd/mocks/utils_cmd_interface.go @@ -1840,13 +1840,13 @@ func (_m *UtilsCmdInterface) UpdateJob(client *ethclient.Client, config types.Co return r0, r1 } -// Vote provides a mock function with given fields: ctx, config, client, rogueData, account, backupNodeActionsToIgnore -func (_m *UtilsCmdInterface) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error { - ret := _m.Called(ctx, config, client, rogueData, account, backupNodeActionsToIgnore) +// Vote provides a mock function with given fields: ctx, config, client, rogueData, account, backupNodeActionsToIgnore, resetAssetCacheChan +func (_m *UtilsCmdInterface) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string, resetAssetCacheChan chan bool) error { + ret := _m.Called(ctx, config, client, rogueData, account, backupNodeActionsToIgnore, resetAssetCacheChan) var r0 error - if rf, ok := ret.Get(0).(func(context.Context, types.Configurations, *ethclient.Client, types.Rogue, types.Account, []string) error); ok { - r0 = rf(ctx, config, client, rogueData, account, backupNodeActionsToIgnore) + if rf, ok := ret.Get(0).(func(context.Context, types.Configurations, *ethclient.Client, types.Rogue, types.Account, []string, chan bool) error); ok { + r0 = rf(ctx, config, client, rogueData, account, backupNodeActionsToIgnore, resetAssetCacheChan) } else { r0 = ret.Error(0) } diff --git a/cmd/vote.go b/cmd/vote.go index bca33e57..f5285891 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -93,8 +93,11 @@ func (*UtilsStruct) ExecuteVote(flagSet *pflag.FlagSet) { err = cmdUtils.InitAssetCache(client) utils.CheckError("Error in initializing asset cache: ", err) + resetAssetCacheChan := make(chan bool) + go utils.HandleResetCache(client, config.BufferPercent, resetAssetCacheChan) + 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 { + if err := cmdUtils.Vote(context.Background(), config, client, rogueData, account, backupNodeActionsToIgnore, resetAssetCacheChan); err != nil { log.Errorf("%v\n", err) osUtils.Exit(1) } @@ -125,7 +128,7 @@ func (*UtilsStruct) HandleExit() { } //This function handles all the states of voting -func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error { +func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string, resetAssetCacheChan chan bool) error { assetCacheTicker := time.NewTicker(time.Second * time.Duration(core.AssetCacheExpiry)) errChan := make(chan error, 1) @@ -134,11 +137,8 @@ func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, clien for { select { case <-assetCacheTicker.C: - log.Info("ASSET CACHE EXPIRED! INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...") - go func() { - err := razorUtils.ResetAssetCache(client, config.BufferPercent) - errChan <- err - }() + log.Info("ASSET CACHE EXPIRY TIME! Sending signal to reset asset cache") + resetAssetCacheChan <- true case err := <-errChan: // Handling the error from ResetAssetCache if err != nil { log.Errorf("Error resetting asset cache: %v", err) diff --git a/cmd/vote_test.go b/cmd/vote_test.go index 87e31ea7..61267b51 100644 --- a/cmd/vote_test.go +++ b/cmd/vote_test.go @@ -149,7 +149,7 @@ func TestExecuteVote(t *testing.T) { 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) + cmdUtilsMock.On("Vote", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tt.args.voteErr) osMock.On("Exit", mock.AnythingOfType("int")).Return() utils := &UtilsStruct{} diff --git a/utils/asset.go b/utils/asset.go index 8b2f0d23..1da8e195 100644 --- a/utils/asset.go +++ b/utils/asset.go @@ -531,7 +531,20 @@ func (*UtilsStruct) HandleOfficialJobsFromJSONFile(client *ethclient.Client, col return overrideJobs, overriddenJobIds } +func HandleResetCache(client *ethclient.Client, bufferPercent int32, resetAssetCacheChan chan bool) { + for { + select { + case <-resetAssetCacheChan: + // Call the ResetAssetCache function when a signal is received + 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 { + log.Info("ASSET CACHE EXPIRED! INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...") state, err := UtilsInterface.GetBufferedState(client, bufferPercent) if err != nil { log.Error("Error in getting buffered state: ", err) From 120c8006d6c48c9143916d7b2de6273c46826f4f Mon Sep 17 00:00:00 2001 From: YashK Date: Thu, 29 Feb 2024 14:37:02 +0530 Subject: [PATCH 09/10] refactor: removed unused errChan --- cmd/vote.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cmd/vote.go b/cmd/vote.go index f5285891..f4f3dd27 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -130,7 +130,6 @@ func (*UtilsStruct) HandleExit() { //This function handles all the states of voting func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string, resetAssetCacheChan chan bool) error { assetCacheTicker := time.NewTicker(time.Second * time.Duration(core.AssetCacheExpiry)) - errChan := make(chan error, 1) header, err := clientUtils.GetLatestBlockWithRetry(client) utils.CheckError("Error in getting block: ", err) @@ -139,11 +138,6 @@ func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, clien case <-assetCacheTicker.C: log.Info("ASSET CACHE EXPIRY TIME! Sending signal to reset asset cache") resetAssetCacheChan <- true - case err := <-errChan: // Handling the error from ResetAssetCache - if err != nil { - log.Errorf("Error resetting asset cache: %v", err) - continue - } case <-ctx.Done(): return nil default: From 436eb3f7b3a594d4a9445cc8d9810b8cb0e3df35 Mon Sep 17 00:00:00 2001 From: YashK Date: Thu, 29 Feb 2024 15:41:52 +0530 Subject: [PATCH 10/10] refactor: Moved ticker to HandleResetCache() --- cmd/interface.go | 2 +- cmd/mocks/utils_cmd_interface.go | 10 +++++----- cmd/vote.go | 12 +++--------- cmd/vote_test.go | 2 +- utils/asset.go | 16 ++++++++-------- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/cmd/interface.go b/cmd/interface.go index 2e0b74cf..f04d0290 100644 --- a/cmd/interface.go +++ b/cmd/interface.go @@ -234,7 +234,7 @@ type UtilsCmdInterface interface { CalculateSecret(account types.Account, epoch uint32, keystorePath string, chainId *big.Int) ([]byte, []byte, error) HandleBlock(client *ethclient.Client, account types.Account, blockNumber *big.Int, config types.Configurations, rogueData types.Rogue, backupNodeActionsToIgnore []string) ExecuteVote(flagSet *pflag.FlagSet) - Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string, resetAssetCacheChan chan bool) error + Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error HandleExit() ExecuteListAccounts(flagSet *pflag.FlagSet) ClaimCommission(flagSet *pflag.FlagSet) diff --git a/cmd/mocks/utils_cmd_interface.go b/cmd/mocks/utils_cmd_interface.go index a36f0b95..cdf4be16 100644 --- a/cmd/mocks/utils_cmd_interface.go +++ b/cmd/mocks/utils_cmd_interface.go @@ -1840,13 +1840,13 @@ func (_m *UtilsCmdInterface) UpdateJob(client *ethclient.Client, config types.Co return r0, r1 } -// Vote provides a mock function with given fields: ctx, config, client, rogueData, account, backupNodeActionsToIgnore, resetAssetCacheChan -func (_m *UtilsCmdInterface) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string, resetAssetCacheChan chan bool) error { - ret := _m.Called(ctx, config, client, rogueData, account, backupNodeActionsToIgnore, resetAssetCacheChan) +// Vote provides a mock function with given fields: ctx, config, client, rogueData, account, backupNodeActionsToIgnore +func (_m *UtilsCmdInterface) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error { + ret := _m.Called(ctx, config, client, rogueData, account, backupNodeActionsToIgnore) var r0 error - if rf, ok := ret.Get(0).(func(context.Context, types.Configurations, *ethclient.Client, types.Rogue, types.Account, []string, chan bool) error); ok { - r0 = rf(ctx, config, client, rogueData, account, backupNodeActionsToIgnore, resetAssetCacheChan) + if rf, ok := ret.Get(0).(func(context.Context, types.Configurations, *ethclient.Client, types.Rogue, types.Account, []string) error); ok { + r0 = rf(ctx, config, client, rogueData, account, backupNodeActionsToIgnore) } else { r0 = ret.Error(0) } diff --git a/cmd/vote.go b/cmd/vote.go index f4f3dd27..68239076 100644 --- a/cmd/vote.go +++ b/cmd/vote.go @@ -93,11 +93,10 @@ func (*UtilsStruct) ExecuteVote(flagSet *pflag.FlagSet) { err = cmdUtils.InitAssetCache(client) utils.CheckError("Error in initializing asset cache: ", err) - resetAssetCacheChan := make(chan bool) - go utils.HandleResetCache(client, config.BufferPercent, resetAssetCacheChan) + 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, resetAssetCacheChan); err != nil { + if err := cmdUtils.Vote(context.Background(), config, client, rogueData, account, backupNodeActionsToIgnore); err != nil { log.Errorf("%v\n", err) osUtils.Exit(1) } @@ -128,16 +127,11 @@ func (*UtilsStruct) HandleExit() { } //This function handles all the states of voting -func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string, resetAssetCacheChan chan bool) error { - assetCacheTicker := time.NewTicker(time.Second * time.Duration(core.AssetCacheExpiry)) - +func (*UtilsStruct) Vote(ctx context.Context, config types.Configurations, client *ethclient.Client, rogueData types.Rogue, account types.Account, backupNodeActionsToIgnore []string) error { header, err := clientUtils.GetLatestBlockWithRetry(client) utils.CheckError("Error in getting block: ", err) for { select { - case <-assetCacheTicker.C: - log.Info("ASSET CACHE EXPIRY TIME! Sending signal to reset asset cache") - resetAssetCacheChan <- true case <-ctx.Done(): return nil default: diff --git a/cmd/vote_test.go b/cmd/vote_test.go index 61267b51..87e31ea7 100644 --- a/cmd/vote_test.go +++ b/cmd/vote_test.go @@ -149,7 +149,7 @@ func TestExecuteVote(t *testing.T) { 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, mock.Anything).Return(tt.args.voteErr) + 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() utils := &UtilsStruct{} diff --git a/utils/asset.go b/utils/asset.go index 1da8e195..383ebe9c 100644 --- a/utils/asset.go +++ b/utils/asset.go @@ -531,20 +531,20 @@ func (*UtilsStruct) HandleOfficialJobsFromJSONFile(client *ethclient.Client, col return overrideJobs, overriddenJobIds } -func HandleResetCache(client *ethclient.Client, bufferPercent int32, resetAssetCacheChan chan bool) { +func HandleResetCache(client *ethclient.Client, bufferPercent int32) { + assetCacheTicker := time.NewTicker(time.Second * time.Duration(core.AssetCacheExpiry)) + defer assetCacheTicker.Stop() + for { - select { - case <-resetAssetCacheChan: - // Call the ResetAssetCache function when a signal is received - if err := UtilsInterface.ResetAssetCache(client, bufferPercent); err != nil { - log.Errorf("Error resetting asset cache: %v", err) - } + <-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 { - log.Info("ASSET CACHE EXPIRED! INITIALIZING JOBS AND COLLECTIONS CACHE AGAIN...") state, err := UtilsInterface.GetBufferedState(client, bufferPercent) if err != nil { log.Error("Error in getting buffered state: ", err)