From cec11e93135015f86221d49042a3b3988c882ffb Mon Sep 17 00:00:00 2001 From: Ivan Belyakov Date: Thu, 23 May 2024 11:16:42 +0400 Subject: [PATCH] feat(wallet)_: Fixed account limit period for history to infinite instead of per day Fix rpc stats doubling. Add test for infinite limit --- rpc/chain/rpc_limiter.go | 8 +++++- rpc/chain/rpc_limiter_test.go | 26 +++++++++++++++++++ services/rpcstats/stats.go | 3 +-- .../wallet/transfer/commands_sequential.go | 8 +++++- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/rpc/chain/rpc_limiter.go b/rpc/chain/rpc_limiter.go index 6c6b8738d..e2ed41f0f 100644 --- a/rpc/chain/rpc_limiter.go +++ b/rpc/chain/rpc_limiter.go @@ -16,7 +16,8 @@ const ( minRequestsPerSecond = 20 requestsPerSecondStep = 10 - tickerInterval = 1 * time.Second + tickerInterval = 1 * time.Second + LimitInfinitely = 0 ) var ( @@ -165,6 +166,11 @@ func (rl *RPCRequestLimiter) Allow(tag string) (bool, error) { return true, nil } + // Check if period is forever + if data.Period.Milliseconds() == LimitInfinitely { + return false, nil + } + // Check if a number of requests is over the limit within the interval if time.Since(data.CreatedAt) < data.Period { if data.NumReqs >= data.MaxReqs { diff --git a/rpc/chain/rpc_limiter_test.go b/rpc/chain/rpc_limiter_test.go index 675a0a01b..7d1f95f7d 100644 --- a/rpc/chain/rpc_limiter_test.go +++ b/rpc/chain/rpc_limiter_test.go @@ -114,3 +114,29 @@ func TestAllowWhenPeriodPassed(t *testing.T) { // Verify the result require.True(t, allow) } + +func TestAllowRestrictInfinitelyWhenLimitReached(t *testing.T) { + storage, rl := setupTest() + + // Define test inputs + tag := "testTag" + maxRequests := 10 + + // Set up the storage with test data + data := &LimitData{ + Tag: tag, + Period: LimitInfinitely, + CreatedAt: time.Now(), + MaxReqs: maxRequests, + NumReqs: maxRequests, + } + err := storage.Set(data) + require.NoError(t, err) + + // Call the Allow method + allow, err := rl.Allow(tag) + require.NoError(t, err) + + // Verify the result + require.False(t, allow) +} diff --git a/services/rpcstats/stats.go b/services/rpcstats/stats.go index 2496c1738..50f9b99d0 100644 --- a/services/rpcstats/stats.go +++ b/services/rpcstats/stats.go @@ -51,6 +51,5 @@ func CountCallWithTag(method string, tag string) { methodMap := value.(*sync.Map) value, _ = methodMap.LoadOrStore(method, uint(0)) methodMap.Store(method, value.(uint)+1) - - CountCall(method) + stats.total++ } diff --git a/services/wallet/transfer/commands_sequential.go b/services/wallet/transfer/commands_sequential.go index 2eb20fb59..ce9787003 100644 --- a/services/wallet/transfer/commands_sequential.go +++ b/services/wallet/transfer/commands_sequential.go @@ -1127,7 +1127,13 @@ func (c *loadBlocksAndTransfersCommand) fetchHistoryBlocksForAccount(group *asyn chainClient := chain.ClientWithTag(c.chainClient, accountTag, transferHistoryTag) storage := chain.NewLimitsDBStorage(c.db.client) limiter := chain.NewRequestLimiter(storage) - err := limiter.SetLimit(accountTag, transferHistoryLimitPerAccount, transferHistoryLimitPeriod) + + // Check if limit is already reached, then skip the comamnd + if allow, _ := limiter.Allow(accountTag); !allow { + continue + } + + err := limiter.SetLimit(accountTag, transferHistoryLimitPerAccount, chain.LimitInfinitely) if err != nil { log.Error("fetchHistoryBlocksForAccount SetLimit", "error", err, "accountTag", accountTag) }