mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Insert ETH as HistoryDB token
This commit is contained in:
@@ -38,6 +38,12 @@ func NewHistoryDB(db *sqlx.DB) *HistoryDB {
|
||||
return &HistoryDB{db: db}
|
||||
}
|
||||
|
||||
// DB returns a pointer to the L2DB.db. This method should be used only for
|
||||
// internal testing purposes.
|
||||
func (hdb *HistoryDB) DB() *sqlx.DB {
|
||||
return hdb.db
|
||||
}
|
||||
|
||||
// AddBlock insert a block into the DB
|
||||
func (hdb *HistoryDB) AddBlock(block *common.Block) error { return hdb.addBlock(hdb.db, block) }
|
||||
func (hdb *HistoryDB) addBlock(d meddler.DB, block *common.Block) error {
|
||||
|
||||
@@ -50,7 +50,7 @@ func TestBlocks(t *testing.T) {
|
||||
fromBlock = 1
|
||||
toBlock = 5
|
||||
// Delete peviously created rows (clean previous test execs)
|
||||
assert.NoError(t, historyDB.Reorg(fromBlock-1))
|
||||
test.WipeDB(historyDB.DB())
|
||||
// Generate fake blocks
|
||||
blocks := test.GenBlocks(fromBlock, toBlock)
|
||||
// Insert blocks into DB
|
||||
@@ -116,9 +116,10 @@ func TestBatches(t *testing.T) {
|
||||
// Test total fee
|
||||
// Generate fake tokens
|
||||
const nTokens = 5
|
||||
tokens := test.GenTokens(nTokens, blocks)
|
||||
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||
err = historyDB.AddTokens(tokens)
|
||||
assert.NoError(t, err)
|
||||
tokens = append([]common.Token{ethToken}, tokens...)
|
||||
feeBatch := batches[0]
|
||||
feeBatch.BatchNum = 9999
|
||||
feeBatch.CollectedFees = make(map[common.TokenID]*big.Int)
|
||||
@@ -174,9 +175,10 @@ func TestTokens(t *testing.T) {
|
||||
blocks := setTestBlocks(fromBlock, toBlock)
|
||||
// Generate fake tokens
|
||||
const nTokens = 5
|
||||
tokens := test.GenTokens(nTokens, blocks)
|
||||
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||
err := historyDB.AddTokens(tokens)
|
||||
assert.NoError(t, err)
|
||||
tokens = append([]common.Token{ethToken}, tokens...)
|
||||
limit := uint(10)
|
||||
// Fetch tokens6
|
||||
fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
|
||||
@@ -201,9 +203,10 @@ func TestAccounts(t *testing.T) {
|
||||
blocks := setTestBlocks(fromBlock, toBlock)
|
||||
// Generate fake tokens
|
||||
const nTokens = 5
|
||||
tokens := test.GenTokens(nTokens, blocks)
|
||||
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||
err := historyDB.AddTokens(tokens)
|
||||
assert.NoError(t, err)
|
||||
tokens = append([]common.Token{ethToken}, tokens...)
|
||||
// Generate fake batches
|
||||
const nBatches = 10
|
||||
batches := test.GenBatches(nBatches, blocks)
|
||||
@@ -231,9 +234,10 @@ func TestTxs(t *testing.T) {
|
||||
blocks := setTestBlocks(fromBlock, toBlock)
|
||||
// Generate fake tokens
|
||||
const nTokens = 500
|
||||
tokens := test.GenTokens(nTokens, blocks)
|
||||
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||
err := historyDB.AddTokens(tokens)
|
||||
assert.NoError(t, err)
|
||||
tokens = append([]common.Token{ethToken}, tokens...)
|
||||
// Generate fake batches
|
||||
const nBatches = 10
|
||||
batches := test.GenBatches(nBatches, blocks)
|
||||
@@ -365,13 +369,15 @@ func fetchAndAssertTxs(t *testing.T, l1txs []common.L1Tx, l2txs []common.L2Tx) {
|
||||
|
||||
func TestExitTree(t *testing.T) {
|
||||
nBatches := 17
|
||||
blocks := setTestBlocks(0, 10)
|
||||
blocks := setTestBlocks(1, 10)
|
||||
batches := test.GenBatches(nBatches, blocks)
|
||||
err := historyDB.AddBatches(batches)
|
||||
const nTokens = 50
|
||||
tokens := test.GenTokens(nTokens, blocks)
|
||||
assert.NoError(t, historyDB.AddTokens(tokens))
|
||||
assert.NoError(t, err)
|
||||
const nTokens = 50
|
||||
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||
err = historyDB.AddTokens(tokens)
|
||||
assert.NoError(t, err)
|
||||
tokens = append([]common.Token{ethToken}, tokens...)
|
||||
const nAccounts = 3
|
||||
accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
|
||||
assert.NoError(t, historyDB.AddAccounts(accs))
|
||||
@@ -381,7 +387,7 @@ func TestExitTree(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetL1UserTxs(t *testing.T) {
|
||||
require.NoError(t, cleanHistoryDB())
|
||||
test.WipeDB(historyDB.DB())
|
||||
|
||||
set := `
|
||||
Type: Blockchain
|
||||
@@ -425,16 +431,10 @@ func TestGetL1UserTxs(t *testing.T) {
|
||||
|
||||
// setTestBlocks WARNING: this will delete the blocks and recreate them
|
||||
func setTestBlocks(from, to int64) []common.Block {
|
||||
if err := cleanHistoryDB(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
test.WipeDB(historyDB.DB())
|
||||
blocks := test.GenBlocks(from, to)
|
||||
if err := historyDB.AddBlocks(blocks); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return blocks
|
||||
}
|
||||
|
||||
func cleanHistoryDB() error {
|
||||
return historyDB.Reorg(-1)
|
||||
}
|
||||
|
||||
@@ -27,10 +27,8 @@ func TestMain(m *testing.M) {
|
||||
panic(err)
|
||||
}
|
||||
l2DB = NewL2DB(db, 10, 100, 24*time.Hour)
|
||||
test.WipeDB(l2DB.DB())
|
||||
tokens, tokensUSD = prepareHistoryDB(db)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Run tests
|
||||
result := m.Run()
|
||||
// Close DB
|
||||
@@ -44,10 +42,6 @@ func prepareHistoryDB(db *sqlx.DB) ([]common.Token, []historydb.TokenWithUSD) {
|
||||
historyDB := historydb.NewHistoryDB(db)
|
||||
const fromBlock int64 = 1
|
||||
const toBlock int64 = 5
|
||||
// Clean historyDB
|
||||
if err := historyDB.Reorg(-1); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Store blocks to historyDB
|
||||
blocks := test.GenBlocks(fromBlock, toBlock)
|
||||
if err := historyDB.AddBlocks(blocks); err != nil {
|
||||
@@ -55,10 +49,11 @@ func prepareHistoryDB(db *sqlx.DB) ([]common.Token, []historydb.TokenWithUSD) {
|
||||
}
|
||||
// Store tokens to historyDB
|
||||
const nTokens = 5
|
||||
tokens := test.GenTokens(nTokens, blocks)
|
||||
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||
if err := historyDB.AddTokens(tokens); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tokens = append([]common.Token{ethToken}, tokens...)
|
||||
readTokens := []historydb.TokenWithUSD{}
|
||||
for i, token := range tokens {
|
||||
readToken := historydb.TokenWithUSD{
|
||||
@@ -86,7 +81,7 @@ func prepareHistoryDB(db *sqlx.DB) ([]common.Token, []historydb.TokenWithUSD) {
|
||||
func TestAddTxTest(t *testing.T) {
|
||||
// Gen poolTxs
|
||||
const nInserts = 20
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
txs := test.GenPoolTxs(nInserts, tokens)
|
||||
for _, tx := range txs {
|
||||
err := l2DB.AddTxTest(tx)
|
||||
@@ -127,7 +122,7 @@ func assertTx(t *testing.T, expected, actual *common.PoolL2Tx) {
|
||||
|
||||
func BenchmarkAddTxTest(b *testing.B) {
|
||||
const nInserts = 20
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
txs := test.GenPoolTxs(nInserts, tokens)
|
||||
now := time.Now()
|
||||
for _, tx := range txs {
|
||||
@@ -139,7 +134,7 @@ func BenchmarkAddTxTest(b *testing.B) {
|
||||
|
||||
func TestGetPending(t *testing.T) {
|
||||
const nInserts = 20
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
txs := test.GenPoolTxs(nInserts, tokens)
|
||||
var pendingTxs []*common.PoolL2Tx
|
||||
for _, tx := range txs {
|
||||
@@ -163,7 +158,7 @@ func TestStartForging(t *testing.T) {
|
||||
// Generate txs
|
||||
const nInserts = 60
|
||||
const fakeBatchNum common.BatchNum = 33
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
txs := test.GenPoolTxs(nInserts, tokens)
|
||||
var startForgingTxIDs []common.TxID
|
||||
randomizer := 0
|
||||
@@ -195,7 +190,7 @@ func TestDoneForging(t *testing.T) {
|
||||
// Generate txs
|
||||
const nInserts = 60
|
||||
const fakeBatchNum common.BatchNum = 33
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
txs := test.GenPoolTxs(nInserts, tokens)
|
||||
var doneForgingTxIDs []common.TxID
|
||||
randomizer := 0
|
||||
@@ -227,7 +222,7 @@ func TestInvalidate(t *testing.T) {
|
||||
// Generate txs
|
||||
const nInserts = 60
|
||||
const fakeBatchNum common.BatchNum = 33
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
txs := test.GenPoolTxs(nInserts, tokens)
|
||||
var invalidTxIDs []common.TxID
|
||||
randomizer := 0
|
||||
@@ -259,7 +254,7 @@ func TestCheckNonces(t *testing.T) {
|
||||
// Generate txs
|
||||
const nInserts = 60
|
||||
const fakeBatchNum common.BatchNum = 33
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
txs := test.GenPoolTxs(nInserts, tokens)
|
||||
var invalidTxIDs []common.TxID
|
||||
// Generate accounts
|
||||
@@ -304,7 +299,7 @@ func TestReorg(t *testing.T) {
|
||||
const nInserts = 20
|
||||
const lastValidBatch common.BatchNum = 20
|
||||
const reorgBatch common.BatchNum = lastValidBatch + 1
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
txs := test.GenPoolTxs(nInserts, tokens)
|
||||
// Add txs to the DB
|
||||
reorgedTxIDs := []common.TxID{}
|
||||
@@ -346,7 +341,7 @@ func TestPurge(t *testing.T) {
|
||||
WARNING: this should be fixed once transaktio is ready
|
||||
// Generate txs
|
||||
nInserts := l2DB.maxTxs + 20
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
txs := test.GenPoolTxs(int(nInserts), tokens)
|
||||
deletedIDs := []common.TxID{}
|
||||
keepedIDs := []common.TxID{}
|
||||
@@ -408,7 +403,7 @@ func TestPurge(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAuth(t *testing.T) {
|
||||
test.CleanL2DB(l2DB.DB())
|
||||
test.WipeDB(l2DB.DB())
|
||||
const nAuths = 5
|
||||
// Generate authorizations
|
||||
auths := test.GenAuths(nAuths)
|
||||
|
||||
@@ -50,6 +50,33 @@ CREATE TABLE token (
|
||||
usd_update TIMESTAMP WITHOUT TIME ZONE
|
||||
);
|
||||
|
||||
-- Add ETH as TokenID 0
|
||||
INSERT INTO block (
|
||||
eth_block_num,
|
||||
timestamp,
|
||||
hash
|
||||
) VALUES (
|
||||
0,
|
||||
'2015-07-30 03:26:13',
|
||||
'\xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3'
|
||||
); -- info from https://etherscan.io/block/0
|
||||
|
||||
INSERT INTO token (
|
||||
token_id,
|
||||
eth_block_num,
|
||||
eth_addr,
|
||||
name,
|
||||
symbol,
|
||||
decimals
|
||||
) VALUES (
|
||||
0,
|
||||
0,
|
||||
'\x0000000000000000000000000000000000000000',
|
||||
'Ether',
|
||||
'ETH',
|
||||
18
|
||||
);
|
||||
|
||||
|
||||
-- +migrate StatementBegin
|
||||
CREATE FUNCTION hez_idx(BIGINT, VARCHAR)
|
||||
@@ -564,15 +591,30 @@ CREATE TABLE account_creation_auth (
|
||||
);
|
||||
|
||||
-- +migrate Down
|
||||
-- drop triggers
|
||||
DROP TRIGGER trigger_token_usd_update ON token;
|
||||
DROP TRIGGER trigger_set_tx ON tx;
|
||||
DROP TRIGGER trigger_forge_l1_txs ON batch;
|
||||
DROP TRIGGER trigger_set_pool_tx ON tx_pool;
|
||||
-- drop functions
|
||||
DROP FUNCTION hez_idx;
|
||||
DROP FUNCTION set_token_usd_update;
|
||||
DROP FUNCTION fee_percentage;
|
||||
DROP FUNCTION set_tx;
|
||||
DROP FUNCTION forge_l1_user_txs;
|
||||
DROP FUNCTION set_pool_tx;
|
||||
-- drop tables
|
||||
DROP TABLE account_creation_auth;
|
||||
DROP TABLE tx_pool;
|
||||
DROP TABLE consensus_vars;
|
||||
DROP TABLE rollup_vars;
|
||||
DROP TABLE account;
|
||||
DROP TABLE tx;
|
||||
DROP TABLE exit_tree;
|
||||
DROP TABLE account;
|
||||
DROP TABLE token;
|
||||
DROP TABLE bid;
|
||||
DROP TABLE exit_tree;
|
||||
DROP TABLE batch;
|
||||
DROP TABLE coordinator;
|
||||
DROP TABLE block;
|
||||
-- drop sequences
|
||||
DROP SEQUENCE tx_item_id;
|
||||
Reference in New Issue
Block a user