2021-09-09 14:28:54 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
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"
|
|
|
|
)
|
|
|
|
|
2021-11-24 12:59:45 +00:00
|
|
|
var errAlreadyRunning = errors.New("already running")
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reactor listens to new blocks and stores transfers into the database.
|
|
|
|
type Reactor struct {
|
2023-02-15 12:28:19 +00:00
|
|
|
db *Database
|
|
|
|
block *Block
|
|
|
|
feed *event.Feed
|
|
|
|
mu sync.Mutex
|
|
|
|
group *async.Group
|
|
|
|
transactionManager *TransactionManager
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
func (r *Reactor) newControlCommand(chainClient *chain.ClientWithFallback, accounts []common.Address) *controlCommand {
|
2021-09-09 14:28:54 +00:00
|
|
|
signer := types.NewLondonSigner(chainClient.ToBigInt())
|
|
|
|
ctl := &controlCommand{
|
|
|
|
db: r.db,
|
|
|
|
chainClient: chainClient,
|
|
|
|
accounts: accounts,
|
|
|
|
block: r.block,
|
|
|
|
eth: ÐDownloader{
|
|
|
|
chainClient: chainClient,
|
|
|
|
accounts: accounts,
|
|
|
|
signer: signer,
|
|
|
|
db: r.db,
|
|
|
|
},
|
2023-02-15 12:28:19 +00:00
|
|
|
erc20: NewERC20TransfersDownloader(chainClient, accounts, signer),
|
|
|
|
feed: r.feed,
|
|
|
|
errorsCount: 0,
|
|
|
|
transactionManager: r.transactionManager,
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start runs reactor loop in background.
|
2023-03-24 08:38:27 +00:00
|
|
|
func (r *Reactor) start(chainClients map[uint64]*chain.ClientWithFallback, accounts []common.Address) error {
|
2021-09-09 14:28:54 +00:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
|
|
|
|
if r.group != nil {
|
|
|
|
return errAlreadyRunning
|
|
|
|
}
|
|
|
|
r.group = async.NewGroup(context.Background())
|
|
|
|
for _, chainClient := range chainClients {
|
|
|
|
ctl := r.newControlCommand(chainClient, accounts)
|
|
|
|
r.group.Add(ctl.Command())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops reactor loop and waits till it exits.
|
|
|
|
func (r *Reactor) stop() {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
if r.group == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.group.Stop()
|
|
|
|
r.group.Wait()
|
|
|
|
r.group = nil
|
|
|
|
}
|
|
|
|
|
2023-03-24 08:38:27 +00:00
|
|
|
func (r *Reactor) restart(chainClients map[uint64]*chain.ClientWithFallback, accounts []common.Address) error {
|
2021-09-09 14:28:54 +00:00
|
|
|
r.stop()
|
|
|
|
return r.start(chainClients, accounts)
|
|
|
|
}
|