tx_pool_test.go 29 KB

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