2021-09-09 14:28:54 +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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("merge old ranges", "account", account, "network", chainID, "ranges", len(ranges))
|
|
|
|
|
|
|
|
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 {
|
2021-09-09 14:28:54 +00:00
|
|
|
log.Debug("insert blocks range", "account", account, "network id", chainID, "from", from, "to", to, "balance", balance, "nonce", nonce)
|
|
|
|
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 {
|
|
|
|
log.Info("Can't get last block", "error", err)
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
log.Info("blocks ranges gap detected", "from", prevTo, "to", blocksRange.from)
|
|
|
|
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 {
|
|
|
|
log.Info("delete blocks range", "account", account, "network", chainID, "from", from, "to", to)
|
|
|
|
delete, err := creator.Prepare(`DELETE FROM blocks_ranges
|
|
|
|
WHERE address = ?
|
|
|
|
AND network_id = ?
|
|
|
|
AND blk_from = ?
|
|
|
|
AND blk_to = ?`)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("some error", "error", err)
|
|
|
|
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 {
|
|
|
|
log.Info("insert blocks range", "account", account, "network", chainID, "from", from, "to", to)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func upsertRange(chainID uint64, creator statementCreator, account common.Address, from *big.Int, to *Block) (err error) {
|
2021-09-09 14:28:54 +00:00
|
|
|
log.Debug("upsert blocks range", "account", account, "network id", chainID, "from", from, "to", to.Number, "balance", to.Balance)
|
|
|
|
update, err := creator.Prepare(`UPDATE blocks_ranges
|
|
|
|
SET blk_to = ?, balance = ?, nonce = ?
|
|
|
|
WHERE address = ?
|
|
|
|
AND network_id = ?
|
|
|
|
AND blk_to = ?`)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := update.Exec((*bigint.SQLBigInt)(to.Number), (*bigint.SQLBigIntBytes)(to.Balance), to.Nonce, account, chainID, (*bigint.SQLBigInt)(from))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
affected, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if affected == 0 {
|
|
|
|
insert, err := creator.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.Number), (*bigint.SQLBigIntBytes)(to.Balance), to.Nonce)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|