transaction_pool_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
  14. tx, _ := types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, big.NewInt(1), nil).SignECDSA(key)
  15. return tx
  16. }
  17. func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
  18. db, _ := ethdb.NewMemDatabase()
  19. statedb := state.New(common.Hash{}, db)
  20. var m event.TypeMux
  21. key, _ := crypto.GenerateKey()
  22. return NewTxPool(&m, func() *state.StateDB { return statedb }, func() *big.Int { return big.NewInt(1000000) }), key
  23. }
  24. func TestInvalidTransactions(t *testing.T) {
  25. pool, key := setupTxPool()
  26. tx := transaction(0, big.NewInt(100), key)
  27. if err := pool.Add(tx); err != ErrNonExistentAccount {
  28. t.Error("expected", ErrNonExistentAccount)
  29. }
  30. from, _ := tx.From()
  31. pool.currentState().AddBalance(from, big.NewInt(1))
  32. if err := pool.Add(tx); err != ErrInsufficientFunds {
  33. t.Error("expected", ErrInsufficientFunds)
  34. }
  35. balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
  36. pool.currentState().AddBalance(from, balance)
  37. if err := pool.Add(tx); err != ErrIntrinsicGas {
  38. t.Error("expected", ErrIntrinsicGas, "got", err)
  39. }
  40. pool.currentState().SetNonce(from, 1)
  41. pool.currentState().AddBalance(from, big.NewInt(0xffffffffffffff))
  42. tx = transaction(0, big.NewInt(100000), key)
  43. if err := pool.Add(tx); err != ErrNonce {
  44. t.Error("expected", ErrNonce)
  45. }
  46. }
  47. func TestTransactionQueue(t *testing.T) {
  48. pool, key := setupTxPool()
  49. tx := transaction(0, big.NewInt(100), key)
  50. from, _ := tx.From()
  51. pool.currentState().AddBalance(from, big.NewInt(1))
  52. pool.queueTx(tx.Hash(), tx)
  53. pool.checkQueue()
  54. if len(pool.pending) != 1 {
  55. t.Error("expected valid txs to be 1 is", len(pool.pending))
  56. }
  57. tx = transaction(1, big.NewInt(100), key)
  58. from, _ = tx.From()
  59. pool.currentState().SetNonce(from, 2)
  60. pool.queueTx(tx.Hash(), tx)
  61. pool.checkQueue()
  62. if _, ok := pool.pending[tx.Hash()]; ok {
  63. t.Error("expected transaction to be in tx pool")
  64. }
  65. if len(pool.queue[from]) > 0 {
  66. t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
  67. }
  68. pool, key = setupTxPool()
  69. tx1 := transaction(0, big.NewInt(100), key)
  70. tx2 := transaction(10, big.NewInt(100), key)
  71. tx3 := transaction(11, big.NewInt(100), key)
  72. pool.queueTx(tx1.Hash(), tx1)
  73. pool.queueTx(tx2.Hash(), tx2)
  74. pool.queueTx(tx3.Hash(), tx3)
  75. from, _ = tx1.From()
  76. pool.checkQueue()
  77. if len(pool.pending) != 1 {
  78. t.Error("expected tx pool to be 1 =")
  79. }
  80. if len(pool.queue[from]) != 2 {
  81. t.Error("expected len(queue) == 2, got", len(pool.queue[from]))
  82. }
  83. }
  84. func TestRemoveTx(t *testing.T) {
  85. pool, key := setupTxPool()
  86. tx := transaction(0, big.NewInt(100), key)
  87. from, _ := tx.From()
  88. pool.currentState().AddBalance(from, big.NewInt(1))
  89. pool.queueTx(tx.Hash(), tx)
  90. pool.addTx(tx.Hash(), from, tx)
  91. if len(pool.queue) != 1 {
  92. t.Error("expected queue to be 1, got", len(pool.queue))
  93. }
  94. if len(pool.pending) != 1 {
  95. t.Error("expected txs to be 1, got", len(pool.pending))
  96. }
  97. pool.removeTx(tx.Hash())
  98. if len(pool.queue) > 0 {
  99. t.Error("expected queue to be 0, got", len(pool.queue))
  100. }
  101. if len(pool.pending) > 0 {
  102. t.Error("expected txs to be 0, got", len(pool.pending))
  103. }
  104. }
  105. func TestNegativeValue(t *testing.T) {
  106. pool, key := setupTxPool()
  107. tx, _ := types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil).SignECDSA(key)
  108. from, _ := tx.From()
  109. pool.currentState().AddBalance(from, big.NewInt(1))
  110. if err := pool.Add(tx); err != ErrNegativeValue {
  111. t.Error("expected", ErrNegativeValue, "got", err)
  112. }
  113. }
  114. func TestTransactionChainFork(t *testing.T) {
  115. pool, key := setupTxPool()
  116. addr := crypto.PubkeyToAddress(key.PublicKey)
  117. resetState := func() {
  118. db, _ := ethdb.NewMemDatabase()
  119. statedb := state.New(common.Hash{}, db)
  120. pool.currentState = func() *state.StateDB { return statedb }
  121. pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
  122. pool.resetState()
  123. }
  124. resetState()
  125. tx := transaction(0, big.NewInt(100000), key)
  126. if err := pool.add(tx); err != nil {
  127. t.Error("didn't expect error", err)
  128. }
  129. pool.RemoveTransactions([]*types.Transaction{tx})
  130. // reset the pool's internal state
  131. resetState()
  132. if err := pool.add(tx); err != nil {
  133. t.Error("didn't expect error", err)
  134. }
  135. }
  136. func TestTransactionDoubleNonce(t *testing.T) {
  137. pool, key := setupTxPool()
  138. addr := crypto.PubkeyToAddress(key.PublicKey)
  139. resetState := func() {
  140. db, _ := ethdb.NewMemDatabase()
  141. statedb := state.New(common.Hash{}, db)
  142. pool.currentState = func() *state.StateDB { return statedb }
  143. pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
  144. pool.resetState()
  145. }
  146. resetState()
  147. tx := transaction(0, big.NewInt(100000), key)
  148. tx2 := transaction(0, big.NewInt(1000000), key)
  149. if err := pool.add(tx); err != nil {
  150. t.Error("didn't expect error", err)
  151. }
  152. if err := pool.add(tx2); err != nil {
  153. t.Error("didn't expect error", err)
  154. }
  155. pool.checkQueue()
  156. if len(pool.pending) != 2 {
  157. t.Error("expected 2 pending txs. Got", len(pool.pending))
  158. }
  159. }
  160. func TestMissingNonce(t *testing.T) {
  161. pool, key := setupTxPool()
  162. addr := crypto.PubkeyToAddress(key.PublicKey)
  163. pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
  164. tx := transaction(1, big.NewInt(100000), key)
  165. if err := pool.add(tx); err != nil {
  166. t.Error("didn't expect error", err)
  167. }
  168. if len(pool.pending) != 0 {
  169. t.Error("expected 0 pending transactions, got", len(pool.pending))
  170. }
  171. if len(pool.queue[addr]) != 1 {
  172. t.Error("expected 1 queued transaction, got", len(pool.queue[addr]))
  173. }
  174. }