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 EthBlockNum: 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 // Process the 1st batch, which contains the L1CoordinatorTxs necessary
87 // to create the Coordinator accounts to receive the fees
88 _, err = txsel.localAccountsDB.ProcessTxs(nil, nil, blocks[0].Rollup.Batches[0].L1CoordinatorTxs, nil)
89 require.Nil(t, err)
90
91 // add the 1st batch of transactions to the TxSelector
92 addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Rollup.Batches[0].L2Txs))
93
94 l1CoordTxs, l2Txs, err := txsel.GetL2TxSelection(coordIdxs, 0)
95 assert.Nil(t, err)
96 assert.Equal(t, 0, len(l2Txs))
97 assert.Equal(t, 0, len(l1CoordTxs))
98
99 _, _, _, err = txsel.GetL1L2TxSelection(coordIdxs, 0, blocks[0].Rollup.L1UserTxs)
100 assert.Nil(t, err)
101
102 // TODO once L2DB is updated to return error in case that AddTxTest
103 // fails, and the Til is updated, update this test, checking that the
104 // selected PoolL2Tx are correctly sorted by Nonce
105
106 // TODO once L2DB is updated to store the parameter AbsoluteFee (which
107 // is used by TxSelector to sort L2Txs), uncomment this next lines of
108 // test, and put the expected value for
109 // l2Txs[len(l2Txs)-1].AbsoluteFee, which is the Tx which has the
110 // Fee==192.
111 /*
112 // add the 3rd batch of transactions to the TxSelector
113 addL2Txs(t, txsel, common.L2TxsToPoolL2Txs(blocks[0].Batches[2].L2Txs))
114
115 _, l2Txs, err = txsel.GetL2TxSelection(coordIdxs, 0)
116 assert.Nil(t, err)
117 for _, tx := range l2Txs {
118 fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
119 }
120 require.Equal(t, 10, len(l2Txs))
121 assert.Equal(t, float64(0), l2Txs[0].AbsoluteFee)
122
123 fmt.Println(l2Txs[len(l2Txs)-1].Amount)
124 assert.Equal(t, float64(4), l2Txs[len(l2Txs)-1].AbsoluteFee)
125 */
126}