mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
Feature/merge history l2 tables (#156)
* WIP rebase * Combine both SQL DBs * API and DB refactor
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package historydb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -9,6 +8,8 @@ import (
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
dbUtils "github.com/hermeznetwork/hermez-node/db"
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/hermeznetwork/hermez-node/test"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -26,17 +27,20 @@ var historyDB *HistoryDB
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// init DB
|
||||
var err error
|
||||
pass := os.Getenv("POSTGRES_PASS")
|
||||
historyDB, err = NewHistoryDB(5432, "localhost", "hermez", pass, "history")
|
||||
db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
historyDB = NewHistoryDB(db)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Run tests
|
||||
result := m.Run()
|
||||
// Close DB
|
||||
if err := historyDB.Close(); err != nil {
|
||||
fmt.Println("Error closing the history DB:", err)
|
||||
if err := db.Close(); err != nil {
|
||||
log.Error("Error closing the history DB:", err)
|
||||
}
|
||||
os.Exit(result)
|
||||
}
|
||||
@@ -145,15 +149,6 @@ func TestTokens(t *testing.T) {
|
||||
tokens := test.GenTokens(nTokens, blocks)
|
||||
err := historyDB.AddTokens(tokens)
|
||||
assert.NoError(t, err)
|
||||
// Update price of generated tokens without price
|
||||
for i := 0; i < len(tokens); i++ {
|
||||
if tokens[i].USD == 0 {
|
||||
value := 3.33 + float64(i)
|
||||
tokens[i].USD = value
|
||||
err := historyDB.UpdateTokenValue(tokens[i].TokenID, value)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
}
|
||||
// Fetch tokens
|
||||
fetchedTokens, err := historyDB.GetTokens()
|
||||
assert.NoError(t, err)
|
||||
@@ -166,7 +161,11 @@ func TestTokens(t *testing.T) {
|
||||
assert.Equal(t, tokens[i].Name, token.Name)
|
||||
assert.Equal(t, tokens[i].Symbol, token.Symbol)
|
||||
assert.Equal(t, tokens[i].USD, token.USD)
|
||||
assert.Greater(t, int64(1*time.Second), int64(time.Since(token.USDUpdate)))
|
||||
if token.USDUpdate != nil {
|
||||
assert.Greater(t, int64(1*time.Second), int64(time.Since(*token.USDUpdate)))
|
||||
} else {
|
||||
assert.Equal(t, tokens[i].USDUpdate, token.USDUpdate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,12 +204,8 @@ func TestTxs(t *testing.T) {
|
||||
// Prepare blocks in the DB
|
||||
blocks := setTestBlocks(fromBlock, toBlock)
|
||||
// Generate fake tokens
|
||||
const nTokens = 5
|
||||
const tokenValue = 1.23456
|
||||
const nTokens = 500
|
||||
tokens := test.GenTokens(nTokens, blocks)
|
||||
for i := 0; i < len(tokens); i++ {
|
||||
tokens[i].USD = tokenValue
|
||||
}
|
||||
err := historyDB.AddTokens(tokens)
|
||||
assert.NoError(t, err)
|
||||
// Generate fake batches
|
||||
@@ -224,60 +219,17 @@ func TestTxs(t *testing.T) {
|
||||
err = historyDB.AddAccounts(accs)
|
||||
assert.NoError(t, err)
|
||||
// Generate fake L1 txs
|
||||
const nL1s = 30
|
||||
const nL1s = 64
|
||||
_, l1txs := test.GenL1Txs(0, nL1s, 0, nil, accs, tokens, blocks, batches)
|
||||
err = historyDB.AddL1Txs(l1txs)
|
||||
assert.NoError(t, err)
|
||||
// Generate fake L2 txs
|
||||
const nL2s = 20
|
||||
const nL2s = 2048 - nL1s
|
||||
_, l2txs := test.GenL2Txs(0, nL2s, 0, nil, accs, tokens, blocks, batches)
|
||||
err = historyDB.AddL2Txs(l2txs)
|
||||
assert.NoError(t, err)
|
||||
// Compare fetched txs vs generated txs.
|
||||
for i := 0; i < len(l1txs); i++ {
|
||||
tx := l1txs[i].Tx()
|
||||
fetchedTx, err := historyDB.GetTx(tx.TxID)
|
||||
assert.NoError(t, err)
|
||||
tx.USD = tokenValue * tx.AmountFloat
|
||||
if fetchedTx.USD > tx.USD {
|
||||
assert.Less(t, 0.999, tx.USD/fetchedTx.USD)
|
||||
} else {
|
||||
assert.Less(t, 0.999, fetchedTx.USD/tx.USD)
|
||||
}
|
||||
tx.LoadAmountUSD = tokenValue * tx.LoadAmountFloat
|
||||
if fetchedTx.LoadAmountUSD > tx.LoadAmountUSD {
|
||||
assert.Less(t, 0.999, tx.LoadAmountUSD/fetchedTx.LoadAmountUSD)
|
||||
} else {
|
||||
assert.Less(t, 0.999, fetchedTx.LoadAmountUSD/tx.LoadAmountUSD)
|
||||
}
|
||||
tx.LoadAmountUSD = 0
|
||||
tx.USD = 0
|
||||
fetchedTx.LoadAmountUSD = 0
|
||||
fetchedTx.USD = 0
|
||||
assert.Equal(t, tx, fetchedTx)
|
||||
}
|
||||
for i := 0; i < len(l2txs); i++ {
|
||||
tx := l2txs[i].Tx()
|
||||
fetchedTx, err := historyDB.GetTx(tx.TxID)
|
||||
assert.NoError(t, err)
|
||||
tx.USD = tokenValue * tx.AmountFloat
|
||||
if fetchedTx.USD > tx.USD {
|
||||
assert.Less(t, 0.999, tx.USD/fetchedTx.USD)
|
||||
} else {
|
||||
assert.Less(t, 0.999, fetchedTx.USD/tx.USD)
|
||||
}
|
||||
tx.FeeUSD = tx.USD * tx.Fee.Percentage()
|
||||
if fetchedTx.FeeUSD > tx.FeeUSD {
|
||||
assert.Less(t, 0.999, tx.FeeUSD/fetchedTx.FeeUSD)
|
||||
} else if fetchedTx.FeeUSD < tx.FeeUSD {
|
||||
assert.Less(t, 0.999, fetchedTx.FeeUSD/tx.FeeUSD)
|
||||
}
|
||||
tx.FeeUSD = 0
|
||||
tx.USD = 0
|
||||
fetchedTx.FeeUSD = 0
|
||||
fetchedTx.USD = 0
|
||||
assert.Equal(t, tx, fetchedTx)
|
||||
}
|
||||
fetchAndAssertTxs(t, l1txs, l2txs)
|
||||
// Test trigger: L1 integrity
|
||||
// from_eth_addr can't be null
|
||||
l1txs[0].FromEthAddr = ethCommon.Address{}
|
||||
@@ -304,23 +256,75 @@ func TestTxs(t *testing.T) {
|
||||
l2txs[0].Nonce = 0
|
||||
err = historyDB.AddL2Txs(l2txs)
|
||||
assert.Error(t, err)
|
||||
// Test trigger: forge L1 txs
|
||||
// add next batch to DB
|
||||
batchNum, toForgeL1TxsNum := test.GetNextToForgeNumAndBatch(batches)
|
||||
batch := batches[0]
|
||||
batch.BatchNum = batchNum
|
||||
batch.ForgeL1TxsNum = toForgeL1TxsNum
|
||||
assert.NoError(t, historyDB.AddBatch(&batch)) // This should update nL1s / 2 rows
|
||||
// Set batch num in txs that should have been marked as forged in the DB
|
||||
for i := 0; i < len(l1txs); i++ {
|
||||
fetchedTx, err := historyDB.GetTx(l1txs[i].TxID)
|
||||
assert.NoError(t, err)
|
||||
if l1txs[i].ToForgeL1TxsNum == toForgeL1TxsNum {
|
||||
assert.Equal(t, batchNum, *fetchedTx.BatchNum)
|
||||
} else {
|
||||
if fetchedTx.BatchNum != nil {
|
||||
assert.NotEqual(t, batchNum, *fetchedTx.BatchNum)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test helper functions for Synchronizer
|
||||
txs, err := historyDB.GetL1UserTxs(2)
|
||||
// GetLastTxsPosition
|
||||
expectedPosition := -1
|
||||
var choosenToForgeL1TxsNum int64 = -1
|
||||
for _, tx := range l1txs {
|
||||
if choosenToForgeL1TxsNum == -1 && tx.ToForgeL1TxsNum > 0 {
|
||||
choosenToForgeL1TxsNum = tx.ToForgeL1TxsNum
|
||||
expectedPosition = tx.Position
|
||||
} else if choosenToForgeL1TxsNum == tx.ToForgeL1TxsNum && expectedPosition < tx.Position {
|
||||
expectedPosition = tx.Position
|
||||
}
|
||||
}
|
||||
position, err := historyDB.GetLastTxsPosition(choosenToForgeL1TxsNum)
|
||||
assert.NoError(t, err)
|
||||
assert.NotZero(t, len(txs))
|
||||
position, err := historyDB.GetLastTxsPosition(2)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 22, position)
|
||||
// Test Update L1 TX Batch_num
|
||||
assert.Equal(t, common.BatchNum(0), txs[0].BatchNum)
|
||||
txs[0].BatchNum = common.BatchNum(1)
|
||||
// err = historyDB.UpdateTxsBatchNum(txs)
|
||||
err = historyDB.SetBatchNumL1UserTxs(2, 1)
|
||||
assert.NoError(t, err)
|
||||
txs, err = historyDB.GetL1UserTxs(2)
|
||||
assert.NoError(t, err)
|
||||
assert.NotZero(t, len(txs))
|
||||
assert.Equal(t, common.BatchNum(1), txs[0].BatchNum)
|
||||
assert.Equal(t, expectedPosition, position)
|
||||
|
||||
// GetL1UserTxs: not needed? tests were broken
|
||||
// txs, err := historyDB.GetL1UserTxs(2)
|
||||
// assert.NoError(t, err)
|
||||
// assert.NotZero(t, len(txs))
|
||||
// assert.NoError(t, err)
|
||||
// assert.Equal(t, 22, position)
|
||||
// // Test Update L1 TX Batch_num
|
||||
// assert.Equal(t, common.BatchNum(0), txs[0].BatchNum)
|
||||
// txs[0].BatchNum = common.BatchNum(1)
|
||||
// txs, err = historyDB.GetL1UserTxs(2)
|
||||
// assert.NoError(t, err)
|
||||
// assert.NotZero(t, len(txs))
|
||||
// assert.Equal(t, common.BatchNum(1), txs[0].BatchNum)
|
||||
}
|
||||
|
||||
func fetchAndAssertTxs(t *testing.T, l1txs []common.L1Tx, l2txs []common.L2Tx) {
|
||||
for i := 0; i < len(l1txs); i++ {
|
||||
tx := l1txs[i].Tx()
|
||||
fetchedTx, err := historyDB.GetTx(tx.TxID)
|
||||
assert.NoError(t, err)
|
||||
test.AssertUSD(t, tx.USD, fetchedTx.USD)
|
||||
test.AssertUSD(t, tx.LoadAmountUSD, fetchedTx.LoadAmountUSD)
|
||||
assert.Equal(t, tx, fetchedTx)
|
||||
}
|
||||
for i := 0; i < len(l2txs); i++ {
|
||||
tx := l2txs[i].Tx()
|
||||
fetchedTx, err := historyDB.GetTx(tx.TxID)
|
||||
tx.TokenID = fetchedTx.TokenID
|
||||
assert.NoError(t, err)
|
||||
test.AssertUSD(t, fetchedTx.USD, tx.USD)
|
||||
test.AssertUSD(t, fetchedTx.FeeUSD, tx.FeeUSD)
|
||||
assert.Equal(t, tx, fetchedTx)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExitTree(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user