2023-05-19 08:19:48 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
|
|
|
)
|
|
|
|
|
2023-12-11 13:29:10 +00:00
|
|
|
type BlockRangeDAOer interface {
|
|
|
|
getBlockRange(chainID uint64, address common.Address) (blockRange *ethTokensBlockRanges, err error)
|
|
|
|
upsertRange(chainID uint64, account common.Address, newBlockRange *ethTokensBlockRanges) (err error)
|
|
|
|
updateTokenRange(chainID uint64, account common.Address, newBlockRange *BlockRange) (err error)
|
|
|
|
upsertEthRange(chainID uint64, account common.Address, newBlockRange *BlockRange) (err error)
|
|
|
|
}
|
|
|
|
|
2023-05-19 08:19:48 +00:00
|
|
|
type BlockRangeSequentialDAO struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockRange struct {
|
|
|
|
Start *big.Int // Block of first transfer
|
|
|
|
FirstKnown *big.Int // Oldest scanned block
|
|
|
|
LastKnown *big.Int // Last scanned block
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockRange() *BlockRange {
|
|
|
|
return &BlockRange{Start: &big.Int{}, FirstKnown: &big.Int{}, LastKnown: &big.Int{}}
|
|
|
|
}
|
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
type ethTokensBlockRanges struct {
|
2024-01-19 15:57:04 +00:00
|
|
|
eth *BlockRange
|
|
|
|
tokens *BlockRange
|
|
|
|
balanceCheckHash string
|
2023-11-27 10:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newEthTokensBlockRanges() *ethTokensBlockRanges {
|
|
|
|
return ðTokensBlockRanges{eth: NewBlockRange(), tokens: NewBlockRange()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlockRangeSequentialDAO) getBlockRange(chainID uint64, address common.Address) (blockRange *ethTokensBlockRanges, err error) {
|
2024-01-19 15:57:04 +00:00
|
|
|
query := `SELECT blk_start, blk_first, blk_last, token_blk_start, token_blk_first, token_blk_last, balance_check_hash FROM blocks_ranges_sequential
|
2023-05-19 08:19:48 +00:00
|
|
|
WHERE address = ?
|
|
|
|
AND network_id = ?`
|
|
|
|
|
|
|
|
rows, err := b.db.Query(query, address, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
blockRange = ðTokensBlockRanges{}
|
2023-05-19 08:19:48 +00:00
|
|
|
if rows.Next() {
|
2023-11-27 10:08:17 +00:00
|
|
|
blockRange = newEthTokensBlockRanges()
|
2024-01-19 15:57:04 +00:00
|
|
|
err = rows.Scan((*bigint.SQLBigInt)(blockRange.eth.Start),
|
|
|
|
(*bigint.SQLBigInt)(blockRange.eth.FirstKnown),
|
|
|
|
(*bigint.SQLBigInt)(blockRange.eth.LastKnown),
|
|
|
|
(*bigint.SQLBigInt)(blockRange.tokens.Start),
|
|
|
|
(*bigint.SQLBigInt)(blockRange.tokens.FirstKnown),
|
|
|
|
(*bigint.SQLBigInt)(blockRange.tokens.LastKnown),
|
|
|
|
&blockRange.balanceCheckHash,
|
|
|
|
)
|
2023-05-19 08:19:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockRange, nil
|
|
|
|
}
|
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
return blockRange, nil
|
2023-05-19 08:19:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 14:23:03 +00:00
|
|
|
func (b *BlockRangeSequentialDAO) deleteRange(account common.Address) error {
|
|
|
|
log.Debug("delete blocks range", "account", account)
|
|
|
|
delete, err := b.db.Prepare(`DELETE FROM blocks_ranges_sequential WHERE address = ?`)
|
2023-05-19 08:19:48 +00:00
|
|
|
if err != nil {
|
2023-05-26 08:27:48 +00:00
|
|
|
log.Error("Failed to prepare deletion of sequential block range", "error", err)
|
2023-05-19 08:19:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-28 14:23:03 +00:00
|
|
|
_, err = delete.Exec(account)
|
2023-05-19 08:19:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
func (b *BlockRangeSequentialDAO) upsertRange(chainID uint64, account common.Address, newBlockRange *ethTokensBlockRanges) (err error) {
|
|
|
|
ethTokensBlockRange, err := b.getBlockRange(chainID, account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ethBlockRange := prepareUpdatedBlockRange(chainID, account, ethTokensBlockRange.eth, newBlockRange.eth)
|
|
|
|
tokensBlockRange := prepareUpdatedBlockRange(chainID, account, ethTokensBlockRange.tokens, newBlockRange.tokens)
|
2023-05-19 08:19:48 +00:00
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
log.Debug("update eth and tokens blocks range", "account", account, "chainID", chainID,
|
|
|
|
"eth.start", ethBlockRange.Start, "eth.first", ethBlockRange.FirstKnown, "eth.last", ethBlockRange.LastKnown,
|
2024-01-19 15:57:04 +00:00
|
|
|
"tokens.start", tokensBlockRange.Start, "tokens.first", ethBlockRange.FirstKnown, "eth.last", ethBlockRange.LastKnown, "hash", newBlockRange.balanceCheckHash)
|
2023-05-19 08:19:48 +00:00
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
upsert, err := b.db.Prepare(`REPLACE INTO blocks_ranges_sequential
|
2024-01-19 15:57:04 +00:00
|
|
|
(network_id, address, blk_start, blk_first, blk_last, token_blk_start, token_blk_first, token_blk_last, balance_check_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
2023-05-19 08:19:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
_, err = upsert.Exec(chainID, account, (*bigint.SQLBigInt)(ethBlockRange.Start), (*bigint.SQLBigInt)(ethBlockRange.FirstKnown), (*bigint.SQLBigInt)(ethBlockRange.LastKnown),
|
2024-01-19 15:57:04 +00:00
|
|
|
(*bigint.SQLBigInt)(tokensBlockRange.Start), (*bigint.SQLBigInt)(tokensBlockRange.FirstKnown), (*bigint.SQLBigInt)(tokensBlockRange.LastKnown), newBlockRange.balanceCheckHash)
|
2023-05-19 08:19:48 +00:00
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-05-19 08:19:48 +00:00
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
func (b *BlockRangeSequentialDAO) upsertEthRange(chainID uint64, account common.Address,
|
|
|
|
newBlockRange *BlockRange) (err error) {
|
2023-05-19 08:19:48 +00:00
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
ethTokensBlockRange, err := b.getBlockRange(chainID, account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-05-19 08:19:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
blockRange := prepareUpdatedBlockRange(chainID, account, ethTokensBlockRange.eth, newBlockRange)
|
|
|
|
|
|
|
|
log.Debug("update eth blocks range", "account", account, "chainID", chainID,
|
2024-01-19 15:57:04 +00:00
|
|
|
"start", blockRange.Start, "first", blockRange.FirstKnown, "last", blockRange.LastKnown, "old hash", ethTokensBlockRange.balanceCheckHash)
|
2023-11-27 10:08:17 +00:00
|
|
|
|
2023-05-26 08:27:48 +00:00
|
|
|
upsert, err := b.db.Prepare(`REPLACE INTO blocks_ranges_sequential
|
2024-01-19 15:57:04 +00:00
|
|
|
(network_id, address, blk_start, blk_first, blk_last, token_blk_start, token_blk_first, token_blk_last, balance_check_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
2023-05-19 08:19:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-17 11:08:56 +00:00
|
|
|
if ethTokensBlockRange.tokens == nil {
|
|
|
|
ethTokensBlockRange.tokens = NewBlockRange()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = upsert.Exec(chainID, account, (*bigint.SQLBigInt)(blockRange.Start), (*bigint.SQLBigInt)(blockRange.FirstKnown), (*bigint.SQLBigInt)(blockRange.LastKnown),
|
2024-01-19 15:57:04 +00:00
|
|
|
(*bigint.SQLBigInt)(ethTokensBlockRange.tokens.Start), (*bigint.SQLBigInt)(ethTokensBlockRange.tokens.FirstKnown), (*bigint.SQLBigInt)(ethTokensBlockRange.tokens.LastKnown), ethTokensBlockRange.balanceCheckHash)
|
2023-05-19 08:19:48 +00:00
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-05 15:11:15 +00:00
|
|
|
func (b *BlockRangeSequentialDAO) updateTokenRange(chainID uint64, account common.Address,
|
2023-11-27 10:08:17 +00:00
|
|
|
newBlockRange *BlockRange) (err error) {
|
|
|
|
|
|
|
|
ethTokensBlockRange, err := b.getBlockRange(chainID, account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blockRange := prepareUpdatedBlockRange(chainID, account, ethTokensBlockRange.tokens, newBlockRange)
|
|
|
|
|
|
|
|
log.Debug("update tokens blocks range", "account", account, "chainID", chainID,
|
2024-01-19 15:57:04 +00:00
|
|
|
"start", blockRange.Start, "first", blockRange.FirstKnown, "last", blockRange.LastKnown, "old hash", ethTokensBlockRange.balanceCheckHash)
|
2023-11-27 10:08:17 +00:00
|
|
|
|
2023-12-05 15:11:15 +00:00
|
|
|
update, err := b.db.Prepare(`UPDATE blocks_ranges_sequential SET token_blk_start = ?, token_blk_first = ?, token_blk_last = ? WHERE network_id = ? AND address = ?`)
|
2023-11-27 10:08:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-17 11:08:56 +00:00
|
|
|
_, err = update.Exec((*bigint.SQLBigInt)(blockRange.Start), (*bigint.SQLBigInt)(blockRange.FirstKnown),
|
|
|
|
(*bigint.SQLBigInt)(blockRange.LastKnown), chainID, account)
|
2023-11-27 10:08:17 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareUpdatedBlockRange(chainID uint64, account common.Address, blockRange, newBlockRange *BlockRange) *BlockRange {
|
|
|
|
// Update existing range
|
|
|
|
if blockRange != nil {
|
|
|
|
if newBlockRange != nil {
|
|
|
|
// Ovewrite start block if there was not any or if new one is older, because it can be precised only
|
|
|
|
// to a greater value, because no history can be before some block that is considered
|
|
|
|
// as a start of history, but due to concurrent block range checks, a newer greater block
|
|
|
|
// can be found that matches criteria of a start block (nonce is zero, balances are equal)
|
|
|
|
if newBlockRange.Start != nil && (blockRange.Start == nil || blockRange.Start.Cmp(newBlockRange.Start) < 0) {
|
|
|
|
blockRange.Start = newBlockRange.Start
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite first known block if there was not any or if new one is older
|
|
|
|
if (blockRange.FirstKnown == nil && newBlockRange.FirstKnown != nil) ||
|
|
|
|
(blockRange.FirstKnown != nil && newBlockRange.FirstKnown != nil && blockRange.FirstKnown.Cmp(newBlockRange.FirstKnown) > 0) {
|
|
|
|
blockRange.FirstKnown = newBlockRange.FirstKnown
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite last known block if there was not any or if new one is newer
|
|
|
|
if (blockRange.LastKnown == nil && newBlockRange.LastKnown != nil) ||
|
|
|
|
(blockRange.LastKnown != nil && newBlockRange.LastKnown != nil && blockRange.LastKnown.Cmp(newBlockRange.LastKnown) < 0) {
|
|
|
|
blockRange.LastKnown = newBlockRange.LastKnown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
blockRange = newBlockRange
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockRange
|
2023-05-19 08:19:48 +00:00
|
|
|
}
|