2021-09-09 14:28:54 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"math/big"
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlocksRange struct {
|
|
|
|
from *big.Int
|
|
|
|
to *big.Int
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
type Block struct {
|
2021-09-09 14:28:54 +00:00
|
|
|
Number *big.Int
|
|
|
|
Balance *big.Int
|
|
|
|
Nonce *int64
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
type BlockView struct {
|
2021-09-09 14:28:54 +00:00
|
|
|
Address common.Address `json:"address"`
|
|
|
|
Number *big.Int `json:"blockNumber"`
|
|
|
|
Balance bigint.BigInt `json:"balance"`
|
|
|
|
Nonce *int64 `json:"nonce"`
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func blocksToViews(blocks map[common.Address]*Block) []BlockView {
|
|
|
|
blocksViews := []BlockView{}
|
2021-09-09 14:28:54 +00:00
|
|
|
for address, block := range blocks {
|
2023-05-08 06:02:00 +00:00
|
|
|
view := BlockView{
|
2021-09-09 14:28:54 +00:00
|
|
|
Address: address,
|
|
|
|
Number: block.Number,
|
2023-01-13 17:12:46 +00:00
|
|
|
Balance: bigint.BigInt{Int: block.Balance},
|
2021-09-09 14:28:54 +00:00
|
|
|
Nonce: block.Nonce,
|
|
|
|
}
|
|
|
|
blocksViews = append(blocksViews, view)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blocksViews
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
type BlockDAO struct {
|
2021-09-09 14:28:54 +00:00
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeBlocksRanges merge old blocks ranges if possible
|
2023-05-08 06:02:00 +00:00
|
|
|
func (b *BlockDAO) mergeBlocksRanges(chainIDs []uint64, accounts []common.Address) error {
|
2021-09-09 14:28:54 +00:00
|
|
|
for _, chainID := range chainIDs {
|
|
|
|
for _, account := range accounts {
|
|
|
|
err := b.mergeRanges(chainID, account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (b *BlockDAO) mergeRanges(chainID uint64, account common.Address) (err error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
var (
|
|
|
|
tx *sql.Tx
|
|
|
|
)
|
|
|
|
|
|
|
|
ranges, err := b.getOldRanges(chainID, account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("merge old ranges",
|
|
|
|
zap.Stringer("account", account),
|
|
|
|
zap.Uint64("network", chainID),
|
|
|
|
zap.Int("ranges", len(ranges)),
|
|
|
|
)
|
2021-09-09 14:28:54 +00:00
|
|
|
|
|
|
|
if len(ranges) <= 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err = b.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = tx.Rollback()
|
|
|
|
}()
|
|
|
|
|
|
|
|
newRanges, deletedRanges := getNewRanges(ranges)
|
|
|
|
|
|
|
|
for _, rangeToDelete := range deletedRanges {
|
|
|
|
err = deleteRange(chainID, tx, account, rangeToDelete.from, rangeToDelete.to)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, newRange := range newRanges {
|
|
|
|
err = insertRange(chainID, tx, account, newRange.from, newRange.to)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (b *BlockDAO) insertRange(chainID uint64, account common.Address, from, to, balance *big.Int, nonce uint64) error {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Debug(
|
|
|
|
"insert blocks range",
|
|
|
|
zap.Stringer("account", account),
|
|
|
|
zap.Uint64("network id", chainID),
|
|
|
|
zap.Stringer("from", from),
|
|
|
|
zap.Stringer("to", to),
|
|
|
|
zap.Stringer("balance", balance),
|
|
|
|
zap.Uint64("nonce", nonce),
|
|
|
|
)
|
2021-09-09 14:28:54 +00:00
|
|
|
insert, err := b.db.Prepare("INSERT INTO blocks_ranges (network_id, address, blk_from, blk_to, balance, nonce) VALUES (?, ?, ?, ?, ?, ?)")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = insert.Exec(chainID, account, (*bigint.SQLBigInt)(from), (*bigint.SQLBigInt)(to), (*bigint.SQLBigIntBytes)(balance), &nonce)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (b *BlockDAO) getOldRanges(chainID uint64, account common.Address) ([]*BlocksRange, error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
query := `select blk_from, blk_to from blocks_ranges
|
|
|
|
where address = ?
|
|
|
|
and network_id = ?
|
|
|
|
order by blk_from`
|
|
|
|
|
|
|
|
rows, err := b.db.Query(query, account, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
ranges := []*BlocksRange{}
|
|
|
|
for rows.Next() {
|
|
|
|
from := &big.Int{}
|
|
|
|
to := &big.Int{}
|
|
|
|
err = rows.Scan((*bigint.SQLBigInt)(from), (*bigint.SQLBigInt)(to))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ranges = append(ranges, &BlocksRange{
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ranges, nil
|
|
|
|
}
|
|
|
|
|
2023-06-02 20:08:45 +00:00
|
|
|
// GetBlocksToLoadByAddress gets unloaded blocks for a given address.
|
|
|
|
func (b *BlockDAO) GetBlocksToLoadByAddress(chainID uint64, address common.Address, limit int) (rst []*big.Int, err error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
query := `SELECT blk_number FROM blocks
|
|
|
|
WHERE address = ? AND network_id = ? AND loaded = 0
|
2023-05-08 06:02:00 +00:00
|
|
|
ORDER BY blk_number DESC
|
2021-09-09 14:28:54 +00:00
|
|
|
LIMIT ?`
|
|
|
|
rows, err := b.db.Query(query, address, chainID, limit)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
block := &big.Int{}
|
|
|
|
err = rows.Scan((*bigint.SQLBigInt)(block))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rst = append(rst, block)
|
|
|
|
}
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (b *BlockDAO) GetLastBlockByAddress(chainID uint64, address common.Address, limit int) (rst *big.Int, err error) {
|
|
|
|
query := `SELECT * FROM
|
2021-09-09 14:28:54 +00:00
|
|
|
(SELECT blk_number FROM blocks WHERE address = ? AND network_id = ? ORDER BY blk_number DESC LIMIT ?)
|
|
|
|
ORDER BY blk_number LIMIT 1`
|
|
|
|
rows, err := b.db.Query(query, address, chainID, limit)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
block := &big.Int{}
|
|
|
|
err = rows.Scan((*bigint.SQLBigInt)(block))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:09:50 +00:00
|
|
|
func (b *BlockDAO) GetFirstSavedBlock(chainID uint64, address common.Address) (rst *DBHeader, err error) {
|
|
|
|
query := `SELECT blk_number, blk_hash, loaded
|
|
|
|
FROM blocks
|
|
|
|
WHERE network_id = ? AND address = ?
|
|
|
|
ORDER BY blk_number LIMIT 1`
|
|
|
|
rows, err := b.db.Query(query, chainID, address)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
header := &DBHeader{Hash: common.Hash{}, Number: new(big.Int)}
|
|
|
|
err = rows.Scan((*bigint.SQLBigInt)(header.Number), &header.Hash, &header.Loaded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return header, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (b *BlockDAO) GetFirstKnownBlock(chainID uint64, address common.Address) (rst *big.Int, err error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
query := `SELECT blk_from FROM blocks_ranges
|
|
|
|
WHERE address = ?
|
|
|
|
AND network_id = ?
|
|
|
|
ORDER BY blk_from
|
|
|
|
LIMIT 1`
|
|
|
|
|
|
|
|
rows, err := b.db.Query(query, address, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
block := &big.Int{}
|
|
|
|
err = rows.Scan((*bigint.SQLBigInt)(block))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (b *BlockDAO) GetLastKnownBlockByAddress(chainID uint64, address common.Address) (block *Block, err error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
query := `SELECT blk_to, balance, nonce FROM blocks_ranges
|
|
|
|
WHERE address = ?
|
|
|
|
AND network_id = ?
|
|
|
|
ORDER BY blk_to DESC
|
|
|
|
LIMIT 1`
|
|
|
|
|
|
|
|
rows, err := b.db.Query(query, address, chainID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
var nonce sql.NullInt64
|
2023-05-08 06:02:00 +00:00
|
|
|
block = &Block{Number: &big.Int{}, Balance: &big.Int{}}
|
2021-09-09 14:28:54 +00:00
|
|
|
err = rows.Scan((*bigint.SQLBigInt)(block.Number), (*bigint.SQLBigIntBytes)(block.Balance), &nonce)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nonce.Valid {
|
|
|
|
block.Nonce = &nonce.Int64
|
|
|
|
}
|
|
|
|
return block, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func (b *BlockDAO) getLastKnownBlocks(chainID uint64, addresses []common.Address) (map[common.Address]*Block, error) {
|
|
|
|
result := map[common.Address]*Block{}
|
2021-09-09 14:28:54 +00:00
|
|
|
for _, address := range addresses {
|
|
|
|
block, error := b.GetLastKnownBlockByAddress(chainID, address)
|
|
|
|
if error != nil {
|
|
|
|
return nil, error
|
|
|
|
}
|
|
|
|
|
|
|
|
if block != nil {
|
|
|
|
result[address] = block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
// TODO Remove the method below, it is used in one place and duplicates getLastKnownBlocks method with slight unneeded change
|
|
|
|
func (b *BlockDAO) GetLastKnownBlockByAddresses(chainID uint64, addresses []common.Address) (map[common.Address]*Block, []common.Address, error) {
|
|
|
|
res := map[common.Address]*Block{}
|
2021-09-09 14:28:54 +00:00
|
|
|
accountsWithoutHistory := []common.Address{}
|
|
|
|
for _, address := range addresses {
|
|
|
|
block, err := b.GetLastKnownBlockByAddress(chainID, address)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("Can't get last block", zap.Error(err))
|
2021-09-09 14:28:54 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if block != nil {
|
|
|
|
res[address] = block
|
|
|
|
} else {
|
|
|
|
accountsWithoutHistory = append(accountsWithoutHistory, address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, accountsWithoutHistory, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNewRanges(ranges []*BlocksRange) ([]*BlocksRange, []*BlocksRange) {
|
|
|
|
initValue := big.NewInt(-1)
|
|
|
|
prevFrom := big.NewInt(-1)
|
|
|
|
prevTo := big.NewInt(-1)
|
|
|
|
hasMergedRanges := false
|
|
|
|
var newRanges []*BlocksRange
|
|
|
|
var deletedRanges []*BlocksRange
|
|
|
|
for idx, blocksRange := range ranges {
|
|
|
|
if prevTo.Cmp(initValue) == 0 {
|
|
|
|
prevTo = blocksRange.to
|
|
|
|
prevFrom = blocksRange.from
|
|
|
|
} else if prevTo.Cmp(blocksRange.from) >= 0 {
|
|
|
|
hasMergedRanges = true
|
|
|
|
deletedRanges = append(deletedRanges, ranges[idx-1])
|
|
|
|
if prevTo.Cmp(blocksRange.to) <= 0 {
|
|
|
|
prevTo = blocksRange.to
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if hasMergedRanges {
|
|
|
|
deletedRanges = append(deletedRanges, ranges[idx-1])
|
|
|
|
newRanges = append(newRanges, &BlocksRange{
|
|
|
|
from: prevFrom,
|
|
|
|
to: prevTo,
|
|
|
|
})
|
|
|
|
}
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("blocks ranges gap detected",
|
|
|
|
zap.Stringer("from", prevTo),
|
|
|
|
zap.Stringer("to", blocksRange.from),
|
|
|
|
)
|
2021-09-09 14:28:54 +00:00
|
|
|
hasMergedRanges = false
|
|
|
|
|
|
|
|
prevFrom = blocksRange.from
|
|
|
|
prevTo = blocksRange.to
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasMergedRanges {
|
|
|
|
deletedRanges = append(deletedRanges, ranges[len(ranges)-1])
|
|
|
|
newRanges = append(newRanges, &BlocksRange{
|
|
|
|
from: prevFrom,
|
|
|
|
to: prevTo,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRanges, deletedRanges
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRange(chainID uint64, creator statementCreator, account common.Address, from *big.Int, to *big.Int) error {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("delete blocks range",
|
|
|
|
zap.Stringer("account", account),
|
|
|
|
zap.Uint64("network", chainID),
|
|
|
|
zap.Stringer("from", from),
|
|
|
|
zap.Stringer("to", to),
|
|
|
|
)
|
2021-09-09 14:28:54 +00:00
|
|
|
delete, err := creator.Prepare(`DELETE FROM blocks_ranges
|
|
|
|
WHERE address = ?
|
|
|
|
AND network_id = ?
|
|
|
|
AND blk_from = ?
|
|
|
|
AND blk_to = ?`)
|
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("some error", zap.Error(err))
|
2021-09-09 14:28:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = delete.Exec(account, chainID, (*bigint.SQLBigInt)(from), (*bigint.SQLBigInt)(to))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-28 14:23:03 +00:00
|
|
|
func deleteAllRanges(creator statementCreator, account common.Address) error {
|
|
|
|
delete, err := creator.Prepare(`DELETE FROM blocks_ranges WHERE address = ?`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = delete.Exec(account)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
func insertRange(chainID uint64, creator statementCreator, account common.Address, from *big.Int, to *big.Int) error {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("insert blocks range",
|
|
|
|
zap.Stringer("account", account),
|
|
|
|
zap.Uint64("network", chainID),
|
|
|
|
zap.Stringer("from", from),
|
|
|
|
zap.Stringer("to", to),
|
|
|
|
)
|
2021-09-09 14:28:54 +00:00
|
|
|
insert, err := creator.Prepare("INSERT INTO blocks_ranges (network_id, address, blk_from, blk_to) VALUES (?, ?, ?, ?)")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = insert.Exec(chainID, account, (*bigint.SQLBigInt)(from), (*bigint.SQLBigInt)(to))
|
|
|
|
return err
|
|
|
|
}
|