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.

66 lines
1.8 KiB

  1. package txselector
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/hermeznetwork/hermez-node/common"
  8. "github.com/hermeznetwork/hermez-node/db/l2db"
  9. "github.com/hermeznetwork/hermez-node/db/statedb"
  10. "github.com/hermeznetwork/hermez-node/test"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func initTest(t *testing.T, testSet string) *TxSelector {
  15. pass := os.Getenv("POSTGRES_PASS")
  16. l2DB, err := l2db.NewL2DB(5432, "localhost", "hermez", pass, "l2", 10, 512, 24*time.Hour)
  17. require.Nil(t, err)
  18. dir, err := ioutil.TempDir("", "tmpdb")
  19. require.Nil(t, err)
  20. sdb, err := statedb.NewStateDB(dir, false, 0)
  21. require.Nil(t, err)
  22. txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
  23. require.Nil(t, err)
  24. txsel, err := NewTxSelector(txselDir, sdb, l2DB, 100, 100, 1000)
  25. require.Nil(t, err)
  26. return txsel
  27. }
  28. func addL2Txs(t *testing.T, txsel *TxSelector, poolL2Txs []*common.PoolL2Tx) {
  29. for i := 0; i < len(poolL2Txs); i++ {
  30. err := txsel.l2db.AddTx(poolL2Txs[i])
  31. require.Nil(t, err)
  32. }
  33. }
  34. func TestGetL2TxSelection(t *testing.T) {
  35. txsel := initTest(t, test.SetTest0)
  36. test.CleanL2DB(txsel.l2db.DB())
  37. // generate test transactions
  38. l1Txs, _, poolL2Txs := test.GenerateTestTxsFromSet(t, test.SetTest0)
  39. // add the first batch of transactions to the TxSelector
  40. addL2Txs(t, txsel, poolL2Txs[0])
  41. _, err := txsel.GetL2TxSelection(0)
  42. assert.Nil(t, err)
  43. _, _, _, err = txsel.GetL1L2TxSelection(0, l1Txs[0])
  44. assert.Nil(t, err)
  45. // txs, err := txsel.GetL2TxSelection(0)
  46. // assert.Nil(t, err)
  47. // for _, tx := range txs {
  48. // fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
  49. // }
  50. // assert.Equal(t, 3, len(txs))
  51. // assert.Equal(t, uint64(6), txs[0].AbsoluteFee)
  52. // assert.Equal(t, uint64(5), txs[1].AbsoluteFee)
  53. // assert.Equal(t, uint64(4), txs[2].AbsoluteFee)
  54. }