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
2.7 KiB

  1. package txselector
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "testing"
  6. "github.com/hermeznetwork/hermez-node/db/l2db"
  7. "github.com/hermeznetwork/hermez-node/db/statedb"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. /*
  12. func initTestDB(l2 *l2db.L2DB, sdb *statedb.StateDB) *mock.MockDB {
  13. txs := []common.Tx{
  14. {
  15. FromIdx: common.Idx(0),
  16. ToIdx: common.Idx(1),
  17. TokenID: 1,
  18. Nonce: 1,
  19. AbsoluteFee: 1,
  20. },
  21. {
  22. FromIdx: common.Idx(0),
  23. ToIdx: common.Idx(1),
  24. TokenID: 1,
  25. Nonce: 2,
  26. AbsoluteFee: 3,
  27. },
  28. {
  29. FromIdx: common.Idx(0),
  30. ToIdx: common.Idx(1),
  31. TokenID: 1,
  32. Nonce: 4,
  33. AbsoluteFee: 6,
  34. },
  35. {
  36. FromIdx: common.Idx(0),
  37. ToIdx: common.Idx(1),
  38. TokenID: 1,
  39. Nonce: 4,
  40. AbsoluteFee: 4,
  41. },
  42. {
  43. ToIdx: common.Idx(1),
  44. FromIdx: common.Idx(0),
  45. TokenID: 1,
  46. Nonce: 1,
  47. AbsoluteFee: 4,
  48. },
  49. {
  50. ToIdx: common.Idx(1),
  51. FromIdx: common.Idx(0),
  52. TokenID: 1,
  53. Nonce: 2,
  54. AbsoluteFee: 3,
  55. },
  56. {
  57. ToIdx: common.Idx(1),
  58. FromIdx: common.Idx(0),
  59. TokenID: 1,
  60. Nonce: 3,
  61. AbsoluteFee: 5,
  62. },
  63. {
  64. // this tx will not be selected, as the ToEthAddr does not have an account
  65. FromIdx: common.Idx(1),
  66. ToIdx: common.Idx(2),
  67. TokenID: 1,
  68. Nonce: 4,
  69. AbsoluteFee: 5,
  70. },
  71. }
  72. // n := 0
  73. nBatch := 0
  74. for i := 0; i < len(txs); i++ {
  75. // for i := 0; i < nBatch; i++ {
  76. // for j := 0; j < len(txs)/nBatch; j++ {
  77. // store tx
  78. l2db.AddTx(uint64(nBatch), txs[i])
  79. // store account if not yet
  80. accountID := getAccountID(txs[i].FromEthAddr, txs[i].TokenID)
  81. if _, ok := m.AccountDB[accountID]; !ok {
  82. account := common.Account{
  83. // EthAddr: txs[i].FromEthAddr,
  84. TokenID: txs[i].TokenID,
  85. Nonce: 0,
  86. Balance: big.NewInt(0),
  87. }
  88. m.AccountDB[accountID] = account
  89. }
  90. // n++
  91. // }
  92. }
  93. return m
  94. }
  95. */
  96. func TestGetL2TxSelection(t *testing.T) {
  97. dir, err := ioutil.TempDir("", "tmpdb")
  98. require.Nil(t, err)
  99. sdb, err := statedb.NewStateDB(dir, false, 0)
  100. assert.Nil(t, err)
  101. testL2DB := &l2db.L2DB{}
  102. // initTestDB(testL2DB, sdb)
  103. txselDir, err := ioutil.TempDir("", "tmpTxSelDB")
  104. require.Nil(t, err)
  105. txsel, err := NewTxSelector(txselDir, sdb, testL2DB, 3, 3, 3)
  106. assert.Nil(t, err)
  107. fmt.Println(txsel)
  108. // txs, err := txsel.GetL2TxSelection(0)
  109. // assert.Nil(t, err)
  110. // for _, tx := range txs {
  111. // fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
  112. // }
  113. // assert.Equal(t, 3, len(txs))
  114. // assert.Equal(t, uint64(6), txs[0].AbsoluteFee)
  115. // assert.Equal(t, uint64(5), txs[1].AbsoluteFee)
  116. // assert.Equal(t, uint64(4), txs[2].AbsoluteFee)
  117. }