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.

125 lines
2.6 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, false, 0)
  100. assert.Nil(t, err)
  101. testL2DB := &l2db.L2DB{}
  102. // initTestDB(testL2DB, sdb)
  103. txsel, err := NewTxSelector(sdb, testL2DB, 3, 3, 3)
  104. assert.Nil(t, err)
  105. fmt.Println(txsel)
  106. // txs, err := txsel.GetL2TxSelection(0)
  107. // assert.Nil(t, err)
  108. // for _, tx := range txs {
  109. // fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
  110. // }
  111. // assert.Equal(t, 3, len(txs))
  112. // assert.Equal(t, uint64(6), txs[0].AbsoluteFee)
  113. // assert.Equal(t, uint64(5), txs[1].AbsoluteFee)
  114. // assert.Equal(t, uint64(4), txs[2].AbsoluteFee)
  115. }