2021-09-09 14:28:54 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
2023-04-21 11:59:29 +00:00
|
|
|
"context"
|
2021-09-09 14:28:54 +00:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
|
2023-05-19 15:31:45 +00:00
|
|
|
w_common "github.com/status-im/status-go/services/wallet/common"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
|
|
|
"github.com/status-im/status-go/walletdatabase"
|
2021-09-09 14:28:54 +00:00
|
|
|
)
|
|
|
|
|
2023-05-08 06:02:00 +00:00
|
|
|
func setupTestDB(t *testing.T) (*Database, *BlockDAO, func()) {
|
2023-08-11 11:28:45 +00:00
|
|
|
db, err := helpers.SetupTestMemorySQLDB(walletdatabase.DbInitializer{})
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, err)
|
2023-05-08 06:02:00 +00:00
|
|
|
return NewDB(db), &BlockDAO{db}, func() {
|
2021-09-09 14:28:54 +00:00
|
|
|
require.NoError(t, db.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
func TestDBSaveBlocks(t *testing.T) {
|
|
|
|
db, _, stop := setupTestDB(t)
|
2021-09-09 14:28:54 +00:00
|
|
|
defer stop()
|
|
|
|
address := common.Address{1}
|
|
|
|
blocks := []*DBHeader{
|
2023-05-19 15:31:45 +00:00
|
|
|
{
|
2023-11-27 10:08:17 +00:00
|
|
|
Number: big.NewInt(1),
|
|
|
|
Hash: common.Hash{1},
|
|
|
|
Address: address,
|
2021-09-09 14:28:54 +00:00
|
|
|
},
|
2023-05-19 15:31:45 +00:00
|
|
|
{
|
2023-11-27 10:08:17 +00:00
|
|
|
Number: big.NewInt(2),
|
|
|
|
Hash: common.Hash{2},
|
|
|
|
Address: address,
|
2021-09-09 14:28:54 +00:00
|
|
|
}}
|
2023-11-27 10:08:17 +00:00
|
|
|
require.NoError(t, db.SaveBlocks(777, blocks))
|
2021-09-09 14:28:54 +00:00
|
|
|
transfers := []Transfer{
|
|
|
|
{
|
|
|
|
ID: common.Hash{1},
|
2023-05-19 15:31:45 +00:00
|
|
|
Type: w_common.EthTransfer,
|
2021-09-09 14:28:54 +00:00
|
|
|
BlockHash: common.Hash{2},
|
|
|
|
BlockNumber: big.NewInt(1),
|
2023-11-27 10:08:17 +00:00
|
|
|
Address: address,
|
2021-09-09 14:28:54 +00:00
|
|
|
Timestamp: 123,
|
2023-11-27 10:08:17 +00:00
|
|
|
From: address,
|
2021-09-09 14:28:54 +00:00
|
|
|
},
|
|
|
|
}
|
2023-08-01 18:50:30 +00:00
|
|
|
tx, err := db.client.BeginTx(context.Background(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NoError(t, saveTransfersMarkBlocksLoaded(tx, 777, address, transfers, []*big.Int{big.NewInt(1), big.NewInt(2)}))
|
|
|
|
require.NoError(t, tx.Commit())
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
2023-11-27 10:08:17 +00:00
|
|
|
func TestDBSaveTransfers(t *testing.T) {
|
2021-09-09 14:28:54 +00:00
|
|
|
db, _, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2023-11-27 10:08:17 +00:00
|
|
|
address := common.Address{1}
|
2021-09-09 14:28:54 +00:00
|
|
|
header := &DBHeader{
|
|
|
|
Number: big.NewInt(1),
|
|
|
|
Hash: common.Hash{1},
|
2023-11-27 10:08:17 +00:00
|
|
|
Address: address,
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
2023-11-27 10:08:17 +00:00
|
|
|
tx := types.NewTransaction(1, address, nil, 10, big.NewInt(10), nil)
|
2021-09-09 14:28:54 +00:00
|
|
|
transfers := []Transfer{
|
|
|
|
{
|
2023-02-15 12:28:19 +00:00
|
|
|
ID: common.Hash{1},
|
2023-05-19 15:31:45 +00:00
|
|
|
Type: w_common.EthTransfer,
|
2023-02-15 12:28:19 +00:00
|
|
|
BlockHash: header.Hash,
|
|
|
|
BlockNumber: header.Number,
|
|
|
|
Transaction: tx,
|
|
|
|
Receipt: types.NewReceipt(nil, false, 100),
|
2023-11-27 10:08:17 +00:00
|
|
|
Address: address,
|
2023-02-15 12:28:19 +00:00
|
|
|
MultiTransactionID: 0,
|
2021-09-09 14:28:54 +00:00
|
|
|
},
|
|
|
|
}
|
2023-11-27 10:08:17 +00:00
|
|
|
require.NoError(t, db.SaveBlocks(777, []*DBHeader{header}))
|
|
|
|
require.NoError(t, saveTransfersMarkBlocksLoaded(db.client, 777, address, transfers, []*big.Int{header.Number}))
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDBGetTransfersFromBlock(t *testing.T) {
|
|
|
|
db, _, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
|
|
|
headers := []*DBHeader{}
|
|
|
|
transfers := []Transfer{}
|
2023-11-27 10:08:17 +00:00
|
|
|
address := common.Address{1}
|
|
|
|
blockNumbers := []*big.Int{}
|
2021-09-09 14:28:54 +00:00
|
|
|
for i := 1; i < 10; i++ {
|
|
|
|
header := &DBHeader{
|
|
|
|
Number: big.NewInt(int64(i)),
|
|
|
|
Hash: common.Hash{byte(i)},
|
2023-11-27 10:08:17 +00:00
|
|
|
Address: address,
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
headers = append(headers, header)
|
2023-11-27 10:08:17 +00:00
|
|
|
blockNumbers = append(blockNumbers, header.Number)
|
|
|
|
tx := types.NewTransaction(uint64(i), address, nil, 10, big.NewInt(10), nil)
|
2021-09-09 14:28:54 +00:00
|
|
|
receipt := types.NewReceipt(nil, false, 100)
|
|
|
|
receipt.Logs = []*types.Log{}
|
|
|
|
transfer := Transfer{
|
|
|
|
ID: tx.Hash(),
|
2023-05-19 15:31:45 +00:00
|
|
|
Type: w_common.EthTransfer,
|
2021-09-09 14:28:54 +00:00
|
|
|
BlockNumber: header.Number,
|
|
|
|
BlockHash: header.Hash,
|
|
|
|
Transaction: tx,
|
|
|
|
Receipt: receipt,
|
2023-11-27 10:08:17 +00:00
|
|
|
Address: address,
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
transfers = append(transfers, transfer)
|
|
|
|
}
|
2023-11-27 10:08:17 +00:00
|
|
|
require.NoError(t, db.SaveBlocks(777, headers))
|
|
|
|
require.NoError(t, saveTransfersMarkBlocksLoaded(db.client, 777, address, transfers, blockNumbers))
|
2021-09-09 14:28:54 +00:00
|
|
|
rst, err := db.GetTransfers(777, big.NewInt(7), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 3)
|
|
|
|
|
|
|
|
rst, err = db.GetTransfers(777, big.NewInt(2), big.NewInt(5))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, rst, 4)
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTransfersForIdentities(t *testing.T) {
|
|
|
|
db, _, stop := setupTestDB(t)
|
|
|
|
defer stop()
|
2021-09-09 14:28:54 +00:00
|
|
|
|
2023-06-14 16:10:20 +00:00
|
|
|
trs, _, _ := GenerateTestTransfers(t, db.client, 1, 4)
|
2023-04-21 11:59:29 +00:00
|
|
|
for i := range trs {
|
2023-06-20 02:50:49 +00:00
|
|
|
InsertTestTransfer(t, db.client, trs[i].To, &trs[i])
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := db.GetTransfersForIdentities(context.Background(), []TransactionIdentity{
|
|
|
|
TransactionIdentity{trs[1].ChainID, trs[1].Hash, trs[1].To},
|
|
|
|
TransactionIdentity{trs[3].ChainID, trs[3].Hash, trs[3].To}})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(entries))
|
|
|
|
require.Equal(t, trs[1].Hash, entries[0].ID)
|
|
|
|
require.Equal(t, trs[3].Hash, entries[1].ID)
|
|
|
|
require.Equal(t, trs[1].From, entries[0].From)
|
|
|
|
require.Equal(t, trs[3].From, entries[1].From)
|
|
|
|
require.Equal(t, trs[1].To, entries[0].Address)
|
|
|
|
require.Equal(t, trs[3].To, entries[1].Address)
|
|
|
|
require.Equal(t, big.NewInt(trs[1].BlkNumber), entries[0].BlockNumber)
|
|
|
|
require.Equal(t, big.NewInt(trs[3].BlkNumber), entries[1].BlockNumber)
|
|
|
|
require.Equal(t, uint64(trs[1].Timestamp), entries[0].Timestamp)
|
|
|
|
require.Equal(t, uint64(trs[3].Timestamp), entries[1].Timestamp)
|
2023-05-11 07:50:07 +00:00
|
|
|
require.Equal(t, uint64(trs[1].ChainID), entries[0].NetworkID)
|
|
|
|
require.Equal(t, uint64(trs[3].ChainID), entries[1].NetworkID)
|
2023-04-21 11:59:29 +00:00
|
|
|
require.Equal(t, MultiTransactionIDType(0), entries[0].MultiTransactionID)
|
|
|
|
require.Equal(t, MultiTransactionIDType(0), entries[1].MultiTransactionID)
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|