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.

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