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.

116 lines
3.4 KiB

  1. package txselector
  2. import (
  3. "fmt"
  4. "math/big"
  5. "testing"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/hermeznetwork/hermez-node/tx-selector/common"
  8. "github.com/hermeznetwork/hermez-node/tx-selector/mock"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func initMockDB() *mock.MockDB {
  12. m := mock.New()
  13. txs := []common.Tx{
  14. {
  15. FromEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
  16. ToEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
  17. TokenID: 1,
  18. Nonce: 1,
  19. UserFeeAbsolute: 1,
  20. },
  21. {
  22. FromEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
  23. ToEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
  24. TokenID: 1,
  25. Nonce: 2,
  26. UserFeeAbsolute: 3,
  27. },
  28. {
  29. FromEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
  30. ToEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
  31. TokenID: 1,
  32. Nonce: 4,
  33. UserFeeAbsolute: 6,
  34. },
  35. {
  36. FromEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
  37. ToEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
  38. TokenID: 1,
  39. Nonce: 4,
  40. UserFeeAbsolute: 4,
  41. },
  42. {
  43. FromEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
  44. ToEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
  45. TokenID: 1,
  46. Nonce: 1,
  47. UserFeeAbsolute: 4,
  48. },
  49. {
  50. FromEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
  51. ToEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
  52. TokenID: 1,
  53. Nonce: 2,
  54. UserFeeAbsolute: 3,
  55. },
  56. {
  57. FromEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
  58. ToEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
  59. TokenID: 1,
  60. Nonce: 3,
  61. UserFeeAbsolute: 5,
  62. },
  63. {
  64. // this tx will not be selected, as the ToEthAddr does not have an account
  65. FromEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
  66. ToEthAddr: ethCommon.HexToAddress("0x4a2CFDF534725D8D6e07Af97B237Fff19BDb3c93"),
  67. TokenID: 1,
  68. Nonce: 4,
  69. UserFeeAbsolute: 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. m.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. func TestGetL2TxSelection(t *testing.T) {
  96. mockDB := initMockDB()
  97. txsel := NewTxSelector(mockDB, 3, 3, 3)
  98. txs, err := txsel.GetL2TxSelection(0)
  99. assert.Nil(t, err)
  100. for _, tx := range txs {
  101. fmt.Println(tx.FromEthAddr.String(), tx.ToEthAddr.String(), tx.UserFeeAbsolute)
  102. }
  103. assert.Equal(t, 3, len(txs))
  104. assert.Equal(t, uint64(6), txs[0].UserFeeAbsolute)
  105. assert.Equal(t, uint64(5), txs[1].UserFeeAbsolute)
  106. assert.Equal(t, uint64(4), txs[2].UserFeeAbsolute)
  107. }