Add til test l2db

This commit is contained in:
laisolizq
2020-11-11 18:06:56 +01:00
parent 8cc165f562
commit 1925110fda
2 changed files with 403 additions and 214 deletions

View File

@@ -1,23 +1,28 @@
package l2db package l2db
import ( import (
"math"
"math/big" "math/big"
"os" "os"
"testing" "testing"
"time" "time"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/common" "github.com/hermeznetwork/hermez-node/common"
dbUtils "github.com/hermeznetwork/hermez-node/db" dbUtils "github.com/hermeznetwork/hermez-node/db"
"github.com/hermeznetwork/hermez-node/db/historydb" "github.com/hermeznetwork/hermez-node/db/historydb"
"github.com/hermeznetwork/hermez-node/log" "github.com/hermeznetwork/hermez-node/log"
"github.com/hermeznetwork/hermez-node/test" "github.com/hermeznetwork/hermez-node/test"
"github.com/jmoiron/sqlx" "github.com/hermeznetwork/hermez-node/test/til"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
var l2DB *L2DB var l2DB *L2DB
var tokens []common.Token var historyDB *historydb.HistoryDB
var tokensUSD []historydb.TokenWithUSD var tc *til.Context
var tokens map[common.TokenID]historydb.TokenWithUSD
var tokensValue map[common.TokenID]float64
var accs map[common.Idx]common.Account
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// init DB // init DB
@@ -28,7 +33,7 @@ func TestMain(m *testing.M) {
} }
l2DB = NewL2DB(db, 10, 100, 24*time.Hour) l2DB = NewL2DB(db, 10, 100, 24*time.Hour)
test.WipeDB(l2DB.DB()) test.WipeDB(l2DB.DB())
tokens, tokensUSD = prepareHistoryDB(db) historyDB = historydb.NewHistoryDB(db)
// Run tests // Run tests
result := m.Run() result := m.Run()
// Close DB // Close DB
@@ -38,24 +43,56 @@ func TestMain(m *testing.M) {
os.Exit(result) os.Exit(result)
} }
func prepareHistoryDB(db *sqlx.DB) ([]common.Token, []historydb.TokenWithUSD) { func prepareHistoryDB(historyDB *historydb.HistoryDB) error {
historyDB := historydb.NewHistoryDB(db) // Reset DB
const fromBlock int64 = 1 test.WipeDB(l2DB.DB())
const toBlock int64 = 5 // Generate pool txs using til
// Store blocks to historyDB setBlockchain := `
blocks := test.GenBlocks(fromBlock, toBlock) Type: Blockchain
if err := historyDB.AddBlocks(blocks); err != nil {
panic(err) AddToken(1)
AddToken(2)
CreateAccountDeposit(1) A: 2000
CreateAccountDeposit(2) A: 2000
CreateAccountDeposit(1) B: 1000
CreateAccountDeposit(2) B: 1000
> batchL1
> batchL1
> block
> block
`
tc = til.NewContext(common.RollupConstMaxL1UserTx)
tilCfgExtra := til.ConfigExtra{
BootCoordAddr: ethCommon.HexToAddress("0xE39fEc6224708f0772D2A74fd3f9055A90E0A9f2"),
CoordUser: "A",
} }
// Store tokens to historyDB blocks, err := tc.GenerateBlocks(setBlockchain)
const nTokens = 5 if err != nil {
tokens, ethToken := test.GenTokens(nTokens, blocks) return err
if err := historyDB.AddTokens(tokens); err != nil {
panic(err)
} }
tokens = append([]common.Token{ethToken}, tokens...)
readTokens := []historydb.TokenWithUSD{} err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
for i, token := range tokens { if err != nil {
return err
}
tokens = make(map[common.TokenID]historydb.TokenWithUSD)
tokensValue = make(map[common.TokenID]float64)
accs = make(map[common.Idx]common.Account)
value := 5 * 5.389329
now := time.Now().UTC()
// Add all blocks except for the last one
for i := range blocks[:len(blocks)-1] {
err = historyDB.AddBlockSCData(&blocks[i])
if err != nil {
return err
}
for _, batch := range blocks[i].Rollup.Batches {
for _, account := range batch.CreatedAccounts {
accs[account.Idx] = account
}
}
for _, token := range blocks[i].Rollup.AddedTokens {
readToken := historydb.TokenWithUSD{ readToken := historydb.TokenWithUSD{
TokenID: token.TokenID, TokenID: token.TokenID,
EthBlockNum: token.EthBlockNum, EthBlockNum: token.EthBlockNum,
@@ -64,33 +101,58 @@ func prepareHistoryDB(db *sqlx.DB) ([]common.Token, []historydb.TokenWithUSD) {
Symbol: token.Symbol, Symbol: token.Symbol,
Decimals: token.Decimals, Decimals: token.Decimals,
} }
if i%2 != 0 { tokensValue[token.TokenID] = value / math.Pow(10, float64(token.Decimals))
value := float64(i) * 5.4321
if err := historyDB.UpdateTokenValue(token.Symbol, value); err != nil {
panic(err)
}
now := time.Now().UTC()
readToken.USDUpdate = &now readToken.USDUpdate = &now
readToken.USD = &value readToken.USD = &value
tokens[token.TokenID] = readToken
} }
readTokens = append(readTokens, readToken) // Set value to the tokens (tokens have no symbol)
tokenSymbol := ""
err := historyDB.UpdateTokenValue(tokenSymbol, value)
if err != nil {
return err
} }
return tokens, readTokens }
return nil
}
func generatePoolL2Txs() ([]common.PoolL2Tx, error) {
setPool := `
Type: PoolL2
PoolTransfer(1) A-B: 6 (4)
PoolTransfer(2) A-B: 3 (1)
PoolTransfer(1) B-A: 5 (2)
PoolTransfer(2) B-A: 10 (3)
PoolTransfer(1) A-B: 7 (2)
PoolTransfer(2) A-B: 2 (1)
PoolTransfer(1) B-A: 8 (2)
PoolTransfer(2) B-A: 1 (1)
PoolTransfer(1) A-B: 3 (1)
PoolTransfer(2) B-A: 5 (2)
PoolExit(1) A: 5 (2)
PoolExit(2) B: 3 (1)
`
poolL2Txs, err := tc.GeneratePoolL2Txs(setPool)
if err != nil {
return nil, err
}
return poolL2Txs, nil
} }
func TestAddTxTest(t *testing.T) { func TestAddTxTest(t *testing.T) {
// Gen poolTxs err := prepareHistoryDB(historyDB)
const nInserts = 20 if err != nil {
test.WipeDB(l2DB.DB()) log.Error("Error prepare historyDB", err)
txs := test.GenPoolTxs(nInserts, tokens) }
for _, tx := range txs { poolL2Txs, err := generatePoolL2Txs()
// TODO: UPDATE with til
err := l2DB.AddTxTest(tx)
assert.NoError(t, err) assert.NoError(t, err)
fetchedTx, err := l2DB.GetTx(tx.TxID) for i := range poolL2Txs {
err := l2DB.AddTxTest(&poolL2Txs[i])
assert.NoError(t, err) assert.NoError(t, err)
// assertReadTx(t, commonToRead(tx, tokens), fetchedTx) fetchedTx, err := l2DB.GetTx(poolL2Txs[i].TxID)
assertTx(t, tx, fetchedTx) assert.NoError(t, err)
assertTx(t, &poolL2Txs[i], fetchedTx)
nameZone, offset := fetchedTx.Timestamp.Zone() nameZone, offset := fetchedTx.Timestamp.Zone()
assert.Equal(t, "UTC", nameZone) assert.Equal(t, "UTC", nameZone)
assert.Equal(t, 0, offset) assert.Equal(t, 0, offset)
@@ -104,13 +166,7 @@ func assertTx(t *testing.T, expected, actual *common.PoolL2Tx) {
expected.Timestamp = actual.Timestamp expected.Timestamp = actual.Timestamp
// Check absolute fee // Check absolute fee
// find token // find token
token := historydb.TokenWithUSD{} token := tokens[expected.TokenID]
for _, tkn := range tokensUSD {
if expected.TokenID == tkn.TokenID {
token = tkn
break
}
}
// If the token has value in USD setted // If the token has value in USD setted
if token.USDUpdate != nil { if token.USDUpdate != nil {
assert.Equal(t, token.USDUpdate.Unix(), actual.AbsoluteFeeUpdate.Unix()) assert.Equal(t, token.USDUpdate.Unix(), actual.AbsoluteFeeUpdate.Unix())
@@ -138,17 +194,17 @@ func assertTx(t *testing.T, expected, actual *common.PoolL2Tx) {
// } // }
func TestGetPending(t *testing.T) { func TestGetPending(t *testing.T) {
const nInserts = 20 err := prepareHistoryDB(historyDB)
test.WipeDB(l2DB.DB()) if err != nil {
// TODO: UPDATE with til log.Error("Error prepare historyDB", err)
txs := test.GenPoolTxs(nInserts, tokens)
var pendingTxs []*common.PoolL2Tx
for _, tx := range txs {
err := l2DB.AddTxTest(tx)
assert.NoError(t, err)
if tx.State == common.PoolL2TxStatePending {
pendingTxs = append(pendingTxs, tx)
} }
poolL2Txs, err := generatePoolL2Txs()
assert.NoError(t, err)
var pendingTxs []*common.PoolL2Tx
for i := range poolL2Txs {
err := l2DB.AddTxTest(&poolL2Txs[i])
assert.NoError(t, err)
pendingTxs = append(pendingTxs, &poolL2Txs[i])
} }
fetchedTxs, err := l2DB.GetPendingTxs() fetchedTxs, err := l2DB.GetPendingTxs()
assert.NoError(t, err) assert.NoError(t, err)
@@ -158,180 +214,296 @@ func TestGetPending(t *testing.T) {
} }
} }
/*
TODO: update with til
func TestStartForging(t *testing.T) { func TestStartForging(t *testing.T) {
// Generate txs // Generate txs
const nInserts = 60
const fakeBatchNum common.BatchNum = 33 const fakeBatchNum common.BatchNum = 33
test.WipeDB(l2DB.DB()) err := prepareHistoryDB(historyDB)
txs := test.GenPoolTxs(nInserts, tokens) if err != nil {
log.Error("Error prepare historyDB", err)
}
poolL2Txs, err := generatePoolL2Txs()
assert.NoError(t, err)
var startForgingTxIDs []common.TxID var startForgingTxIDs []common.TxID
randomizer := 0 randomizer := 0
// Add txs to DB // Add txs to DB
for _, tx := range txs { for i := range poolL2Txs {
err := l2DB.AddTxTest(tx) err := l2DB.AddTxTest(&poolL2Txs[i])
assert.NoError(t, err) assert.NoError(t, err)
if tx.State == common.PoolL2TxStatePending && randomizer%2 == 0 { if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
randomizer++ startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
startForgingTxIDs = append(startForgingTxIDs, tx.TxID)
} }
randomizer++
} }
// Start forging txs // Start forging txs
err := l2DB.StartForging(startForgingTxIDs, fakeBatchNum) err = l2DB.StartForging(startForgingTxIDs, fakeBatchNum)
assert.NoError(t, err) assert.NoError(t, err)
// Fetch txs and check that they've been updated correctly // Fetch txs and check that they've been updated correctly
for _, id := range startForgingTxIDs { for _, id := range startForgingTxIDs {
fetchedTx, err := l2DB.GetTx(id) fetchedTx, err := l2DB.GetTxAPI(id)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, common.PoolL2TxStateForging, fetchedTx.State) assert.Equal(t, common.PoolL2TxStateForging, fetchedTx.State)
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum) assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
} }
} }
*/
/*
TODO: update with til
func TestDoneForging(t *testing.T) { func TestDoneForging(t *testing.T) {
// Generate txs // Generate txs
const nInserts = 60
const fakeBatchNum common.BatchNum = 33 const fakeBatchNum common.BatchNum = 33
test.WipeDB(l2DB.DB()) err := prepareHistoryDB(historyDB)
txs := test.GenPoolTxs(nInserts, tokens) if err != nil {
var doneForgingTxIDs []common.TxID log.Error("Error prepare historyDB", err)
}
poolL2Txs, err := generatePoolL2Txs()
assert.NoError(t, err)
var startForgingTxIDs []common.TxID
randomizer := 0 randomizer := 0
// Add txs to DB // Add txs to DB
for _, tx := range txs { for i := range poolL2Txs {
err := l2DB.AddTxTest(tx) err := l2DB.AddTxTest(&poolL2Txs[i])
assert.NoError(t, err) assert.NoError(t, err)
if tx.State == common.PoolL2TxStateForging && randomizer%2 == 0 { if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
randomizer++ startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
doneForgingTxIDs = append(doneForgingTxIDs, tx.TxID)
} }
randomizer++
} }
// Start forging txs // Start forging txs
err := l2DB.DoneForging(doneForgingTxIDs, fakeBatchNum) err = l2DB.StartForging(startForgingTxIDs, fakeBatchNum)
assert.NoError(t, err) assert.NoError(t, err)
var doneForgingTxIDs []common.TxID
randomizer = 0
for _, txID := range startForgingTxIDs {
if randomizer%2 == 0 {
doneForgingTxIDs = append(doneForgingTxIDs, txID)
}
randomizer++
}
// Done forging txs
err = l2DB.DoneForging(doneForgingTxIDs, fakeBatchNum)
assert.NoError(t, err)
// Fetch txs and check that they've been updated correctly // Fetch txs and check that they've been updated correctly
for _, id := range doneForgingTxIDs { for _, id := range doneForgingTxIDs {
fetchedTx, err := l2DB.GetTx(id) fetchedTx, err := l2DB.GetTxAPI(id)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, common.PoolL2TxStateForged, fetchedTx.State) assert.Equal(t, common.PoolL2TxStateForged, fetchedTx.State)
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum) assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
} }
} }
*/
/*
TODO: update with til
func TestInvalidate(t *testing.T) { func TestInvalidate(t *testing.T) {
// Generate txs // Generate txs
const nInserts = 60
const fakeBatchNum common.BatchNum = 33 const fakeBatchNum common.BatchNum = 33
test.WipeDB(l2DB.DB()) err := prepareHistoryDB(historyDB)
txs := test.GenPoolTxs(nInserts, tokens) if err != nil {
log.Error("Error prepare historyDB", err)
}
poolL2Txs, err := generatePoolL2Txs()
assert.NoError(t, err)
var invalidTxIDs []common.TxID var invalidTxIDs []common.TxID
randomizer := 0 randomizer := 0
// Add txs to DB // Add txs to DB
for _, tx := range txs { for i := range poolL2Txs {
err := l2DB.AddTxTest(tx) err := l2DB.AddTxTest(&poolL2Txs[i])
assert.NoError(t, err) assert.NoError(t, err)
if tx.State != common.PoolL2TxStateInvalid && randomizer%2 == 0 { if poolL2Txs[i].State != common.PoolL2TxStateInvalid && randomizer%2 == 0 {
randomizer++ randomizer++
invalidTxIDs = append(invalidTxIDs, tx.TxID) invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
} }
} }
// Start forging txs // Invalidate txs
err := l2DB.InvalidateTxs(invalidTxIDs, fakeBatchNum) err = l2DB.InvalidateTxs(invalidTxIDs, fakeBatchNum)
assert.NoError(t, err) assert.NoError(t, err)
// Fetch txs and check that they've been updated correctly // Fetch txs and check that they've been updated correctly
for _, id := range invalidTxIDs { for _, id := range invalidTxIDs {
fetchedTx, err := l2DB.GetTx(id) fetchedTx, err := l2DB.GetTxAPI(id)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State) assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State)
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum) assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
} }
} }
*/
/*
TODO: update with til
func TestCheckNonces(t *testing.T) { func TestCheckNonces(t *testing.T) {
// Generate txs // Generate txs
const nInserts = 60
const fakeBatchNum common.BatchNum = 33 const fakeBatchNum common.BatchNum = 33
test.WipeDB(l2DB.DB()) err := prepareHistoryDB(historyDB)
txs := test.GenPoolTxs(nInserts, tokens) if err != nil {
var invalidTxIDs []common.TxID log.Error("Error prepare historyDB", err)
// Generate accounts }
const nAccoutns = 2 poolL2Txs, err := generatePoolL2Txs()
const currentNonce = 2 assert.NoError(t, err)
accs := []common.Account{} // Update Accounts currentNonce
for i := 0; i < nAccoutns; i++ { var updateAccounts []common.Account
accs = append(accs, common.Account{ const currentNonce = common.Nonce(1)
Idx: common.Idx(i), for i := range accs {
Nonce: currentNonce, account := accs[i]
}) account.Nonce = common.Nonce(currentNonce)
updateAccounts = append(updateAccounts, account)
} }
// Add txs to DB // Add txs to DB
for i := 0; i < len(txs); i++ { var invalidTxIDs []common.TxID
if txs[i].State != common.PoolL2TxStateInvalid { for i := range poolL2Txs {
if i%2 == 0 { // Ensure transaction will be marked as invalid due to old nonce if poolL2Txs[i].Nonce <= currentNonce {
txs[i].Nonce = accs[i%len(accs)].Nonce invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
txs[i].FromIdx = accs[i%len(accs)].Idx
invalidTxIDs = append(invalidTxIDs, txs[i].TxID)
} else { // Ensure transaction will NOT be marked as invalid due to old nonce
txs[i].Nonce = currentNonce + 1
} }
} err := l2DB.AddTxTest(&poolL2Txs[i])
err := l2DB.AddTxTest(txs[i])
assert.NoError(t, err) assert.NoError(t, err)
} }
// Start forging txs
err := l2DB.InvalidateTxs(invalidTxIDs, fakeBatchNum) err = l2DB.CheckNonces(updateAccounts, fakeBatchNum)
assert.NoError(t, err) assert.NoError(t, err)
// Fetch txs and check that they've been updated correctly // Fetch txs and check that they've been updated correctly
for _, id := range invalidTxIDs { for _, id := range invalidTxIDs {
fetchedTx, err := l2DB.GetTx(id) fetchedTx, err := l2DB.GetTxAPI(id)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State) assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State)
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum) assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
} }
} }
*/
// TestReorg: first part of the test with reorg
// With invalidated transactions BEFORE reorgBatch
// And forged transactions in reorgBatch
func TestReorg(t *testing.T) { func TestReorg(t *testing.T) {
// Generate txs // Generate txs
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.WipeDB(l2DB.DB()) err := prepareHistoryDB(historyDB)
// TODO: update with til if err != nil {
txs := test.GenPoolTxs(nInserts, tokens) log.Error("Error prepare historyDB", err)
// Add txs to the DB }
poolL2Txs, err := generatePoolL2Txs()
assert.NoError(t, err)
reorgedTxIDs := []common.TxID{} reorgedTxIDs := []common.TxID{}
nonReorgedTxIDs := []common.TxID{} nonReorgedTxIDs := []common.TxID{}
for i := 0; i < len(txs); i++ { var startForgingTxIDs []common.TxID
err := l2DB.AddTxTest(txs[i]) var invalidTxIDs []common.TxID
var allTxRandomize []common.TxID
randomizer := 0
// Add txs to DB
for i := range poolL2Txs {
err := l2DB.AddTxTest(&poolL2Txs[i])
assert.NoError(t, err) assert.NoError(t, err)
var batchNum common.BatchNum if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
if txs[i].State == common.PoolL2TxStateForged || txs[i].State == common.PoolL2TxStateInvalid { startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
reorgedTxIDs = append(reorgedTxIDs, txs[i].TxID) allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
batchNum = reorgBatch } else if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%3 == 0 {
invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
}
randomizer++
}
// Start forging txs
err = l2DB.StartForging(startForgingTxIDs, lastValidBatch)
assert.NoError(t, err)
var doneForgingTxIDs []common.TxID
randomizer = 0
for _, txID := range allTxRandomize {
invalidTx := false
for i := range invalidTxIDs {
if invalidTxIDs[i] == txID {
invalidTx = true
nonReorgedTxIDs = append(nonReorgedTxIDs, txID)
}
}
if !invalidTx {
if randomizer%2 == 0 {
doneForgingTxIDs = append(doneForgingTxIDs, txID)
reorgedTxIDs = append(reorgedTxIDs, txID)
} else { } else {
nonReorgedTxIDs = append(nonReorgedTxIDs, txs[i].TxID) nonReorgedTxIDs = append(nonReorgedTxIDs, txID)
batchNum = lastValidBatch
} }
_, err = l2DB.db.Exec( randomizer++
"UPDATE tx_pool SET batch_num = $1 WHERE tx_id = $2;", }
batchNum, txs[i].TxID, }
)
// Invalidate txs BEFORE reorgBatch --> nonReorg
err = l2DB.InvalidateTxs(invalidTxIDs, lastValidBatch)
assert.NoError(t, err) assert.NoError(t, err)
// Done forging txs in reorgBatch --> Reorg
err = l2DB.DoneForging(doneForgingTxIDs, reorgBatch)
assert.NoError(t, err)
err = l2DB.Reorg(lastValidBatch)
assert.NoError(t, err)
for _, id := range reorgedTxIDs {
tx, err := l2DB.GetTxAPI(id)
assert.NoError(t, err)
assert.Nil(t, tx.BatchNum)
assert.Equal(t, common.PoolL2TxStatePending, tx.State)
} }
err := l2DB.Reorg(lastValidBatch) for _, id := range nonReorgedTxIDs {
fetchedTx, err := l2DB.GetTxAPI(id)
assert.NoError(t, err)
assert.Equal(t, lastValidBatch, *fetchedTx.BatchNum)
}
}
// TestReorg: second part of test with reorg
// With invalidated transactions in reorgBatch
// And forged transactions BEFORE reorgBatch
func TestReorg2(t *testing.T) {
// Generate txs
const lastValidBatch common.BatchNum = 20
const reorgBatch common.BatchNum = lastValidBatch + 1
err := prepareHistoryDB(historyDB)
if err != nil {
log.Error("Error prepare historyDB", err)
}
poolL2Txs, err := generatePoolL2Txs()
assert.NoError(t, err)
reorgedTxIDs := []common.TxID{}
nonReorgedTxIDs := []common.TxID{}
var startForgingTxIDs []common.TxID
var invalidTxIDs []common.TxID
var allTxRandomize []common.TxID
randomizer := 0
// Add txs to DB
for i := range poolL2Txs {
err := l2DB.AddTxTest(&poolL2Txs[i])
assert.NoError(t, err)
if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%2 == 0 {
startForgingTxIDs = append(startForgingTxIDs, poolL2Txs[i].TxID)
allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
} else if poolL2Txs[i].State == common.PoolL2TxStatePending && randomizer%3 == 0 {
invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
allTxRandomize = append(allTxRandomize, poolL2Txs[i].TxID)
}
randomizer++
}
// Start forging txs
err = l2DB.StartForging(startForgingTxIDs, lastValidBatch)
assert.NoError(t, err)
var doneForgingTxIDs []common.TxID
randomizer = 0
for _, txID := range allTxRandomize {
invalidTx := false
for i := range invalidTxIDs {
if invalidTxIDs[i] == txID {
invalidTx = true
reorgedTxIDs = append(reorgedTxIDs, txID)
}
}
if !invalidTx {
if randomizer%2 == 0 {
doneForgingTxIDs = append(doneForgingTxIDs, txID)
}
nonReorgedTxIDs = append(nonReorgedTxIDs, txID)
randomizer++
}
}
// Done forging txs BEFORE reorgBatch --> nonReorg
err = l2DB.DoneForging(doneForgingTxIDs, lastValidBatch)
assert.NoError(t, err)
// Invalidate txs in reorgBatch --> Reorg
err = l2DB.InvalidateTxs(invalidTxIDs, reorgBatch)
assert.NoError(t, err)
err = l2DB.Reorg(lastValidBatch)
assert.NoError(t, err) assert.NoError(t, err)
for _, id := range reorgedTxIDs { for _, id := range reorgedTxIDs {
tx, err := l2DB.GetTxAPI(id) tx, err := l2DB.GetTxAPI(id)
@@ -347,58 +519,75 @@ func TestReorg(t *testing.T) {
} }
func TestPurge(t *testing.T) { func TestPurge(t *testing.T) {
/*
TODO: update with til
WARNING: this should be fixed once transaktio is ready
// Generate txs // Generate txs
nInserts := l2DB.maxTxs + 20 err := prepareHistoryDB(historyDB)
test.WipeDB(l2DB.DB()) if err != nil {
txs := test.GenPoolTxs(int(nInserts), tokens) log.Error("Error prepare historyDB", err)
}
// generatePoolL2Txs generate 10 txs
generateTx := int(l2DB.maxTxs/10 + 1)
var poolL2Tx []common.PoolL2Tx
for i := 0; i < generateTx; i++ {
poolL2TxAux, err := generatePoolL2Txs()
assert.NoError(t, err)
poolL2Tx = append(poolL2Tx, poolL2TxAux...)
}
deletedIDs := []common.TxID{} deletedIDs := []common.TxID{}
keepedIDs := []common.TxID{} keepedIDs := []common.TxID{}
var invalidTxIDs []common.TxID
var doneForgingTxIDs []common.TxID
const toDeleteBatchNum common.BatchNum = 30 const toDeleteBatchNum common.BatchNum = 30
safeBatchNum := toDeleteBatchNum + l2DB.safetyPeriod + 1 safeBatchNum := toDeleteBatchNum + l2DB.safetyPeriod + 1
// Add txs to the DB // Add txs to the DB
for i := 0; i < int(l2DB.maxTxs); i++ { for i := 0; i < int(l2DB.maxTxs); i++ {
var batchNum common.BatchNum tx := poolL2Tx[i]
if i%2 == 0 { // keep tx if i%2 == 0 { // keep tx
batchNum = safeBatchNum keepedIDs = append(keepedIDs, tx.TxID)
keepedIDs = append(keepedIDs, txs[i].TxID)
} else { // delete after safety period } else { // delete after safety period
batchNum = toDeleteBatchNum
if i%3 == 0 { if i%3 == 0 {
txs[i].State = common.PoolL2TxStateForged doneForgingTxIDs = append(doneForgingTxIDs, tx.TxID)
} else { } else {
txs[i].State = common.PoolL2TxStateInvalid invalidTxIDs = append(invalidTxIDs, tx.TxID)
} }
deletedIDs = append(deletedIDs, txs[i].TxID) deletedIDs = append(deletedIDs, tx.TxID)
} }
err := l2DB.AddTxTest(txs[i]) err := l2DB.AddTxTest(&tx)
assert.NoError(t, err) assert.NoError(t, err)
// Set batchNum }
// Set batchNum keeped txs
for i := range keepedIDs {
_, err = l2DB.db.Exec( _, err = l2DB.db.Exec(
"UPDATE tx_pool SET batch_num = $1 WHERE tx_id = $2;", "UPDATE tx_pool SET batch_num = $1 WHERE tx_id = $2;",
batchNum, txs[i].TxID, safeBatchNum, keepedIDs[i],
) )
assert.NoError(t, err) assert.NoError(t, err)
} }
for i := int(l2DB.maxTxs); i < len(txs); i++ { // Start forging txs and set batchNum
err = l2DB.StartForging(doneForgingTxIDs, toDeleteBatchNum)
assert.NoError(t, err)
// Done forging txs and set batchNum
err = l2DB.DoneForging(doneForgingTxIDs, toDeleteBatchNum)
assert.NoError(t, err)
// Invalidate txs and set batchNum
err = l2DB.InvalidateTxs(invalidTxIDs, toDeleteBatchNum)
assert.NoError(t, err)
for i := int(l2DB.maxTxs); i < len(poolL2Tx); i++ {
// Delete after TTL // Delete after TTL
deletedIDs = append(deletedIDs, txs[i].TxID) deletedIDs = append(deletedIDs, poolL2Tx[i].TxID)
err := l2DB.AddTxTest(txs[i]) err := l2DB.AddTxTest(&poolL2Tx[i])
assert.NoError(t, err) assert.NoError(t, err)
// Set timestamp // Set timestamp
deleteTimestamp := time.Unix(time.Now().UTC().Unix()-int64(l2DB.ttl.Seconds()+float64(4*time.Second)), 0) deleteTimestamp := time.Unix(time.Now().UTC().Unix()-int64(l2DB.ttl.Seconds()+float64(4*time.Second)), 0)
_, err = l2DB.db.Exec( _, err = l2DB.db.Exec(
"UPDATE tx_pool SET timestamp = $1 WHERE tx_id = $2;", "UPDATE tx_pool SET timestamp = $1 WHERE tx_id = $2;",
deleteTimestamp, txs[i].TxID, deleteTimestamp, poolL2Tx[i].TxID,
) )
assert.NoError(t, err) assert.NoError(t, err)
} }
// Purge txs // Purge txs
err := l2DB.Purge(safeBatchNum) err = l2DB.Purge(safeBatchNum)
assert.NoError(t, err) assert.NoError(t, err)
// Check results // Check results
for _, id := range deletedIDs { for _, id := range deletedIDs {
@@ -412,7 +601,6 @@ func TestPurge(t *testing.T) {
_, err := l2DB.GetTx(id) _, err := l2DB.GetTx(id)
assert.NoError(t, err) assert.NoError(t, err)
} }
*/
} }
func TestAuth(t *testing.T) { func TestAuth(t *testing.T) {

View File

@@ -591,6 +591,7 @@ func (tc *Context) GeneratePoolL2Txs(set string) ([]common.PoolL2Tx, error) {
TokenID: inst.tokenID, TokenID: inst.tokenID,
Amount: big.NewInt(int64(inst.amount)), Amount: big.NewInt(int64(inst.amount)),
Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce, Nonce: tc.Users[inst.from].Accounts[inst.tokenID].Nonce,
State: common.PoolL2TxStatePending,
Type: common.TxTypeExit, Type: common.TxTypeExit,
} }
nTx, err := common.NewPoolL2Tx(&tx) nTx, err := common.NewPoolL2Tx(&tx)