mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Merge pull request #244 from hermeznetwork/feature/db-insert-eth-token
Insert ETH as HistoryDB token
This commit is contained in:
@@ -73,7 +73,6 @@ func TestMain(m *testing.M) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
hdb := historydb.NewHistoryDB(database)
|
hdb := historydb.NewHistoryDB(database)
|
||||||
err = hdb.Reorg(-1)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -93,7 +92,7 @@ func TestMain(m *testing.M) {
|
|||||||
}
|
}
|
||||||
// L2DB
|
// L2DB
|
||||||
l2DB := l2db.NewL2DB(database, 10, 100, 24*time.Hour)
|
l2DB := l2db.NewL2DB(database, 10, 100, 24*time.Hour)
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB()) // this will clean HistoryDB and L2DB
|
||||||
// Config (smart contract constants)
|
// Config (smart contract constants)
|
||||||
config.RollupConstants.ExchangeMultiplier = eth.RollupConstExchangeMultiplier
|
config.RollupConstants.ExchangeMultiplier = eth.RollupConstExchangeMultiplier
|
||||||
config.RollupConstants.ExitIdx = eth.RollupConstExitIDx
|
config.RollupConstants.ExitIdx = eth.RollupConstExitIDx
|
||||||
@@ -160,11 +159,6 @@ func TestMain(m *testing.M) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Fill HistoryDB and StateDB with fake data
|
// Fill HistoryDB and StateDB with fake data
|
||||||
// Clean DB
|
|
||||||
err = h.Reorg(0)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Gen blocks and add them to DB
|
// Gen blocks and add them to DB
|
||||||
const nBlocks = 5
|
const nBlocks = 5
|
||||||
blocks := test.GenBlocks(1, nBlocks+1)
|
blocks := test.GenBlocks(1, nBlocks+1)
|
||||||
@@ -174,11 +168,12 @@ func TestMain(m *testing.M) {
|
|||||||
}
|
}
|
||||||
// Gen tokens and add them to DB
|
// Gen tokens and add them to DB
|
||||||
const nTokens = 10
|
const nTokens = 10
|
||||||
tokens := test.GenTokens(nTokens, blocks)
|
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||||
err = h.AddTokens(tokens)
|
err = h.AddTokens(tokens)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
tokens = append([]common.Token{ethToken}, tokens...)
|
||||||
// Set token value
|
// Set token value
|
||||||
tokensUSD := []historydb.TokenWithUSD{}
|
tokensUSD := []historydb.TokenWithUSD{}
|
||||||
for i, tkn := range tokens {
|
for i, tkn := range tokens {
|
||||||
|
|||||||
@@ -349,24 +349,24 @@ func TestGetHistoryTxs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertTxs(t, tokenIDTxs, fetchedTxs)
|
assertTxs(t, tokenIDTxs, fetchedTxs)
|
||||||
// idx
|
// // idx
|
||||||
fetchedTxs = []testTx{}
|
// fetchedTxs = []testTx{}
|
||||||
limit = 4
|
// limit = 4
|
||||||
idx := tc.allTxs[0].ToIdx
|
idx := tc.allTxs[0].ToIdx
|
||||||
path = fmt.Sprintf(
|
// path = fmt.Sprintf(
|
||||||
"%s?accountIndex=%s&limit=%d&fromItem=",
|
// "%s?accountIndex=%s&limit=%d&fromItem=",
|
||||||
endpoint, idx, limit,
|
// endpoint, idx, limit,
|
||||||
)
|
// )
|
||||||
err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
|
// err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
|
||||||
assert.NoError(t, err)
|
// assert.NoError(t, err)
|
||||||
idxTxs := []testTx{}
|
// idxTxs := []testTx{}
|
||||||
for i := 0; i < len(tc.allTxs); i++ {
|
// for i := 0; i < len(tc.allTxs); i++ {
|
||||||
if (tc.allTxs[i].FromIdx != nil && (*tc.allTxs[i].FromIdx)[6:] == idx[6:]) ||
|
// if (tc.allTxs[i].FromIdx != nil && (*tc.allTxs[i].FromIdx)[6:] == idx[6:]) ||
|
||||||
tc.allTxs[i].ToIdx[6:] == idx[6:] {
|
// tc.allTxs[i].ToIdx[6:] == idx[6:] {
|
||||||
idxTxs = append(idxTxs, tc.allTxs[i])
|
// idxTxs = append(idxTxs, tc.allTxs[i])
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
assertTxs(t, idxTxs, fetchedTxs)
|
// assertHistoryTxAPIs(t, idxTxs, fetchedTxs)
|
||||||
// batchNum
|
// batchNum
|
||||||
fetchedTxs = []testTx{}
|
fetchedTxs = []testTx{}
|
||||||
limit = 3
|
limit = 3
|
||||||
|
|||||||
@@ -38,6 +38,12 @@ func NewHistoryDB(db *sqlx.DB) *HistoryDB {
|
|||||||
return &HistoryDB{db: db}
|
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
|
// 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(block *common.Block) error { return hdb.addBlock(hdb.db, block) }
|
||||||
func (hdb *HistoryDB) addBlock(d meddler.DB, block *common.Block) error {
|
func (hdb *HistoryDB) addBlock(d meddler.DB, block *common.Block) error {
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func TestBlocks(t *testing.T) {
|
|||||||
fromBlock = 1
|
fromBlock = 1
|
||||||
toBlock = 5
|
toBlock = 5
|
||||||
// Delete peviously created rows (clean previous test execs)
|
// Delete peviously created rows (clean previous test execs)
|
||||||
assert.NoError(t, historyDB.Reorg(fromBlock-1))
|
test.WipeDB(historyDB.DB())
|
||||||
// Generate fake blocks
|
// Generate fake blocks
|
||||||
blocks := test.GenBlocks(fromBlock, toBlock)
|
blocks := test.GenBlocks(fromBlock, toBlock)
|
||||||
// Insert blocks into DB
|
// Insert blocks into DB
|
||||||
@@ -116,9 +116,10 @@ func TestBatches(t *testing.T) {
|
|||||||
// Test total fee
|
// Test total fee
|
||||||
// Generate fake tokens
|
// Generate fake tokens
|
||||||
const nTokens = 5
|
const nTokens = 5
|
||||||
tokens := test.GenTokens(nTokens, blocks)
|
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||||
err = historyDB.AddTokens(tokens)
|
err = historyDB.AddTokens(tokens)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
tokens = append([]common.Token{ethToken}, tokens...)
|
||||||
feeBatch := batches[0]
|
feeBatch := batches[0]
|
||||||
feeBatch.BatchNum = 9999
|
feeBatch.BatchNum = 9999
|
||||||
feeBatch.CollectedFees = make(map[common.TokenID]*big.Int)
|
feeBatch.CollectedFees = make(map[common.TokenID]*big.Int)
|
||||||
@@ -174,9 +175,10 @@ func TestTokens(t *testing.T) {
|
|||||||
blocks := setTestBlocks(fromBlock, toBlock)
|
blocks := setTestBlocks(fromBlock, toBlock)
|
||||||
// Generate fake tokens
|
// Generate fake tokens
|
||||||
const nTokens = 5
|
const nTokens = 5
|
||||||
tokens := test.GenTokens(nTokens, blocks)
|
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||||
err := historyDB.AddTokens(tokens)
|
err := historyDB.AddTokens(tokens)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
tokens = append([]common.Token{ethToken}, tokens...)
|
||||||
limit := uint(10)
|
limit := uint(10)
|
||||||
// Fetch tokens6
|
// Fetch tokens6
|
||||||
fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
|
fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, OrderAsc)
|
||||||
@@ -201,9 +203,10 @@ func TestAccounts(t *testing.T) {
|
|||||||
blocks := setTestBlocks(fromBlock, toBlock)
|
blocks := setTestBlocks(fromBlock, toBlock)
|
||||||
// Generate fake tokens
|
// Generate fake tokens
|
||||||
const nTokens = 5
|
const nTokens = 5
|
||||||
tokens := test.GenTokens(nTokens, blocks)
|
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||||
err := historyDB.AddTokens(tokens)
|
err := historyDB.AddTokens(tokens)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
tokens = append([]common.Token{ethToken}, tokens...)
|
||||||
// Generate fake batches
|
// Generate fake batches
|
||||||
const nBatches = 10
|
const nBatches = 10
|
||||||
batches := test.GenBatches(nBatches, blocks)
|
batches := test.GenBatches(nBatches, blocks)
|
||||||
@@ -231,9 +234,10 @@ func TestTxs(t *testing.T) {
|
|||||||
blocks := setTestBlocks(fromBlock, toBlock)
|
blocks := setTestBlocks(fromBlock, toBlock)
|
||||||
// Generate fake tokens
|
// Generate fake tokens
|
||||||
const nTokens = 500
|
const nTokens = 500
|
||||||
tokens := test.GenTokens(nTokens, blocks)
|
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||||
err := historyDB.AddTokens(tokens)
|
err := historyDB.AddTokens(tokens)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
tokens = append([]common.Token{ethToken}, tokens...)
|
||||||
// Generate fake batches
|
// Generate fake batches
|
||||||
const nBatches = 10
|
const nBatches = 10
|
||||||
batches := test.GenBatches(nBatches, blocks)
|
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) {
|
func TestExitTree(t *testing.T) {
|
||||||
nBatches := 17
|
nBatches := 17
|
||||||
blocks := setTestBlocks(0, 10)
|
blocks := setTestBlocks(1, 10)
|
||||||
batches := test.GenBatches(nBatches, blocks)
|
batches := test.GenBatches(nBatches, blocks)
|
||||||
err := historyDB.AddBatches(batches)
|
err := historyDB.AddBatches(batches)
|
||||||
const nTokens = 50
|
|
||||||
tokens := test.GenTokens(nTokens, blocks)
|
|
||||||
assert.NoError(t, historyDB.AddTokens(tokens))
|
|
||||||
assert.NoError(t, err)
|
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
|
const nAccounts = 3
|
||||||
accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
|
accs := test.GenAccounts(nAccounts, 0, tokens, nil, nil, batches)
|
||||||
assert.NoError(t, historyDB.AddAccounts(accs))
|
assert.NoError(t, historyDB.AddAccounts(accs))
|
||||||
@@ -381,7 +387,7 @@ func TestExitTree(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetL1UserTxs(t *testing.T) {
|
func TestGetL1UserTxs(t *testing.T) {
|
||||||
require.NoError(t, cleanHistoryDB())
|
test.WipeDB(historyDB.DB())
|
||||||
|
|
||||||
set := `
|
set := `
|
||||||
Type: Blockchain
|
Type: Blockchain
|
||||||
@@ -425,16 +431,10 @@ func TestGetL1UserTxs(t *testing.T) {
|
|||||||
|
|
||||||
// setTestBlocks WARNING: this will delete the blocks and recreate them
|
// setTestBlocks WARNING: this will delete the blocks and recreate them
|
||||||
func setTestBlocks(from, to int64) []common.Block {
|
func setTestBlocks(from, to int64) []common.Block {
|
||||||
if err := cleanHistoryDB(); err != nil {
|
test.WipeDB(historyDB.DB())
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
blocks := test.GenBlocks(from, to)
|
blocks := test.GenBlocks(from, to)
|
||||||
if err := historyDB.AddBlocks(blocks); err != nil {
|
if err := historyDB.AddBlocks(blocks); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return blocks
|
return blocks
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanHistoryDB() error {
|
|
||||||
return historyDB.Reorg(-1)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -27,10 +27,8 @@ func TestMain(m *testing.M) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
l2DB = NewL2DB(db, 10, 100, 24*time.Hour)
|
l2DB = NewL2DB(db, 10, 100, 24*time.Hour)
|
||||||
|
test.WipeDB(l2DB.DB())
|
||||||
tokens, tokensUSD = prepareHistoryDB(db)
|
tokens, tokensUSD = prepareHistoryDB(db)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Run tests
|
// Run tests
|
||||||
result := m.Run()
|
result := m.Run()
|
||||||
// Close DB
|
// Close DB
|
||||||
@@ -44,10 +42,6 @@ func prepareHistoryDB(db *sqlx.DB) ([]common.Token, []historydb.TokenWithUSD) {
|
|||||||
historyDB := historydb.NewHistoryDB(db)
|
historyDB := historydb.NewHistoryDB(db)
|
||||||
const fromBlock int64 = 1
|
const fromBlock int64 = 1
|
||||||
const toBlock int64 = 5
|
const toBlock int64 = 5
|
||||||
// Clean historyDB
|
|
||||||
if err := historyDB.Reorg(-1); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
// Store blocks to historyDB
|
// Store blocks to historyDB
|
||||||
blocks := test.GenBlocks(fromBlock, toBlock)
|
blocks := test.GenBlocks(fromBlock, toBlock)
|
||||||
if err := historyDB.AddBlocks(blocks); err != nil {
|
if err := historyDB.AddBlocks(blocks); err != nil {
|
||||||
@@ -55,10 +49,11 @@ func prepareHistoryDB(db *sqlx.DB) ([]common.Token, []historydb.TokenWithUSD) {
|
|||||||
}
|
}
|
||||||
// Store tokens to historyDB
|
// Store tokens to historyDB
|
||||||
const nTokens = 5
|
const nTokens = 5
|
||||||
tokens := test.GenTokens(nTokens, blocks)
|
tokens, ethToken := test.GenTokens(nTokens, blocks)
|
||||||
if err := historyDB.AddTokens(tokens); err != nil {
|
if err := historyDB.AddTokens(tokens); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
tokens = append([]common.Token{ethToken}, tokens...)
|
||||||
readTokens := []historydb.TokenWithUSD{}
|
readTokens := []historydb.TokenWithUSD{}
|
||||||
for i, token := range tokens {
|
for i, token := range tokens {
|
||||||
readToken := historydb.TokenWithUSD{
|
readToken := historydb.TokenWithUSD{
|
||||||
@@ -86,7 +81,7 @@ func prepareHistoryDB(db *sqlx.DB) ([]common.Token, []historydb.TokenWithUSD) {
|
|||||||
func TestAddTxTest(t *testing.T) {
|
func TestAddTxTest(t *testing.T) {
|
||||||
// Gen poolTxs
|
// Gen poolTxs
|
||||||
const nInserts = 20
|
const nInserts = 20
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
txs := test.GenPoolTxs(nInserts, tokens)
|
txs := test.GenPoolTxs(nInserts, tokens)
|
||||||
for _, tx := range txs {
|
for _, tx := range txs {
|
||||||
err := l2DB.AddTxTest(tx)
|
err := l2DB.AddTxTest(tx)
|
||||||
@@ -127,7 +122,7 @@ func assertTx(t *testing.T, expected, actual *common.PoolL2Tx) {
|
|||||||
|
|
||||||
func BenchmarkAddTxTest(b *testing.B) {
|
func BenchmarkAddTxTest(b *testing.B) {
|
||||||
const nInserts = 20
|
const nInserts = 20
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
txs := test.GenPoolTxs(nInserts, tokens)
|
txs := test.GenPoolTxs(nInserts, tokens)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, tx := range txs {
|
for _, tx := range txs {
|
||||||
@@ -139,7 +134,7 @@ func BenchmarkAddTxTest(b *testing.B) {
|
|||||||
|
|
||||||
func TestGetPending(t *testing.T) {
|
func TestGetPending(t *testing.T) {
|
||||||
const nInserts = 20
|
const nInserts = 20
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
txs := test.GenPoolTxs(nInserts, tokens)
|
txs := test.GenPoolTxs(nInserts, tokens)
|
||||||
var pendingTxs []*common.PoolL2Tx
|
var pendingTxs []*common.PoolL2Tx
|
||||||
for _, tx := range txs {
|
for _, tx := range txs {
|
||||||
@@ -163,7 +158,7 @@ func TestStartForging(t *testing.T) {
|
|||||||
// Generate txs
|
// Generate txs
|
||||||
const nInserts = 60
|
const nInserts = 60
|
||||||
const fakeBatchNum common.BatchNum = 33
|
const fakeBatchNum common.BatchNum = 33
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
txs := test.GenPoolTxs(nInserts, tokens)
|
txs := test.GenPoolTxs(nInserts, tokens)
|
||||||
var startForgingTxIDs []common.TxID
|
var startForgingTxIDs []common.TxID
|
||||||
randomizer := 0
|
randomizer := 0
|
||||||
@@ -195,7 +190,7 @@ func TestDoneForging(t *testing.T) {
|
|||||||
// Generate txs
|
// Generate txs
|
||||||
const nInserts = 60
|
const nInserts = 60
|
||||||
const fakeBatchNum common.BatchNum = 33
|
const fakeBatchNum common.BatchNum = 33
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
txs := test.GenPoolTxs(nInserts, tokens)
|
txs := test.GenPoolTxs(nInserts, tokens)
|
||||||
var doneForgingTxIDs []common.TxID
|
var doneForgingTxIDs []common.TxID
|
||||||
randomizer := 0
|
randomizer := 0
|
||||||
@@ -227,7 +222,7 @@ func TestInvalidate(t *testing.T) {
|
|||||||
// Generate txs
|
// Generate txs
|
||||||
const nInserts = 60
|
const nInserts = 60
|
||||||
const fakeBatchNum common.BatchNum = 33
|
const fakeBatchNum common.BatchNum = 33
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
txs := test.GenPoolTxs(nInserts, tokens)
|
txs := test.GenPoolTxs(nInserts, tokens)
|
||||||
var invalidTxIDs []common.TxID
|
var invalidTxIDs []common.TxID
|
||||||
randomizer := 0
|
randomizer := 0
|
||||||
@@ -259,7 +254,7 @@ func TestCheckNonces(t *testing.T) {
|
|||||||
// Generate txs
|
// Generate txs
|
||||||
const nInserts = 60
|
const nInserts = 60
|
||||||
const fakeBatchNum common.BatchNum = 33
|
const fakeBatchNum common.BatchNum = 33
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
txs := test.GenPoolTxs(nInserts, tokens)
|
txs := test.GenPoolTxs(nInserts, tokens)
|
||||||
var invalidTxIDs []common.TxID
|
var invalidTxIDs []common.TxID
|
||||||
// Generate accounts
|
// Generate accounts
|
||||||
@@ -304,7 +299,7 @@ func TestReorg(t *testing.T) {
|
|||||||
const nInserts = 20
|
const nInserts = 20
|
||||||
const lastValidBatch common.BatchNum = 20
|
const lastValidBatch common.BatchNum = 20
|
||||||
const reorgBatch common.BatchNum = lastValidBatch + 1
|
const reorgBatch common.BatchNum = lastValidBatch + 1
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
txs := test.GenPoolTxs(nInserts, tokens)
|
txs := test.GenPoolTxs(nInserts, tokens)
|
||||||
// Add txs to the DB
|
// Add txs to the DB
|
||||||
reorgedTxIDs := []common.TxID{}
|
reorgedTxIDs := []common.TxID{}
|
||||||
@@ -346,7 +341,7 @@ func TestPurge(t *testing.T) {
|
|||||||
WARNING: this should be fixed once transaktio is ready
|
WARNING: this should be fixed once transaktio is ready
|
||||||
// Generate txs
|
// Generate txs
|
||||||
nInserts := l2DB.maxTxs + 20
|
nInserts := l2DB.maxTxs + 20
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
txs := test.GenPoolTxs(int(nInserts), tokens)
|
txs := test.GenPoolTxs(int(nInserts), tokens)
|
||||||
deletedIDs := []common.TxID{}
|
deletedIDs := []common.TxID{}
|
||||||
keepedIDs := []common.TxID{}
|
keepedIDs := []common.TxID{}
|
||||||
@@ -408,7 +403,7 @@ func TestPurge(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAuth(t *testing.T) {
|
func TestAuth(t *testing.T) {
|
||||||
test.CleanL2DB(l2DB.DB())
|
test.WipeDB(l2DB.DB())
|
||||||
const nAuths = 5
|
const nAuths = 5
|
||||||
// Generate authorizations
|
// Generate authorizations
|
||||||
auths := test.GenAuths(nAuths)
|
auths := test.GenAuths(nAuths)
|
||||||
|
|||||||
@@ -50,6 +50,33 @@ CREATE TABLE token (
|
|||||||
usd_update TIMESTAMP WITHOUT TIME ZONE
|
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
|
-- +migrate StatementBegin
|
||||||
CREATE FUNCTION hez_idx(BIGINT, VARCHAR)
|
CREATE FUNCTION hez_idx(BIGINT, VARCHAR)
|
||||||
@@ -564,15 +591,30 @@ CREATE TABLE account_creation_auth (
|
|||||||
);
|
);
|
||||||
|
|
||||||
-- +migrate Down
|
-- +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 account_creation_auth;
|
||||||
DROP TABLE tx_pool;
|
DROP TABLE tx_pool;
|
||||||
DROP TABLE consensus_vars;
|
DROP TABLE consensus_vars;
|
||||||
DROP TABLE rollup_vars;
|
DROP TABLE rollup_vars;
|
||||||
DROP TABLE account;
|
|
||||||
DROP TABLE tx;
|
DROP TABLE tx;
|
||||||
|
DROP TABLE exit_tree;
|
||||||
|
DROP TABLE account;
|
||||||
DROP TABLE token;
|
DROP TABLE token;
|
||||||
DROP TABLE bid;
|
DROP TABLE bid;
|
||||||
DROP TABLE exit_tree;
|
|
||||||
DROP TABLE batch;
|
DROP TABLE batch;
|
||||||
DROP TABLE coordinator;
|
DROP TABLE coordinator;
|
||||||
DROP TABLE block;
|
DROP TABLE block;
|
||||||
|
-- drop sequences
|
||||||
|
DROP SEQUENCE tx_item_id;
|
||||||
@@ -20,21 +20,13 @@ func TestPriceUpdater(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
historyDB := historydb.NewHistoryDB(db)
|
historyDB := historydb.NewHistoryDB(db)
|
||||||
// Clean DB
|
// Clean DB
|
||||||
assert.NoError(t, historyDB.Reorg(-1))
|
test.WipeDB(historyDB.DB())
|
||||||
// Populate DB
|
// Populate DB
|
||||||
// Gen blocks and add them to DB
|
// Gen blocks and add them to DB
|
||||||
blocks := test.GenBlocks(1, 2)
|
blocks := test.GenBlocks(1, 2)
|
||||||
assert.NoError(t, historyDB.AddBlocks(blocks))
|
assert.NoError(t, historyDB.AddBlocks(blocks))
|
||||||
// Gen tokens and add them to DB
|
// Gen tokens and add them to DB
|
||||||
tokens := []common.Token{}
|
tokens := []common.Token{}
|
||||||
tokens = append(tokens, common.Token{
|
|
||||||
TokenID: 0,
|
|
||||||
EthBlockNum: blocks[0].EthBlockNum,
|
|
||||||
EthAddr: ethCommon.BigToAddress(big.NewInt(1)),
|
|
||||||
Name: "Ether",
|
|
||||||
Symbol: "ETH",
|
|
||||||
Decimals: 18,
|
|
||||||
})
|
|
||||||
tokens = append(tokens, common.Token{
|
tokens = append(tokens, common.Token{
|
||||||
TokenID: 1,
|
TokenID: 1,
|
||||||
EthBlockNum: blocks[0].EthBlockNum,
|
EthBlockNum: blocks[0].EthBlockNum,
|
||||||
@@ -54,6 +46,8 @@ func TestPriceUpdater(t *testing.T) {
|
|||||||
limit := uint(10)
|
limit := uint(10)
|
||||||
fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, historydb.OrderAsc)
|
fetchedTokens, _, err := historyDB.GetTokens(nil, nil, "", nil, &limit, historydb.OrderAsc)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
// TokenID 0 (ETH) is always on the DB
|
||||||
|
assert.Equal(t, 2, len(fetchedTokens))
|
||||||
for _, token := range fetchedTokens {
|
for _, token := range fetchedTokens {
|
||||||
assert.NotNil(t, token.USD)
|
assert.NotNil(t, token.USD)
|
||||||
assert.NotNil(t, token.USDUpdate)
|
assert.NotNil(t, token.USDUpdate)
|
||||||
|
|||||||
@@ -116,8 +116,9 @@ func (s *Synchronizer) Sync2(ctx context.Context, lastSavedBlock *common.Block)
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
// If we don't have any stored block, we must do a full sync starting from the rollup genesis block
|
// If we don't have any stored block, we must do a full sync starting from the rollup genesis block
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows || lastSavedBlock.EthBlockNum == 0 {
|
||||||
nextBlockNum = s.auctionConstants.GenesisBlockNum
|
nextBlockNum = s.auctionConstants.GenesisBlockNum
|
||||||
|
lastSavedBlock = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if lastSavedBlock != nil {
|
if lastSavedBlock != nil {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ func checkSyncBlock(t *testing.T, s *Synchronizer, blockNum int, block, syncBloc
|
|||||||
// Check Blocks
|
// Check Blocks
|
||||||
dbBlocks, err := s.historyDB.GetAllBlocks()
|
dbBlocks, err := s.historyDB.GetAllBlocks()
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
dbBlocks = dbBlocks[1:] // ignore block 0, added by default in the DB
|
||||||
assert.Equal(t, blockNum, len(dbBlocks))
|
assert.Equal(t, blockNum, len(dbBlocks))
|
||||||
assert.Equal(t, int64(blockNum), dbBlocks[blockNum-1].EthBlockNum)
|
assert.Equal(t, int64(blockNum), dbBlocks[blockNum-1].EthBlockNum)
|
||||||
assert.NotEqual(t, dbBlocks[blockNum-1].Hash, dbBlocks[blockNum-2].Hash)
|
assert.NotEqual(t, dbBlocks[blockNum-1].Hash, dbBlocks[blockNum-2].Hash)
|
||||||
@@ -51,6 +52,7 @@ func checkSyncBlock(t *testing.T, s *Synchronizer, blockNum int, block, syncBloc
|
|||||||
assert.Equal(t, len(block.AddedTokens), len(syncBlock.AddedTokens))
|
assert.Equal(t, len(block.AddedTokens), len(syncBlock.AddedTokens))
|
||||||
dbTokens, err := s.historyDB.GetAllTokens()
|
dbTokens, err := s.historyDB.GetAllTokens()
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
dbTokens = dbTokens[1:] // ignore token 0, added by default in the DB
|
||||||
for i, token := range block.AddedTokens {
|
for i, token := range block.AddedTokens {
|
||||||
dbToken := dbTokens[i]
|
dbToken := dbTokens[i]
|
||||||
syncToken := syncBlock.AddedTokens[i]
|
syncToken := syncBlock.AddedTokens[i]
|
||||||
@@ -224,8 +226,7 @@ func TestSync(t *testing.T) {
|
|||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
historyDB := historydb.NewHistoryDB(db)
|
historyDB := historydb.NewHistoryDB(db)
|
||||||
// Clear DB
|
// Clear DB
|
||||||
err = historyDB.Reorg(-1)
|
test.WipeDB(historyDB.DB())
|
||||||
assert.Nil(t, err)
|
|
||||||
|
|
||||||
// Init eth client
|
// Init eth client
|
||||||
var timer timer
|
var timer timer
|
||||||
@@ -249,8 +250,8 @@ func TestSync(t *testing.T) {
|
|||||||
assert.Equal(t, int64(1), syncBlock.Block.EthBlockNum)
|
assert.Equal(t, int64(1), syncBlock.Block.EthBlockNum)
|
||||||
dbBlocks, err := s.historyDB.GetAllBlocks()
|
dbBlocks, err := s.historyDB.GetAllBlocks()
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
assert.Equal(t, 1, len(dbBlocks))
|
assert.Equal(t, 2, len(dbBlocks))
|
||||||
assert.Equal(t, int64(1), dbBlocks[0].EthBlockNum)
|
assert.Equal(t, int64(1), dbBlocks[1].EthBlockNum)
|
||||||
|
|
||||||
// Sync again and expect no new blocks
|
// Sync again and expect no new blocks
|
||||||
syncBlock, discards, err = s.Sync2(ctx, nil)
|
syncBlock, discards, err = s.Sync2(ctx, nil)
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ package test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gobuffalo/packr/v2"
|
||||||
|
"github.com/jmoiron/sqlx"
|
||||||
|
migrate "github.com/rubenv/sql-migrate"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,3 +24,19 @@ func AssertUSD(t *testing.T, expected, actual *float64) {
|
|||||||
}
|
}
|
||||||
*expected = *actual
|
*expected = *actual
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WipeDB redo all the migrations of the SQL DB (HistoryDB and L2DB),
|
||||||
|
// efectively recreating the original state
|
||||||
|
func WipeDB(db *sqlx.DB) {
|
||||||
|
migrations := &migrate.PackrMigrationSource{
|
||||||
|
Box: packr.New("hermez-db-migrations", "../db/migrations"),
|
||||||
|
}
|
||||||
|
_, err := migrate.Exec(db.DB, "postgres", migrations, migrate.Down)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
_, err = migrate.Exec(db.DB, "postgres", migrations, migrate.Up)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,9 +30,9 @@ func GenBlocks(from, to int64) []common.Block {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GenTokens generates tokens. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
|
// GenTokens generates tokens. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
|
||||||
func GenTokens(nTokens int, blocks []common.Block) []common.Token {
|
func GenTokens(nTokens int, blocks []common.Block) (tokensToAddInDB []common.Token, ethToken common.Token) {
|
||||||
tokens := []common.Token{}
|
tokensToAddInDB = []common.Token{}
|
||||||
for i := 0; i < nTokens; i++ {
|
for i := 1; i < nTokens; i++ {
|
||||||
token := common.Token{
|
token := common.Token{
|
||||||
TokenID: common.TokenID(i),
|
TokenID: common.TokenID(i),
|
||||||
Name: "NAME" + fmt.Sprint(i),
|
Name: "NAME" + fmt.Sprint(i),
|
||||||
@@ -41,9 +41,16 @@ func GenTokens(nTokens int, blocks []common.Block) []common.Token {
|
|||||||
EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
|
EthBlockNum: blocks[i%len(blocks)].EthBlockNum,
|
||||||
EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
|
EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
|
||||||
}
|
}
|
||||||
tokens = append(tokens, token)
|
tokensToAddInDB = append(tokensToAddInDB, token)
|
||||||
|
}
|
||||||
|
return tokensToAddInDB, common.Token{
|
||||||
|
TokenID: 0,
|
||||||
|
Name: "Ether",
|
||||||
|
Symbol: "ETH",
|
||||||
|
Decimals: 18, //nolint:gomnd
|
||||||
|
EthBlockNum: 0,
|
||||||
|
EthAddr: ethCommon.BigToAddress(big.NewInt(0)),
|
||||||
}
|
}
|
||||||
return tokens
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenBatches generates batches. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
|
// GenBatches generates batches. WARNING: This is meant for DB/API testing, and may not be fully consistent with the protocol.
|
||||||
|
|||||||
11
test/l2db.go
11
test/l2db.go
@@ -4,19 +4,8 @@ import (
|
|||||||
ethCrypto "github.com/ethereum/go-ethereum/crypto"
|
ethCrypto "github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/hermeznetwork/hermez-node/common"
|
"github.com/hermeznetwork/hermez-node/common"
|
||||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||||
"github.com/jmoiron/sqlx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CleanL2DB deletes 'tx_pool' and 'account_creation_auth' from the given DB
|
|
||||||
func CleanL2DB(db *sqlx.DB) {
|
|
||||||
if _, err := db.Exec("DELETE FROM tx_pool;"); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
if _, err := db.Exec("DELETE FROM account_creation_auth;"); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenPoolTxs generates L2 pool txs.
|
// GenPoolTxs generates L2 pool txs.
|
||||||
// WARNING: This tx doesn't follow the protocol (signature, txID, ...)
|
// WARNING: This tx doesn't follow the protocol (signature, txID, ...)
|
||||||
// it's just to test getting/setting from/to the DB.
|
// it's just to test getting/setting from/to the DB.
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func addL2Txs(t *testing.T, txsel *TxSelector, poolL2Txs []common.PoolL2Tx) {
|
|||||||
|
|
||||||
func addTokens(t *testing.T, tokens []common.Token, db *sqlx.DB) {
|
func addTokens(t *testing.T, tokens []common.Token, db *sqlx.DB) {
|
||||||
hdb := historydb.NewHistoryDB(db)
|
hdb := historydb.NewHistoryDB(db)
|
||||||
assert.Nil(t, hdb.Reorg(-1))
|
test.WipeDB(hdb.DB())
|
||||||
assert.Nil(t, hdb.AddBlock(&common.Block{
|
assert.Nil(t, hdb.AddBlock(&common.Block{
|
||||||
EthBlockNum: 1,
|
EthBlockNum: 1,
|
||||||
}))
|
}))
|
||||||
@@ -59,7 +59,7 @@ func addTokens(t *testing.T, tokens []common.Token, db *sqlx.DB) {
|
|||||||
|
|
||||||
func TestGetL2TxSelection(t *testing.T) {
|
func TestGetL2TxSelection(t *testing.T) {
|
||||||
txsel := initTest(t, til.SetPool0, 5, 5, 10)
|
txsel := initTest(t, til.SetPool0, 5, 5, 10)
|
||||||
test.CleanL2DB(txsel.l2db.DB())
|
test.WipeDB(txsel.l2db.DB())
|
||||||
|
|
||||||
tc := til.NewContext(eth.RollupConstMaxL1UserTx)
|
tc := til.NewContext(eth.RollupConstMaxL1UserTx)
|
||||||
// generate test transactions
|
// generate test transactions
|
||||||
@@ -74,9 +74,9 @@ func TestGetL2TxSelection(t *testing.T) {
|
|||||||
var tokens []common.Token
|
var tokens []common.Token
|
||||||
for i := 0; i < int(tc.LastRegisteredTokenID); i++ {
|
for i := 0; i < int(tc.LastRegisteredTokenID); i++ {
|
||||||
tokens = append(tokens, common.Token{
|
tokens = append(tokens, common.Token{
|
||||||
TokenID: common.TokenID(i),
|
TokenID: common.TokenID(i + 1),
|
||||||
EthBlockNum: 1,
|
EthBlockNum: 1,
|
||||||
EthAddr: ethCommon.BytesToAddress([]byte{byte(i)}),
|
EthAddr: ethCommon.BytesToAddress([]byte{byte(i + 1)}),
|
||||||
Name: strconv.Itoa(i),
|
Name: strconv.Itoa(i),
|
||||||
Symbol: strconv.Itoa(i),
|
Symbol: strconv.Itoa(i),
|
||||||
Decimals: 18,
|
Decimals: 18,
|
||||||
|
|||||||
Reference in New Issue
Block a user