tx_pool_test.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. "math/rand"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/state"
  25. "github.com/ethereum/go-ethereum/core/types"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/ethdb"
  28. "github.com/ethereum/go-ethereum/event"
  29. )
  30. func transaction(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
  31. tx, _ := types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, big.NewInt(1), nil).SignECDSA(types.HomesteadSigner{}, key)
  32. return tx
  33. }
  34. func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
  35. db, _ := ethdb.NewMemDatabase()
  36. statedb, _ := state.New(common.Hash{}, db)
  37. key, _ := crypto.GenerateKey()
  38. newPool := NewTxPool(testChainConfig(), new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
  39. newPool.resetState()
  40. return newPool, key
  41. }
  42. func deriveSender(tx *types.Transaction) (common.Address, error) {
  43. return types.Sender(types.HomesteadSigner{}, tx)
  44. }
  45. // This test simulates a scenario where a new block is imported during a
  46. // state reset and tests whether the pending state is in sync with the
  47. // block head event that initiated the resetState().
  48. func TestStateChangeDuringPoolReset(t *testing.T) {
  49. var (
  50. db, _ = ethdb.NewMemDatabase()
  51. key, _ = crypto.GenerateKey()
  52. address = crypto.PubkeyToAddress(key.PublicKey)
  53. mux = new(event.TypeMux)
  54. statedb, _ = state.New(common.Hash{}, db)
  55. trigger = false
  56. )
  57. // setup pool with 2 transaction in it
  58. statedb.SetBalance(address, new(big.Int).Mul(common.Big1, common.Ether))
  59. tx0 := transaction(0, big.NewInt(100000), key)
  60. tx1 := transaction(1, big.NewInt(100000), key)
  61. // stateFunc is used multiple times to reset the pending state.
  62. // when simulate is true it will create a state that indicates
  63. // that tx0 and tx1 are included in the chain.
  64. stateFunc := func() (*state.StateDB, error) {
  65. // delay "state change" by one. The tx pool fetches the
  66. // state multiple times and by delaying it a bit we simulate
  67. // a state change between those fetches.
  68. stdb := statedb
  69. if trigger {
  70. statedb, _ = state.New(common.Hash{}, db)
  71. // simulate that the new head block included tx0 and tx1
  72. statedb.SetNonce(address, 2)
  73. statedb.SetBalance(address, new(big.Int).Mul(common.Big1, common.Ether))
  74. trigger = false
  75. }
  76. return stdb, nil
  77. }
  78. gasLimitFunc := func() *big.Int { return big.NewInt(1000000000) }
  79. txpool := NewTxPool(testChainConfig(), mux, stateFunc, gasLimitFunc)
  80. txpool.resetState()
  81. nonce := txpool.State().GetNonce(address)
  82. if nonce != 0 {
  83. t.Fatalf("Invalid nonce, want 0, got %d", nonce)
  84. }
  85. txpool.AddBatch(types.Transactions{tx0, tx1})
  86. nonce = txpool.State().GetNonce(address)
  87. if nonce != 2 {
  88. t.Fatalf("Invalid nonce, want 2, got %d", nonce)
  89. }
  90. // trigger state change in the background
  91. trigger = true
  92. txpool.resetState()
  93. pendingTx, err := txpool.Pending()
  94. if err != nil {
  95. t.Fatalf("Could not fetch pending transactions: %v", err)
  96. }
  97. for addr, txs := range pendingTx {
  98. t.Logf("%0x: %d\n", addr, len(txs))
  99. }
  100. nonce = txpool.State().GetNonce(address)
  101. if nonce != 2 {
  102. t.Fatalf("Invalid nonce, want 2, got %d", nonce)
  103. }
  104. }
  105. func TestInvalidTransactions(t *testing.T) {
  106. pool, key := setupTxPool()
  107. tx := transaction(0, big.NewInt(100), key)
  108. if err := pool.Add(tx); err != ErrNonExistentAccount {
  109. t.Error("expected", ErrNonExistentAccount)
  110. }
  111. from, _ := deriveSender(tx)
  112. currentState, _ := pool.currentState()
  113. currentState.AddBalance(from, big.NewInt(1))
  114. if err := pool.Add(tx); err != ErrInsufficientFunds {
  115. t.Error("expected", ErrInsufficientFunds)
  116. }
  117. balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
  118. currentState.AddBalance(from, balance)
  119. if err := pool.Add(tx); err != ErrIntrinsicGas {
  120. t.Error("expected", ErrIntrinsicGas, "got", err)
  121. }
  122. currentState.SetNonce(from, 1)
  123. currentState.AddBalance(from, big.NewInt(0xffffffffffffff))
  124. tx = transaction(0, big.NewInt(100000), key)
  125. if err := pool.Add(tx); err != ErrNonce {
  126. t.Error("expected", ErrNonce)
  127. }
  128. tx = transaction(1, big.NewInt(100000), key)
  129. pool.minGasPrice = big.NewInt(1000)
  130. if err := pool.Add(tx); err != ErrCheap {
  131. t.Error("expected", ErrCheap, "got", err)
  132. }
  133. pool.SetLocal(tx)
  134. if err := pool.Add(tx); err != nil {
  135. t.Error("expected", nil, "got", err)
  136. }
  137. }
  138. func TestTransactionQueue(t *testing.T) {
  139. pool, key := setupTxPool()
  140. tx := transaction(0, big.NewInt(100), key)
  141. from, _ := deriveSender(tx)
  142. currentState, _ := pool.currentState()
  143. currentState.AddBalance(from, big.NewInt(1000))
  144. pool.resetState()
  145. pool.enqueueTx(tx.Hash(), tx)
  146. pool.promoteExecutables(currentState)
  147. if len(pool.pending) != 1 {
  148. t.Error("expected valid txs to be 1 is", len(pool.pending))
  149. }
  150. tx = transaction(1, big.NewInt(100), key)
  151. from, _ = deriveSender(tx)
  152. currentState.SetNonce(from, 2)
  153. pool.enqueueTx(tx.Hash(), tx)
  154. pool.promoteExecutables(currentState)
  155. if _, ok := pool.pending[from].txs.items[tx.Nonce()]; ok {
  156. t.Error("expected transaction to be in tx pool")
  157. }
  158. if len(pool.queue) > 0 {
  159. t.Error("expected transaction queue to be empty. is", len(pool.queue))
  160. }
  161. pool, key = setupTxPool()
  162. tx1 := transaction(0, big.NewInt(100), key)
  163. tx2 := transaction(10, big.NewInt(100), key)
  164. tx3 := transaction(11, big.NewInt(100), key)
  165. from, _ = deriveSender(tx1)
  166. currentState, _ = pool.currentState()
  167. currentState.AddBalance(from, big.NewInt(1000))
  168. pool.resetState()
  169. pool.enqueueTx(tx1.Hash(), tx1)
  170. pool.enqueueTx(tx2.Hash(), tx2)
  171. pool.enqueueTx(tx3.Hash(), tx3)
  172. pool.promoteExecutables(currentState)
  173. if len(pool.pending) != 1 {
  174. t.Error("expected tx pool to be 1, got", len(pool.pending))
  175. }
  176. if pool.queue[from].Len() != 2 {
  177. t.Error("expected len(queue) == 2, got", pool.queue[from].Len())
  178. }
  179. }
  180. func TestRemoveTx(t *testing.T) {
  181. pool, key := setupTxPool()
  182. tx := transaction(0, big.NewInt(100), key)
  183. from, _ := deriveSender(tx)
  184. currentState, _ := pool.currentState()
  185. currentState.AddBalance(from, big.NewInt(1))
  186. pool.enqueueTx(tx.Hash(), tx)
  187. pool.promoteTx(from, tx.Hash(), tx)
  188. if len(pool.queue) != 1 {
  189. t.Error("expected queue to be 1, got", len(pool.queue))
  190. }
  191. if len(pool.pending) != 1 {
  192. t.Error("expected pending to be 1, got", len(pool.pending))
  193. }
  194. pool.Remove(tx.Hash())
  195. if len(pool.queue) > 0 {
  196. t.Error("expected queue to be 0, got", len(pool.queue))
  197. }
  198. if len(pool.pending) > 0 {
  199. t.Error("expected pending to be 0, got", len(pool.pending))
  200. }
  201. }
  202. func TestNegativeValue(t *testing.T) {
  203. pool, key := setupTxPool()
  204. tx, _ := types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil).SignECDSA(types.HomesteadSigner{}, key)
  205. from, _ := deriveSender(tx)
  206. currentState, _ := pool.currentState()
  207. currentState.AddBalance(from, big.NewInt(1))
  208. if err := pool.Add(tx); err != ErrNegativeValue {
  209. t.Error("expected", ErrNegativeValue, "got", err)
  210. }
  211. }
  212. func TestTransactionChainFork(t *testing.T) {
  213. pool, key := setupTxPool()
  214. addr := crypto.PubkeyToAddress(key.PublicKey)
  215. resetState := func() {
  216. db, _ := ethdb.NewMemDatabase()
  217. statedb, _ := state.New(common.Hash{}, db)
  218. pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
  219. currentState, _ := pool.currentState()
  220. currentState.AddBalance(addr, big.NewInt(100000000000000))
  221. pool.resetState()
  222. }
  223. resetState()
  224. tx := transaction(0, big.NewInt(100000), key)
  225. if err := pool.add(tx); err != nil {
  226. t.Error("didn't expect error", err)
  227. }
  228. pool.RemoveBatch([]*types.Transaction{tx})
  229. // reset the pool's internal state
  230. resetState()
  231. if err := pool.add(tx); err != nil {
  232. t.Error("didn't expect error", err)
  233. }
  234. }
  235. func TestTransactionDoubleNonce(t *testing.T) {
  236. pool, key := setupTxPool()
  237. addr := crypto.PubkeyToAddress(key.PublicKey)
  238. resetState := func() {
  239. db, _ := ethdb.NewMemDatabase()
  240. statedb, _ := state.New(common.Hash{}, db)
  241. pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
  242. currentState, _ := pool.currentState()
  243. currentState.AddBalance(addr, big.NewInt(100000000000000))
  244. pool.resetState()
  245. }
  246. resetState()
  247. signer := types.HomesteadSigner{}
  248. tx1, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(100000), big.NewInt(1), nil).SignECDSA(signer, key)
  249. tx2, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(2), nil).SignECDSA(signer, key)
  250. tx3, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(1), nil).SignECDSA(signer, key)
  251. // Add the first two transaction, ensure higher priced stays only
  252. if err := pool.add(tx1); err != nil {
  253. t.Error("didn't expect error", err)
  254. }
  255. if err := pool.add(tx2); err != nil {
  256. t.Error("didn't expect error", err)
  257. }
  258. state, _ := pool.currentState()
  259. pool.promoteExecutables(state)
  260. if pool.pending[addr].Len() != 1 {
  261. t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
  262. }
  263. if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
  264. t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
  265. }
  266. // Add the thid transaction and ensure it's not saved (smaller price)
  267. if err := pool.add(tx3); err != nil {
  268. t.Error("didn't expect error", err)
  269. }
  270. pool.promoteExecutables(state)
  271. if pool.pending[addr].Len() != 1 {
  272. t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
  273. }
  274. if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
  275. t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
  276. }
  277. // Ensure the total transaction count is correct
  278. if len(pool.all) != 1 {
  279. t.Error("expected 1 total transactions, got", len(pool.all))
  280. }
  281. }
  282. func TestMissingNonce(t *testing.T) {
  283. pool, key := setupTxPool()
  284. addr := crypto.PubkeyToAddress(key.PublicKey)
  285. currentState, _ := pool.currentState()
  286. currentState.AddBalance(addr, big.NewInt(100000000000000))
  287. tx := transaction(1, big.NewInt(100000), key)
  288. if err := pool.add(tx); err != nil {
  289. t.Error("didn't expect error", err)
  290. }
  291. if len(pool.pending) != 0 {
  292. t.Error("expected 0 pending transactions, got", len(pool.pending))
  293. }
  294. if pool.queue[addr].Len() != 1 {
  295. t.Error("expected 1 queued transaction, got", pool.queue[addr].Len())
  296. }
  297. if len(pool.all) != 1 {
  298. t.Error("expected 1 total transactions, got", len(pool.all))
  299. }
  300. }
  301. func TestNonceRecovery(t *testing.T) {
  302. const n = 10
  303. pool, key := setupTxPool()
  304. addr := crypto.PubkeyToAddress(key.PublicKey)
  305. currentState, _ := pool.currentState()
  306. currentState.SetNonce(addr, n)
  307. currentState.AddBalance(addr, big.NewInt(100000000000000))
  308. pool.resetState()
  309. tx := transaction(n, big.NewInt(100000), key)
  310. if err := pool.Add(tx); err != nil {
  311. t.Error(err)
  312. }
  313. // simulate some weird re-order of transactions and missing nonce(s)
  314. currentState.SetNonce(addr, n-1)
  315. pool.resetState()
  316. if fn := pool.pendingState.GetNonce(addr); fn != n+1 {
  317. t.Errorf("expected nonce to be %d, got %d", n+1, fn)
  318. }
  319. }
  320. func TestRemovedTxEvent(t *testing.T) {
  321. pool, key := setupTxPool()
  322. tx := transaction(0, big.NewInt(1000000), key)
  323. from, _ := deriveSender(tx)
  324. currentState, _ := pool.currentState()
  325. currentState.AddBalance(from, big.NewInt(1000000000000))
  326. pool.resetState()
  327. pool.eventMux.Post(RemovedTransactionEvent{types.Transactions{tx}})
  328. pool.eventMux.Post(ChainHeadEvent{nil})
  329. if pool.pending[from].Len() != 1 {
  330. t.Error("expected 1 pending tx, got", pool.pending[from].Len())
  331. }
  332. if len(pool.all) != 1 {
  333. t.Error("expected 1 total transactions, got", len(pool.all))
  334. }
  335. }
  336. // Tests that if an account runs out of funds, any pending and queued transactions
  337. // are dropped.
  338. func TestTransactionDropping(t *testing.T) {
  339. // Create a test account and fund it
  340. pool, key := setupTxPool()
  341. account, _ := deriveSender(transaction(0, big.NewInt(0), key))
  342. state, _ := pool.currentState()
  343. state.AddBalance(account, big.NewInt(1000))
  344. // Add some pending and some queued transactions
  345. var (
  346. tx0 = transaction(0, big.NewInt(100), key)
  347. tx1 = transaction(1, big.NewInt(200), key)
  348. tx10 = transaction(10, big.NewInt(100), key)
  349. tx11 = transaction(11, big.NewInt(200), key)
  350. )
  351. pool.promoteTx(account, tx0.Hash(), tx0)
  352. pool.promoteTx(account, tx1.Hash(), tx1)
  353. pool.enqueueTx(tx10.Hash(), tx10)
  354. pool.enqueueTx(tx11.Hash(), tx11)
  355. // Check that pre and post validations leave the pool as is
  356. if pool.pending[account].Len() != 2 {
  357. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 2)
  358. }
  359. if pool.queue[account].Len() != 2 {
  360. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 2)
  361. }
  362. if len(pool.all) != 4 {
  363. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
  364. }
  365. pool.resetState()
  366. if pool.pending[account].Len() != 2 {
  367. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 2)
  368. }
  369. if pool.queue[account].Len() != 2 {
  370. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 2)
  371. }
  372. if len(pool.all) != 4 {
  373. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
  374. }
  375. // Reduce the balance of the account, and check that invalidated transactions are dropped
  376. state.AddBalance(account, big.NewInt(-750))
  377. pool.resetState()
  378. if _, ok := pool.pending[account].txs.items[tx0.Nonce()]; !ok {
  379. t.Errorf("funded pending transaction missing: %v", tx0)
  380. }
  381. if _, ok := pool.pending[account].txs.items[tx1.Nonce()]; ok {
  382. t.Errorf("out-of-fund pending transaction present: %v", tx1)
  383. }
  384. if _, ok := pool.queue[account].txs.items[tx10.Nonce()]; !ok {
  385. t.Errorf("funded queued transaction missing: %v", tx10)
  386. }
  387. if _, ok := pool.queue[account].txs.items[tx11.Nonce()]; ok {
  388. t.Errorf("out-of-fund queued transaction present: %v", tx11)
  389. }
  390. if len(pool.all) != 2 {
  391. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 2)
  392. }
  393. }
  394. // Tests that if a transaction is dropped from the current pending pool (e.g. out
  395. // of fund), all consecutive (still valid, but not executable) transactions are
  396. // postponed back into the future queue to prevent broadcasting them.
  397. func TestTransactionPostponing(t *testing.T) {
  398. // Create a test account and fund it
  399. pool, key := setupTxPool()
  400. account, _ := deriveSender(transaction(0, big.NewInt(0), key))
  401. state, _ := pool.currentState()
  402. state.AddBalance(account, big.NewInt(1000))
  403. // Add a batch consecutive pending transactions for validation
  404. txns := []*types.Transaction{}
  405. for i := 0; i < 100; i++ {
  406. var tx *types.Transaction
  407. if i%2 == 0 {
  408. tx = transaction(uint64(i), big.NewInt(100), key)
  409. } else {
  410. tx = transaction(uint64(i), big.NewInt(500), key)
  411. }
  412. pool.promoteTx(account, tx.Hash(), tx)
  413. txns = append(txns, tx)
  414. }
  415. // Check that pre and post validations leave the pool as is
  416. if pool.pending[account].Len() != len(txns) {
  417. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
  418. }
  419. if len(pool.queue) != 0 {
  420. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
  421. }
  422. if len(pool.all) != len(txns) {
  423. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
  424. }
  425. pool.resetState()
  426. if pool.pending[account].Len() != len(txns) {
  427. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
  428. }
  429. if len(pool.queue) != 0 {
  430. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
  431. }
  432. if len(pool.all) != len(txns) {
  433. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
  434. }
  435. // Reduce the balance of the account, and check that transactions are reorganised
  436. state.AddBalance(account, big.NewInt(-750))
  437. pool.resetState()
  438. if _, ok := pool.pending[account].txs.items[txns[0].Nonce()]; !ok {
  439. t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txns[0])
  440. }
  441. if _, ok := pool.queue[account].txs.items[txns[0].Nonce()]; ok {
  442. t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txns[0])
  443. }
  444. for i, tx := range txns[1:] {
  445. if i%2 == 1 {
  446. if _, ok := pool.pending[account].txs.items[tx.Nonce()]; ok {
  447. t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx)
  448. }
  449. if _, ok := pool.queue[account].txs.items[tx.Nonce()]; !ok {
  450. t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx)
  451. }
  452. } else {
  453. if _, ok := pool.pending[account].txs.items[tx.Nonce()]; ok {
  454. t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx)
  455. }
  456. if _, ok := pool.queue[account].txs.items[tx.Nonce()]; ok {
  457. t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx)
  458. }
  459. }
  460. }
  461. if len(pool.all) != len(txns)/2 {
  462. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns)/2)
  463. }
  464. }
  465. // Tests that if the transaction count belonging to a single account goes above
  466. // some threshold, the higher transactions are dropped to prevent DOS attacks.
  467. func TestTransactionQueueAccountLimiting(t *testing.T) {
  468. // Create a test account and fund it
  469. pool, key := setupTxPool()
  470. account, _ := deriveSender(transaction(0, big.NewInt(0), key))
  471. state, _ := pool.currentState()
  472. state.AddBalance(account, big.NewInt(1000000))
  473. pool.resetState()
  474. // Keep queuing up transactions and make sure all above a limit are dropped
  475. for i := uint64(1); i <= maxQueuedPerAccount+5; i++ {
  476. if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
  477. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  478. }
  479. if len(pool.pending) != 0 {
  480. t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending), 0)
  481. }
  482. if i <= maxQueuedPerAccount {
  483. if pool.queue[account].Len() != int(i) {
  484. t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), i)
  485. }
  486. } else {
  487. if pool.queue[account].Len() != int(maxQueuedPerAccount) {
  488. t.Errorf("tx %d: queue limit mismatch: have %d, want %d", i, pool.queue[account].Len(), maxQueuedPerAccount)
  489. }
  490. }
  491. }
  492. if len(pool.all) != int(maxQueuedPerAccount) {
  493. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueuedPerAccount)
  494. }
  495. }
  496. // Tests that if the transaction count belonging to multiple accounts go above
  497. // some threshold, the higher transactions are dropped to prevent DOS attacks.
  498. func TestTransactionQueueGlobalLimiting(t *testing.T) {
  499. // Reduce the queue limits to shorten test time
  500. defer func(old uint64) { maxQueuedInTotal = old }(maxQueuedInTotal)
  501. maxQueuedInTotal = maxQueuedPerAccount * 3
  502. // Create the pool to test the limit enforcement with
  503. db, _ := ethdb.NewMemDatabase()
  504. statedb, _ := state.New(common.Hash{}, db)
  505. pool := NewTxPool(testChainConfig(), new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
  506. pool.resetState()
  507. // Create a number of test accounts and fund them
  508. state, _ := pool.currentState()
  509. keys := make([]*ecdsa.PrivateKey, 5)
  510. for i := 0; i < len(keys); i++ {
  511. keys[i], _ = crypto.GenerateKey()
  512. state.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  513. }
  514. // Generate and queue a batch of transactions
  515. nonces := make(map[common.Address]uint64)
  516. txs := make(types.Transactions, 0, 3*maxQueuedInTotal)
  517. for len(txs) < cap(txs) {
  518. key := keys[rand.Intn(len(keys))]
  519. addr := crypto.PubkeyToAddress(key.PublicKey)
  520. txs = append(txs, transaction(nonces[addr]+1, big.NewInt(100000), key))
  521. nonces[addr]++
  522. }
  523. // Import the batch and verify that limits have been enforced
  524. pool.AddBatch(txs)
  525. queued := 0
  526. for addr, list := range pool.queue {
  527. if list.Len() > int(maxQueuedPerAccount) {
  528. t.Errorf("addr %x: queued accounts overflown allowance: %d > %d", addr, list.Len(), maxQueuedPerAccount)
  529. }
  530. queued += list.Len()
  531. }
  532. if queued > int(maxQueuedInTotal) {
  533. t.Fatalf("total transactions overflow allowance: %d > %d", queued, maxQueuedInTotal)
  534. }
  535. }
  536. // Tests that if an account remains idle for a prolonged amount of time, any
  537. // non-executable transactions queued up are dropped to prevent wasting resources
  538. // on shuffling them around.
  539. func TestTransactionQueueTimeLimiting(t *testing.T) {
  540. // Reduce the queue limits to shorten test time
  541. defer func(old time.Duration) { maxQueuedLifetime = old }(maxQueuedLifetime)
  542. defer func(old time.Duration) { evictionInterval = old }(evictionInterval)
  543. maxQueuedLifetime = time.Second
  544. evictionInterval = time.Second
  545. // Create a test account and fund it
  546. pool, key := setupTxPool()
  547. account, _ := deriveSender(transaction(0, big.NewInt(0), key))
  548. state, _ := pool.currentState()
  549. state.AddBalance(account, big.NewInt(1000000))
  550. // Queue up a batch of transactions
  551. for i := uint64(1); i <= maxQueuedPerAccount; i++ {
  552. if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
  553. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  554. }
  555. }
  556. // Wait until at least two expiration cycles hit and make sure the transactions are gone
  557. time.Sleep(2 * evictionInterval)
  558. if len(pool.queue) > 0 {
  559. t.Fatalf("old transactions remained after eviction")
  560. }
  561. }
  562. // Tests that even if the transaction count belonging to a single account goes
  563. // above some threshold, as long as the transactions are executable, they are
  564. // accepted.
  565. func TestTransactionPendingLimiting(t *testing.T) {
  566. // Create a test account and fund it
  567. pool, key := setupTxPool()
  568. account, _ := deriveSender(transaction(0, big.NewInt(0), key))
  569. state, _ := pool.currentState()
  570. state.AddBalance(account, big.NewInt(1000000))
  571. pool.resetState()
  572. // Keep queuing up transactions and make sure all above a limit are dropped
  573. for i := uint64(0); i < maxQueuedPerAccount+5; i++ {
  574. if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
  575. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  576. }
  577. if pool.pending[account].Len() != int(i)+1 {
  578. t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, pool.pending[account].Len(), i+1)
  579. }
  580. if len(pool.queue) != 0 {
  581. t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), 0)
  582. }
  583. }
  584. if len(pool.all) != int(maxQueuedPerAccount+5) {
  585. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueuedPerAccount+5)
  586. }
  587. }
  588. // Tests that the transaction limits are enforced the same way irrelevant whether
  589. // the transactions are added one by one or in batches.
  590. func TestTransactionQueueLimitingEquivalency(t *testing.T) { testTransactionLimitingEquivalency(t, 1) }
  591. func TestTransactionPendingLimitingEquivalency(t *testing.T) { testTransactionLimitingEquivalency(t, 0) }
  592. func testTransactionLimitingEquivalency(t *testing.T, origin uint64) {
  593. // Add a batch of transactions to a pool one by one
  594. pool1, key1 := setupTxPool()
  595. account1, _ := deriveSender(transaction(0, big.NewInt(0), key1))
  596. state1, _ := pool1.currentState()
  597. state1.AddBalance(account1, big.NewInt(1000000))
  598. for i := uint64(0); i < maxQueuedPerAccount+5; i++ {
  599. if err := pool1.Add(transaction(origin+i, big.NewInt(100000), key1)); err != nil {
  600. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  601. }
  602. }
  603. // Add a batch of transactions to a pool in one big batch
  604. pool2, key2 := setupTxPool()
  605. account2, _ := deriveSender(transaction(0, big.NewInt(0), key2))
  606. state2, _ := pool2.currentState()
  607. state2.AddBalance(account2, big.NewInt(1000000))
  608. txns := []*types.Transaction{}
  609. for i := uint64(0); i < maxQueuedPerAccount+5; i++ {
  610. txns = append(txns, transaction(origin+i, big.NewInt(100000), key2))
  611. }
  612. pool2.AddBatch(txns)
  613. // Ensure the batch optimization honors the same pool mechanics
  614. if len(pool1.pending) != len(pool2.pending) {
  615. t.Errorf("pending transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.pending), len(pool2.pending))
  616. }
  617. if len(pool1.queue) != len(pool2.queue) {
  618. t.Errorf("queued transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.queue), len(pool2.queue))
  619. }
  620. if len(pool1.all) != len(pool2.all) {
  621. t.Errorf("total transaction count mismatch: one-by-one algo %d, batch algo %d", len(pool1.all), len(pool2.all))
  622. }
  623. }
  624. // Tests that if the transaction count belonging to multiple accounts go above
  625. // some hard threshold, the higher transactions are dropped to prevent DOS
  626. // attacks.
  627. func TestTransactionPendingGlobalLimiting(t *testing.T) {
  628. // Reduce the queue limits to shorten test time
  629. defer func(old uint64) { maxPendingTotal = old }(maxPendingTotal)
  630. maxPendingTotal = minPendingPerAccount * 10
  631. // Create the pool to test the limit enforcement with
  632. db, _ := ethdb.NewMemDatabase()
  633. statedb, _ := state.New(common.Hash{}, db)
  634. pool := NewTxPool(testChainConfig(), new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
  635. pool.resetState()
  636. // Create a number of test accounts and fund them
  637. state, _ := pool.currentState()
  638. keys := make([]*ecdsa.PrivateKey, 5)
  639. for i := 0; i < len(keys); i++ {
  640. keys[i], _ = crypto.GenerateKey()
  641. state.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  642. }
  643. // Generate and queue a batch of transactions
  644. nonces := make(map[common.Address]uint64)
  645. txs := types.Transactions{}
  646. for _, key := range keys {
  647. addr := crypto.PubkeyToAddress(key.PublicKey)
  648. for j := 0; j < int(maxPendingTotal)/len(keys)*2; j++ {
  649. txs = append(txs, transaction(nonces[addr], big.NewInt(100000), key))
  650. nonces[addr]++
  651. }
  652. }
  653. // Import the batch and verify that limits have been enforced
  654. pool.AddBatch(txs)
  655. pending := 0
  656. for _, list := range pool.pending {
  657. pending += list.Len()
  658. }
  659. if pending > int(maxPendingTotal) {
  660. t.Fatalf("total pending transactions overflow allowance: %d > %d", pending, maxPendingTotal)
  661. }
  662. }
  663. // Tests that if the transaction count belonging to multiple accounts go above
  664. // some hard threshold, if they are under the minimum guaranteed slot count then
  665. // the transactions are still kept.
  666. func TestTransactionPendingMinimumAllowance(t *testing.T) {
  667. // Reduce the queue limits to shorten test time
  668. defer func(old uint64) { maxPendingTotal = old }(maxPendingTotal)
  669. maxPendingTotal = 0
  670. // Create the pool to test the limit enforcement with
  671. db, _ := ethdb.NewMemDatabase()
  672. statedb, _ := state.New(common.Hash{}, db)
  673. pool := NewTxPool(testChainConfig(), new(event.TypeMux), func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
  674. pool.resetState()
  675. // Create a number of test accounts and fund them
  676. state, _ := pool.currentState()
  677. keys := make([]*ecdsa.PrivateKey, 5)
  678. for i := 0; i < len(keys); i++ {
  679. keys[i], _ = crypto.GenerateKey()
  680. state.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  681. }
  682. // Generate and queue a batch of transactions
  683. nonces := make(map[common.Address]uint64)
  684. txs := types.Transactions{}
  685. for _, key := range keys {
  686. addr := crypto.PubkeyToAddress(key.PublicKey)
  687. for j := 0; j < int(minPendingPerAccount)*2; j++ {
  688. txs = append(txs, transaction(nonces[addr], big.NewInt(100000), key))
  689. nonces[addr]++
  690. }
  691. }
  692. // Import the batch and verify that limits have been enforced
  693. pool.AddBatch(txs)
  694. for addr, list := range pool.pending {
  695. if list.Len() != int(minPendingPerAccount) {
  696. t.Errorf("addr %x: total pending transactions mismatch: have %d, want %d", addr, list.Len(), minPendingPerAccount)
  697. }
  698. }
  699. }
  700. // Benchmarks the speed of validating the contents of the pending queue of the
  701. // transaction pool.
  702. func BenchmarkPendingDemotion100(b *testing.B) { benchmarkPendingDemotion(b, 100) }
  703. func BenchmarkPendingDemotion1000(b *testing.B) { benchmarkPendingDemotion(b, 1000) }
  704. func BenchmarkPendingDemotion10000(b *testing.B) { benchmarkPendingDemotion(b, 10000) }
  705. func benchmarkPendingDemotion(b *testing.B, size int) {
  706. // Add a batch of transactions to a pool one by one
  707. pool, key := setupTxPool()
  708. account, _ := deriveSender(transaction(0, big.NewInt(0), key))
  709. state, _ := pool.currentState()
  710. state.AddBalance(account, big.NewInt(1000000))
  711. for i := 0; i < size; i++ {
  712. tx := transaction(uint64(i), big.NewInt(100000), key)
  713. pool.promoteTx(account, tx.Hash(), tx)
  714. }
  715. // Benchmark the speed of pool validation
  716. b.ResetTimer()
  717. for i := 0; i < b.N; i++ {
  718. pool.demoteUnexecutables(state)
  719. }
  720. }
  721. // Benchmarks the speed of scheduling the contents of the future queue of the
  722. // transaction pool.
  723. func BenchmarkFuturePromotion100(b *testing.B) { benchmarkFuturePromotion(b, 100) }
  724. func BenchmarkFuturePromotion1000(b *testing.B) { benchmarkFuturePromotion(b, 1000) }
  725. func BenchmarkFuturePromotion10000(b *testing.B) { benchmarkFuturePromotion(b, 10000) }
  726. func benchmarkFuturePromotion(b *testing.B, size int) {
  727. // Add a batch of transactions to a pool one by one
  728. pool, key := setupTxPool()
  729. account, _ := deriveSender(transaction(0, big.NewInt(0), key))
  730. state, _ := pool.currentState()
  731. state.AddBalance(account, big.NewInt(1000000))
  732. for i := 0; i < size; i++ {
  733. tx := transaction(uint64(1+i), big.NewInt(100000), key)
  734. pool.enqueueTx(tx.Hash(), tx)
  735. }
  736. // Benchmark the speed of pool validation
  737. b.ResetTimer()
  738. for i := 0; i < b.N; i++ {
  739. pool.promoteExecutables(state)
  740. }
  741. }
  742. // Benchmarks the speed of iterative transaction insertion.
  743. func BenchmarkPoolInsert(b *testing.B) {
  744. // Generate a batch of transactions to enqueue into the pool
  745. pool, key := setupTxPool()
  746. account, _ := deriveSender(transaction(0, big.NewInt(0), key))
  747. state, _ := pool.currentState()
  748. state.AddBalance(account, big.NewInt(1000000))
  749. txs := make(types.Transactions, b.N)
  750. for i := 0; i < b.N; i++ {
  751. txs[i] = transaction(uint64(i), big.NewInt(100000), key)
  752. }
  753. // Benchmark importing the transactions into the queue
  754. b.ResetTimer()
  755. for _, tx := range txs {
  756. pool.Add(tx)
  757. }
  758. }
  759. // Benchmarks the speed of batched transaction insertion.
  760. func BenchmarkPoolBatchInsert100(b *testing.B) { benchmarkPoolBatchInsert(b, 100) }
  761. func BenchmarkPoolBatchInsert1000(b *testing.B) { benchmarkPoolBatchInsert(b, 1000) }
  762. func BenchmarkPoolBatchInsert10000(b *testing.B) { benchmarkPoolBatchInsert(b, 10000) }
  763. func benchmarkPoolBatchInsert(b *testing.B, size int) {
  764. // Generate a batch of transactions to enqueue into the pool
  765. pool, key := setupTxPool()
  766. account, _ := deriveSender(transaction(0, big.NewInt(0), key))
  767. state, _ := pool.currentState()
  768. state.AddBalance(account, big.NewInt(1000000))
  769. batches := make([]types.Transactions, b.N)
  770. for i := 0; i < b.N; i++ {
  771. batches[i] = make(types.Transactions, size)
  772. for j := 0; j < size; j++ {
  773. batches[i][j] = transaction(uint64(size*i+j), big.NewInt(100000), key)
  774. }
  775. }
  776. // Benchmark importing the transactions into the queue
  777. b.ResetTimer()
  778. for _, batch := range batches {
  779. pool.AddBatch(batch)
  780. }
  781. }