transaction_pool_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "crypto/ecdsa"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/state"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/event"
  27. )
  28. func transaction(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
  29. tx, _ := types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, big.NewInt(1), nil).SignECDSA(key)
  30. return tx
  31. }
  32. func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
  33. db, _ := ethdb.NewMemDatabase()
  34. statedb := state.New(common.Hash{}, db)
  35. var m event.TypeMux
  36. key, _ := crypto.GenerateKey()
  37. return NewTxPool(&m, func() *state.StateDB { return statedb }, func() *big.Int { return big.NewInt(1000000) }), key
  38. }
  39. func TestInvalidTransactions(t *testing.T) {
  40. pool, key := setupTxPool()
  41. tx := transaction(0, big.NewInt(100), key)
  42. if err := pool.Add(tx); err != ErrNonExistentAccount {
  43. t.Error("expected", ErrNonExistentAccount)
  44. }
  45. from, _ := tx.From()
  46. pool.currentState().AddBalance(from, big.NewInt(1))
  47. if err := pool.Add(tx); err != ErrInsufficientFunds {
  48. t.Error("expected", ErrInsufficientFunds)
  49. }
  50. balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
  51. pool.currentState().AddBalance(from, balance)
  52. if err := pool.Add(tx); err != ErrIntrinsicGas {
  53. t.Error("expected", ErrIntrinsicGas, "got", err)
  54. }
  55. pool.currentState().SetNonce(from, 1)
  56. pool.currentState().AddBalance(from, big.NewInt(0xffffffffffffff))
  57. tx = transaction(0, big.NewInt(100000), key)
  58. if err := pool.Add(tx); err != ErrNonce {
  59. t.Error("expected", ErrNonce)
  60. }
  61. }
  62. func TestTransactionQueue(t *testing.T) {
  63. pool, key := setupTxPool()
  64. tx := transaction(0, big.NewInt(100), key)
  65. from, _ := tx.From()
  66. pool.currentState().AddBalance(from, big.NewInt(1))
  67. pool.queueTx(tx.Hash(), tx)
  68. pool.checkQueue()
  69. if len(pool.pending) != 1 {
  70. t.Error("expected valid txs to be 1 is", len(pool.pending))
  71. }
  72. tx = transaction(1, big.NewInt(100), key)
  73. from, _ = tx.From()
  74. pool.currentState().SetNonce(from, 2)
  75. pool.queueTx(tx.Hash(), tx)
  76. pool.checkQueue()
  77. if _, ok := pool.pending[tx.Hash()]; ok {
  78. t.Error("expected transaction to be in tx pool")
  79. }
  80. if len(pool.queue[from]) > 0 {
  81. t.Error("expected transaction queue to be empty. is", len(pool.queue[from]))
  82. }
  83. pool, key = setupTxPool()
  84. tx1 := transaction(0, big.NewInt(100), key)
  85. tx2 := transaction(10, big.NewInt(100), key)
  86. tx3 := transaction(11, big.NewInt(100), key)
  87. pool.queueTx(tx1.Hash(), tx1)
  88. pool.queueTx(tx2.Hash(), tx2)
  89. pool.queueTx(tx3.Hash(), tx3)
  90. from, _ = tx1.From()
  91. pool.checkQueue()
  92. if len(pool.pending) != 1 {
  93. t.Error("expected tx pool to be 1 =")
  94. }
  95. if len(pool.queue[from]) != 2 {
  96. t.Error("expected len(queue) == 2, got", len(pool.queue[from]))
  97. }
  98. }
  99. func TestRemoveTx(t *testing.T) {
  100. pool, key := setupTxPool()
  101. tx := transaction(0, big.NewInt(100), key)
  102. from, _ := tx.From()
  103. pool.currentState().AddBalance(from, big.NewInt(1))
  104. pool.queueTx(tx.Hash(), tx)
  105. pool.addTx(tx.Hash(), from, tx)
  106. if len(pool.queue) != 1 {
  107. t.Error("expected queue to be 1, got", len(pool.queue))
  108. }
  109. if len(pool.pending) != 1 {
  110. t.Error("expected txs to be 1, got", len(pool.pending))
  111. }
  112. pool.removeTx(tx.Hash())
  113. if len(pool.queue) > 0 {
  114. t.Error("expected queue to be 0, got", len(pool.queue))
  115. }
  116. if len(pool.pending) > 0 {
  117. t.Error("expected txs to be 0, got", len(pool.pending))
  118. }
  119. }
  120. func TestNegativeValue(t *testing.T) {
  121. pool, key := setupTxPool()
  122. tx, _ := types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil).SignECDSA(key)
  123. from, _ := tx.From()
  124. pool.currentState().AddBalance(from, big.NewInt(1))
  125. if err := pool.Add(tx); err != ErrNegativeValue {
  126. t.Error("expected", ErrNegativeValue, "got", err)
  127. }
  128. }
  129. func TestTransactionChainFork(t *testing.T) {
  130. pool, key := setupTxPool()
  131. addr := crypto.PubkeyToAddress(key.PublicKey)
  132. resetState := func() {
  133. db, _ := ethdb.NewMemDatabase()
  134. statedb := state.New(common.Hash{}, db)
  135. pool.currentState = func() *state.StateDB { return statedb }
  136. pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
  137. pool.resetState()
  138. }
  139. resetState()
  140. tx := transaction(0, big.NewInt(100000), key)
  141. if err := pool.add(tx); err != nil {
  142. t.Error("didn't expect error", err)
  143. }
  144. pool.RemoveTransactions([]*types.Transaction{tx})
  145. // reset the pool's internal state
  146. resetState()
  147. if err := pool.add(tx); err != nil {
  148. t.Error("didn't expect error", err)
  149. }
  150. }
  151. func TestTransactionDoubleNonce(t *testing.T) {
  152. pool, key := setupTxPool()
  153. addr := crypto.PubkeyToAddress(key.PublicKey)
  154. resetState := func() {
  155. db, _ := ethdb.NewMemDatabase()
  156. statedb := state.New(common.Hash{}, db)
  157. pool.currentState = func() *state.StateDB { return statedb }
  158. pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
  159. pool.resetState()
  160. }
  161. resetState()
  162. tx := transaction(0, big.NewInt(100000), key)
  163. tx2 := transaction(0, big.NewInt(1000000), key)
  164. if err := pool.add(tx); err != nil {
  165. t.Error("didn't expect error", err)
  166. }
  167. if err := pool.add(tx2); err != nil {
  168. t.Error("didn't expect error", err)
  169. }
  170. pool.checkQueue()
  171. if len(pool.pending) != 2 {
  172. t.Error("expected 2 pending txs. Got", len(pool.pending))
  173. }
  174. }
  175. func TestMissingNonce(t *testing.T) {
  176. pool, key := setupTxPool()
  177. addr := crypto.PubkeyToAddress(key.PublicKey)
  178. pool.currentState().AddBalance(addr, big.NewInt(100000000000000))
  179. tx := transaction(1, big.NewInt(100000), key)
  180. if err := pool.add(tx); err != nil {
  181. t.Error("didn't expect error", err)
  182. }
  183. if len(pool.pending) != 0 {
  184. t.Error("expected 0 pending transactions, got", len(pool.pending))
  185. }
  186. if len(pool.queue[addr]) != 1 {
  187. t.Error("expected 1 queued transaction, got", len(pool.queue[addr]))
  188. }
  189. }