@ -7,6 +7,7 @@ import (
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/common"
"github.com/hermeznetwork/hermez-node/eth"
"github.com/hermeznetwork/hermez-node/eth"
"github.com/hermeznetwork/hermez-node/log"
"github.com/hermeznetwork/hermez-node/test/til"
"github.com/hermeznetwork/hermez-node/test/til"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
@ -28,53 +29,74 @@ func TestProcessTxsSynchronizer(t *testing.T) {
blocks , err := tc . GenerateBlocks ( til . SetBlockchain0 )
blocks , err := tc . GenerateBlocks ( til . SetBlockchain0 )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , 29 , len ( blocks [ 0 ] . L1UserTxs ) )
assert . Equal ( t , 0 , len ( blocks [ 0 ] . Batches [ 0 ] . L1CoordinatorTxs ) )
assert . Equal ( t , 21 , len ( blocks [ 0 ] . Batches [ 1 ] . L2Txs ) )
assert . Equal ( t , 31 , len ( blocks [ 0 ] . L1UserTxs ) )
assert . Equal ( t , 4 , len ( blocks [ 0 ] . Batches [ 0 ] . L1CoordinatorTxs ) )
assert . Equal ( t , 0 , len ( blocks [ 0 ] . Batches [ 1 ] . L1CoordinatorTxs ) )
assert . Equal ( t , 22 , len ( blocks [ 0 ] . Batches [ 2 ] . L2Txs ) )
assert . Equal ( t , 1 , len ( blocks [ 1 ] . Batches [ 0 ] . L1CoordinatorTxs ) )
assert . Equal ( t , 1 , len ( blocks [ 1 ] . Batches [ 0 ] . L1CoordinatorTxs ) )
assert . Equal ( t , 59 , len ( blocks [ 1 ] . Batches [ 0 ] . L2Txs ) )
assert . Equal ( t , 59 , len ( blocks [ 1 ] . Batches [ 0 ] . L2Txs ) )
assert . Equal ( t , 1 , len ( blocks [ 1 ] . Batches [ 1 ] . L1CoordinatorTxs ) )
assert . Equal ( t , 1 , len ( blocks [ 1 ] . Batches [ 1 ] . L1CoordinatorTxs ) )
assert . Equal ( t , 8 , len ( blocks [ 1 ] . Batches [ 1 ] . L2Txs ) )
assert . Equal ( t , 8 , len ( blocks [ 1 ] . Batches [ 1 ] . L2Txs ) )
// use first batch
l2Txs := common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 0 ] . L2Txs )
_ , exitInfos , err := sdb . ProcessTxs ( blocks [ 0 ] . L1UserTxs , blocks [ 0 ] . Batches [ 0 ] . L1CoordinatorTxs , l2Txs )
// Coordinator Idx where to send the fees
coordIdxs := [ ] common . Idx { 256 , 257 , 258 , 259 }
// Idx of user 'A'
idxA1 := tc . Users [ "A" ] . Accounts [ common . TokenID ( 1 ) ] . Idx
log . Debug ( "1st batch, 1st block, only L1CoordinatorTxs" )
_ , _ , err = sdb . ProcessTxs ( nil , nil , blocks [ 0 ] . Batches [ 0 ] . L1CoordinatorTxs , nil )
require . Nil ( t , err )
log . Debug ( "2nd batch, 1st block" )
l2Txs := common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 1 ] . L2Txs )
_ , exitInfos , err := sdb . ProcessTxs ( coordIdxs , blocks [ 0 ] . L1UserTxs , blocks [ 0 ] . Batches [ 1 ] . L1CoordinatorTxs , l2Txs )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , 0 , len ( exitInfos ) )
assert . Equal ( t , 0 , len ( exitInfos ) )
acc , err := sdb . GetAccount ( common . Idx ( 256 ) )
acc , err := sdb . GetAccount ( idxA1 )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , "50" , acc . Balance . String ( ) )
assert . Equal ( t , "50" , acc . Balance . String ( ) )
// second batch of first block
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 1 ] . L2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( nil , blocks [ 0 ] . Batches [ 1 ] . L1CoordinatorTxs , l2Txs )
log . Debug ( "3rd batch, 1st block" )
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 2 ] . L2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( coordIdxs , nil , blocks [ 0 ] . Batches [ 2 ] . L1CoordinatorTxs , l2Txs )
require . Nil ( t , err )
require . Nil ( t , err )
// TODO once TTGL is updated, add a check that a input poolL2Tx with
// TODO once TTGL is updated, add a check that a input poolL2Tx with
// Nonce & TokenID =0, after ProcessTxs call has the expected value
// Nonce & TokenID =0, after ProcessTxs call has the expected value
assert . Equal ( t , 0 , len ( exitInfos ) )
assert . Equal ( t , 0 , len ( exitInfos ) )
acc , err = sdb . GetAccount ( common . Idx ( 256 ) )
acc , err = sdb . GetAccount ( idxA1 )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , "28" , acc . Balance . String ( ) )
assert . Equal ( t , "28" , acc . Balance . String ( ) )
// use second batch
log . Debug ( "1st batch, 2nd block" )
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 1 ] . Batches [ 0 ] . L2Txs )
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 1 ] . Batches [ 0 ] . L2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( nil , blocks [ 1 ] . Batches [ 0 ] . L1CoordinatorTxs , l2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( coordIdxs , nil , blocks [ 1 ] . Batches [ 0 ] . L1CoordinatorTxs , l2Txs )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , 4 , len ( exitInfos ) ) // the 'ForceExit(1)' is not computed yet, as the batch is without L1UserTxs
assert . Equal ( t , 4 , len ( exitInfos ) ) // the 'ForceExit(1)' is not computed yet, as the batch is without L1UserTxs
acc , err = sdb . GetAccount ( common . Idx ( 256 ) )
acc , err = sdb . GetAccount ( idxA1 )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , "53" , acc . Balance . String ( ) )
assert . Equal ( t , "53" , acc . Balance . String ( ) )
// use third batch
log . Debug ( "2nd batch, 2nd block" )
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 1 ] . Batches [ 1 ] . L2Txs )
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 1 ] . Batches [ 1 ] . L2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( blocks [ 1 ] . L1UserTxs , blocks [ 1 ] . Batches [ 1 ] . L1CoordinatorTxs , l2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( coordIdxs , blocks [ 1 ] . L1UserTxs , blocks [ 1 ] . Batches [ 1 ] . L1CoordinatorTxs , l2Txs )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , 2 , len ( exitInfos ) ) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
assert . Equal ( t , 2 , len ( exitInfos ) ) // 2, as previous batch was without L1UserTxs, and has pending the 'ForceExit(1) A: 5'
acc , err = sdb . GetAccount ( common . Idx ( 256 ) )
acc , err = sdb . GetAccount ( idxA1 )
assert . Nil ( t , err )
assert . Nil ( t , err )
assert . Equal ( t , "78" , acc . Balance . String ( ) )
assert . Equal ( t , "78" , acc . Balance . String ( ) )
idxB0 := tc . Users [ "C" ] . Accounts [ common . TokenID ( 0 ) ] . Idx
acc , err = sdb . GetAccount ( idxB0 )
require . Nil ( t , err )
assert . Equal ( t , "51" , acc . Balance . String ( ) )
// get balance of Coordinator account for TokenID==0
acc , err = sdb . GetAccount ( common . Idx ( 256 ) )
require . Nil ( t , err )
assert . Equal ( t , "2" , acc . Balance . String ( ) )
}
}
/ *
/ *
@ -102,30 +124,33 @@ func TestProcessTxsBatchBuilder(t *testing.T) {
assert . Equal ( t , 0 , len ( blocks [ 0 ] . Batches [ 2 ] . L1CoordinatorTxs ) )
assert . Equal ( t , 0 , len ( blocks [ 0 ] . Batches [ 2 ] . L1CoordinatorTxs ) )
assert . Equal ( t , 8 , len ( blocks [ 0 ] . Batches [ 2 ] . L2Txs ) )
assert . Equal ( t , 8 , len ( blocks [ 0 ] . Batches [ 2 ] . L2Txs ) )
// Idx of user 'A'
idxA1 := tc . Users [ "A" ] . Accounts [ common . TokenID ( 1 ) ] . Idx
// use first batch
// use first batch
l2Txs := common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 0 ] . L2Txs )
l2Txs := common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 0 ] . L2Txs )
_ , exitInfos , err := sdb . ProcessTxs ( blocks [ 0 ] . Batches [ 0 ] . L1UserTxs , blocks [ 0 ] . Batches [ 0 ] . L1CoordinatorTxs , l2Txs )
_ , exitInfos , err := sdb . ProcessTxs ( coordIdxs , blocks [ 0 ] . Batches [ 0 ] . L1UserTxs , blocks [ 0 ] . Batches [ 0 ] . L1CoordinatorTxs , l2Txs )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , 0 , len ( exitInfos ) )
assert . Equal ( t , 0 , len ( exitInfos ) )
acc , err := sdb . GetAccount ( common . Idx ( 256 ) )
acc , err := sdb . GetAccount ( idxA1 )
assert . Nil ( t , err )
assert . Nil ( t , err )
assert . Equal ( t , "28" , acc . Balance . String ( ) )
assert . Equal ( t , "28" , acc . Balance . String ( ) )
// use second batch
// use second batch
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 1 ] . L2Txs )
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 1 ] . L2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( blocks [ 0 ] . Batches [ 1 ] . L1UserTxs , blocks [ 0 ] . Batches [ 1 ] . L1CoordinatorTxs , l2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( coordIdxs , blocks [ 0 ] . Batches [ 1 ] . L1UserTxs , blocks [ 0 ] . Batches [ 1 ] . L1CoordinatorTxs , l2Txs )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , 5 , len ( exitInfos ) )
assert . Equal ( t , 5 , len ( exitInfos ) )
acc , err = sdb . GetAccount ( common . Idx ( 256 ) )
acc , err = sdb . GetAccount ( idxA1 )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , "48" , acc . Balance . String ( ) )
assert . Equal ( t , "48" , acc . Balance . String ( ) )
// use third batch
// use third batch
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 2 ] . L2Txs )
l2Txs = common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 2 ] . L2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( blocks [ 0 ] . Batches [ 2 ] . L1UserTxs , blocks [ 0 ] . Batches [ 2 ] . L1CoordinatorTxs , l2Txs )
_ , exitInfos , err = sdb . ProcessTxs ( coordIdxs , blocks [ 0 ] . Batches [ 2 ] . L1UserTxs , blocks [ 0 ] . Batches [ 2 ] . L1CoordinatorTxs , l2Txs )
require . Nil ( t , err )
require . Nil ( t , err )
assert . Equal ( t , 1 , len ( exitInfos ) )
assert . Equal ( t , 1 , len ( exitInfos ) )
acc , err = sdb . GetAccount ( common . Idx ( 256 ) )
acc , err = sdb . GetAccount ( idxA1 )
assert . Nil ( t , err )
assert . Nil ( t , err )
assert . Equal ( t , "23" , acc . Balance . String ( ) )
assert . Equal ( t , "23" , acc . Balance . String ( ) )
}
}
@ -146,7 +171,7 @@ func TestZKInputsGeneration(t *testing.T) {
assert . Equal ( t , 21 , len ( blocks [ 0 ] . Batches [ 0 ] . L2Txs ) )
assert . Equal ( t , 21 , len ( blocks [ 0 ] . Batches [ 0 ] . L2Txs ) )
l2Txs := common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 0 ] . L2Txs )
l2Txs := common . L2TxsToPoolL2Txs ( blocks [ 0 ] . Batches [ 0 ] . L2Txs )
zki , _ , err := sdb . ProcessTxs ( blocks [ 0 ] . Batches [ 0 ] . L1UserTxs , blocks [ 0 ] . Batches [ 0 ] . L1CoordinatorTxs , l2Txs )
zki , _ , err := sdb . ProcessTxs ( coordIdxs , blocks [ 0 ] . Batches [ 0 ] . L1UserTxs , blocks [ 0 ] . Batches [ 0 ] . L1CoordinatorTxs , l2Txs )
require . Nil ( t , err )
require . Nil ( t , err )
s , err := json . Marshal ( zki )
s , err := json . Marshal ( zki )