tx_pool_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. "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. newPool := NewTxPool(testChainConfig(), &m, func() (*state.StateDB, error) { return statedb, nil }, func() *big.Int { return big.NewInt(1000000) })
  38. newPool.resetState()
  39. return newPool, key
  40. }
  41. func TestInvalidTransactions(t *testing.T) {
  42. pool, key := setupTxPool()
  43. tx := transaction(0, big.NewInt(100), key)
  44. if err := pool.Add(tx); err != ErrNonExistentAccount {
  45. t.Error("expected", ErrNonExistentAccount)
  46. }
  47. from, _ := tx.From()
  48. currentState, _ := pool.currentState()
  49. currentState.AddBalance(from, big.NewInt(1))
  50. if err := pool.Add(tx); err != ErrInsufficientFunds {
  51. t.Error("expected", ErrInsufficientFunds)
  52. }
  53. balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(tx.Gas(), tx.GasPrice()))
  54. currentState.AddBalance(from, balance)
  55. if err := pool.Add(tx); err != ErrIntrinsicGas {
  56. t.Error("expected", ErrIntrinsicGas, "got", err)
  57. }
  58. currentState.SetNonce(from, 1)
  59. currentState.AddBalance(from, big.NewInt(0xffffffffffffff))
  60. tx = transaction(0, big.NewInt(100000), key)
  61. if err := pool.Add(tx); err != ErrNonce {
  62. t.Error("expected", ErrNonce)
  63. }
  64. tx = transaction(1, big.NewInt(100000), key)
  65. pool.minGasPrice = big.NewInt(1000)
  66. if err := pool.Add(tx); err != ErrCheap {
  67. t.Error("expected", ErrCheap, "got", err)
  68. }
  69. pool.SetLocal(tx)
  70. if err := pool.Add(tx); err != nil {
  71. t.Error("expected", nil, "got", err)
  72. }
  73. }
  74. func TestTransactionQueue(t *testing.T) {
  75. pool, key := setupTxPool()
  76. tx := transaction(0, big.NewInt(100), key)
  77. from, _ := tx.From()
  78. currentState, _ := pool.currentState()
  79. currentState.AddBalance(from, big.NewInt(1000))
  80. pool.enqueueTx(tx.Hash(), tx)
  81. pool.promoteExecutables()
  82. if len(pool.pending) != 1 {
  83. t.Error("expected valid txs to be 1 is", len(pool.pending))
  84. }
  85. tx = transaction(1, big.NewInt(100), key)
  86. from, _ = tx.From()
  87. currentState.SetNonce(from, 2)
  88. pool.enqueueTx(tx.Hash(), tx)
  89. pool.promoteExecutables()
  90. if _, ok := pool.pending[from].items[tx.Nonce()]; ok {
  91. t.Error("expected transaction to be in tx pool")
  92. }
  93. if len(pool.queue) > 0 {
  94. t.Error("expected transaction queue to be empty. is", len(pool.queue))
  95. }
  96. pool, key = setupTxPool()
  97. tx1 := transaction(0, big.NewInt(100), key)
  98. tx2 := transaction(10, big.NewInt(100), key)
  99. tx3 := transaction(11, big.NewInt(100), key)
  100. from, _ = tx1.From()
  101. currentState, _ = pool.currentState()
  102. currentState.AddBalance(from, big.NewInt(1000))
  103. pool.enqueueTx(tx1.Hash(), tx1)
  104. pool.enqueueTx(tx2.Hash(), tx2)
  105. pool.enqueueTx(tx3.Hash(), tx3)
  106. pool.promoteExecutables()
  107. if len(pool.pending) != 1 {
  108. t.Error("expected tx pool to be 1, got", len(pool.pending))
  109. }
  110. if pool.queue[from].Len() != 2 {
  111. t.Error("expected len(queue) == 2, got", pool.queue[from].Len())
  112. }
  113. }
  114. func TestRemoveTx(t *testing.T) {
  115. pool, key := setupTxPool()
  116. tx := transaction(0, big.NewInt(100), key)
  117. from, _ := tx.From()
  118. currentState, _ := pool.currentState()
  119. currentState.AddBalance(from, big.NewInt(1))
  120. pool.enqueueTx(tx.Hash(), tx)
  121. pool.promoteTx(from, tx.Hash(), tx)
  122. if len(pool.queue) != 1 {
  123. t.Error("expected queue to be 1, got", len(pool.queue))
  124. }
  125. if len(pool.pending) != 1 {
  126. t.Error("expected pending to be 1, got", len(pool.pending))
  127. }
  128. pool.Remove(tx.Hash())
  129. if len(pool.queue) > 0 {
  130. t.Error("expected queue to be 0, got", len(pool.queue))
  131. }
  132. if len(pool.pending) > 0 {
  133. t.Error("expected pending to be 0, got", len(pool.pending))
  134. }
  135. }
  136. func TestNegativeValue(t *testing.T) {
  137. pool, key := setupTxPool()
  138. tx, _ := types.NewTransaction(0, common.Address{}, big.NewInt(-1), big.NewInt(100), big.NewInt(1), nil).SignECDSA(key)
  139. from, _ := tx.From()
  140. currentState, _ := pool.currentState()
  141. currentState.AddBalance(from, big.NewInt(1))
  142. if err := pool.Add(tx); err != ErrNegativeValue {
  143. t.Error("expected", ErrNegativeValue, "got", err)
  144. }
  145. }
  146. func TestTransactionChainFork(t *testing.T) {
  147. pool, key := setupTxPool()
  148. addr := crypto.PubkeyToAddress(key.PublicKey)
  149. resetState := func() {
  150. db, _ := ethdb.NewMemDatabase()
  151. statedb, _ := state.New(common.Hash{}, db)
  152. pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
  153. currentState, _ := pool.currentState()
  154. currentState.AddBalance(addr, big.NewInt(100000000000000))
  155. pool.resetState()
  156. }
  157. resetState()
  158. tx := transaction(0, big.NewInt(100000), key)
  159. if err := pool.add(tx); err != nil {
  160. t.Error("didn't expect error", err)
  161. }
  162. pool.RemoveBatch([]*types.Transaction{tx})
  163. // reset the pool's internal state
  164. resetState()
  165. if err := pool.add(tx); err != nil {
  166. t.Error("didn't expect error", err)
  167. }
  168. }
  169. func TestTransactionDoubleNonce(t *testing.T) {
  170. pool, key := setupTxPool()
  171. addr := crypto.PubkeyToAddress(key.PublicKey)
  172. resetState := func() {
  173. db, _ := ethdb.NewMemDatabase()
  174. statedb, _ := state.New(common.Hash{}, db)
  175. pool.currentState = func() (*state.StateDB, error) { return statedb, nil }
  176. currentState, _ := pool.currentState()
  177. currentState.AddBalance(addr, big.NewInt(100000000000000))
  178. pool.resetState()
  179. }
  180. resetState()
  181. tx1, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(100000), big.NewInt(1), nil).SignECDSA(key)
  182. tx2, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(2), nil).SignECDSA(key)
  183. tx3, _ := types.NewTransaction(0, common.Address{}, big.NewInt(100), big.NewInt(1000000), big.NewInt(1), nil).SignECDSA(key)
  184. // Add the first two transaction, ensure higher priced stays only
  185. if err := pool.add(tx1); err != nil {
  186. t.Error("didn't expect error", err)
  187. }
  188. if err := pool.add(tx2); err != nil {
  189. t.Error("didn't expect error", err)
  190. }
  191. pool.promoteExecutables()
  192. if pool.pending[addr].Len() != 1 {
  193. t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
  194. }
  195. if tx := pool.pending[addr].items[0]; tx.Hash() != tx2.Hash() {
  196. t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
  197. }
  198. // Add the thid transaction and ensure it's not saved (smaller price)
  199. if err := pool.add(tx3); err != nil {
  200. t.Error("didn't expect error", err)
  201. }
  202. pool.promoteExecutables()
  203. if pool.pending[addr].Len() != 1 {
  204. t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
  205. }
  206. if tx := pool.pending[addr].items[0]; tx.Hash() != tx2.Hash() {
  207. t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
  208. }
  209. // Ensure the total transaction count is correct
  210. if len(pool.all) != 1 {
  211. t.Error("expected 1 total transactions, got", len(pool.all))
  212. }
  213. }
  214. func TestMissingNonce(t *testing.T) {
  215. pool, key := setupTxPool()
  216. addr := crypto.PubkeyToAddress(key.PublicKey)
  217. currentState, _ := pool.currentState()
  218. currentState.AddBalance(addr, big.NewInt(100000000000000))
  219. tx := transaction(1, big.NewInt(100000), key)
  220. if err := pool.add(tx); err != nil {
  221. t.Error("didn't expect error", err)
  222. }
  223. if len(pool.pending) != 0 {
  224. t.Error("expected 0 pending transactions, got", len(pool.pending))
  225. }
  226. if pool.queue[addr].Len() != 1 {
  227. t.Error("expected 1 queued transaction, got", pool.queue[addr].Len())
  228. }
  229. if len(pool.all) != 1 {
  230. t.Error("expected 1 total transactions, got", len(pool.all))
  231. }
  232. }
  233. func TestNonceRecovery(t *testing.T) {
  234. const n = 10
  235. pool, key := setupTxPool()
  236. addr := crypto.PubkeyToAddress(key.PublicKey)
  237. currentState, _ := pool.currentState()
  238. currentState.SetNonce(addr, n)
  239. currentState.AddBalance(addr, big.NewInt(100000000000000))
  240. pool.resetState()
  241. tx := transaction(n, big.NewInt(100000), key)
  242. if err := pool.Add(tx); err != nil {
  243. t.Error(err)
  244. }
  245. // simulate some weird re-order of transactions and missing nonce(s)
  246. currentState.SetNonce(addr, n-1)
  247. pool.resetState()
  248. if fn := pool.pendingState.GetNonce(addr); fn != n+1 {
  249. t.Errorf("expected nonce to be %d, got %d", n+1, fn)
  250. }
  251. }
  252. func TestRemovedTxEvent(t *testing.T) {
  253. pool, key := setupTxPool()
  254. tx := transaction(0, big.NewInt(1000000), key)
  255. from, _ := tx.From()
  256. currentState, _ := pool.currentState()
  257. currentState.AddBalance(from, big.NewInt(1000000000000))
  258. pool.eventMux.Post(RemovedTransactionEvent{types.Transactions{tx}})
  259. pool.eventMux.Post(ChainHeadEvent{nil})
  260. if pool.pending[from].Len() != 1 {
  261. t.Error("expected 1 pending tx, got", pool.pending[from].Len())
  262. }
  263. if len(pool.all) != 1 {
  264. t.Error("expected 1 total transactions, got", len(pool.all))
  265. }
  266. }
  267. // Tests that if an account runs out of funds, any pending and queued transactions
  268. // are dropped.
  269. func TestTransactionDropping(t *testing.T) {
  270. // Create a test account and fund it
  271. pool, key := setupTxPool()
  272. account, _ := transaction(0, big.NewInt(0), key).From()
  273. state, _ := pool.currentState()
  274. state.AddBalance(account, big.NewInt(1000))
  275. // Add some pending and some queued transactions
  276. var (
  277. tx0 = transaction(0, big.NewInt(100), key)
  278. tx1 = transaction(1, big.NewInt(200), key)
  279. tx10 = transaction(10, big.NewInt(100), key)
  280. tx11 = transaction(11, big.NewInt(200), key)
  281. )
  282. pool.promoteTx(account, tx0.Hash(), tx0)
  283. pool.promoteTx(account, tx1.Hash(), tx1)
  284. pool.enqueueTx(tx10.Hash(), tx10)
  285. pool.enqueueTx(tx11.Hash(), tx11)
  286. // Check that pre and post validations leave the pool as is
  287. if pool.pending[account].Len() != 2 {
  288. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 2)
  289. }
  290. if pool.queue[account].Len() != 2 {
  291. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 2)
  292. }
  293. if len(pool.all) != 4 {
  294. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
  295. }
  296. pool.resetState()
  297. if pool.pending[account].Len() != 2 {
  298. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 2)
  299. }
  300. if pool.queue[account].Len() != 2 {
  301. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 2)
  302. }
  303. if len(pool.all) != 4 {
  304. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
  305. }
  306. // Reduce the balance of the account, and check that invalidated transactions are dropped
  307. state.AddBalance(account, big.NewInt(-750))
  308. pool.resetState()
  309. if _, ok := pool.pending[account].items[tx0.Nonce()]; !ok {
  310. t.Errorf("funded pending transaction missing: %v", tx0)
  311. }
  312. if _, ok := pool.pending[account].items[tx1.Nonce()]; ok {
  313. t.Errorf("out-of-fund pending transaction present: %v", tx1)
  314. }
  315. if _, ok := pool.queue[account].items[tx10.Nonce()]; !ok {
  316. t.Errorf("funded queued transaction missing: %v", tx10)
  317. }
  318. if _, ok := pool.queue[account].items[tx11.Nonce()]; ok {
  319. t.Errorf("out-of-fund queued transaction present: %v", tx11)
  320. }
  321. if len(pool.all) != 2 {
  322. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 2)
  323. }
  324. }
  325. // Tests that if a transaction is dropped from the current pending pool (e.g. out
  326. // of fund), all consecutive (still valid, but not executable) transactions are
  327. // postponed back into the future queue to prevent broadcasting them.
  328. func TestTransactionPostponing(t *testing.T) {
  329. // Create a test account and fund it
  330. pool, key := setupTxPool()
  331. account, _ := transaction(0, big.NewInt(0), key).From()
  332. state, _ := pool.currentState()
  333. state.AddBalance(account, big.NewInt(1000))
  334. // Add a batch consecutive pending transactions for validation
  335. txns := []*types.Transaction{}
  336. for i := 0; i < 100; i++ {
  337. var tx *types.Transaction
  338. if i%2 == 0 {
  339. tx = transaction(uint64(i), big.NewInt(100), key)
  340. } else {
  341. tx = transaction(uint64(i), big.NewInt(500), key)
  342. }
  343. pool.promoteTx(account, tx.Hash(), tx)
  344. txns = append(txns, tx)
  345. }
  346. // Check that pre and post validations leave the pool as is
  347. if pool.pending[account].Len() != len(txns) {
  348. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
  349. }
  350. if len(pool.queue) != 0 {
  351. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
  352. }
  353. if len(pool.all) != len(txns) {
  354. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
  355. }
  356. pool.resetState()
  357. if pool.pending[account].Len() != len(txns) {
  358. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), len(txns))
  359. }
  360. if len(pool.queue) != 0 {
  361. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 0)
  362. }
  363. if len(pool.all) != len(txns) {
  364. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns))
  365. }
  366. // Reduce the balance of the account, and check that transactions are reorganised
  367. state.AddBalance(account, big.NewInt(-750))
  368. pool.resetState()
  369. if _, ok := pool.pending[account].items[txns[0].Nonce()]; !ok {
  370. t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txns[0])
  371. }
  372. if _, ok := pool.queue[account].items[txns[0].Nonce()]; ok {
  373. t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txns[0])
  374. }
  375. for i, tx := range txns[1:] {
  376. if i%2 == 1 {
  377. if _, ok := pool.pending[account].items[tx.Nonce()]; ok {
  378. t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx)
  379. }
  380. if _, ok := pool.queue[account].items[tx.Nonce()]; !ok {
  381. t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx)
  382. }
  383. } else {
  384. if _, ok := pool.pending[account].items[tx.Nonce()]; ok {
  385. t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx)
  386. }
  387. if _, ok := pool.queue[account].items[tx.Nonce()]; ok {
  388. t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx)
  389. }
  390. }
  391. }
  392. if len(pool.all) != len(txns)/2 {
  393. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txns)/2)
  394. }
  395. }
  396. // Tests that if the transaction count belonging to a single account goes above
  397. // some threshold, the higher transactions are dropped to prevent DOS attacks.
  398. func TestTransactionQueueLimiting(t *testing.T) {
  399. // Create a test account and fund it
  400. pool, key := setupTxPool()
  401. account, _ := transaction(0, big.NewInt(0), key).From()
  402. state, _ := pool.currentState()
  403. state.AddBalance(account, big.NewInt(1000000))
  404. // Keep queuing up transactions and make sure all above a limit are dropped
  405. for i := uint64(1); i <= maxQueued+5; i++ {
  406. if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
  407. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  408. }
  409. if len(pool.pending) != 0 {
  410. t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending), 0)
  411. }
  412. if i <= maxQueued {
  413. if pool.queue[account].Len() != int(i) {
  414. t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), i)
  415. }
  416. } else {
  417. if pool.queue[account].Len() != maxQueued {
  418. t.Errorf("tx %d: queue limit mismatch: have %d, want %d", i, pool.queue[account].Len(), maxQueued)
  419. }
  420. }
  421. }
  422. if len(pool.all) != maxQueued {
  423. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueued)
  424. }
  425. }
  426. // Tests that even if the transaction count belonging to a single account goes
  427. // above some threshold, as long as the transactions are executable, they are
  428. // accepted.
  429. func TestTransactionPendingLimiting(t *testing.T) {
  430. // Create a test account and fund it
  431. pool, key := setupTxPool()
  432. account, _ := transaction(0, big.NewInt(0), key).From()
  433. state, _ := pool.currentState()
  434. state.AddBalance(account, big.NewInt(1000000))
  435. // Keep queuing up transactions and make sure all above a limit are dropped
  436. for i := uint64(0); i < maxQueued+5; i++ {
  437. if err := pool.Add(transaction(i, big.NewInt(100000), key)); err != nil {
  438. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  439. }
  440. if pool.pending[account].Len() != int(i)+1 {
  441. t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, pool.pending[account].Len(), i+1)
  442. }
  443. if len(pool.queue) != 0 {
  444. t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), 0)
  445. }
  446. }
  447. if len(pool.all) != maxQueued+5 {
  448. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), maxQueued+5)
  449. }
  450. }
  451. // Tests that the transaction limits are enforced the same way irrelevant whether
  452. // the transactions are added one by one or in batches.
  453. func TestTransactionQueueLimitingEquivalency(t *testing.T) { testTransactionLimitingEquivalency(t, 1) }
  454. func TestTransactionPendingLimitingEquivalency(t *testing.T) { testTransactionLimitingEquivalency(t, 0) }
  455. func testTransactionLimitingEquivalency(t *testing.T, origin uint64) {
  456. // Add a batch of transactions to a pool one by one
  457. pool1, key1 := setupTxPool()
  458. account1, _ := transaction(0, big.NewInt(0), key1).From()
  459. state1, _ := pool1.currentState()
  460. state1.AddBalance(account1, big.NewInt(1000000))
  461. for i := uint64(0); i < maxQueued+5; i++ {
  462. if err := pool1.Add(transaction(origin+i, big.NewInt(100000), key1)); err != nil {
  463. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  464. }
  465. }
  466. // Add a batch of transactions to a pool in one big batch
  467. pool2, key2 := setupTxPool()
  468. account2, _ := transaction(0, big.NewInt(0), key2).From()
  469. state2, _ := pool2.currentState()
  470. state2.AddBalance(account2, big.NewInt(1000000))
  471. txns := []*types.Transaction{}
  472. for i := uint64(0); i < maxQueued+5; i++ {
  473. txns = append(txns, transaction(origin+i, big.NewInt(100000), key2))
  474. }
  475. pool2.AddBatch(txns)
  476. // Ensure the batch optimization honors the same pool mechanics
  477. if len(pool1.pending) != len(pool2.pending) {
  478. t.Errorf("pending transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.pending), len(pool2.pending))
  479. }
  480. if len(pool1.queue) != len(pool2.queue) {
  481. t.Errorf("queued transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.queue), len(pool2.queue))
  482. }
  483. if len(pool1.all) != len(pool2.all) {
  484. t.Errorf("total transaction count mismatch: one-by-one algo %d, batch algo %d", len(pool1.all), len(pool2.all))
  485. }
  486. }
  487. // Benchmarks the speed of validating the contents of the pending queue of the
  488. // transaction pool.
  489. func BenchmarkPendingDemotion100(b *testing.B) { benchmarkPendingDemotion(b, 100) }
  490. func BenchmarkPendingDemotion1000(b *testing.B) { benchmarkPendingDemotion(b, 1000) }
  491. func BenchmarkPendingDemotion10000(b *testing.B) { benchmarkPendingDemotion(b, 10000) }
  492. func benchmarkPendingDemotion(b *testing.B, size int) {
  493. // Add a batch of transactions to a pool one by one
  494. pool, key := setupTxPool()
  495. account, _ := transaction(0, big.NewInt(0), key).From()
  496. state, _ := pool.currentState()
  497. state.AddBalance(account, big.NewInt(1000000))
  498. for i := 0; i < size; i++ {
  499. tx := transaction(uint64(i), big.NewInt(100000), key)
  500. pool.promoteTx(account, tx.Hash(), tx)
  501. }
  502. // Benchmark the speed of pool validation
  503. b.ResetTimer()
  504. for i := 0; i < b.N; i++ {
  505. pool.demoteUnexecutables()
  506. }
  507. }
  508. // Benchmarks the speed of scheduling the contents of the future queue of the
  509. // transaction pool.
  510. func BenchmarkFuturePromotion100(b *testing.B) { benchmarkFuturePromotion(b, 100) }
  511. func BenchmarkFuturePromotion1000(b *testing.B) { benchmarkFuturePromotion(b, 1000) }
  512. func BenchmarkFuturePromotion10000(b *testing.B) { benchmarkFuturePromotion(b, 10000) }
  513. func benchmarkFuturePromotion(b *testing.B, size int) {
  514. // Add a batch of transactions to a pool one by one
  515. pool, key := setupTxPool()
  516. account, _ := transaction(0, big.NewInt(0), key).From()
  517. state, _ := pool.currentState()
  518. state.AddBalance(account, big.NewInt(1000000))
  519. for i := 0; i < size; i++ {
  520. tx := transaction(uint64(1+i), big.NewInt(100000), key)
  521. pool.enqueueTx(tx.Hash(), tx)
  522. }
  523. // Benchmark the speed of pool validation
  524. b.ResetTimer()
  525. for i := 0; i < b.N; i++ {
  526. pool.promoteExecutables()
  527. }
  528. }
  529. // Benchmarks the speed of iterative transaction insertion.
  530. func BenchmarkPoolInsert(b *testing.B) {
  531. // Generate a batch of transactions to enqueue into the pool
  532. pool, key := setupTxPool()
  533. account, _ := transaction(0, big.NewInt(0), key).From()
  534. state, _ := pool.currentState()
  535. state.AddBalance(account, big.NewInt(1000000))
  536. txs := make(types.Transactions, b.N)
  537. for i := 0; i < b.N; i++ {
  538. txs[i] = transaction(uint64(i), big.NewInt(100000), key)
  539. }
  540. // Benchmark importing the transactions into the queue
  541. b.ResetTimer()
  542. for _, tx := range txs {
  543. pool.Add(tx)
  544. }
  545. }
  546. // Benchmarks the speed of batched transaction insertion.
  547. func BenchmarkPoolBatchInsert100(b *testing.B) { benchmarkPoolBatchInsert(b, 100) }
  548. func BenchmarkPoolBatchInsert1000(b *testing.B) { benchmarkPoolBatchInsert(b, 1000) }
  549. func BenchmarkPoolBatchInsert10000(b *testing.B) { benchmarkPoolBatchInsert(b, 10000) }
  550. func benchmarkPoolBatchInsert(b *testing.B, size int) {
  551. // Generate a batch of transactions to enqueue into the pool
  552. pool, key := setupTxPool()
  553. account, _ := transaction(0, big.NewInt(0), key).From()
  554. state, _ := pool.currentState()
  555. state.AddBalance(account, big.NewInt(1000000))
  556. batches := make([]types.Transactions, b.N)
  557. for i := 0; i < b.N; i++ {
  558. batches[i] = make(types.Transactions, size)
  559. for j := 0; j < size; j++ {
  560. batches[i][j] = transaction(uint64(size*i+j), big.NewInt(100000), key)
  561. }
  562. }
  563. // Benchmark importing the transactions into the queue
  564. b.ResetTimer()
  565. for _, batch := range batches {
  566. pool.AddBatch(batch)
  567. }
  568. }