2021-09-09 14:28:54 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2023-08-01 18:50:30 +00:00
|
|
|
"fmt"
|
2021-09-09 14:28:54 +00:00
|
|
|
"math/big"
|
2023-05-08 06:02:00 +00:00
|
|
|
"time"
|
2021-09-09 14:28:54 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-11-24 12:59:45 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2021-09-22 17:49:20 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2023-02-20 09:32:45 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain"
|
2023-05-31 19:45:11 +00:00
|
|
|
"github.com/status-im/status-go/services/accounts/accountsevent"
|
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-06-21 14:09:55 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Controller struct {
|
2023-02-15 12:28:19 +00:00
|
|
|
db *Database
|
|
|
|
rpcClient *rpc.Client
|
2023-05-08 06:02:00 +00:00
|
|
|
blockDAO *BlockDAO
|
2023-02-15 12:28:19 +00:00
|
|
|
reactor *Reactor
|
|
|
|
accountFeed *event.Feed
|
|
|
|
TransferFeed *event.Feed
|
|
|
|
group *async.Group
|
|
|
|
transactionManager *TransactionManager
|
2023-08-01 18:50:30 +00:00
|
|
|
pendingTxManager *transactions.PendingTxTracker
|
2023-06-02 20:08:45 +00:00
|
|
|
tokenManager *token.Manager
|
2023-06-05 15:05:50 +00:00
|
|
|
loadAllTransfers bool
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func NewTransferController(db *sql.DB, rpcClient *rpc.Client, accountFeed *event.Feed, transferFeed *event.Feed,
|
2023-08-01 18:50:30 +00:00
|
|
|
transactionManager *TransactionManager, pendingTxManager *transactions.PendingTxTracker, tokenManager *token.Manager, loadAllTransfers bool) *Controller {
|
2023-05-08 06:02:00 +00:00
|
|
|
|
|
|
|
blockDAO := &BlockDAO{db}
|
2021-09-09 14:28:54 +00:00
|
|
|
return &Controller{
|
2023-02-15 12:28:19 +00:00
|
|
|
db: NewDB(db),
|
2023-05-08 06:02:00 +00:00
|
|
|
blockDAO: blockDAO,
|
2023-02-15 12:28:19 +00:00
|
|
|
rpcClient: rpcClient,
|
|
|
|
accountFeed: accountFeed,
|
|
|
|
TransferFeed: transferFeed,
|
|
|
|
transactionManager: transactionManager,
|
2023-06-21 14:09:55 +00:00
|
|
|
pendingTxManager: pendingTxManager,
|
2023-06-02 20:08:45 +00:00
|
|
|
tokenManager: tokenManager,
|
2023-06-05 15:05:50 +00:00
|
|
|
loadAllTransfers: loadAllTransfers,
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 09:19:32 +00:00
|
|
|
func (c *Controller) Start() {
|
2021-09-09 14:28:54 +00:00
|
|
|
c.group = async.NewGroup(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) Stop() {
|
|
|
|
if c.reactor != nil {
|
|
|
|
c.reactor.stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.group != nil {
|
|
|
|
c.group.Stop()
|
|
|
|
c.group.Wait()
|
|
|
|
c.group = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) SetInitialBlocksRange(chainIDs []uint64) error {
|
2023-02-20 09:32:45 +00:00
|
|
|
chainClients, err := c.rpcClient.EthClients(chainIDs)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
for chainID, chainClient := range chainClients {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
toHeader, err := chainClient.HeaderByNumber(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
from := big.NewInt(0)
|
|
|
|
|
|
|
|
err = c.blockDAO.setInitialBlocksRange(chainID, from, toHeader.Number)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) CheckRecentHistory(chainIDs []uint64, accounts []common.Address) error {
|
|
|
|
if len(accounts) == 0 {
|
|
|
|
log.Info("no accounts provided")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(chainIDs) == 0 {
|
|
|
|
log.Info("no chain provided")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
err := c.blockDAO.mergeBlocksRanges(chainIDs, accounts)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-20 09:32:45 +00:00
|
|
|
chainClients, err := c.rpcClient.EthClients(chainIDs)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.reactor != nil {
|
2023-06-05 15:05:50 +00:00
|
|
|
err := c.reactor.restart(chainClients, accounts, c.loadAllTransfers)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-08 06:02:00 +00:00
|
|
|
} else {
|
2023-06-21 14:09:55 +00:00
|
|
|
c.reactor = NewReactor(c.db, c.blockDAO, c.TransferFeed, c.transactionManager,
|
|
|
|
c.pendingTxManager, c.tokenManager)
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2023-06-05 15:05:50 +00:00
|
|
|
err = c.reactor.start(chainClients, accounts, c.loadAllTransfers)
|
2023-05-08 06:02:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
c.group.Add(func(ctx context.Context) error {
|
2023-06-05 15:05:50 +00:00
|
|
|
return watchAccountsChanges(ctx, c.accountFeed, c.reactor, chainClients, accounts, c.loadAllTransfers)
|
2023-05-08 06:02:00 +00:00
|
|
|
})
|
|
|
|
}
|
2021-09-09 14:28:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-15 12:28:19 +00:00
|
|
|
// watchAccountsChanges subscribes to a feed and watches for changes in accounts list. If there are new or removed accounts
|
2021-09-09 14:28:54 +00:00
|
|
|
// reactor will be restarted.
|
2023-05-08 06:02:00 +00:00
|
|
|
func watchAccountsChanges(ctx context.Context, accountFeed *event.Feed, reactor *Reactor,
|
2023-06-05 15:05:50 +00:00
|
|
|
chainClients map[uint64]*chain.ClientWithFallback, initial []common.Address, loadAllTransfers bool) error {
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2023-05-31 19:45:11 +00:00
|
|
|
ch := make(chan accountsevent.Event, 1) // it may block if the rate of updates will be significantly higher
|
|
|
|
sub := accountFeed.Subscribe(ch)
|
2021-09-09 14:28:54 +00:00
|
|
|
defer sub.Unsubscribe()
|
|
|
|
listen := make(map[common.Address]struct{}, len(initial))
|
|
|
|
for _, address := range initial {
|
|
|
|
listen[address] = struct{}{}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case err := <-sub.Err():
|
|
|
|
if err != nil {
|
|
|
|
log.Error("accounts watcher subscription failed", "error", err)
|
|
|
|
}
|
2023-05-31 19:45:11 +00:00
|
|
|
case ev := <-ch:
|
2021-09-09 14:28:54 +00:00
|
|
|
restart := false
|
2023-05-31 19:45:11 +00:00
|
|
|
|
|
|
|
for _, address := range ev.Accounts {
|
|
|
|
_, exist := listen[address]
|
|
|
|
if ev.Type == accountsevent.EventTypeAdded && !exist {
|
|
|
|
listen[address] = struct{}{}
|
|
|
|
restart = true
|
|
|
|
} else if ev.Type == accountsevent.EventTypeRemoved && exist {
|
|
|
|
delete(listen, address)
|
2021-09-09 14:28:54 +00:00
|
|
|
restart = true
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 19:45:11 +00:00
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
if !restart {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-31 19:45:11 +00:00
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
listenList := mapToList(listen)
|
|
|
|
log.Debug("list of accounts was changed from a previous version. reactor will be restarted", "new", listenList)
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2023-06-05 15:05:50 +00:00
|
|
|
err := reactor.restart(chainClients, listenList, loadAllTransfers)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to restart reactor with new accounts", "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapToList(m map[common.Address]struct{}) []common.Address {
|
|
|
|
rst := make([]common.Address, 0, len(m))
|
|
|
|
for address := range m {
|
|
|
|
rst = append(rst, address)
|
|
|
|
}
|
|
|
|
return rst
|
|
|
|
}
|
|
|
|
|
2023-06-02 20:08:45 +00:00
|
|
|
// Only used by status-mobile
|
2021-11-24 12:59:45 +00:00
|
|
|
func (c *Controller) LoadTransferByHash(ctx context.Context, rpcClient *rpc.Client, address common.Address, hash common.Hash) error {
|
2023-02-20 09:32:45 +00:00
|
|
|
chainClient, err := rpcClient.EthClient(rpcClient.UpstreamChainID)
|
2021-11-24 12:59:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-13 14:20:48 +00:00
|
|
|
signer := types.LatestSignerForChainID(chainClient.ToBigInt())
|
2021-11-24 12:59:45 +00:00
|
|
|
|
|
|
|
transfer, err := getTransferByHash(ctx, chainClient, signer, address, hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
transfers := []Transfer{*transfer}
|
|
|
|
|
|
|
|
err = c.db.InsertBlock(rpcClient.UpstreamChainID, address, transfer.BlockNumber, transfer.BlockHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:50:30 +00:00
|
|
|
tx, err := c.db.client.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:59:45 +00:00
|
|
|
blocks := []*big.Int{transfer.BlockNumber}
|
2023-08-01 18:50:30 +00:00
|
|
|
err = saveTransfersMarkBlocksLoaded(tx, rpcClient.UpstreamChainID, address, transfers, blocks)
|
2021-11-24 12:59:45 +00:00
|
|
|
if err != nil {
|
2023-08-01 18:50:30 +00:00
|
|
|
rollErr := tx.Rollback()
|
|
|
|
if rollErr != nil {
|
|
|
|
return fmt.Errorf("failed to rollback transaction due to error: %v", err)
|
|
|
|
}
|
2021-11-24 12:59:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (c *Controller) GetTransfersByAddress(ctx context.Context, chainID uint64, address common.Address, toBlock *big.Int,
|
|
|
|
limit int64, fetchMore bool) ([]View, error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
rst, err := c.reactor.getTransfersByAddress(ctx, chainID, address, toBlock, limit, fetchMore)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("[WalletAPI:: GetTransfersByAddress] can't fetch transfers", "err", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return castToTransferViews(rst), nil
|
|
|
|
}
|
|
|
|
|
2023-04-21 11:59:29 +00:00
|
|
|
func (c *Controller) GetTransfersForIdentities(ctx context.Context, identities []TransactionIdentity) ([]View, error) {
|
|
|
|
rst, err := c.db.GetTransfersForIdentities(ctx, identities)
|
|
|
|
if err != nil {
|
2023-06-13 14:20:48 +00:00
|
|
|
log.Error("[transfer.Controller.GetTransfersForIdentities] DB err", err)
|
2023-04-21 11:59:29 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return castToTransferViews(rst), nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (c *Controller) GetCachedBalances(ctx context.Context, chainID uint64, addresses []common.Address) ([]BlockView, error) {
|
|
|
|
result, error := c.blockDAO.getLastKnownBlocks(chainID, addresses)
|
2021-09-09 14:28:54 +00:00
|
|
|
if error != nil {
|
|
|
|
return nil, error
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocksToViews(result), nil
|
|
|
|
}
|