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.

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