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.

81 lines
2.3 KiB

  1. package txselector
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. dbUtils "github.com/hermeznetwork/hermez-node/db"
  9. "github.com/hermeznetwork/hermez-node/db/historydb"
  10. "github.com/hermeznetwork/hermez-node/db/l2db"
  11. "github.com/hermeznetwork/hermez-node/db/statedb"
  12. "github.com/hermeznetwork/hermez-node/test"
  13. "github.com/jmoiron/sqlx"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func initTest(t *testing.T, testSet string) *TxSelector {
  18. pass := os.Getenv("POSTGRES_PASS")
  19. db, err := dbUtils.InitSQLDB(5432, "localhost", "hermez", pass, "hermez")
  20. require.Nil(t, err)
  21. l2DB := l2db.NewL2DB(db, 10, 100, 24*time.Hour)
  22. dir, err := ioutil.TempDir("", "tmpdb")
  23. require.Nil(t, err)
  24. sdb, err := statedb.NewStateDB(dir, statedb.TypeTxSelector, 0)
  25. require.Nil(t, err)
  26. txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
  27. require.Nil(t, err)
  28. txsel, err := NewTxSelector(txselDir, sdb, l2DB, 100, 100, 1000)
  29. require.Nil(t, err)
  30. return txsel
  31. }
  32. func addL2Txs(t *testing.T, txsel *TxSelector, poolL2Txs []common.PoolL2Tx) {
  33. for i := 0; i < len(poolL2Txs); i++ {
  34. err := txsel.l2db.AddTxTest(&poolL2Txs[i])
  35. require.Nil(t, err)
  36. }
  37. }
  38. func addTokens(t *testing.T, tokens []common.Token, db *sqlx.DB) {
  39. hdb := historydb.NewHistoryDB(db)
  40. assert.NoError(t, hdb.Reorg(-1))
  41. assert.NoError(t, hdb.AddBlock(&common.Block{
  42. EthBlockNum: 1,
  43. }))
  44. assert.NoError(t, hdb.AddTokens(tokens))
  45. }
  46. func TestGetL2TxSelection(t *testing.T) {
  47. txsel := initTest(t, test.SetTest0)
  48. test.CleanL2DB(txsel.l2db.DB())
  49. // generate test transactions
  50. l1Txs, _, poolL2Txs, tokens := test.GenerateTestTxsFromSet(t, test.SetTest0)
  51. // add tokens to HistoryDB to avoid breaking FK constrains
  52. addTokens(t, tokens, txsel.l2db.DB())
  53. // add the first batch of transactions to the TxSelector
  54. addL2Txs(t, txsel, poolL2Txs[0])
  55. _, err := txsel.GetL2TxSelection(0)
  56. assert.Nil(t, err)
  57. _, _, _, err = txsel.GetL1L2TxSelection(0, l1Txs[0])
  58. assert.Nil(t, err)
  59. // txs, err := txsel.GetL2TxSelection(0)
  60. // assert.Nil(t, err)
  61. // for _, tx := range txs {
  62. // fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
  63. // }
  64. // assert.Equal(t, 3, len(txs))
  65. // assert.Equal(t, uint64(6), txs[0].AbsoluteFee)
  66. // assert.Equal(t, uint64(5), txs[1].AbsoluteFee)
  67. // assert.Equal(t, uint64(4), txs[2].AbsoluteFee)
  68. }