transaction_pool_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package core
  2. import (
  3. "crypto/ecdsa"
  4. "math/big"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/core/state"
  8. "github.com/ethereum/go-ethereum/core/types"
  9. "github.com/ethereum/go-ethereum/crypto"
  10. "github.com/ethereum/go-ethereum/ethdb"
  11. "github.com/ethereum/go-ethereum/event"
  12. )
  13. func transaction() *types.Transaction {
  14. return types.NewTransactionMessage(common.Address{}, big.NewInt(100), big.NewInt(100), big.NewInt(100), nil)
  15. }
  16. func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
  17. db, _ := ethdb.NewMemDatabase()
  18. statedb := state.New(common.Hash{}, db)
  19. var m event.TypeMux
  20. key, _ := crypto.GenerateKey()
  21. return NewTxPool(&m, func() *state.StateDB { return statedb }, func() *big.Int { return big.NewInt(1000000) }), key
  22. }
  23. func TestInvalidTransactions(t *testing.T) {
  24. pool, key := setupTxPool()
  25. tx := transaction()
  26. tx.SignECDSA(key)
  27. err := pool.Add(tx)
  28. if err != ErrNonExistentAccount {
  29. t.Error("expected", ErrNonExistentAccount)
  30. }
  31. from, _ := tx.From()
  32. pool.state.AddBalance(from, big.NewInt(1))
  33. err = pool.Add(tx)
  34. if err != ErrInsufficientFunds {
  35. t.Error("expected", ErrInsufficientFunds)
  36. }
  37. balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
  38. pool.state.AddBalance(from, balance)
  39. err = pool.Add(tx)
  40. if err != ErrIntrinsicGas {
  41. t.Error("expected", ErrIntrinsicGas, "got", err)
  42. }
  43. pool.state.SetNonce(from, 1)
  44. pool.state.AddBalance(from, big.NewInt(0xffffffffffffff))
  45. tx.GasLimit = big.NewInt(100000)
  46. tx.Price = big.NewInt(1)
  47. tx.SignECDSA(key)
  48. err = pool.Add(tx)
  49. if err != ErrNonce {
  50. t.Error("expected", ErrNonce)
  51. }
  52. }
  53. func TestTransactionQueue(t *testing.T) {
  54. pool, key := setupTxPool()
  55. tx := transaction()
  56. tx.SignECDSA(key)
  57. from, _ := tx.From()
  58. pool.state.AddBalance(from, big.NewInt(1))
  59. pool.queueTx(tx.Hash(), tx)
  60. pool.checkQueue()
  61. if len(pool.txs) != 1 {
  62. t.Error("expected valid txs to be 1 is", len(pool.txs))
  63. }
  64. tx = transaction()
  65. tx.SetNonce(1)
  66. tx.SignECDSA(key)
  67. from, _ = tx.From()
  68. pool.state.SetNonce(from, 2)
  69. pool.queueTx(tx.Hash(), tx)
  70. pool.checkQueue()
  71. if _, ok := pool.txs[tx.Hash()]; ok {
  72. t.Error("expected transaction to be in tx pool")
  73. }
  74. if len(pool.queue[from]) > 0 {
  75. t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
  76. }
  77. pool, key = setupTxPool()
  78. tx1, tx2, tx3 := transaction(), transaction(), transaction()
  79. tx2.SetNonce(10)
  80. tx3.SetNonce(11)
  81. tx1.SignECDSA(key)
  82. tx2.SignECDSA(key)
  83. tx3.SignECDSA(key)
  84. pool.queueTx(tx1.Hash(), tx1)
  85. pool.queueTx(tx2.Hash(), tx2)
  86. pool.queueTx(tx3.Hash(), tx3)
  87. from, _ = tx1.From()
  88. pool.checkQueue()
  89. if len(pool.txs) != 1 {
  90. t.Error("expected tx pool to be 1 =")
  91. }
  92. if len(pool.queue[from]) != 2 {
  93. t.Error("expected len(queue) == 2, got", len(pool.queue[from]))
  94. }
  95. }
  96. func TestRemoveTx(t *testing.T) {
  97. pool, key := setupTxPool()
  98. tx := transaction()
  99. tx.SignECDSA(key)
  100. from, _ := tx.From()
  101. pool.state.AddBalance(from, big.NewInt(1))
  102. pool.queueTx(tx.Hash(), tx)
  103. pool.addTx(tx.Hash(), tx)
  104. if len(pool.queue) != 1 {
  105. t.Error("expected queue to be 1, got", len(pool.queue))
  106. }
  107. if len(pool.txs) != 1 {
  108. t.Error("expected txs to be 1, got", len(pool.txs))
  109. }
  110. pool.removeTx(tx.Hash())
  111. if len(pool.queue) > 0 {
  112. t.Error("expected queue to be 0, got", len(pool.queue))
  113. }
  114. if len(pool.txs) > 0 {
  115. t.Error("expected txs to be 0, got", len(pool.txs))
  116. }
  117. }
  118. func TestNegativeValue(t *testing.T) {
  119. pool, key := setupTxPool()
  120. tx := transaction()
  121. tx.Value().Set(big.NewInt(-1))
  122. tx.SignECDSA(key)
  123. from, _ := tx.From()
  124. pool.state.AddBalance(from, big.NewInt(1))
  125. err := pool.Add(tx)
  126. if err != ErrNegativeValue {
  127. t.Error("expected", ErrNegativeValue, "got", err)
  128. }
  129. }