arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/txselector/txselector_test.go
txselector/txselector_test.goRaw
  1package txselector
  2
  3import (
  4	"io/ioutil"
  5	"os"
  6	"strconv"
  7	"testing"
  8	"time"
  9
 10	ethCommon "github.com/ethereum/go-ethereum/common"
 11	"github.com/hermeznetwork/hermez-node/common"
 12	dbUtils "github.com/hermeznetwork/hermez-node/db"
 13	"github.com/hermeznetwork/hermez-node/db/historydb"
 14	"github.com/hermeznetwork/hermez-node/db/l2db"
 15	"github.com/hermeznetwork/hermez-node/db/statedb"
 16	"github.com/hermeznetwork/hermez-node/test"
 17	"github.com/hermeznetwork/hermez-node/test/til"
 18	"github.com/jmoiron/sqlx"
 19	"github.com/stretchr/testify/assert"
 20	"github.com/stretchr/testify/require"
 21)
 22
 23func initTest(t *testing.T, testSet string, maxL1UserTxs, maxL1OperatorTxs, maxTxs uint64) *TxSelector {
 24	pass := os.Getenv("POSTGRES_PASS")
 25	db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
 26	require.Nil(t, err)
 27	l2DB := l2db.NewL2DB(db, 10, 100, 24*time.Hour)
 28
 29	dir, err := ioutil.TempDir("", "tmpdb")
 30	require.Nil(t, err)
 31	defer assert.Nil(t, os.RemoveAll(dir))
 32	sdb, err := statedb.NewStateDB(dir, statedb.TypeTxSelector, 0)
 33	require.Nil(t, err)
 34
 35	txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
 36	require.Nil(t, err)
 37	defer assert.Nil(t, os.RemoveAll(dir))
 38	txsel, err := NewTxSelector(txselDir, sdb, l2DB, maxL1UserTxs, maxL1OperatorTxs, maxTxs)
 39	require.Nil(t, err)
 40
 41	return txsel
 42}
 43func addL2Txs(t *testing.T, txsel *TxSelector, poolL2Txs []common.PoolL2Tx) {
 44	for i := 0; i < len(poolL2Txs); i++ {
 45		err := txsel.l2db.AddTxTest(&poolL2Txs[i])
 46		require.Nil(t, err)
 47	}
 48}
 49
 50func addTokens(t *testing.T, tokens []common.Token, db *sqlx.DB) {
 51	hdb := historydb.NewHistoryDB(db)
 52	test.WipeDB(hdb.DB())
 53	assert.Nil(t, hdb.AddBlock(&common.Block{
 54		Num: 1,
 55	}))
 56	assert.Nil(t, hdb.AddTokens(tokens))
 57}
 58
 59func TestGetL2TxSelection(t *testing.T) {
 60	txsel := initTest(t, til.SetPool0, 5, 5, 10)
 61	test.WipeDB(txsel.l2db.DB())
 62
 63	tc := til.NewContext(common.RollupConstMaxL1UserTx)
 64	// generate test transactions
 65	blocks, err := tc.GenerateBlocks(til.SetBlockchain0)
 66	assert.Nil(t, err)
 67	// poolL2Txs, err := tc.GeneratePoolL2Txs(til.SetPool0)
 68	// assert.Nil(t, err)
 69
 70	coordIdxs := []common.Idx{256, 257, 258, 259}
 71
 72	// add tokens to HistoryDB to avoid breaking FK constrains
 73	var tokens []common.Token
 74	for i := 0; i < int(tc.LastRegisteredTokenID); i++ {
 75		tokens = append(tokens, common.Token{
 76			TokenID:     common.TokenID(i + 1),
 77			EthBlockNum: 1,
 78			EthAddr:     ethCommon.BytesToAddress([]byte{byte(i + 1)}),
 79			Name:        strconv.Itoa(i),
 80			Symbol:      strconv.Itoa(i),
 81			Decimals:    18,
 82		})
 83	}
 84	addTokens(t, tokens, txsel.l2db.DB())
 85
 86	ptc := statedb.ProcessTxsConfig{
 87		NLevels:  32,
 88		MaxFeeTx: 64,
 89		MaxTx:    512,
 90		MaxL1Tx:  64,
 91	}
 92	// Process the 1st batch, which contains the L1CoordinatorTxs necessary
 93	// to create the Coordinator accounts to receive the fees
 94	_, err = txsel.localAccountsDB.ProcessTxs(ptc, nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
 95	require.Nil(t, err)
 96
 97	// add the 1st batch of transactions to the TxSelector
 98	addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[0].L2Txs))
 99
100	l1CoordTxs, l2Txs, err := txsel.GetL2TxSelection(coordIdxs, 0)
101	assert.Nil(t, err)
102	assert.Equal(t, 0, len(l2Txs))
103	assert.Equal(t, 0, len(l1CoordTxs))
104
105	_, _, _, err = txsel.GetL1L2TxSelection(coordIdxs, 0, blocks[0].Rollup.L1UserTxs)
106	assert.Nil(t, err)
107
108	// TODO once L2DB is updated to return error in case that AddTxTest
109	// fails, and the Til is updated, update this test, checking that the
110	// selected PoolL2Tx are correctly sorted by Nonce
111
112	// TODO once L2DB is updated to store the parameter AbsoluteFee (which
113	// is used by TxSelector to sort L2Txs), uncomment this next lines of
114	// test, and put the expected value for
115	// l2Txs[len(l2Txs)-1].AbsoluteFee, which is the Tx which has the
116	// Fee==192.
117	/*
118		// add the 3rd batch of transactions to the TxSelector
119		addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs))
120
121		_, l2Txs, err = txsel.GetL2TxSelection(coordIdxs, 0)
122		assert.Nil(t, err)
123		for _, tx := range l2Txs {
124			fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
125		}
126		require.Equal(t, 10, len(l2Txs))
127		assert.Equal(t, float64(0), l2Txs[0].AbsoluteFee)
128
129		fmt.Println(l2Txs[len(l2Txs)-1].Amount)
130		assert.Equal(t, float64(4), l2Txs[len(l2Txs)-1].AbsoluteFee)
131	*/
132}