From 363ab0a2ab8dce98193c899c2c202cc885a45143 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 16 Feb 2021 13:30:29 +0200 Subject: [PATCH] [wallet] Avoid some unnecessary RPC requests - avoid making RPC request for `zero - zero` range - avoid checking of nonce for a lower block in the range if it is zero in a higher block - on `wallet_getTransfersByAddress` scanning of history is skipped if zero block is already reached - no need to fetch block num before fetching token balances --- services/wallet/api.go | 3 ++- services/wallet/balance.go | 10 +--------- services/wallet/commands.go | 6 +++++- services/wallet/concurrent.go | 10 ++++++++-- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/services/wallet/api.go b/services/wallet/api.go index e04695d9c..0393d80cd 100644 --- a/services/wallet/api.go +++ b/services/wallet/api.go @@ -50,7 +50,8 @@ func (api *API) GetTransfersByAddress(ctx context.Context, address common.Addres return nil, err } - if block == nil { + // if zero block was already checked there is nothing to find more + if block == nil || big.NewInt(0).Cmp(block) == 0 { return castToTransferViews(rst), nil } diff --git a/services/wallet/balance.go b/services/wallet/balance.go index e0ff708f0..a4db2ba96 100644 --- a/services/wallet/balance.go +++ b/services/wallet/balance.go @@ -24,13 +24,6 @@ func GetTokensBalances(parent context.Context, client *ethclient.Client, account mu sync.Mutex response = map[common.Address]map[common.Address]*hexutil.Big{} ) - // requested current head to request balance on the same block number - ctx, cancel := context.WithTimeout(parent, requestTimeout) - header, err := client.HeaderByNumber(ctx, nil) - cancel() - if err != nil { - return nil, err - } for _, token := range tokens { caller, err := ierc20.NewIERC20Caller(token, client) token := token @@ -43,8 +36,7 @@ func GetTokensBalances(parent context.Context, client *ethclient.Client, account group.Add(func(parent context.Context) error { ctx, cancel := context.WithTimeout(parent, requestTimeout) balance, err := caller.BalanceOf(&bind.CallOpts{ - BlockNumber: header.Number, - Context: ctx, + Context: ctx, }, account) cancel() // We don't want to return an error here and prevent diff --git a/services/wallet/commands.go b/services/wallet/commands.go index 86715f98b..76a567a44 100644 --- a/services/wallet/commands.go +++ b/services/wallet/commands.go @@ -41,7 +41,7 @@ func (c *ethHistoricalCommand) Command() Command { func (c *ethHistoricalCommand) Run(ctx context.Context) (err error) { start := time.Now() totalRequests, cacheHits := c.balanceCache.getStats(c.address) - log.Info("balance cache before checking range", "total", totalRequests, "cached", totalRequests-cacheHits) + log.Info("balance cache before checking range", "total", totalRequests, "cached", totalRequests-cacheHits, "from", c.from, "to", c.to) from, headers, err := findBlocksWithEthTransfers(ctx, c.client, c.balanceCache, c.eth, c.address, c.from, c.to, c.noLimit) if err != nil { @@ -648,6 +648,10 @@ func findFirstRange(c context.Context, account common.Address, initialTo *big.In to := initialTo goal := uint64(20) + if from.Cmp(to) == 0 { + return to, nil + } + firstNonce, err := client.NonceAt(c, account, to) log.Info("find range with 20 <= len(tx) <= 25", "account", account, "firstNonce", firstNonce, "from", from, "to", to) diff --git a/services/wallet/concurrent.go b/services/wallet/concurrent.go index a400fa25c..9a27a2f1e 100644 --- a/services/wallet/concurrent.go +++ b/services/wallet/concurrent.go @@ -111,11 +111,17 @@ func checkRanges(parent context.Context, client reactorClient, cache BalanceCach if lb.Cmp(hb) == 0 { log.Debug("balances are equal", "from", from, "to", to) - ln, err := client.NonceAt(ctx, account, from) + hn, err := client.NonceAt(ctx, account, to) if err != nil { return err } - hn, err := client.NonceAt(ctx, account, to) + // if nonce is zero in a newer block then there is no need to check an older one + if hn == 0 { + log.Debug("zero nonce", "to", to) + return nil + } + + ln, err := client.NonceAt(ctx, account, from) if err != nil { return err }