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"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
2023-11-28 14:23:03 +00:00
|
|
|
"golang.org/x/exp/slices" // since 1.21, this is in the standard library
|
|
|
|
|
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"
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2023-10-18 10:02:35 +00:00
|
|
|
statusaccounts "github.com/status-im/status-go/multiaccounts/accounts"
|
2021-09-22 17:49:20 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
2024-09-24 13:07:26 +00:00
|
|
|
"github.com/status-im/status-go/rpc/chain/rpclimiter"
|
2023-05-31 19:45:11 +00:00
|
|
|
"github.com/status-im/status-go/services/accounts/accountsevent"
|
2023-09-04 05:34:09 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/balance"
|
2024-01-25 12:05:59 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/blockchainstate"
|
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
|
2023-10-18 10:02:35 +00:00
|
|
|
accountsDB *statusaccounts.Database
|
2023-02-15 12:28:19 +00:00
|
|
|
rpcClient *rpc.Client
|
2023-05-08 06:02:00 +00:00
|
|
|
blockDAO *BlockDAO
|
2023-11-28 14:23:03 +00:00
|
|
|
blockRangesSeqDAO *BlockRangeSequentialDAO
|
2023-02-15 12:28:19 +00:00
|
|
|
reactor *Reactor
|
|
|
|
accountFeed *event.Feed
|
|
|
|
TransferFeed *event.Feed
|
2023-11-28 14:23:03 +00:00
|
|
|
accWatcher *accountsevent.Watcher
|
2023-02-15 12:28:19 +00:00
|
|
|
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-09-04 05:34:09 +00:00
|
|
|
balanceCacher balance.Cacher
|
2024-01-25 12:05:59 +00:00
|
|
|
blockChainState *blockchainstate.BlockChainState
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 10:02:35 +00:00
|
|
|
func NewTransferController(db *sql.DB, accountsDB *statusaccounts.Database, rpcClient *rpc.Client, accountFeed *event.Feed, transferFeed *event.Feed,
|
2023-09-04 05:34:09 +00:00
|
|
|
transactionManager *TransactionManager, pendingTxManager *transactions.PendingTxTracker, tokenManager *token.Manager,
|
2024-01-25 12:05:59 +00:00
|
|
|
balanceCacher balance.Cacher, blockChainState *blockchainstate.BlockChainState) *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-10-18 10:02:35 +00:00
|
|
|
accountsDB: accountsDB,
|
2023-05-08 06:02:00 +00:00
|
|
|
blockDAO: blockDAO,
|
2023-11-28 14:23:03 +00:00
|
|
|
blockRangesSeqDAO: &BlockRangeSequentialDAO{db},
|
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-09-04 05:34:09 +00:00
|
|
|
balanceCacher: balanceCacher,
|
2024-01-25 12:05:59 +00:00
|
|
|
blockChainState: blockChainState,
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 09:19:32 +00:00
|
|
|
func (c *Controller) Start() {
|
2024-09-26 22:37:32 +00:00
|
|
|
go func() {
|
|
|
|
defer gocommon.LogOnPanic()
|
|
|
|
_ = c.cleanupAccountsLeftovers()
|
|
|
|
}()
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) Stop() {
|
|
|
|
if c.reactor != nil {
|
|
|
|
c.reactor.stop()
|
|
|
|
}
|
|
|
|
|
2023-11-28 14:23:03 +00:00
|
|
|
if c.accWatcher != nil {
|
|
|
|
c.accWatcher.Stop()
|
|
|
|
c.accWatcher = nil
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:40 +00:00
|
|
|
func sameChains(chainIDs1 []uint64, chainIDs2 []uint64) bool {
|
|
|
|
if len(chainIDs1) != len(chainIDs2) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chainID := range chainIDs1 {
|
|
|
|
if !slices.Contains(chainIDs2, chainID) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
func (c *Controller) CheckRecentHistory(chainIDs []uint64, accounts []common.Address) error {
|
|
|
|
if len(accounts) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(chainIDs) == 0 {
|
|
|
|
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 {
|
2024-02-01 16:26:40 +00:00
|
|
|
if !sameChains(chainIDs, c.reactor.chainIDs) {
|
|
|
|
err := c.reactor.restart(chainClients, accounts)
|
2023-10-18 10:02:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-29 14:03:30 +00:00
|
|
|
const omitHistory = true
|
2024-02-01 16:26:40 +00:00
|
|
|
c.reactor = NewReactor(c.db, c.blockDAO, c.blockRangesSeqDAO, c.accountsDB, c.TransferFeed, c.transactionManager,
|
|
|
|
c.pendingTxManager, c.tokenManager, c.balanceCacher, omitHistory, c.blockChainState)
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2024-02-01 16:26:40 +00:00
|
|
|
err = c.reactor.start(chainClients, accounts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-08 06:02:00 +00:00
|
|
|
}
|
2024-02-01 16:26:40 +00:00
|
|
|
|
|
|
|
c.startAccountWatcher(chainIDs)
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-28 14:23:03 +00:00
|
|
|
func (c *Controller) startAccountWatcher(chainIDs []uint64) {
|
|
|
|
if c.accWatcher == nil {
|
|
|
|
c.accWatcher = accountsevent.NewWatcher(c.accountsDB, c.accountFeed, func(changedAddresses []common.Address, eventType accountsevent.EventType, currentAddresses []common.Address) {
|
|
|
|
c.onAccountsChanged(changedAddresses, eventType, currentAddresses, chainIDs)
|
|
|
|
})
|
|
|
|
}
|
2024-03-03 07:59:21 +00:00
|
|
|
c.accWatcher.Start()
|
2023-11-28 14:23:03 +00:00
|
|
|
}
|
2023-05-31 19:45:11 +00:00
|
|
|
|
2023-11-28 14:23:03 +00:00
|
|
|
func (c *Controller) onAccountsChanged(changedAddresses []common.Address, eventType accountsevent.EventType, currentAddresses []common.Address, chainIDs []uint64) {
|
|
|
|
if eventType == accountsevent.EventTypeRemoved {
|
|
|
|
for _, address := range changedAddresses {
|
|
|
|
c.cleanUpRemovedAccount(address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.reactor == nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Warn("reactor is not initialized")
|
2023-11-28 14:23:03 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-31 19:45:11 +00:00
|
|
|
|
2023-11-28 14:23:03 +00:00
|
|
|
if eventType == accountsevent.EventTypeAdded || eventType == accountsevent.EventTypeRemoved {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Debug("list of accounts was changed from a previous version. reactor will be restarted", zap.Stringers("new", currentAddresses))
|
2023-05-08 06:02:00 +00:00
|
|
|
|
2023-11-28 14:23:03 +00:00
|
|
|
chainClients, err := c.rpcClient.EthClients(chainIDs)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 14:23:03 +00:00
|
|
|
err = c.reactor.restart(chainClients, currentAddresses)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("failed to restart reactor with new accounts", zap.Error(err))
|
2023-11-28 14:23:03 +00:00
|
|
|
}
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-15 14:30:56 +00:00
|
|
|
rst, err := c.reactor.getTransfersByAddress(ctx, chainID, address, toBlock, limit)
|
2021-09-09 14:28:54 +00:00
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("[WalletAPI:: GetTransfersByAddress] can't fetch transfers", zap.Error(err))
|
2021-09-09 14:28:54 +00:00
|
|
|
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 {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("[transfer.Controller.GetTransfersForIdentities] DB err", zap.Error(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
|
|
|
|
}
|
2023-11-28 14:23:03 +00:00
|
|
|
|
|
|
|
func (c *Controller) cleanUpRemovedAccount(address common.Address) {
|
|
|
|
// Transfers will be deleted by foreign key constraint by cascade
|
|
|
|
err := deleteBlocks(c.db.client, address)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Failed to delete blocks", zap.Error(err))
|
2023-11-28 14:23:03 +00:00
|
|
|
}
|
|
|
|
err = deleteAllRanges(c.db.client, address)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Failed to delete old blocks ranges", zap.Error(err))
|
2023-11-28 14:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = c.blockRangesSeqDAO.deleteRange(address)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Failed to delete blocks ranges sequential", zap.Error(err))
|
2023-11-28 14:23:03 +00:00
|
|
|
}
|
2024-03-26 12:05:49 +00:00
|
|
|
|
|
|
|
err = c.transactionManager.removeMultiTransactionByAddress(address)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Failed to delete multitransactions", zap.Error(err))
|
2024-03-26 12:05:49 +00:00
|
|
|
}
|
2024-06-19 05:20:32 +00:00
|
|
|
|
2024-09-24 13:07:26 +00:00
|
|
|
rpcLimitsStorage := rpclimiter.NewLimitsDBStorage(c.db.client)
|
2024-06-19 05:20:32 +00:00
|
|
|
err = rpcLimitsStorage.Delete(accountLimiterTag(address))
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Failed to delete limits", zap.Error(err))
|
2024-06-19 05:20:32 +00:00
|
|
|
}
|
2023-11-28 14:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) cleanupAccountsLeftovers() error {
|
|
|
|
// We clean up accounts that were deleted and soft removed
|
|
|
|
accounts, err := c.accountsDB.GetWalletAddresses()
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Failed to get accounts", zap.Error(err))
|
2023-11-28 14:23:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
existingAddresses := make([]common.Address, len(accounts))
|
|
|
|
for i, account := range accounts {
|
|
|
|
existingAddresses[i] = (common.Address)(account)
|
|
|
|
}
|
|
|
|
|
|
|
|
addressesInWalletDB, err := getAddresses(c.db.client)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("Failed to get addresses from wallet db", zap.Error(err))
|
2023-11-28 14:23:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
missing := findMissingItems(addressesInWalletDB, existingAddresses)
|
|
|
|
for _, address := range missing {
|
|
|
|
c.cleanUpRemovedAccount(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// find items from one slice that are not in another
|
|
|
|
func findMissingItems(slice1 []common.Address, slice2 []common.Address) []common.Address {
|
|
|
|
var missing []common.Address
|
|
|
|
for _, item := range slice1 {
|
|
|
|
if !slices.Contains(slice2, item) {
|
|
|
|
missing = append(missing, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return missing
|
|
|
|
}
|