You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.2 KiB

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