2021-09-09 14:28:54 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-05-08 06:02:00 +00:00
|
|
|
"fmt"
|
2021-09-09 14:28:54 +00:00
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2023-05-08 06:02:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-02-20 09:32:45 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain"
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/async"
|
2023-06-02 20:08:45 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/token"
|
2023-05-08 06:02:00 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/walletevent"
|
2023-06-21 14:09:55 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2023-05-08 06:02:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ReactorNotStarted string = "reactor not started"
|
|
|
|
|
|
|
|
NonArchivalNodeBlockChunkSize = 100
|
2023-05-19 11:46:54 +00:00
|
|
|
DefaultNodeBlockChunkSize = 100000
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
|
2021-11-24 12:59:45 +00:00
|
|
|
var errAlreadyRunning = errors.New("already running")
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
type FetchStrategyType int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
OnDemandFetchStrategyType FetchStrategyType = iota
|
|
|
|
SequentialFetchStrategyType
|
|
|
|
)
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
// HeaderReader interface for reading headers using block number or hash.
|
|
|
|
type HeaderReader interface {
|
|
|
|
HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error)
|
|
|
|
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BalanceReader interface for reading balance at a specifeid address.
|
|
|
|
type BalanceReader interface {
|
|
|
|
BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error)
|
|
|
|
NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)
|
|
|
|
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
|
2023-06-01 17:16:50 +00:00
|
|
|
FullTransactionByBlockNumberAndIndex(ctx context.Context, blockNumber *big.Int, index uint) (*chain.FullTransaction, error)
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
type HistoryFetcher interface {
|
|
|
|
start() error
|
|
|
|
stop()
|
|
|
|
kind() FetchStrategyType
|
|
|
|
|
|
|
|
getTransfersByAddress(ctx context.Context, chainID uint64, address common.Address, toBlock *big.Int,
|
|
|
|
limit int64, fetchMore bool) ([]Transfer, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type OnDemandFetchStrategy struct {
|
2023-02-15 12:28:19 +00:00
|
|
|
db *Database
|
2023-05-08 06:02:00 +00:00
|
|
|
blockDAO *BlockDAO
|
2023-02-15 12:28:19 +00:00
|
|
|
feed *event.Feed
|
|
|
|
mu sync.Mutex
|
|
|
|
group *async.Group
|
2023-05-08 06:02:00 +00:00
|
|
|
balanceCache *balanceCache
|
2023-02-15 12:28:19 +00:00
|
|
|
transactionManager *TransactionManager
|
2023-06-21 14:09:55 +00:00
|
|
|
pendingTxManager *transactions.TransactionManager
|
2023-06-02 20:08:45 +00:00
|
|
|
tokenManager *token.Manager
|
2023-05-08 06:02:00 +00:00
|
|
|
chainClients map[uint64]*chain.ClientWithFallback
|
|
|
|
accounts []common.Address
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (s *OnDemandFetchStrategy) newControlCommand(chainClient *chain.ClientWithFallback, accounts []common.Address) *controlCommand {
|
2023-06-13 14:20:48 +00:00
|
|
|
signer := types.LatestSignerForChainID(chainClient.ToBigInt())
|
2021-09-09 14:28:54 +00:00
|
|
|
ctl := &controlCommand{
|
2023-05-08 06:02:00 +00:00
|
|
|
db: s.db,
|
2021-09-09 14:28:54 +00:00
|
|
|
chainClient: chainClient,
|
|
|
|
accounts: accounts,
|
2023-05-08 06:02:00 +00:00
|
|
|
blockDAO: s.blockDAO,
|
2021-09-09 14:28:54 +00:00
|
|
|
eth: ÐDownloader{
|
|
|
|
chainClient: chainClient,
|
|
|
|
accounts: accounts,
|
|
|
|
signer: signer,
|
2023-05-08 06:02:00 +00:00
|
|
|
db: s.db,
|
2021-09-09 14:28:54 +00:00
|
|
|
},
|
2023-02-15 12:28:19 +00:00
|
|
|
erc20: NewERC20TransfersDownloader(chainClient, accounts, signer),
|
2023-05-08 06:02:00 +00:00
|
|
|
feed: s.feed,
|
2023-02-15 12:28:19 +00:00
|
|
|
errorsCount: 0,
|
2023-05-08 06:02:00 +00:00
|
|
|
transactionManager: s.transactionManager,
|
2023-06-21 14:09:55 +00:00
|
|
|
pendingTxManager: s.pendingTxManager,
|
2023-06-02 20:08:45 +00:00
|
|
|
tokenManager: s.tokenManager,
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctl
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (s *OnDemandFetchStrategy) start() error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
if s.group != nil {
|
2021-09-09 14:28:54 +00:00
|
|
|
return errAlreadyRunning
|
|
|
|
}
|
2023-05-08 06:02:00 +00:00
|
|
|
s.group = async.NewGroup(context.Background())
|
|
|
|
|
|
|
|
for _, chainClient := range s.chainClients {
|
|
|
|
ctl := s.newControlCommand(chainClient, s.accounts)
|
|
|
|
s.group.Add(ctl.Command())
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops reactor loop and waits till it exits.
|
2023-05-08 06:02:00 +00:00
|
|
|
func (s *OnDemandFetchStrategy) stop() {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.group == nil {
|
2021-09-09 14:28:54 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-08 06:02:00 +00:00
|
|
|
s.group.Stop()
|
|
|
|
s.group.Wait()
|
|
|
|
s.group = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OnDemandFetchStrategy) kind() FetchStrategyType {
|
|
|
|
return OnDemandFetchStrategyType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OnDemandFetchStrategy) getTransfersByAddress(ctx context.Context, chainID uint64, address common.Address, toBlock *big.Int,
|
|
|
|
limit int64, fetchMore bool) ([]Transfer, error) {
|
|
|
|
|
|
|
|
log.Info("[WalletAPI:: GetTransfersByAddress] get transfers for an address", "address", address, "fetchMore", fetchMore,
|
|
|
|
"chainID", chainID, "toBlock", toBlock, "limit", limit)
|
|
|
|
|
|
|
|
rst, err := s.db.GetTransfersByAddress(chainID, address, toBlock, limit)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("[WalletAPI:: GetTransfersByAddress] can't fetch transfers", "err", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
transfersCount := int64(len(rst))
|
|
|
|
|
|
|
|
if fetchMore && limit > transfersCount {
|
|
|
|
|
|
|
|
block, err := s.blockDAO.GetFirstKnownBlock(chainID, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if zero block was already checked there is nothing to find more
|
|
|
|
if block == nil || big.NewInt(0).Cmp(block) == 0 {
|
|
|
|
log.Info("[WalletAPI:: GetTransfersByAddress] ZERO block is found for", "address", address, "chaindID", chainID)
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
2023-05-19 08:19:48 +00:00
|
|
|
chainClient, err := getChainClientByID(s.chainClients, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
from, err := findFirstRange(ctx, address, block, chainClient)
|
|
|
|
if err != nil {
|
|
|
|
if nonArchivalNodeError(err) {
|
|
|
|
if s.feed != nil {
|
|
|
|
s.feed.Send(walletevent.Event{
|
|
|
|
Type: EventNonArchivalNodeDetected,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if block.Cmp(big.NewInt(NonArchivalNodeBlockChunkSize)) >= 0 {
|
|
|
|
from = big.NewInt(0).Sub(block, big.NewInt(NonArchivalNodeBlockChunkSize))
|
|
|
|
} else {
|
|
|
|
from = big.NewInt(0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Error("first range error", "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fromByAddress := map[common.Address]*Block{address: {
|
|
|
|
Number: from,
|
|
|
|
}}
|
|
|
|
toByAddress := map[common.Address]*big.Int{address: block}
|
|
|
|
|
|
|
|
if s.balanceCache == nil {
|
|
|
|
s.balanceCache = newBalanceCache()
|
|
|
|
}
|
|
|
|
blocksCommand := &findAndCheckBlockRangeCommand{
|
|
|
|
accounts: []common.Address{address},
|
|
|
|
db: s.db,
|
|
|
|
chainClient: chainClient,
|
|
|
|
balanceCache: s.balanceCache,
|
|
|
|
feed: s.feed,
|
|
|
|
fromByAddress: fromByAddress,
|
|
|
|
toByAddress: toByAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = blocksCommand.Command()(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-06-05 12:33:51 +00:00
|
|
|
s.balanceCache.Clear()
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2023-06-02 20:08:45 +00:00
|
|
|
blocks, err := s.blockDAO.GetBlocksToLoadByAddress(chainID, address, numberOfBlocksCheckedPerIteration)
|
2023-05-08 06:02:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("checking blocks again", "blocks", len(blocks))
|
|
|
|
if len(blocks) > 0 {
|
|
|
|
txCommand := &loadTransfersCommand{
|
|
|
|
accounts: []common.Address{address},
|
|
|
|
db: s.db,
|
|
|
|
blockDAO: s.blockDAO,
|
|
|
|
chainClient: chainClient,
|
|
|
|
transactionManager: s.transactionManager,
|
2023-05-19 08:19:48 +00:00
|
|
|
blocksLimit: numberOfBlocksCheckedPerIteration,
|
2023-06-02 20:08:45 +00:00
|
|
|
tokenManager: s.tokenManager,
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = txCommand.Command()(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rst, err = s.db.GetTransfersByAddress(chainID, address, toBlock, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rst, nil
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
// Reactor listens to new blocks and stores transfers into the database.
|
|
|
|
type Reactor struct {
|
|
|
|
db *Database
|
|
|
|
blockDAO *BlockDAO
|
|
|
|
feed *event.Feed
|
|
|
|
transactionManager *TransactionManager
|
2023-06-21 14:09:55 +00:00
|
|
|
pendingTxManager *transactions.TransactionManager
|
2023-06-02 20:08:45 +00:00
|
|
|
tokenManager *token.Manager
|
2023-05-08 06:02:00 +00:00
|
|
|
strategy HistoryFetcher
|
|
|
|
}
|
|
|
|
|
2023-06-21 14:09:55 +00:00
|
|
|
func NewReactor(db *Database, blockDAO *BlockDAO, feed *event.Feed, tm *TransactionManager,
|
|
|
|
pendingTxManager *transactions.TransactionManager, tokenManager *token.Manager) *Reactor {
|
2023-05-08 06:02:00 +00:00
|
|
|
return &Reactor{
|
|
|
|
db: db,
|
|
|
|
blockDAO: blockDAO,
|
|
|
|
feed: feed,
|
|
|
|
transactionManager: tm,
|
2023-06-21 14:09:55 +00:00
|
|
|
pendingTxManager: pendingTxManager,
|
2023-06-02 20:08:45 +00:00
|
|
|
tokenManager: tokenManager,
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start runs reactor loop in background.
|
|
|
|
func (r *Reactor) start(chainClients map[uint64]*chain.ClientWithFallback, accounts []common.Address,
|
2023-06-05 15:05:50 +00:00
|
|
|
loadAllTransfers bool) error {
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2023-06-05 15:05:50 +00:00
|
|
|
r.strategy = r.createFetchStrategy(chainClients, accounts, loadAllTransfers)
|
2023-05-08 06:02:00 +00:00
|
|
|
return r.strategy.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops reactor loop and waits till it exits.
|
|
|
|
func (r *Reactor) stop() {
|
|
|
|
if r.strategy != nil {
|
|
|
|
r.strategy.stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reactor) restart(chainClients map[uint64]*chain.ClientWithFallback, accounts []common.Address,
|
2023-06-05 15:05:50 +00:00
|
|
|
loadAllTransfers bool) error {
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
r.stop()
|
2023-06-05 15:05:50 +00:00
|
|
|
return r.start(chainClients, accounts, loadAllTransfers)
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reactor) createFetchStrategy(chainClients map[uint64]*chain.ClientWithFallback,
|
2023-06-05 15:05:50 +00:00
|
|
|
accounts []common.Address, loadAllTransfers bool) HistoryFetcher {
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2023-06-05 15:05:50 +00:00
|
|
|
if loadAllTransfers {
|
2023-05-19 08:19:48 +00:00
|
|
|
return NewSequentialFetchStrategy(
|
|
|
|
r.db,
|
|
|
|
r.blockDAO,
|
|
|
|
r.feed,
|
|
|
|
r.transactionManager,
|
2023-06-21 14:09:55 +00:00
|
|
|
r.pendingTxManager,
|
2023-06-02 20:08:45 +00:00
|
|
|
r.tokenManager,
|
2023-05-19 08:19:48 +00:00
|
|
|
chainClients,
|
|
|
|
accounts,
|
|
|
|
)
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &OnDemandFetchStrategy{
|
|
|
|
db: r.db,
|
|
|
|
feed: r.feed,
|
|
|
|
blockDAO: r.blockDAO,
|
|
|
|
transactionManager: r.transactionManager,
|
2023-06-21 14:09:55 +00:00
|
|
|
pendingTxManager: r.pendingTxManager,
|
2023-06-02 20:08:45 +00:00
|
|
|
tokenManager: r.tokenManager,
|
2023-05-08 06:02:00 +00:00
|
|
|
chainClients: chainClients,
|
|
|
|
accounts: accounts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reactor) getTransfersByAddress(ctx context.Context, chainID uint64, address common.Address, toBlock *big.Int,
|
|
|
|
limit int64, fetchMore bool) ([]Transfer, error) {
|
|
|
|
|
|
|
|
if r.strategy != nil {
|
|
|
|
return r.strategy.getTransfersByAddress(ctx, chainID, address, toBlock, limit, fetchMore)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New(ReactorNotStarted)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getChainClientByID(clients map[uint64]*chain.ClientWithFallback, id uint64) (*chain.ClientWithFallback, error) {
|
|
|
|
for _, client := range clients {
|
|
|
|
if client.ChainID == id {
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("chain client not found with id=%d", id)
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|