Abstract TxProcessor from StateDB

- Abstract TxProcessor from StateDB
- Upgrade to last version of go-merkletree for the key-value DB usage
This commit is contained in:
arnaucube
2020-12-28 16:57:13 +01:00
parent 025b56c8c3
commit 0cf1ed217b
23 changed files with 778 additions and 777 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/hermeznetwork/hermez-node/db/l2db"
"github.com/hermeznetwork/hermez-node/db/statedb"
"github.com/hermeznetwork/hermez-node/log"
"github.com/hermeznetwork/hermez-node/txprocessor"
"github.com/hermeznetwork/tracerr"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-merkletree/db"
@@ -55,8 +56,8 @@ type SelectionConfig struct {
// MaxL1CoordinatorTxs is the maximum L1-coordinator-tx for a batch
MaxL1CoordinatorTxs uint64
// ProcessTxsConfig contains the config for ProcessTxs
ProcessTxsConfig statedb.ProcessTxsConfig
// TxProcessorConfig contains the config for ProcessTxs
TxProcessorConfig txprocessor.Config
}
// TxSelector implements all the functionalities to select the txs for the next
@@ -66,7 +67,7 @@ type TxSelector struct {
localAccountsDB *statedb.LocalStateDB
coordAccount *CoordAccount
coordIdxsDB *pebble.PebbleStorage
coordIdxsDB *pebble.Storage
}
// NewTxSelector returns a *TxSelector
@@ -232,6 +233,9 @@ func (txsel *TxSelector) GetL1L2TxSelection(selectionConfig *SelectionConfig,
return nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
txselStateDB := txsel.localAccountsDB.StateDB
tp := txprocessor.NewTxProcessor(txselStateDB, selectionConfig.TxProcessorConfig)
var validTxs txs
var l1CoordinatorTxs []common.L1Tx
positionL1 := len(l1Txs)
@@ -239,7 +243,7 @@ func (txsel *TxSelector) GetL1L2TxSelection(selectionConfig *SelectionConfig,
// Process L1UserTxs
for i := 0; i < len(l1Txs); i++ {
// assumption: l1usertx are sorted by L1Tx.Position
_, _, _, _, err := txsel.localAccountsDB.ProcessL1Tx(nil, &l1Txs[i])
_, _, _, _, err := tp.ProcessL1Tx(nil, &l1Txs[i])
if err != nil {
return nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
@@ -302,14 +306,14 @@ func (txsel *TxSelector) GetL1L2TxSelection(selectionConfig *SelectionConfig,
// Process L1CoordinatorTxs
for i := 0; i < len(l1CoordinatorTxs); i++ {
_, _, _, _, err := txsel.localAccountsDB.ProcessL1Tx(nil, &l1CoordinatorTxs[i])
_, _, _, _, err := tp.ProcessL1Tx(nil, &l1CoordinatorTxs[i])
if err != nil {
return nil, nil, nil, nil, nil, tracerr.Wrap(err)
}
}
txsel.localAccountsDB.AccumulatedFees = make(map[common.Idx]*big.Int)
tp.AccumulatedFees = make(map[common.Idx]*big.Int)
for _, idx := range coordIdxs {
txsel.localAccountsDB.AccumulatedFees[idx] = big.NewInt(0)
tp.AccumulatedFees[idx] = big.NewInt(0)
}
// once L1UserTxs & L1CoordinatorTxs are processed, get TokenIDs of
@@ -321,12 +325,12 @@ func (txsel *TxSelector) GetL1L2TxSelection(selectionConfig *SelectionConfig,
}
// get most profitable L2-tx
maxL2Txs := selectionConfig.ProcessTxsConfig.MaxTx - uint32(len(l1CoordinatorTxs)) // - len(l1UserTxs) // TODO if there are L1UserTxs take them in to account
maxL2Txs := selectionConfig.TxProcessorConfig.MaxTx - uint32(len(l1CoordinatorTxs)) // - len(l1UserTxs) // TODO if there are L1UserTxs take them in to account
l2Txs := txsel.getL2Profitable(validTxs, maxL2Txs)
// Process L2Txs
for i := 0; i < len(l2Txs); i++ {
_, _, _, err = txsel.localAccountsDB.ProcessL2Tx(coordIdxsMap, nil, nil, &l2Txs[i])
_, _, _, err = tp.ProcessL2Tx(coordIdxsMap, nil, nil, &l2Txs[i])
if err != nil {
return nil, nil, nil, nil, nil, tracerr.Wrap(err)
}

View File

@@ -15,6 +15,7 @@ import (
"github.com/hermeznetwork/hermez-node/db/statedb"
"github.com/hermeznetwork/hermez-node/test"
"github.com/hermeznetwork/hermez-node/test/til"
"github.com/hermeznetwork/hermez-node/txprocessor"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -29,7 +30,7 @@ func initTest(t *testing.T, chainID uint16, testSet string) *TxSelector {
dir, err := ioutil.TempDir("", "tmpdb")
require.NoError(t, err)
defer assert.NoError(t, os.RemoveAll(dir))
sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeTxSelector, 0, chainID)
sdb, err := statedb.NewStateDB(dir, 128, statedb.TypeTxSelector, 0)
require.NoError(t, err)
txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
@@ -115,21 +116,24 @@ func TestGetL2TxSelection(t *testing.T) {
}
addTokens(t, tokens, txsel.l2db.DB())
ptc := statedb.ProcessTxsConfig{
tpc := txprocessor.Config{
NLevels: 32,
MaxFeeTx: 64,
MaxTx: 512,
MaxL1Tx: 64,
ChainID: chainID,
}
selectionConfig := &SelectionConfig{
MaxL1UserTxs: 32,
MaxL1CoordinatorTxs: 32,
ProcessTxsConfig: ptc,
TxProcessorConfig: tpc,
}
txselStateDB := txsel.localAccountsDB.StateDB
tp := txprocessor.NewTxProcessor(txselStateDB, selectionConfig.TxProcessorConfig)
// Process the 1st batch, which contains the L1CoordinatorTxs necessary
// to create the Coordinator accounts to receive the fees
_, err = txsel.localAccountsDB.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
_, err = tp.ProcessTxs(nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
require.NoError(t, err)
// add the 1st batch of transactions to the TxSelector