@ -1,17 +1,14 @@
package historydb
import (
"math/big"
"os"
"testing"
"time"
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"
)
@ -218,100 +215,107 @@ func TestTxs(t *testing.T) {
accs := test . GenAccounts ( nAccounts , 0 , tokens , nil , nil , batches )
err = historyDB . AddAccounts ( accs )
assert . NoError ( t , err )
// Generate fake L1 txs
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 = 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.
fetchAndAssertTxs ( t , l1txs , l2txs )
// Test trigger: L1 integrity
// from_eth_addr can't be null
l1txs [ 0 ] . FromEthAddr = ethCommon . Address { }
err = historyDB . AddL1Txs ( l1txs )
assert . Error ( t , err )
l1txs [ 0 ] . FromEthAddr = ethCommon . BigToAddress ( big . NewInt ( int64 ( 5 ) ) )
// from_bjj can't be null
l1txs [ 0 ] . FromBJJ = nil
err = historyDB . AddL1Txs ( l1txs )
assert . Error ( t , err )
privK := babyjub . NewRandPrivKey ( )
l1txs [ 0 ] . FromBJJ = privK . Public ( )
// load_amount can't be null
l1txs [ 0 ] . LoadAmount = nil
err = historyDB . AddL1Txs ( l1txs )
assert . Error ( t , err )
// Test trigger: L2 integrity
// batch_num can't be null
l2txs [ 0 ] . BatchNum = 0
err = historyDB . AddL2Txs ( l2txs )
assert . Error ( t , err )
l2txs [ 0 ] . BatchNum = 1
// nonce can't be null
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 )
/ *
Uncomment once the transaction generation is fixed
// Generate fake L1 txs
const nL1s = 64
_ , l1txs := test . GenL1Txs ( 256 , nL1s , 0 , nil , accs , tokens , blocks , batches )
err = historyDB . AddL1Txs ( l1txs )
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 )
// Generate fake L2 txs
const nL2s = 2048 - nL1s
_ , l2txs := test . GenL2Txs ( 256 , nL2s , 0 , nil , accs , tokens , blocks , batches )
err = historyDB . AddL2Txs ( l2txs )
assert . NoError ( t , err )
// Compare fetched txs vs generated txs.
fetchAndAssertTxs ( t , l1txs , l2txs )
// Test trigger: L1 integrity
// from_eth_addr can't be null
l1txs [ 0 ] . FromEthAddr = ethCommon . Address { }
err = historyDB . AddL1Txs ( l1txs )
assert . Error ( t , err )
l1txs [ 0 ] . FromEthAddr = ethCommon . BigToAddress ( big . NewInt ( int64 ( 5 ) ) )
// from_bjj can't be null
l1txs [ 0 ] . FromBJJ = nil
err = historyDB . AddL1Txs ( l1txs )
assert . Error ( t , err )
privK := babyjub . NewRandPrivKey ( )
l1txs [ 0 ] . FromBJJ = privK . Public ( )
// load_amount can't be null
l1txs [ 0 ] . LoadAmount = nil
err = historyDB . AddL1Txs ( l1txs )
assert . Error ( t , err )
// Test trigger: L2 integrity
// batch_num can't be null
l2txs [ 0 ] . BatchNum = 0
err = historyDB . AddL2Txs ( l2txs )
assert . Error ( t , err )
l2txs [ 0 ] . BatchNum = 1
// nonce can't be null
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
// 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
// Test helper functions for Synchronizer
// 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 . Equal ( t , expectedPosition , position )
position , err := historyDB . GetLastTxsPosition ( choosenToForgeL1TxsNum )
assert . NoError ( t , err )
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)
// 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 ( )
fmt . Println ( "ASDF" , i , tx . TxID )
fetchedTx , err := historyDB . GetTx ( tx . TxID )
assert . NoError ( t , err )
require . NoError ( t , err )
test . AssertUSD ( t , tx . USD , fetchedTx . USD )
test . AssertUSD ( t , tx . LoadAmountUSD , fetchedTx . LoadAmountUSD )
assert . Equal ( t , tx , fetchedTx )
@ -326,6 +330,7 @@ func fetchAndAssertTxs(t *testing.T, l1txs []common.L1Tx, l2txs []common.L2Tx) {
assert . Equal ( t , tx , fetchedTx )
}
}
* /
func TestExitTree ( t * testing . T ) {
nBatches := 17