Move logic to extend til blocks to til

Previously, the synchronizer test was extending the output from til to
precalculate many fields to compare it with the synchronizer and BD output.
Since this is useful outside of the syncrhonizer testing: move this
functionality to til via a function that extends the output
(til.Context.FillBlocksExtra).

Also, add new functionality: calculate fee idxs dynamically by setting a user
name, and calculate collected fees.
This commit is contained in:
Eduard S
2020-11-04 18:19:17 +01:00
parent 6e4b9b4b70
commit 5ebf46e045
6 changed files with 282 additions and 154 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/hermeznetwork/hermez-node/common"
@@ -467,6 +468,8 @@ func (s *Synchronizer) rollupSync(ethBlock *common.Block) (*common.RollupData, e
for i := range processTxsOut.CreatedAccounts {
createdAccount := &processTxsOut.CreatedAccounts[i]
createdAccount.Nonce = 0
createdAccount.Balance = big.NewInt(0)
createdAccount.BatchNum = batchNum
}
batchData.CreatedAccounts = processTxsOut.CreatedAccounts
@@ -478,16 +481,18 @@ func (s *Synchronizer) rollupSync(ethBlock *common.Block) (*common.RollupData, e
}
// Get Batch information
// fmt.Printf("DBG: %#v\n", forgeBatchArgs.FeeIdxCoordinator)
batch := common.Batch{
BatchNum: batchNum,
EthBlockNum: blockNum,
ForgerAddr: *sender,
CollectedFees: processTxsOut.CollectedFees,
StateRoot: forgeBatchArgs.NewStRoot,
NumAccounts: len(batchData.CreatedAccounts),
LastIdx: forgeBatchArgs.NewLastIdx,
ExitRoot: forgeBatchArgs.NewExitRoot,
SlotNum: slotNum,
BatchNum: batchNum,
EthBlockNum: blockNum,
ForgerAddr: *sender,
CollectedFees: processTxsOut.CollectedFees,
FeeIdxsCoordinator: forgeBatchArgs.FeeIdxCoordinator,
StateRoot: forgeBatchArgs.NewStRoot,
NumAccounts: len(batchData.CreatedAccounts),
LastIdx: forgeBatchArgs.NewLastIdx,
ExitRoot: forgeBatchArgs.NewExitRoot,
SlotNum: slotNum,
}
nextForgeL1TxsNumCpy := nextForgeL1TxsNum
if forgeBatchArgs.L1Batch {

View File

@@ -23,8 +23,6 @@ import (
)
var tokenConsts = map[common.TokenID]eth.ERC20Consts{}
var forceExits = map[int64][]common.ExitInfo{} // ForgeL1TxsNum -> []exit
var nonces = map[common.Idx]common.Nonce{}
type timer struct {
time int64
@@ -103,10 +101,8 @@ func checkSyncBlock(t *testing.T, s *Synchronizer, blockNum int, block, syncBloc
dbL1CoordinatorTxs, err := s.historyDB.GetAllL1CoordinatorTxs()
require.Nil(t, err)
// fmt.Printf("DBG dbL1CoordinatorTxs: %+v\n", dbL1CoordinatorTxs)
dbL2Txs, err := s.historyDB.GetAllL2Txs()
require.Nil(t, err)
// fmt.Printf("DBG dbL2Txs: %+v\n", dbL2Txs)
dbExits, err := s.historyDB.GetAllExits()
require.Nil(t, err)
// dbL1CoordinatorTxs := []common.L1Tx{}
@@ -125,7 +121,7 @@ func checkSyncBlock(t *testing.T, s *Synchronizer, blockNum int, block, syncBloc
// We don't care about TotalFeesUSD. Use the syncBatch that
// has a TotalFeesUSD inserted by the HistoryDB
batch.Batch.TotalFeesUSD = syncBatch.Batch.TotalFeesUSD
batch.CreatedAccounts = syncBatch.CreatedAccounts // til doesn't output CreatedAccounts
assert.Equal(t, batch.CreatedAccounts, syncBatch.CreatedAccounts)
batch.Batch.NumAccounts = len(batch.CreatedAccounts)
// Test field by field to facilitate debugging of errors
@@ -139,12 +135,6 @@ func checkSyncBlock(t *testing.T, s *Synchronizer, blockNum int, block, syncBloc
assert.Equal(t, exit.Balance, syncBatch.ExitTree[j].Balance)
*exit = syncBatch.ExitTree[j]
}
// We are collecting fees after blockNum=2 in 2 idxs
if block.Block.EthBlockNum > 2 {
// fmt.Printf("DBG collectedFees: %+v\n", syncBatch.Batch.CollectedFees)
assert.Equal(t, 2, len(syncBatch.Batch.CollectedFees))
}
batch.Batch.CollectedFees = syncBatch.Batch.CollectedFees
assert.Equal(t, batch.Batch, syncBatch.Batch)
assert.Equal(t, batch, syncBatch)
assert.Equal(t, &batch.Batch, dbBatch) //nolint:gosec
@@ -305,9 +295,12 @@ func TestSync(t *testing.T) {
> batchL1 // forge L1UserTxs{nil}, freeze defined L1UserTxs{2}
> batchL1 // forge L1UserTxs{2}, freeze defined L1UserTxs{nil}
> block // blockNum=3
`
tc := til.NewContext(common.RollupConstMaxL1UserTx)
tilCfgExtra := til.ConfigExtra{
BootCoordAddr: bootCoordAddr,
CoordUser: "A",
}
blocks, err := tc.GenerateBlocks(set1)
require.Nil(t, err)
// Sanity check
@@ -339,6 +332,9 @@ func TestSync(t *testing.T) {
}
}
err = tc.FillBlocksExtra(blocks, &tilCfgExtra)
assert.Nil(t, err)
// Add block data to the smart contracts
for _, block := range blocks {
for _, token := range block.Rollup.AddedTokens {
@@ -352,12 +348,12 @@ func TestSync(t *testing.T) {
require.Nil(t, err)
}
client.CtlSetAddr(bootCoordAddr)
feeIdxCoordinator := []common.Idx{}
if block.Block.EthBlockNum > 2 {
// After blockNum=2 we have some accounts, use them as
// coordinator owned to receive fees.
feeIdxCoordinator = []common.Idx{common.Idx(256), common.Idx(259)}
}
// feeIdxCoordinator := []common.Idx{}
// if block.Block.EthBlockNum > 2 {
// // After blockNum=2 we have some accounts, use them as
// // coordinator owned to receive fees.
// feeIdxCoordinator = []common.Idx{common.Idx(256), common.Idx(259)}
// }
for _, batch := range block.Rollup.Batches {
_, err := client.RollupForgeBatch(&eth.RollupForgeBatchArgs{
NewLastIdx: batch.Batch.LastIdx,
@@ -366,7 +362,7 @@ func TestSync(t *testing.T) {
L1CoordinatorTxs: batch.L1CoordinatorTxs,
L1CoordinatorTxsAuths: [][]byte{}, // Intentionally empty
L2TxsData: batch.L2Txs,
FeeIdxCoordinator: feeIdxCoordinator,
FeeIdxCoordinator: batch.Batch.FeeIdxsCoordinator,
// Circuit selector
VerifierIdx: 0, // Intentionally empty
L1Batch: batch.L1Batch,
@@ -380,113 +376,6 @@ func TestSync(t *testing.T) {
client.CtlMineBlock()
}
// Fill extra fields not generated by til in til block
openToForge := int64(0)
toForgeL1TxsNum := int64(0)
l1UserTxsLen := map[int64]int{} // ForgeL1TxsNum -> len(L1UserTxs)
for i := range blocks {
block := &blocks[i]
// Count number of L1UserTxs in each queue, to figure out later
// position of L1CoordinatorTxs and L2Txs
for j := range block.Rollup.L1UserTxs {
tx := &block.Rollup.L1UserTxs[j]
l1UserTxsLen[*tx.ToForgeL1TxsNum]++
if tx.Type == common.TxTypeForceExit {
forceExits[*tx.ToForgeL1TxsNum] = append(forceExits[*tx.ToForgeL1TxsNum],
common.ExitInfo{
AccountIdx: tx.FromIdx,
Balance: tx.Amount,
})
}
}
for j := range block.Rollup.Batches {
batch := &block.Rollup.Batches[j]
if batch.L1Batch {
// Set BatchNum for forged L1UserTxs to til blocks
bn := batch.Batch.BatchNum
for k := range blocks {
block := &blocks[k]
for l := range block.Rollup.L1UserTxs {
tx := &block.Rollup.L1UserTxs[l]
if *tx.ToForgeL1TxsNum == openToForge {
tx.BatchNum = &bn
}
}
}
openToForge++
}
batch.Batch.EthBlockNum = block.Block.EthBlockNum
batch.Batch.ForgerAddr = bootCoordAddr // til doesn't fill the batch forger addr
if batch.L1Batch {
toForgeL1TxsNumCpy := toForgeL1TxsNum
batch.Batch.ForgeL1TxsNum = &toForgeL1TxsNumCpy // til doesn't fill the ForgeL1TxsNum
toForgeL1TxsNum++
}
batchNum := batch.Batch.BatchNum
for k := range batch.L1CoordinatorTxs {
tx := &batch.L1CoordinatorTxs[k]
tx.BatchNum = &batchNum
tx.EthBlockNum = batch.Batch.EthBlockNum
}
}
}
// Fill expected positions in L1CoordinatorTxs and L2Txs
for i := range blocks {
block := &blocks[i]
for j := range block.Rollup.Batches {
batch := &block.Rollup.Batches[j]
position := 0
if batch.L1Batch {
position = l1UserTxsLen[*batch.Batch.ForgeL1TxsNum]
}
for k := range batch.L1CoordinatorTxs {
tx := &batch.L1CoordinatorTxs[k]
tx.Position = position
position++
nTx, err := common.NewL1Tx(tx)
require.Nil(t, err)
*tx = *nTx
}
for k := range batch.L2Txs {
tx := &batch.L2Txs[k]
tx.Position = position
position++
nonces[tx.FromIdx]++
tx.Nonce = nonces[tx.FromIdx]
nTx, err := common.NewL2Tx(tx)
require.Nil(t, err)
*tx = *nTx
}
}
}
// Fill ExitTree (only AccountIdx and Balance)
for i := range blocks {
block := &blocks[i]
for j := range block.Rollup.Batches {
batch := &block.Rollup.Batches[j]
if batch.L1Batch {
for forgeL1TxsNum, exits := range forceExits {
if forgeL1TxsNum == *batch.Batch.ForgeL1TxsNum {
batch.ExitTree = append(batch.ExitTree, exits...)
}
}
}
for k := range batch.L2Txs {
tx := &batch.L2Txs[k]
if tx.Type == common.TxTypeExit {
batch.ExitTree = append(batch.ExitTree, common.ExitInfo{
AccountIdx: tx.FromIdx,
Balance: tx.Amount,
})
}
}
}
}
//
// Sync to synchronize the current state from the test smart contracts,
// and check the outcome