From 71ae7ca1a0d7a72fee086ae99780bf03d0576fdf Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Fri, 26 Jan 2024 13:57:16 +0100 Subject: [PATCH] [#4604] Skip balance requests on checking nonce (#4629) --- .../wallet/transfer/commands_sequential.go | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/services/wallet/transfer/commands_sequential.go b/services/wallet/transfer/commands_sequential.go index ba8225846..7c717539a 100644 --- a/services/wallet/transfer/commands_sequential.go +++ b/services/wallet/transfer/commands_sequential.go @@ -142,6 +142,29 @@ func (c *findNewBlocksCommand) detectTransfers(parent context.Context, accounts return blockNum, addressesToCheck, nil } +func (c *findNewBlocksCommand) detectNonceChange(parent context.Context, from, to *big.Int, accounts []common.Address) ([]common.Address, error) { + addressesWithChange := []common.Address{} + for _, account := range accounts { + oldNonce, err := c.balanceCacher.NonceAt(parent, c.chainClient, account, from) + if err != nil { + log.Error("findNewBlocksCommand can't get nonce", "error", err, "account", account, "chain", c.chainClient.NetworkID()) + return nil, err + } + + newNonce, err := c.balanceCacher.NonceAt(parent, c.chainClient, account, to) + if err != nil { + log.Error("findNewBlocksCommand can't get nonce", "error", err, "account", account, "chain", c.chainClient.NetworkID()) + return nil, err + } + + if *newNonce != *oldNonce { + addressesWithChange = append(addressesWithChange, account) + } + } + + return addressesWithChange, nil +} + var nonceCheckIntervalIterations = 30 var logsCheckIntervalIterations = 5 @@ -191,9 +214,18 @@ func (c *findNewBlocksCommand) Run(parent context.Context) error { if len(accountsWithDetectedChanges) != 0 { c.findAndSaveEthBlocks(parent, c.fromBlockNumber, headNum, accountsToCheck) } else if c.iteration%nonceCheckIntervalIterations == 0 && len(accountsWithOutsideTransfers) > 0 { - c.findAndSaveEthBlocks(parent, c.fromBlockNumber, headNum, accountsWithOutsideTransfers) + accountsWithNonceChanges, err := c.detectNonceChange(parent, c.fromBlockNumber, headNum, accountsWithOutsideTransfers) + if err != nil { + c.error = err + return err + } + + if len(accountsWithNonceChanges) > 0 { + c.findAndSaveEthBlocks(parent, c.fromBlockNumber, headNum, accountsWithNonceChanges) + } + for _, account := range accountsToCheck { - if slices.Contains(accountsWithOutsideTransfers, account) { + if slices.Contains(accountsWithNonceChanges, account) { continue } err := c.markEthBlockRangeChecked(account, &BlockRange{nil, c.fromBlockNumber, headNum})