From 436eb3f7b3a594d4a9445cc8d9810b8cb0e3df35 Mon Sep 17 00:00:00 2001 From: YashK Date: Thu, 29 Feb 2024 15:41:52 +0530 Subject: [PATCH] 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)