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