[#4605] Skip nonce check when possible during tx detection

This commit is contained in:
Roman Volosovskyi 2024-01-24 11:31:14 +01:00
parent 9050ed7aaf
commit 647c3b0fd8
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
2 changed files with 46 additions and 11 deletions

View File

@ -6,6 +6,8 @@ import (
"sync"
"time"
"golang.org/x/exp/slices"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
@ -148,36 +150,56 @@ func (c *findNewBlocksCommand) Run(parent context.Context) error {
return err
}
accounts := []common.Address{}
accountsToCheck := []common.Address{}
// accounts which might have outgoing transfers initiated outside
// the application, e.g. watch only or restored from mnemonic phrase
accountsWithOutsideTransfers := []common.Address{}
for _, account := range c.accounts {
acc, err := c.accountsDB.GetAccountByAddress(nodetypes.Address(account))
if err != nil {
c.error = err
return err
}
if mnemonicWasNotShown {
acc, err := c.accountsDB.GetAccountByAddress(nodetypes.Address(account))
if err != nil {
c.error = err
return err
}
if acc.AddressWasNotShown {
log.Info("skip findNewBlocksCommand, mnemonic has not been shown and the address has not been shared yet", "address", account)
continue
}
}
accounts = append(accounts, account)
if !mnemonicWasNotShown || acc.Type != accounts.AccountTypeGenerated {
accountsWithOutsideTransfers = append(accountsWithOutsideTransfers, account)
}
accountsToCheck = append(accountsToCheck, account)
}
if len(accounts) == 0 {
if len(accountsToCheck) == 0 {
return nil
}
headNum, accountsWithDetectedChanges, err := c.detectTransfers(parent, accounts)
headNum, accountsWithDetectedChanges, err := c.detectTransfers(parent, accountsToCheck)
if err != nil {
log.Error("findNewBlocksCommand error on transfer detection", "error", err, "chain", c.chainClient.NetworkID())
return err
}
if len(accountsWithDetectedChanges) != 0 || c.iteration%nonceCheckIntervalIterations == 0 {
c.findAndSaveEthBlocks(parent, c.fromBlockNumber, headNum, accounts)
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)
for _, account := range accountsToCheck {
if slices.Contains(accountsWithOutsideTransfers, account) {
continue
}
err := c.markEthBlockRangeChecked(account, &BlockRange{nil, c.fromBlockNumber, headNum})
if err != nil {
c.error = err
return err
}
}
}
if len(accountsWithDetectedChanges) != 0 || c.iteration%logsCheckIntervalIterations == 0 {
c.findAndSaveTokenBlocks(parent, c.fromBlockNumber, headNum)
}

View File

@ -38,6 +38,7 @@ import (
"github.com/status-im/status-go/t/utils"
"github.com/status-im/status-go/multiaccounts/accounts"
multicommon "github.com/status-im/status-go/multiaccounts/common"
"github.com/status-im/status-go/params"
statusRpc "github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/rpc/network"
@ -1368,6 +1369,18 @@ func TestFetchNewBlocksCommand(t *testing.T) {
accDB, err := accounts.NewDB(appdb)
require.NoError(t, err)
for _, address := range []*common.Address{&address1, &address2} {
acc := &accounts.Account{
Address: ethtypes.BytesToAddress(address.Bytes()),
Type: accounts.AccountTypeWatch,
Name: address.String(),
ColorID: multicommon.CustomizationColorPrimary,
Emoji: "emoji",
}
err = accDB.SaveOrUpdateAccounts([]*accounts.Account{acc}, false)
require.NoError(t, err)
}
tc := &TestClient{
t: t,
balances: map[common.Address][][]int{},