tx_pool_test.go 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  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. "fmt"
  20. "io/ioutil"
  21. "math/big"
  22. "math/rand"
  23. "os"
  24. "testing"
  25. "time"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/crypto"
  30. "github.com/ethereum/go-ethereum/ethdb"
  31. "github.com/ethereum/go-ethereum/event"
  32. "github.com/ethereum/go-ethereum/params"
  33. )
  34. // testTxPoolConfig is a transaction pool configuration without stateful disk
  35. // sideeffects used during testing.
  36. var testTxPoolConfig TxPoolConfig
  37. func init() {
  38. testTxPoolConfig = DefaultTxPoolConfig
  39. testTxPoolConfig.Journal = ""
  40. }
  41. type testBlockChain struct {
  42. statedb *state.StateDB
  43. gasLimit uint64
  44. chainHeadFeed *event.Feed
  45. }
  46. func (bc *testBlockChain) CurrentBlock() *types.Block {
  47. return types.NewBlock(&types.Header{
  48. GasLimit: bc.gasLimit,
  49. }, nil, nil, nil)
  50. }
  51. func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block {
  52. return bc.CurrentBlock()
  53. }
  54. func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) {
  55. return bc.statedb, nil
  56. }
  57. func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) event.Subscription {
  58. return bc.chainHeadFeed.Subscribe(ch)
  59. }
  60. func transaction(nonce uint64, gaslimit uint64, key *ecdsa.PrivateKey) *types.Transaction {
  61. return pricedTransaction(nonce, gaslimit, big.NewInt(1), key)
  62. }
  63. func pricedTransaction(nonce uint64, gaslimit uint64, gasprice *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
  64. tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{}, big.NewInt(100), gaslimit, gasprice, nil), types.HomesteadSigner{}, key)
  65. return tx
  66. }
  67. func setupTxPool() (*TxPool, *ecdsa.PrivateKey) {
  68. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  69. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  70. key, _ := crypto.GenerateKey()
  71. pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
  72. return pool, key
  73. }
  74. // validateTxPoolInternals checks various consistency invariants within the pool.
  75. func validateTxPoolInternals(pool *TxPool) error {
  76. pool.mu.RLock()
  77. defer pool.mu.RUnlock()
  78. // Ensure the total transaction set is consistent with pending + queued
  79. pending, queued := pool.stats()
  80. if total := len(pool.all); total != pending+queued {
  81. return fmt.Errorf("total transaction count %d != %d pending + %d queued", total, pending, queued)
  82. }
  83. if priced := pool.priced.items.Len() - pool.priced.stales; priced != pending+queued {
  84. return fmt.Errorf("total priced transaction count %d != %d pending + %d queued", priced, pending, queued)
  85. }
  86. // Ensure the next nonce to assign is the correct one
  87. for addr, txs := range pool.pending {
  88. // Find the last transaction
  89. var last uint64
  90. for nonce := range txs.txs.items {
  91. if last < nonce {
  92. last = nonce
  93. }
  94. }
  95. if nonce := pool.pendingState.GetNonce(addr); nonce != last+1 {
  96. return fmt.Errorf("pending nonce mismatch: have %v, want %v", nonce, last+1)
  97. }
  98. }
  99. return nil
  100. }
  101. // validateEvents checks that the correct number of transaction addition events
  102. // were fired on the pool's event feed.
  103. func validateEvents(events chan TxPreEvent, count int) error {
  104. for i := 0; i < count; i++ {
  105. select {
  106. case <-events:
  107. case <-time.After(time.Second):
  108. return fmt.Errorf("event #%d not fired", i)
  109. }
  110. }
  111. select {
  112. case tx := <-events:
  113. return fmt.Errorf("more than %d events fired: %v", count, tx.Tx)
  114. case <-time.After(50 * time.Millisecond):
  115. // This branch should be "default", but it's a data race between goroutines,
  116. // reading the event channel and pushng into it, so better wait a bit ensuring
  117. // really nothing gets injected.
  118. }
  119. return nil
  120. }
  121. func deriveSender(tx *types.Transaction) (common.Address, error) {
  122. return types.Sender(types.HomesteadSigner{}, tx)
  123. }
  124. type testChain struct {
  125. *testBlockChain
  126. address common.Address
  127. trigger *bool
  128. }
  129. // testChain.State() is used multiple times to reset the pending state.
  130. // when simulate is true it will create a state that indicates
  131. // that tx0 and tx1 are included in the chain.
  132. func (c *testChain) State() (*state.StateDB, error) {
  133. // delay "state change" by one. The tx pool fetches the
  134. // state multiple times and by delaying it a bit we simulate
  135. // a state change between those fetches.
  136. stdb := c.statedb
  137. if *c.trigger {
  138. c.statedb, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  139. // simulate that the new head block included tx0 and tx1
  140. c.statedb.SetNonce(c.address, 2)
  141. c.statedb.SetBalance(c.address, new(big.Int).SetUint64(params.Ether))
  142. *c.trigger = false
  143. }
  144. return stdb, nil
  145. }
  146. // This test simulates a scenario where a new block is imported during a
  147. // state reset and tests whether the pending state is in sync with the
  148. // block head event that initiated the resetState().
  149. func TestStateChangeDuringTransactionPoolReset(t *testing.T) {
  150. t.Parallel()
  151. var (
  152. key, _ = crypto.GenerateKey()
  153. address = crypto.PubkeyToAddress(key.PublicKey)
  154. statedb, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  155. trigger = false
  156. )
  157. // setup pool with 2 transaction in it
  158. statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether))
  159. blockchain := &testChain{&testBlockChain{statedb, 1000000000, new(event.Feed)}, address, &trigger}
  160. tx0 := transaction(0, 100000, key)
  161. tx1 := transaction(1, 100000, key)
  162. pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
  163. defer pool.Stop()
  164. nonce := pool.State().GetNonce(address)
  165. if nonce != 0 {
  166. t.Fatalf("Invalid nonce, want 0, got %d", nonce)
  167. }
  168. pool.AddRemotes(types.Transactions{tx0, tx1})
  169. nonce = pool.State().GetNonce(address)
  170. if nonce != 2 {
  171. t.Fatalf("Invalid nonce, want 2, got %d", nonce)
  172. }
  173. // trigger state change in the background
  174. trigger = true
  175. pool.lockedReset(nil, nil)
  176. _, err := pool.Pending()
  177. if err != nil {
  178. t.Fatalf("Could not fetch pending transactions: %v", err)
  179. }
  180. nonce = pool.State().GetNonce(address)
  181. if nonce != 2 {
  182. t.Fatalf("Invalid nonce, want 2, got %d", nonce)
  183. }
  184. }
  185. func TestInvalidTransactions(t *testing.T) {
  186. t.Parallel()
  187. pool, key := setupTxPool()
  188. defer pool.Stop()
  189. tx := transaction(0, 100, key)
  190. from, _ := deriveSender(tx)
  191. pool.currentState.AddBalance(from, big.NewInt(1))
  192. if err := pool.AddRemote(tx); err != ErrInsufficientFunds {
  193. t.Error("expected", ErrInsufficientFunds)
  194. }
  195. balance := new(big.Int).Add(tx.Value(), new(big.Int).Mul(new(big.Int).SetUint64(tx.Gas()), tx.GasPrice()))
  196. pool.currentState.AddBalance(from, balance)
  197. if err := pool.AddRemote(tx); err != ErrIntrinsicGas {
  198. t.Error("expected", ErrIntrinsicGas, "got", err)
  199. }
  200. pool.currentState.SetNonce(from, 1)
  201. pool.currentState.AddBalance(from, big.NewInt(0xffffffffffffff))
  202. tx = transaction(0, 100000, key)
  203. if err := pool.AddRemote(tx); err != ErrNonceTooLow {
  204. t.Error("expected", ErrNonceTooLow)
  205. }
  206. tx = transaction(1, 100000, key)
  207. pool.gasPrice = big.NewInt(1000)
  208. if err := pool.AddRemote(tx); err != ErrUnderpriced {
  209. t.Error("expected", ErrUnderpriced, "got", err)
  210. }
  211. if err := pool.AddLocal(tx); err != nil {
  212. t.Error("expected", nil, "got", err)
  213. }
  214. }
  215. func TestTransactionQueue(t *testing.T) {
  216. t.Parallel()
  217. pool, key := setupTxPool()
  218. defer pool.Stop()
  219. tx := transaction(0, 100, key)
  220. from, _ := deriveSender(tx)
  221. pool.currentState.AddBalance(from, big.NewInt(1000))
  222. pool.lockedReset(nil, nil)
  223. pool.enqueueTx(tx.Hash(), tx)
  224. pool.promoteExecutables([]common.Address{from})
  225. if len(pool.pending) != 1 {
  226. t.Error("expected valid txs to be 1 is", len(pool.pending))
  227. }
  228. tx = transaction(1, 100, key)
  229. from, _ = deriveSender(tx)
  230. pool.currentState.SetNonce(from, 2)
  231. pool.enqueueTx(tx.Hash(), tx)
  232. pool.promoteExecutables([]common.Address{from})
  233. if _, ok := pool.pending[from].txs.items[tx.Nonce()]; ok {
  234. t.Error("expected transaction to be in tx pool")
  235. }
  236. if len(pool.queue) > 0 {
  237. t.Error("expected transaction queue to be empty. is", len(pool.queue))
  238. }
  239. pool, key = setupTxPool()
  240. defer pool.Stop()
  241. tx1 := transaction(0, 100, key)
  242. tx2 := transaction(10, 100, key)
  243. tx3 := transaction(11, 100, key)
  244. from, _ = deriveSender(tx1)
  245. pool.currentState.AddBalance(from, big.NewInt(1000))
  246. pool.lockedReset(nil, nil)
  247. pool.enqueueTx(tx1.Hash(), tx1)
  248. pool.enqueueTx(tx2.Hash(), tx2)
  249. pool.enqueueTx(tx3.Hash(), tx3)
  250. pool.promoteExecutables([]common.Address{from})
  251. if len(pool.pending) != 1 {
  252. t.Error("expected tx pool to be 1, got", len(pool.pending))
  253. }
  254. if pool.queue[from].Len() != 2 {
  255. t.Error("expected len(queue) == 2, got", pool.queue[from].Len())
  256. }
  257. }
  258. func TestTransactionNegativeValue(t *testing.T) {
  259. t.Parallel()
  260. pool, key := setupTxPool()
  261. defer pool.Stop()
  262. tx, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(-1), 100, big.NewInt(1), nil), types.HomesteadSigner{}, key)
  263. from, _ := deriveSender(tx)
  264. pool.currentState.AddBalance(from, big.NewInt(1))
  265. if err := pool.AddRemote(tx); err != ErrNegativeValue {
  266. t.Error("expected", ErrNegativeValue, "got", err)
  267. }
  268. }
  269. func TestTransactionChainFork(t *testing.T) {
  270. t.Parallel()
  271. pool, key := setupTxPool()
  272. defer pool.Stop()
  273. addr := crypto.PubkeyToAddress(key.PublicKey)
  274. resetState := func() {
  275. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  276. statedb.AddBalance(addr, big.NewInt(100000000000000))
  277. pool.chain = &testBlockChain{statedb, 1000000, new(event.Feed)}
  278. pool.lockedReset(nil, nil)
  279. }
  280. resetState()
  281. tx := transaction(0, 100000, key)
  282. if _, err := pool.add(tx, false); err != nil {
  283. t.Error("didn't expect error", err)
  284. }
  285. pool.removeTx(tx.Hash(), true)
  286. // reset the pool's internal state
  287. resetState()
  288. if _, err := pool.add(tx, false); err != nil {
  289. t.Error("didn't expect error", err)
  290. }
  291. }
  292. func TestTransactionDoubleNonce(t *testing.T) {
  293. t.Parallel()
  294. pool, key := setupTxPool()
  295. defer pool.Stop()
  296. addr := crypto.PubkeyToAddress(key.PublicKey)
  297. resetState := func() {
  298. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  299. statedb.AddBalance(addr, big.NewInt(100000000000000))
  300. pool.chain = &testBlockChain{statedb, 1000000, new(event.Feed)}
  301. pool.lockedReset(nil, nil)
  302. }
  303. resetState()
  304. signer := types.HomesteadSigner{}
  305. tx1, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), 100000, big.NewInt(1), nil), signer, key)
  306. tx2, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), 1000000, big.NewInt(2), nil), signer, key)
  307. tx3, _ := types.SignTx(types.NewTransaction(0, common.Address{}, big.NewInt(100), 1000000, big.NewInt(1), nil), signer, key)
  308. // Add the first two transaction, ensure higher priced stays only
  309. if replace, err := pool.add(tx1, false); err != nil || replace {
  310. t.Errorf("first transaction insert failed (%v) or reported replacement (%v)", err, replace)
  311. }
  312. if replace, err := pool.add(tx2, false); err != nil || !replace {
  313. t.Errorf("second transaction insert failed (%v) or not reported replacement (%v)", err, replace)
  314. }
  315. pool.promoteExecutables([]common.Address{addr})
  316. if pool.pending[addr].Len() != 1 {
  317. t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
  318. }
  319. if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
  320. t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
  321. }
  322. // Add the third transaction and ensure it's not saved (smaller price)
  323. pool.add(tx3, false)
  324. pool.promoteExecutables([]common.Address{addr})
  325. if pool.pending[addr].Len() != 1 {
  326. t.Error("expected 1 pending transactions, got", pool.pending[addr].Len())
  327. }
  328. if tx := pool.pending[addr].txs.items[0]; tx.Hash() != tx2.Hash() {
  329. t.Errorf("transaction mismatch: have %x, want %x", tx.Hash(), tx2.Hash())
  330. }
  331. // Ensure the total transaction count is correct
  332. if len(pool.all) != 1 {
  333. t.Error("expected 1 total transactions, got", len(pool.all))
  334. }
  335. }
  336. func TestTransactionMissingNonce(t *testing.T) {
  337. t.Parallel()
  338. pool, key := setupTxPool()
  339. defer pool.Stop()
  340. addr := crypto.PubkeyToAddress(key.PublicKey)
  341. pool.currentState.AddBalance(addr, big.NewInt(100000000000000))
  342. tx := transaction(1, 100000, key)
  343. if _, err := pool.add(tx, false); err != nil {
  344. t.Error("didn't expect error", err)
  345. }
  346. if len(pool.pending) != 0 {
  347. t.Error("expected 0 pending transactions, got", len(pool.pending))
  348. }
  349. if pool.queue[addr].Len() != 1 {
  350. t.Error("expected 1 queued transaction, got", pool.queue[addr].Len())
  351. }
  352. if len(pool.all) != 1 {
  353. t.Error("expected 1 total transactions, got", len(pool.all))
  354. }
  355. }
  356. func TestTransactionNonceRecovery(t *testing.T) {
  357. t.Parallel()
  358. const n = 10
  359. pool, key := setupTxPool()
  360. defer pool.Stop()
  361. addr := crypto.PubkeyToAddress(key.PublicKey)
  362. pool.currentState.SetNonce(addr, n)
  363. pool.currentState.AddBalance(addr, big.NewInt(100000000000000))
  364. pool.lockedReset(nil, nil)
  365. tx := transaction(n, 100000, key)
  366. if err := pool.AddRemote(tx); err != nil {
  367. t.Error(err)
  368. }
  369. // simulate some weird re-order of transactions and missing nonce(s)
  370. pool.currentState.SetNonce(addr, n-1)
  371. pool.lockedReset(nil, nil)
  372. if fn := pool.pendingState.GetNonce(addr); fn != n-1 {
  373. t.Errorf("expected nonce to be %d, got %d", n-1, fn)
  374. }
  375. }
  376. // Tests that if an account runs out of funds, any pending and queued transactions
  377. // are dropped.
  378. func TestTransactionDropping(t *testing.T) {
  379. t.Parallel()
  380. // Create a test account and fund it
  381. pool, key := setupTxPool()
  382. defer pool.Stop()
  383. account, _ := deriveSender(transaction(0, 0, key))
  384. pool.currentState.AddBalance(account, big.NewInt(1000))
  385. // Add some pending and some queued transactions
  386. var (
  387. tx0 = transaction(0, 100, key)
  388. tx1 = transaction(1, 200, key)
  389. tx2 = transaction(2, 300, key)
  390. tx10 = transaction(10, 100, key)
  391. tx11 = transaction(11, 200, key)
  392. tx12 = transaction(12, 300, key)
  393. )
  394. pool.promoteTx(account, tx0.Hash(), tx0)
  395. pool.promoteTx(account, tx1.Hash(), tx1)
  396. pool.promoteTx(account, tx2.Hash(), tx2)
  397. pool.enqueueTx(tx10.Hash(), tx10)
  398. pool.enqueueTx(tx11.Hash(), tx11)
  399. pool.enqueueTx(tx12.Hash(), tx12)
  400. // Check that pre and post validations leave the pool as is
  401. if pool.pending[account].Len() != 3 {
  402. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 3)
  403. }
  404. if pool.queue[account].Len() != 3 {
  405. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 3)
  406. }
  407. if len(pool.all) != 6 {
  408. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 6)
  409. }
  410. pool.lockedReset(nil, nil)
  411. if pool.pending[account].Len() != 3 {
  412. t.Errorf("pending transaction mismatch: have %d, want %d", pool.pending[account].Len(), 3)
  413. }
  414. if pool.queue[account].Len() != 3 {
  415. t.Errorf("queued transaction mismatch: have %d, want %d", pool.queue[account].Len(), 3)
  416. }
  417. if len(pool.all) != 6 {
  418. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 6)
  419. }
  420. // Reduce the balance of the account, and check that invalidated transactions are dropped
  421. pool.currentState.AddBalance(account, big.NewInt(-650))
  422. pool.lockedReset(nil, nil)
  423. if _, ok := pool.pending[account].txs.items[tx0.Nonce()]; !ok {
  424. t.Errorf("funded pending transaction missing: %v", tx0)
  425. }
  426. if _, ok := pool.pending[account].txs.items[tx1.Nonce()]; !ok {
  427. t.Errorf("funded pending transaction missing: %v", tx0)
  428. }
  429. if _, ok := pool.pending[account].txs.items[tx2.Nonce()]; ok {
  430. t.Errorf("out-of-fund pending transaction present: %v", tx1)
  431. }
  432. if _, ok := pool.queue[account].txs.items[tx10.Nonce()]; !ok {
  433. t.Errorf("funded queued transaction missing: %v", tx10)
  434. }
  435. if _, ok := pool.queue[account].txs.items[tx11.Nonce()]; !ok {
  436. t.Errorf("funded queued transaction missing: %v", tx10)
  437. }
  438. if _, ok := pool.queue[account].txs.items[tx12.Nonce()]; ok {
  439. t.Errorf("out-of-fund queued transaction present: %v", tx11)
  440. }
  441. if len(pool.all) != 4 {
  442. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 4)
  443. }
  444. // Reduce the block gas limit, check that invalidated transactions are dropped
  445. pool.chain.(*testBlockChain).gasLimit = 100
  446. pool.lockedReset(nil, nil)
  447. if _, ok := pool.pending[account].txs.items[tx0.Nonce()]; !ok {
  448. t.Errorf("funded pending transaction missing: %v", tx0)
  449. }
  450. if _, ok := pool.pending[account].txs.items[tx1.Nonce()]; ok {
  451. t.Errorf("over-gased pending transaction present: %v", tx1)
  452. }
  453. if _, ok := pool.queue[account].txs.items[tx10.Nonce()]; !ok {
  454. t.Errorf("funded queued transaction missing: %v", tx10)
  455. }
  456. if _, ok := pool.queue[account].txs.items[tx11.Nonce()]; ok {
  457. t.Errorf("over-gased queued transaction present: %v", tx11)
  458. }
  459. if len(pool.all) != 2 {
  460. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), 2)
  461. }
  462. }
  463. // Tests that if a transaction is dropped from the current pending pool (e.g. out
  464. // of fund), all consecutive (still valid, but not executable) transactions are
  465. // postponed back into the future queue to prevent broadcasting them.
  466. func TestTransactionPostponing(t *testing.T) {
  467. t.Parallel()
  468. // Create the pool to test the postponing with
  469. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  470. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  471. pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
  472. defer pool.Stop()
  473. // Create two test accounts to produce different gap profiles with
  474. keys := make([]*ecdsa.PrivateKey, 2)
  475. accs := make([]common.Address, len(keys))
  476. for i := 0; i < len(keys); i++ {
  477. keys[i], _ = crypto.GenerateKey()
  478. accs[i] = crypto.PubkeyToAddress(keys[i].PublicKey)
  479. pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(50100))
  480. }
  481. // Add a batch consecutive pending transactions for validation
  482. txs := []*types.Transaction{}
  483. for i, key := range keys {
  484. for j := 0; j < 100; j++ {
  485. var tx *types.Transaction
  486. if (i+j)%2 == 0 {
  487. tx = transaction(uint64(j), 25000, key)
  488. } else {
  489. tx = transaction(uint64(j), 50000, key)
  490. }
  491. txs = append(txs, tx)
  492. }
  493. }
  494. for i, err := range pool.AddRemotes(txs) {
  495. if err != nil {
  496. t.Fatalf("tx %d: failed to add transactions: %v", i, err)
  497. }
  498. }
  499. // Check that pre and post validations leave the pool as is
  500. if pending := pool.pending[accs[0]].Len() + pool.pending[accs[1]].Len(); pending != len(txs) {
  501. t.Errorf("pending transaction mismatch: have %d, want %d", pending, len(txs))
  502. }
  503. if len(pool.queue) != 0 {
  504. t.Errorf("queued accounts mismatch: have %d, want %d", len(pool.queue), 0)
  505. }
  506. if len(pool.all) != len(txs) {
  507. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txs))
  508. }
  509. pool.lockedReset(nil, nil)
  510. if pending := pool.pending[accs[0]].Len() + pool.pending[accs[1]].Len(); pending != len(txs) {
  511. t.Errorf("pending transaction mismatch: have %d, want %d", pending, len(txs))
  512. }
  513. if len(pool.queue) != 0 {
  514. t.Errorf("queued accounts mismatch: have %d, want %d", len(pool.queue), 0)
  515. }
  516. if len(pool.all) != len(txs) {
  517. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txs))
  518. }
  519. // Reduce the balance of the account, and check that transactions are reorganised
  520. for _, addr := range accs {
  521. pool.currentState.AddBalance(addr, big.NewInt(-1))
  522. }
  523. pool.lockedReset(nil, nil)
  524. // The first account's first transaction remains valid, check that subsequent
  525. // ones are either filtered out, or queued up for later.
  526. if _, ok := pool.pending[accs[0]].txs.items[txs[0].Nonce()]; !ok {
  527. t.Errorf("tx %d: valid and funded transaction missing from pending pool: %v", 0, txs[0])
  528. }
  529. if _, ok := pool.queue[accs[0]].txs.items[txs[0].Nonce()]; ok {
  530. t.Errorf("tx %d: valid and funded transaction present in future queue: %v", 0, txs[0])
  531. }
  532. for i, tx := range txs[1:100] {
  533. if i%2 == 1 {
  534. if _, ok := pool.pending[accs[0]].txs.items[tx.Nonce()]; ok {
  535. t.Errorf("tx %d: valid but future transaction present in pending pool: %v", i+1, tx)
  536. }
  537. if _, ok := pool.queue[accs[0]].txs.items[tx.Nonce()]; !ok {
  538. t.Errorf("tx %d: valid but future transaction missing from future queue: %v", i+1, tx)
  539. }
  540. } else {
  541. if _, ok := pool.pending[accs[0]].txs.items[tx.Nonce()]; ok {
  542. t.Errorf("tx %d: out-of-fund transaction present in pending pool: %v", i+1, tx)
  543. }
  544. if _, ok := pool.queue[accs[0]].txs.items[tx.Nonce()]; ok {
  545. t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", i+1, tx)
  546. }
  547. }
  548. }
  549. // The second account's first transaction got invalid, check that all transactions
  550. // are either filtered out, or queued up for later.
  551. if pool.pending[accs[1]] != nil {
  552. t.Errorf("invalidated account still has pending transactions")
  553. }
  554. for i, tx := range txs[100:] {
  555. if i%2 == 1 {
  556. if _, ok := pool.queue[accs[1]].txs.items[tx.Nonce()]; !ok {
  557. t.Errorf("tx %d: valid but future transaction missing from future queue: %v", 100+i, tx)
  558. }
  559. } else {
  560. if _, ok := pool.queue[accs[1]].txs.items[tx.Nonce()]; ok {
  561. t.Errorf("tx %d: out-of-fund transaction present in future queue: %v", 100+i, tx)
  562. }
  563. }
  564. }
  565. if len(pool.all) != len(txs)/2 {
  566. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), len(txs)/2)
  567. }
  568. }
  569. // Tests that if the transaction pool has both executable and non-executable
  570. // transactions from an origin account, filling the nonce gap moves all queued
  571. // ones into the pending pool.
  572. func TestTransactionGapFilling(t *testing.T) {
  573. t.Parallel()
  574. // Create a test account and fund it
  575. pool, key := setupTxPool()
  576. defer pool.Stop()
  577. account, _ := deriveSender(transaction(0, 0, key))
  578. pool.currentState.AddBalance(account, big.NewInt(1000000))
  579. // Keep track of transaction events to ensure all executables get announced
  580. events := make(chan TxPreEvent, testTxPoolConfig.AccountQueue+5)
  581. sub := pool.txFeed.Subscribe(events)
  582. defer sub.Unsubscribe()
  583. // Create a pending and a queued transaction with a nonce-gap in between
  584. if err := pool.AddRemote(transaction(0, 100000, key)); err != nil {
  585. t.Fatalf("failed to add pending transaction: %v", err)
  586. }
  587. if err := pool.AddRemote(transaction(2, 100000, key)); err != nil {
  588. t.Fatalf("failed to add queued transaction: %v", err)
  589. }
  590. pending, queued := pool.Stats()
  591. if pending != 1 {
  592. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 1)
  593. }
  594. if queued != 1 {
  595. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
  596. }
  597. if err := validateEvents(events, 1); err != nil {
  598. t.Fatalf("original event firing failed: %v", err)
  599. }
  600. if err := validateTxPoolInternals(pool); err != nil {
  601. t.Fatalf("pool internal state corrupted: %v", err)
  602. }
  603. // Fill the nonce gap and ensure all transactions become pending
  604. if err := pool.AddRemote(transaction(1, 100000, key)); err != nil {
  605. t.Fatalf("failed to add gapped transaction: %v", err)
  606. }
  607. pending, queued = pool.Stats()
  608. if pending != 3 {
  609. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
  610. }
  611. if queued != 0 {
  612. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
  613. }
  614. if err := validateEvents(events, 2); err != nil {
  615. t.Fatalf("gap-filling event firing failed: %v", err)
  616. }
  617. if err := validateTxPoolInternals(pool); err != nil {
  618. t.Fatalf("pool internal state corrupted: %v", err)
  619. }
  620. }
  621. // Tests that if the transaction count belonging to a single account goes above
  622. // some threshold, the higher transactions are dropped to prevent DOS attacks.
  623. func TestTransactionQueueAccountLimiting(t *testing.T) {
  624. t.Parallel()
  625. // Create a test account and fund it
  626. pool, key := setupTxPool()
  627. defer pool.Stop()
  628. account, _ := deriveSender(transaction(0, 0, key))
  629. pool.currentState.AddBalance(account, big.NewInt(1000000))
  630. // Keep queuing up transactions and make sure all above a limit are dropped
  631. for i := uint64(1); i <= testTxPoolConfig.AccountQueue+5; i++ {
  632. if err := pool.AddRemote(transaction(i, 100000, key)); err != nil {
  633. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  634. }
  635. if len(pool.pending) != 0 {
  636. t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, len(pool.pending), 0)
  637. }
  638. if i <= testTxPoolConfig.AccountQueue {
  639. if pool.queue[account].Len() != int(i) {
  640. t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), i)
  641. }
  642. } else {
  643. if pool.queue[account].Len() != int(testTxPoolConfig.AccountQueue) {
  644. t.Errorf("tx %d: queue limit mismatch: have %d, want %d", i, pool.queue[account].Len(), testTxPoolConfig.AccountQueue)
  645. }
  646. }
  647. }
  648. if len(pool.all) != int(testTxPoolConfig.AccountQueue) {
  649. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), testTxPoolConfig.AccountQueue)
  650. }
  651. }
  652. // Tests that if the transaction count belonging to multiple accounts go above
  653. // some threshold, the higher transactions are dropped to prevent DOS attacks.
  654. //
  655. // This logic should not hold for local transactions, unless the local tracking
  656. // mechanism is disabled.
  657. func TestTransactionQueueGlobalLimiting(t *testing.T) {
  658. testTransactionQueueGlobalLimiting(t, false)
  659. }
  660. func TestTransactionQueueGlobalLimitingNoLocals(t *testing.T) {
  661. testTransactionQueueGlobalLimiting(t, true)
  662. }
  663. func testTransactionQueueGlobalLimiting(t *testing.T, nolocals bool) {
  664. t.Parallel()
  665. // Create the pool to test the limit enforcement with
  666. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  667. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  668. config := testTxPoolConfig
  669. config.NoLocals = nolocals
  670. config.GlobalQueue = config.AccountQueue*3 - 1 // reduce the queue limits to shorten test time (-1 to make it non divisible)
  671. pool := NewTxPool(config, params.TestChainConfig, blockchain)
  672. defer pool.Stop()
  673. // Create a number of test accounts and fund them (last one will be the local)
  674. keys := make([]*ecdsa.PrivateKey, 5)
  675. for i := 0; i < len(keys); i++ {
  676. keys[i], _ = crypto.GenerateKey()
  677. pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  678. }
  679. local := keys[len(keys)-1]
  680. // Generate and queue a batch of transactions
  681. nonces := make(map[common.Address]uint64)
  682. txs := make(types.Transactions, 0, 3*config.GlobalQueue)
  683. for len(txs) < cap(txs) {
  684. key := keys[rand.Intn(len(keys)-1)] // skip adding transactions with the local account
  685. addr := crypto.PubkeyToAddress(key.PublicKey)
  686. txs = append(txs, transaction(nonces[addr]+1, 100000, key))
  687. nonces[addr]++
  688. }
  689. // Import the batch and verify that limits have been enforced
  690. pool.AddRemotes(txs)
  691. queued := 0
  692. for addr, list := range pool.queue {
  693. if list.Len() > int(config.AccountQueue) {
  694. t.Errorf("addr %x: queued accounts overflown allowance: %d > %d", addr, list.Len(), config.AccountQueue)
  695. }
  696. queued += list.Len()
  697. }
  698. if queued > int(config.GlobalQueue) {
  699. t.Fatalf("total transactions overflow allowance: %d > %d", queued, config.GlobalQueue)
  700. }
  701. // Generate a batch of transactions from the local account and import them
  702. txs = txs[:0]
  703. for i := uint64(0); i < 3*config.GlobalQueue; i++ {
  704. txs = append(txs, transaction(i+1, 100000, local))
  705. }
  706. pool.AddLocals(txs)
  707. // If locals are disabled, the previous eviction algorithm should apply here too
  708. if nolocals {
  709. queued := 0
  710. for addr, list := range pool.queue {
  711. if list.Len() > int(config.AccountQueue) {
  712. t.Errorf("addr %x: queued accounts overflown allowance: %d > %d", addr, list.Len(), config.AccountQueue)
  713. }
  714. queued += list.Len()
  715. }
  716. if queued > int(config.GlobalQueue) {
  717. t.Fatalf("total transactions overflow allowance: %d > %d", queued, config.GlobalQueue)
  718. }
  719. } else {
  720. // Local exemptions are enabled, make sure the local account owned the queue
  721. if len(pool.queue) != 1 {
  722. t.Errorf("multiple accounts in queue: have %v, want %v", len(pool.queue), 1)
  723. }
  724. // Also ensure no local transactions are ever dropped, even if above global limits
  725. if queued := pool.queue[crypto.PubkeyToAddress(local.PublicKey)].Len(); uint64(queued) != 3*config.GlobalQueue {
  726. t.Fatalf("local account queued transaction count mismatch: have %v, want %v", queued, 3*config.GlobalQueue)
  727. }
  728. }
  729. }
  730. // Tests that if an account remains idle for a prolonged amount of time, any
  731. // non-executable transactions queued up are dropped to prevent wasting resources
  732. // on shuffling them around.
  733. //
  734. // This logic should not hold for local transactions, unless the local tracking
  735. // mechanism is disabled.
  736. func TestTransactionQueueTimeLimiting(t *testing.T) { testTransactionQueueTimeLimiting(t, false) }
  737. func TestTransactionQueueTimeLimitingNoLocals(t *testing.T) { testTransactionQueueTimeLimiting(t, true) }
  738. func testTransactionQueueTimeLimiting(t *testing.T, nolocals bool) {
  739. // Reduce the eviction interval to a testable amount
  740. defer func(old time.Duration) { evictionInterval = old }(evictionInterval)
  741. evictionInterval = time.Second
  742. // Create the pool to test the non-expiration enforcement
  743. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  744. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  745. config := testTxPoolConfig
  746. config.Lifetime = time.Second
  747. config.NoLocals = nolocals
  748. pool := NewTxPool(config, params.TestChainConfig, blockchain)
  749. defer pool.Stop()
  750. // Create two test accounts to ensure remotes expire but locals do not
  751. local, _ := crypto.GenerateKey()
  752. remote, _ := crypto.GenerateKey()
  753. pool.currentState.AddBalance(crypto.PubkeyToAddress(local.PublicKey), big.NewInt(1000000000))
  754. pool.currentState.AddBalance(crypto.PubkeyToAddress(remote.PublicKey), big.NewInt(1000000000))
  755. // Add the two transactions and ensure they both are queued up
  756. if err := pool.AddLocal(pricedTransaction(1, 100000, big.NewInt(1), local)); err != nil {
  757. t.Fatalf("failed to add local transaction: %v", err)
  758. }
  759. if err := pool.AddRemote(pricedTransaction(1, 100000, big.NewInt(1), remote)); err != nil {
  760. t.Fatalf("failed to add remote transaction: %v", err)
  761. }
  762. pending, queued := pool.Stats()
  763. if pending != 0 {
  764. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0)
  765. }
  766. if queued != 2 {
  767. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2)
  768. }
  769. if err := validateTxPoolInternals(pool); err != nil {
  770. t.Fatalf("pool internal state corrupted: %v", err)
  771. }
  772. // Wait a bit for eviction to run and clean up any leftovers, and ensure only the local remains
  773. time.Sleep(2 * config.Lifetime)
  774. pending, queued = pool.Stats()
  775. if pending != 0 {
  776. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0)
  777. }
  778. if nolocals {
  779. if queued != 0 {
  780. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
  781. }
  782. } else {
  783. if queued != 1 {
  784. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
  785. }
  786. }
  787. if err := validateTxPoolInternals(pool); err != nil {
  788. t.Fatalf("pool internal state corrupted: %v", err)
  789. }
  790. }
  791. // Tests that even if the transaction count belonging to a single account goes
  792. // above some threshold, as long as the transactions are executable, they are
  793. // accepted.
  794. func TestTransactionPendingLimiting(t *testing.T) {
  795. t.Parallel()
  796. // Create a test account and fund it
  797. pool, key := setupTxPool()
  798. defer pool.Stop()
  799. account, _ := deriveSender(transaction(0, 0, key))
  800. pool.currentState.AddBalance(account, big.NewInt(1000000))
  801. // Keep track of transaction events to ensure all executables get announced
  802. events := make(chan TxPreEvent, testTxPoolConfig.AccountQueue+5)
  803. sub := pool.txFeed.Subscribe(events)
  804. defer sub.Unsubscribe()
  805. // Keep queuing up transactions and make sure all above a limit are dropped
  806. for i := uint64(0); i < testTxPoolConfig.AccountQueue+5; i++ {
  807. if err := pool.AddRemote(transaction(i, 100000, key)); err != nil {
  808. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  809. }
  810. if pool.pending[account].Len() != int(i)+1 {
  811. t.Errorf("tx %d: pending pool size mismatch: have %d, want %d", i, pool.pending[account].Len(), i+1)
  812. }
  813. if len(pool.queue) != 0 {
  814. t.Errorf("tx %d: queue size mismatch: have %d, want %d", i, pool.queue[account].Len(), 0)
  815. }
  816. }
  817. if len(pool.all) != int(testTxPoolConfig.AccountQueue+5) {
  818. t.Errorf("total transaction mismatch: have %d, want %d", len(pool.all), testTxPoolConfig.AccountQueue+5)
  819. }
  820. if err := validateEvents(events, int(testTxPoolConfig.AccountQueue+5)); err != nil {
  821. t.Fatalf("event firing failed: %v", err)
  822. }
  823. if err := validateTxPoolInternals(pool); err != nil {
  824. t.Fatalf("pool internal state corrupted: %v", err)
  825. }
  826. }
  827. // Tests that the transaction limits are enforced the same way irrelevant whether
  828. // the transactions are added one by one or in batches.
  829. func TestTransactionQueueLimitingEquivalency(t *testing.T) { testTransactionLimitingEquivalency(t, 1) }
  830. func TestTransactionPendingLimitingEquivalency(t *testing.T) { testTransactionLimitingEquivalency(t, 0) }
  831. func testTransactionLimitingEquivalency(t *testing.T, origin uint64) {
  832. t.Parallel()
  833. // Add a batch of transactions to a pool one by one
  834. pool1, key1 := setupTxPool()
  835. defer pool1.Stop()
  836. account1, _ := deriveSender(transaction(0, 0, key1))
  837. pool1.currentState.AddBalance(account1, big.NewInt(1000000))
  838. for i := uint64(0); i < testTxPoolConfig.AccountQueue+5; i++ {
  839. if err := pool1.AddRemote(transaction(origin+i, 100000, key1)); err != nil {
  840. t.Fatalf("tx %d: failed to add transaction: %v", i, err)
  841. }
  842. }
  843. // Add a batch of transactions to a pool in one big batch
  844. pool2, key2 := setupTxPool()
  845. defer pool2.Stop()
  846. account2, _ := deriveSender(transaction(0, 0, key2))
  847. pool2.currentState.AddBalance(account2, big.NewInt(1000000))
  848. txs := []*types.Transaction{}
  849. for i := uint64(0); i < testTxPoolConfig.AccountQueue+5; i++ {
  850. txs = append(txs, transaction(origin+i, 100000, key2))
  851. }
  852. pool2.AddRemotes(txs)
  853. // Ensure the batch optimization honors the same pool mechanics
  854. if len(pool1.pending) != len(pool2.pending) {
  855. t.Errorf("pending transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.pending), len(pool2.pending))
  856. }
  857. if len(pool1.queue) != len(pool2.queue) {
  858. t.Errorf("queued transaction count mismatch: one-by-one algo: %d, batch algo: %d", len(pool1.queue), len(pool2.queue))
  859. }
  860. if len(pool1.all) != len(pool2.all) {
  861. t.Errorf("total transaction count mismatch: one-by-one algo %d, batch algo %d", len(pool1.all), len(pool2.all))
  862. }
  863. if err := validateTxPoolInternals(pool1); err != nil {
  864. t.Errorf("pool 1 internal state corrupted: %v", err)
  865. }
  866. if err := validateTxPoolInternals(pool2); err != nil {
  867. t.Errorf("pool 2 internal state corrupted: %v", err)
  868. }
  869. }
  870. // Tests that if the transaction count belonging to multiple accounts go above
  871. // some hard threshold, the higher transactions are dropped to prevent DOS
  872. // attacks.
  873. func TestTransactionPendingGlobalLimiting(t *testing.T) {
  874. t.Parallel()
  875. // Create the pool to test the limit enforcement with
  876. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  877. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  878. config := testTxPoolConfig
  879. config.GlobalSlots = config.AccountSlots * 10
  880. pool := NewTxPool(config, params.TestChainConfig, blockchain)
  881. defer pool.Stop()
  882. // Create a number of test accounts and fund them
  883. keys := make([]*ecdsa.PrivateKey, 5)
  884. for i := 0; i < len(keys); i++ {
  885. keys[i], _ = crypto.GenerateKey()
  886. pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  887. }
  888. // Generate and queue a batch of transactions
  889. nonces := make(map[common.Address]uint64)
  890. txs := types.Transactions{}
  891. for _, key := range keys {
  892. addr := crypto.PubkeyToAddress(key.PublicKey)
  893. for j := 0; j < int(config.GlobalSlots)/len(keys)*2; j++ {
  894. txs = append(txs, transaction(nonces[addr], 100000, key))
  895. nonces[addr]++
  896. }
  897. }
  898. // Import the batch and verify that limits have been enforced
  899. pool.AddRemotes(txs)
  900. pending := 0
  901. for _, list := range pool.pending {
  902. pending += list.Len()
  903. }
  904. if pending > int(config.GlobalSlots) {
  905. t.Fatalf("total pending transactions overflow allowance: %d > %d", pending, config.GlobalSlots)
  906. }
  907. if err := validateTxPoolInternals(pool); err != nil {
  908. t.Fatalf("pool internal state corrupted: %v", err)
  909. }
  910. }
  911. // Tests that if transactions start being capped, transactions are also removed from 'all'
  912. func TestTransactionCapClearsFromAll(t *testing.T) {
  913. t.Parallel()
  914. // Create the pool to test the limit enforcement with
  915. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  916. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  917. config := testTxPoolConfig
  918. config.AccountSlots = 2
  919. config.AccountQueue = 2
  920. config.GlobalSlots = 8
  921. pool := NewTxPool(config, params.TestChainConfig, blockchain)
  922. defer pool.Stop()
  923. // Create a number of test accounts and fund them
  924. key, _ := crypto.GenerateKey()
  925. addr := crypto.PubkeyToAddress(key.PublicKey)
  926. pool.currentState.AddBalance(addr, big.NewInt(1000000))
  927. txs := types.Transactions{}
  928. for j := 0; j < int(config.GlobalSlots)*2; j++ {
  929. txs = append(txs, transaction(uint64(j), 100000, key))
  930. }
  931. // Import the batch and verify that limits have been enforced
  932. pool.AddRemotes(txs)
  933. if err := validateTxPoolInternals(pool); err != nil {
  934. t.Fatalf("pool internal state corrupted: %v", err)
  935. }
  936. }
  937. // Tests that if the transaction count belonging to multiple accounts go above
  938. // some hard threshold, if they are under the minimum guaranteed slot count then
  939. // the transactions are still kept.
  940. func TestTransactionPendingMinimumAllowance(t *testing.T) {
  941. t.Parallel()
  942. // Create the pool to test the limit enforcement with
  943. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  944. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  945. config := testTxPoolConfig
  946. config.GlobalSlots = 0
  947. pool := NewTxPool(config, params.TestChainConfig, blockchain)
  948. defer pool.Stop()
  949. // Create a number of test accounts and fund them
  950. keys := make([]*ecdsa.PrivateKey, 5)
  951. for i := 0; i < len(keys); i++ {
  952. keys[i], _ = crypto.GenerateKey()
  953. pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  954. }
  955. // Generate and queue a batch of transactions
  956. nonces := make(map[common.Address]uint64)
  957. txs := types.Transactions{}
  958. for _, key := range keys {
  959. addr := crypto.PubkeyToAddress(key.PublicKey)
  960. for j := 0; j < int(config.AccountSlots)*2; j++ {
  961. txs = append(txs, transaction(nonces[addr], 100000, key))
  962. nonces[addr]++
  963. }
  964. }
  965. // Import the batch and verify that limits have been enforced
  966. pool.AddRemotes(txs)
  967. for addr, list := range pool.pending {
  968. if list.Len() != int(config.AccountSlots) {
  969. t.Errorf("addr %x: total pending transactions mismatch: have %d, want %d", addr, list.Len(), config.AccountSlots)
  970. }
  971. }
  972. if err := validateTxPoolInternals(pool); err != nil {
  973. t.Fatalf("pool internal state corrupted: %v", err)
  974. }
  975. }
  976. // Tests that setting the transaction pool gas price to a higher value correctly
  977. // discards everything cheaper than that and moves any gapped transactions back
  978. // from the pending pool to the queue.
  979. //
  980. // Note, local transactions are never allowed to be dropped.
  981. func TestTransactionPoolRepricing(t *testing.T) {
  982. t.Parallel()
  983. // Create the pool to test the pricing enforcement with
  984. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  985. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  986. pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
  987. defer pool.Stop()
  988. // Keep track of transaction events to ensure all executables get announced
  989. events := make(chan TxPreEvent, 32)
  990. sub := pool.txFeed.Subscribe(events)
  991. defer sub.Unsubscribe()
  992. // Create a number of test accounts and fund them
  993. keys := make([]*ecdsa.PrivateKey, 4)
  994. for i := 0; i < len(keys); i++ {
  995. keys[i], _ = crypto.GenerateKey()
  996. pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  997. }
  998. // Generate and queue a batch of transactions, both pending and queued
  999. txs := types.Transactions{}
  1000. txs = append(txs, pricedTransaction(0, 100000, big.NewInt(2), keys[0]))
  1001. txs = append(txs, pricedTransaction(1, 100000, big.NewInt(1), keys[0]))
  1002. txs = append(txs, pricedTransaction(2, 100000, big.NewInt(2), keys[0]))
  1003. txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[1]))
  1004. txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[1]))
  1005. txs = append(txs, pricedTransaction(2, 100000, big.NewInt(2), keys[1]))
  1006. txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[2]))
  1007. txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[2]))
  1008. txs = append(txs, pricedTransaction(3, 100000, big.NewInt(2), keys[2]))
  1009. ltx := pricedTransaction(0, 100000, big.NewInt(1), keys[3])
  1010. // Import the batch and that both pending and queued transactions match up
  1011. pool.AddRemotes(txs)
  1012. pool.AddLocal(ltx)
  1013. pending, queued := pool.Stats()
  1014. if pending != 7 {
  1015. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 7)
  1016. }
  1017. if queued != 3 {
  1018. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 3)
  1019. }
  1020. if err := validateEvents(events, 7); err != nil {
  1021. t.Fatalf("original event firing failed: %v", err)
  1022. }
  1023. if err := validateTxPoolInternals(pool); err != nil {
  1024. t.Fatalf("pool internal state corrupted: %v", err)
  1025. }
  1026. // Reprice the pool and check that underpriced transactions get dropped
  1027. pool.SetGasPrice(big.NewInt(2))
  1028. pending, queued = pool.Stats()
  1029. if pending != 2 {
  1030. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
  1031. }
  1032. if queued != 5 {
  1033. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 5)
  1034. }
  1035. if err := validateEvents(events, 0); err != nil {
  1036. t.Fatalf("reprice event firing failed: %v", err)
  1037. }
  1038. if err := validateTxPoolInternals(pool); err != nil {
  1039. t.Fatalf("pool internal state corrupted: %v", err)
  1040. }
  1041. // Check that we can't add the old transactions back
  1042. if err := pool.AddRemote(pricedTransaction(1, 100000, big.NewInt(1), keys[0])); err != ErrUnderpriced {
  1043. t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
  1044. }
  1045. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(1), keys[1])); err != ErrUnderpriced {
  1046. t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
  1047. }
  1048. if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(1), keys[2])); err != ErrUnderpriced {
  1049. t.Fatalf("adding underpriced queued transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
  1050. }
  1051. if err := validateEvents(events, 0); err != nil {
  1052. t.Fatalf("post-reprice event firing failed: %v", err)
  1053. }
  1054. if err := validateTxPoolInternals(pool); err != nil {
  1055. t.Fatalf("pool internal state corrupted: %v", err)
  1056. }
  1057. // However we can add local underpriced transactions
  1058. tx := pricedTransaction(1, 100000, big.NewInt(1), keys[3])
  1059. if err := pool.AddLocal(tx); err != nil {
  1060. t.Fatalf("failed to add underpriced local transaction: %v", err)
  1061. }
  1062. if pending, _ = pool.Stats(); pending != 3 {
  1063. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
  1064. }
  1065. if err := validateEvents(events, 1); err != nil {
  1066. t.Fatalf("post-reprice local event firing failed: %v", err)
  1067. }
  1068. if err := validateTxPoolInternals(pool); err != nil {
  1069. t.Fatalf("pool internal state corrupted: %v", err)
  1070. }
  1071. // And we can fill gaps with properly priced transactions
  1072. if err := pool.AddRemote(pricedTransaction(1, 100000, big.NewInt(2), keys[0])); err != nil {
  1073. t.Fatalf("failed to add pending transaction: %v", err)
  1074. }
  1075. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(2), keys[1])); err != nil {
  1076. t.Fatalf("failed to add pending transaction: %v", err)
  1077. }
  1078. if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(2), keys[2])); err != nil {
  1079. t.Fatalf("failed to add queued transaction: %v", err)
  1080. }
  1081. if err := validateEvents(events, 5); err != nil {
  1082. t.Fatalf("post-reprice event firing failed: %v", err)
  1083. }
  1084. if err := validateTxPoolInternals(pool); err != nil {
  1085. t.Fatalf("pool internal state corrupted: %v", err)
  1086. }
  1087. }
  1088. // Tests that setting the transaction pool gas price to a higher value does not
  1089. // remove local transactions.
  1090. func TestTransactionPoolRepricingKeepsLocals(t *testing.T) {
  1091. t.Parallel()
  1092. // Create the pool to test the pricing enforcement with
  1093. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  1094. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  1095. pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
  1096. defer pool.Stop()
  1097. // Create a number of test accounts and fund them
  1098. keys := make([]*ecdsa.PrivateKey, 3)
  1099. for i := 0; i < len(keys); i++ {
  1100. keys[i], _ = crypto.GenerateKey()
  1101. pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000*1000000))
  1102. }
  1103. // Create transaction (both pending and queued) with a linearly growing gasprice
  1104. for i := uint64(0); i < 500; i++ {
  1105. // Add pending
  1106. p_tx := pricedTransaction(i, 100000, big.NewInt(int64(i)), keys[2])
  1107. if err := pool.AddLocal(p_tx); err != nil {
  1108. t.Fatal(err)
  1109. }
  1110. // Add queued
  1111. q_tx := pricedTransaction(i+501, 100000, big.NewInt(int64(i)), keys[2])
  1112. if err := pool.AddLocal(q_tx); err != nil {
  1113. t.Fatal(err)
  1114. }
  1115. }
  1116. pending, queued := pool.Stats()
  1117. expPending, expQueued := 500, 500
  1118. validate := func() {
  1119. pending, queued = pool.Stats()
  1120. if pending != expPending {
  1121. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, expPending)
  1122. }
  1123. if queued != expQueued {
  1124. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, expQueued)
  1125. }
  1126. if err := validateTxPoolInternals(pool); err != nil {
  1127. t.Fatalf("pool internal state corrupted: %v", err)
  1128. }
  1129. }
  1130. validate()
  1131. // Reprice the pool and check that nothing is dropped
  1132. pool.SetGasPrice(big.NewInt(2))
  1133. validate()
  1134. pool.SetGasPrice(big.NewInt(2))
  1135. pool.SetGasPrice(big.NewInt(4))
  1136. pool.SetGasPrice(big.NewInt(8))
  1137. pool.SetGasPrice(big.NewInt(100))
  1138. validate()
  1139. }
  1140. // Tests that when the pool reaches its global transaction limit, underpriced
  1141. // transactions are gradually shifted out for more expensive ones and any gapped
  1142. // pending transactions are moved into the queue.
  1143. //
  1144. // Note, local transactions are never allowed to be dropped.
  1145. func TestTransactionPoolUnderpricing(t *testing.T) {
  1146. t.Parallel()
  1147. // Create the pool to test the pricing enforcement with
  1148. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  1149. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  1150. config := testTxPoolConfig
  1151. config.GlobalSlots = 2
  1152. config.GlobalQueue = 2
  1153. pool := NewTxPool(config, params.TestChainConfig, blockchain)
  1154. defer pool.Stop()
  1155. // Keep track of transaction events to ensure all executables get announced
  1156. events := make(chan TxPreEvent, 32)
  1157. sub := pool.txFeed.Subscribe(events)
  1158. defer sub.Unsubscribe()
  1159. // Create a number of test accounts and fund them
  1160. keys := make([]*ecdsa.PrivateKey, 4)
  1161. for i := 0; i < len(keys); i++ {
  1162. keys[i], _ = crypto.GenerateKey()
  1163. pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  1164. }
  1165. // Generate and queue a batch of transactions, both pending and queued
  1166. txs := types.Transactions{}
  1167. txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[0]))
  1168. txs = append(txs, pricedTransaction(1, 100000, big.NewInt(2), keys[0]))
  1169. txs = append(txs, pricedTransaction(1, 100000, big.NewInt(1), keys[1]))
  1170. ltx := pricedTransaction(0, 100000, big.NewInt(1), keys[2])
  1171. // Import the batch and that both pending and queued transactions match up
  1172. pool.AddRemotes(txs)
  1173. pool.AddLocal(ltx)
  1174. pending, queued := pool.Stats()
  1175. if pending != 3 {
  1176. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
  1177. }
  1178. if queued != 1 {
  1179. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
  1180. }
  1181. if err := validateEvents(events, 3); err != nil {
  1182. t.Fatalf("original event firing failed: %v", err)
  1183. }
  1184. if err := validateTxPoolInternals(pool); err != nil {
  1185. t.Fatalf("pool internal state corrupted: %v", err)
  1186. }
  1187. // Ensure that adding an underpriced transaction on block limit fails
  1188. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(1), keys[1])); err != ErrUnderpriced {
  1189. t.Fatalf("adding underpriced pending transaction error mismatch: have %v, want %v", err, ErrUnderpriced)
  1190. }
  1191. // Ensure that adding high priced transactions drops cheap ones, but not own
  1192. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(3), keys[1])); err != nil { // +K1:0 => -K1:1 => Pend K0:0, K0:1, K1:0, K2:0; Que -
  1193. t.Fatalf("failed to add well priced transaction: %v", err)
  1194. }
  1195. if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(4), keys[1])); err != nil { // +K1:2 => -K0:0 => Pend K1:0, K2:0; Que K0:1 K1:2
  1196. t.Fatalf("failed to add well priced transaction: %v", err)
  1197. }
  1198. if err := pool.AddRemote(pricedTransaction(3, 100000, big.NewInt(5), keys[1])); err != nil { // +K1:3 => -K0:1 => Pend K1:0, K2:0; Que K1:2 K1:3
  1199. t.Fatalf("failed to add well priced transaction: %v", err)
  1200. }
  1201. pending, queued = pool.Stats()
  1202. if pending != 2 {
  1203. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
  1204. }
  1205. if queued != 2 {
  1206. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2)
  1207. }
  1208. if err := validateEvents(events, 1); err != nil {
  1209. t.Fatalf("additional event firing failed: %v", err)
  1210. }
  1211. if err := validateTxPoolInternals(pool); err != nil {
  1212. t.Fatalf("pool internal state corrupted: %v", err)
  1213. }
  1214. // Ensure that adding local transactions can push out even higher priced ones
  1215. ltx = pricedTransaction(1, 100000, big.NewInt(0), keys[2])
  1216. if err := pool.AddLocal(ltx); err != nil {
  1217. t.Fatalf("failed to append underpriced local transaction: %v", err)
  1218. }
  1219. ltx = pricedTransaction(0, 100000, big.NewInt(0), keys[3])
  1220. if err := pool.AddLocal(ltx); err != nil {
  1221. t.Fatalf("failed to add new underpriced local transaction: %v", err)
  1222. }
  1223. pending, queued = pool.Stats()
  1224. if pending != 3 {
  1225. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 3)
  1226. }
  1227. if queued != 1 {
  1228. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
  1229. }
  1230. if err := validateEvents(events, 2); err != nil {
  1231. t.Fatalf("local event firing failed: %v", err)
  1232. }
  1233. if err := validateTxPoolInternals(pool); err != nil {
  1234. t.Fatalf("pool internal state corrupted: %v", err)
  1235. }
  1236. }
  1237. // Tests that more expensive transactions push out cheap ones from the pool, but
  1238. // without producing instability by creating gaps that start jumping transactions
  1239. // back and forth between queued/pending.
  1240. func TestTransactionPoolStableUnderpricing(t *testing.T) {
  1241. t.Parallel()
  1242. // Create the pool to test the pricing enforcement with
  1243. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  1244. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  1245. config := testTxPoolConfig
  1246. config.GlobalSlots = 128
  1247. config.GlobalQueue = 0
  1248. pool := NewTxPool(config, params.TestChainConfig, blockchain)
  1249. defer pool.Stop()
  1250. // Keep track of transaction events to ensure all executables get announced
  1251. events := make(chan TxPreEvent, 32)
  1252. sub := pool.txFeed.Subscribe(events)
  1253. defer sub.Unsubscribe()
  1254. // Create a number of test accounts and fund them
  1255. keys := make([]*ecdsa.PrivateKey, 2)
  1256. for i := 0; i < len(keys); i++ {
  1257. keys[i], _ = crypto.GenerateKey()
  1258. pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  1259. }
  1260. // Fill up the entire queue with the same transaction price points
  1261. txs := types.Transactions{}
  1262. for i := uint64(0); i < config.GlobalSlots; i++ {
  1263. txs = append(txs, pricedTransaction(i, 100000, big.NewInt(1), keys[0]))
  1264. }
  1265. pool.AddRemotes(txs)
  1266. pending, queued := pool.Stats()
  1267. if pending != int(config.GlobalSlots) {
  1268. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, config.GlobalSlots)
  1269. }
  1270. if queued != 0 {
  1271. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
  1272. }
  1273. if err := validateEvents(events, int(config.GlobalSlots)); err != nil {
  1274. t.Fatalf("original event firing failed: %v", err)
  1275. }
  1276. if err := validateTxPoolInternals(pool); err != nil {
  1277. t.Fatalf("pool internal state corrupted: %v", err)
  1278. }
  1279. // Ensure that adding high priced transactions drops a cheap, but doesn't produce a gap
  1280. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(3), keys[1])); err != nil {
  1281. t.Fatalf("failed to add well priced transaction: %v", err)
  1282. }
  1283. pending, queued = pool.Stats()
  1284. if pending != int(config.GlobalSlots) {
  1285. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, config.GlobalSlots)
  1286. }
  1287. if queued != 0 {
  1288. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
  1289. }
  1290. if err := validateEvents(events, 1); err != nil {
  1291. t.Fatalf("additional event firing failed: %v", err)
  1292. }
  1293. if err := validateTxPoolInternals(pool); err != nil {
  1294. t.Fatalf("pool internal state corrupted: %v", err)
  1295. }
  1296. }
  1297. // Tests that the pool rejects replacement transactions that don't meet the minimum
  1298. // price bump required.
  1299. func TestTransactionReplacement(t *testing.T) {
  1300. t.Parallel()
  1301. // Create the pool to test the pricing enforcement with
  1302. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  1303. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  1304. pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
  1305. defer pool.Stop()
  1306. // Keep track of transaction events to ensure all executables get announced
  1307. events := make(chan TxPreEvent, 32)
  1308. sub := pool.txFeed.Subscribe(events)
  1309. defer sub.Unsubscribe()
  1310. // Create a test account to add transactions with
  1311. key, _ := crypto.GenerateKey()
  1312. pool.currentState.AddBalance(crypto.PubkeyToAddress(key.PublicKey), big.NewInt(1000000000))
  1313. // Add pending transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too)
  1314. price := int64(100)
  1315. threshold := (price * (100 + int64(testTxPoolConfig.PriceBump))) / 100
  1316. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(1), key)); err != nil {
  1317. t.Fatalf("failed to add original cheap pending transaction: %v", err)
  1318. }
  1319. if err := pool.AddRemote(pricedTransaction(0, 100001, big.NewInt(1), key)); err != ErrReplaceUnderpriced {
  1320. t.Fatalf("original cheap pending transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced)
  1321. }
  1322. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(2), key)); err != nil {
  1323. t.Fatalf("failed to replace original cheap pending transaction: %v", err)
  1324. }
  1325. if err := validateEvents(events, 2); err != nil {
  1326. t.Fatalf("cheap replacement event firing failed: %v", err)
  1327. }
  1328. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(price), key)); err != nil {
  1329. t.Fatalf("failed to add original proper pending transaction: %v", err)
  1330. }
  1331. if err := pool.AddRemote(pricedTransaction(0, 100001, big.NewInt(threshold-1), key)); err != ErrReplaceUnderpriced {
  1332. t.Fatalf("original proper pending transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced)
  1333. }
  1334. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(threshold), key)); err != nil {
  1335. t.Fatalf("failed to replace original proper pending transaction: %v", err)
  1336. }
  1337. if err := validateEvents(events, 2); err != nil {
  1338. t.Fatalf("proper replacement event firing failed: %v", err)
  1339. }
  1340. // Add queued transactions, ensuring the minimum price bump is enforced for replacement (for ultra low prices too)
  1341. if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(1), key)); err != nil {
  1342. t.Fatalf("failed to add original cheap queued transaction: %v", err)
  1343. }
  1344. if err := pool.AddRemote(pricedTransaction(2, 100001, big.NewInt(1), key)); err != ErrReplaceUnderpriced {
  1345. t.Fatalf("original cheap queued transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced)
  1346. }
  1347. if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(2), key)); err != nil {
  1348. t.Fatalf("failed to replace original cheap queued transaction: %v", err)
  1349. }
  1350. if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(price), key)); err != nil {
  1351. t.Fatalf("failed to add original proper queued transaction: %v", err)
  1352. }
  1353. if err := pool.AddRemote(pricedTransaction(2, 100001, big.NewInt(threshold-1), key)); err != ErrReplaceUnderpriced {
  1354. t.Fatalf("original proper queued transaction replacement error mismatch: have %v, want %v", err, ErrReplaceUnderpriced)
  1355. }
  1356. if err := pool.AddRemote(pricedTransaction(2, 100000, big.NewInt(threshold), key)); err != nil {
  1357. t.Fatalf("failed to replace original proper queued transaction: %v", err)
  1358. }
  1359. if err := validateEvents(events, 0); err != nil {
  1360. t.Fatalf("queued replacement event firing failed: %v", err)
  1361. }
  1362. if err := validateTxPoolInternals(pool); err != nil {
  1363. t.Fatalf("pool internal state corrupted: %v", err)
  1364. }
  1365. }
  1366. // Tests that local transactions are journaled to disk, but remote transactions
  1367. // get discarded between restarts.
  1368. func TestTransactionJournaling(t *testing.T) { testTransactionJournaling(t, false) }
  1369. func TestTransactionJournalingNoLocals(t *testing.T) { testTransactionJournaling(t, true) }
  1370. func testTransactionJournaling(t *testing.T, nolocals bool) {
  1371. t.Parallel()
  1372. // Create a temporary file for the journal
  1373. file, err := ioutil.TempFile("", "")
  1374. if err != nil {
  1375. t.Fatalf("failed to create temporary journal: %v", err)
  1376. }
  1377. journal := file.Name()
  1378. defer os.Remove(journal)
  1379. // Clean up the temporary file, we only need the path for now
  1380. file.Close()
  1381. os.Remove(journal)
  1382. // Create the original pool to inject transaction into the journal
  1383. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  1384. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  1385. config := testTxPoolConfig
  1386. config.NoLocals = nolocals
  1387. config.Journal = journal
  1388. config.Rejournal = time.Second
  1389. pool := NewTxPool(config, params.TestChainConfig, blockchain)
  1390. // Create two test accounts to ensure remotes expire but locals do not
  1391. local, _ := crypto.GenerateKey()
  1392. remote, _ := crypto.GenerateKey()
  1393. pool.currentState.AddBalance(crypto.PubkeyToAddress(local.PublicKey), big.NewInt(1000000000))
  1394. pool.currentState.AddBalance(crypto.PubkeyToAddress(remote.PublicKey), big.NewInt(1000000000))
  1395. // Add three local and a remote transactions and ensure they are queued up
  1396. if err := pool.AddLocal(pricedTransaction(0, 100000, big.NewInt(1), local)); err != nil {
  1397. t.Fatalf("failed to add local transaction: %v", err)
  1398. }
  1399. if err := pool.AddLocal(pricedTransaction(1, 100000, big.NewInt(1), local)); err != nil {
  1400. t.Fatalf("failed to add local transaction: %v", err)
  1401. }
  1402. if err := pool.AddLocal(pricedTransaction(2, 100000, big.NewInt(1), local)); err != nil {
  1403. t.Fatalf("failed to add local transaction: %v", err)
  1404. }
  1405. if err := pool.AddRemote(pricedTransaction(0, 100000, big.NewInt(1), remote)); err != nil {
  1406. t.Fatalf("failed to add remote transaction: %v", err)
  1407. }
  1408. pending, queued := pool.Stats()
  1409. if pending != 4 {
  1410. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 4)
  1411. }
  1412. if queued != 0 {
  1413. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
  1414. }
  1415. if err := validateTxPoolInternals(pool); err != nil {
  1416. t.Fatalf("pool internal state corrupted: %v", err)
  1417. }
  1418. // Terminate the old pool, bump the local nonce, create a new pool and ensure relevant transaction survive
  1419. pool.Stop()
  1420. statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 1)
  1421. blockchain = &testBlockChain{statedb, 1000000, new(event.Feed)}
  1422. pool = NewTxPool(config, params.TestChainConfig, blockchain)
  1423. pending, queued = pool.Stats()
  1424. if queued != 0 {
  1425. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
  1426. }
  1427. if nolocals {
  1428. if pending != 0 {
  1429. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0)
  1430. }
  1431. } else {
  1432. if pending != 2 {
  1433. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
  1434. }
  1435. }
  1436. if err := validateTxPoolInternals(pool); err != nil {
  1437. t.Fatalf("pool internal state corrupted: %v", err)
  1438. }
  1439. // Bump the nonce temporarily and ensure the newly invalidated transaction is removed
  1440. statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 2)
  1441. pool.lockedReset(nil, nil)
  1442. time.Sleep(2 * config.Rejournal)
  1443. pool.Stop()
  1444. statedb.SetNonce(crypto.PubkeyToAddress(local.PublicKey), 1)
  1445. blockchain = &testBlockChain{statedb, 1000000, new(event.Feed)}
  1446. pool = NewTxPool(config, params.TestChainConfig, blockchain)
  1447. pending, queued = pool.Stats()
  1448. if pending != 0 {
  1449. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 0)
  1450. }
  1451. if nolocals {
  1452. if queued != 0 {
  1453. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 0)
  1454. }
  1455. } else {
  1456. if queued != 1 {
  1457. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 1)
  1458. }
  1459. }
  1460. if err := validateTxPoolInternals(pool); err != nil {
  1461. t.Fatalf("pool internal state corrupted: %v", err)
  1462. }
  1463. pool.Stop()
  1464. }
  1465. // TestTransactionStatusCheck tests that the pool can correctly retrieve the
  1466. // pending status of individual transactions.
  1467. func TestTransactionStatusCheck(t *testing.T) {
  1468. t.Parallel()
  1469. // Create the pool to test the status retrievals with
  1470. statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  1471. blockchain := &testBlockChain{statedb, 1000000, new(event.Feed)}
  1472. pool := NewTxPool(testTxPoolConfig, params.TestChainConfig, blockchain)
  1473. defer pool.Stop()
  1474. // Create the test accounts to check various transaction statuses with
  1475. keys := make([]*ecdsa.PrivateKey, 3)
  1476. for i := 0; i < len(keys); i++ {
  1477. keys[i], _ = crypto.GenerateKey()
  1478. pool.currentState.AddBalance(crypto.PubkeyToAddress(keys[i].PublicKey), big.NewInt(1000000))
  1479. }
  1480. // Generate and queue a batch of transactions, both pending and queued
  1481. txs := types.Transactions{}
  1482. txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[0])) // Pending only
  1483. txs = append(txs, pricedTransaction(0, 100000, big.NewInt(1), keys[1])) // Pending and queued
  1484. txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[1]))
  1485. txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[2])) // Queued only
  1486. // Import the transaction and ensure they are correctly added
  1487. pool.AddRemotes(txs)
  1488. pending, queued := pool.Stats()
  1489. if pending != 2 {
  1490. t.Fatalf("pending transactions mismatched: have %d, want %d", pending, 2)
  1491. }
  1492. if queued != 2 {
  1493. t.Fatalf("queued transactions mismatched: have %d, want %d", queued, 2)
  1494. }
  1495. if err := validateTxPoolInternals(pool); err != nil {
  1496. t.Fatalf("pool internal state corrupted: %v", err)
  1497. }
  1498. // Retrieve the status of each transaction and validate them
  1499. hashes := make([]common.Hash, len(txs))
  1500. for i, tx := range txs {
  1501. hashes[i] = tx.Hash()
  1502. }
  1503. hashes = append(hashes, common.Hash{})
  1504. statuses := pool.Status(hashes)
  1505. expect := []TxStatus{TxStatusPending, TxStatusPending, TxStatusQueued, TxStatusQueued, TxStatusUnknown}
  1506. for i := 0; i < len(statuses); i++ {
  1507. if statuses[i] != expect[i] {
  1508. t.Errorf("transaction %d: status mismatch: have %v, want %v", i, statuses[i], expect[i])
  1509. }
  1510. }
  1511. }
  1512. // Benchmarks the speed of validating the contents of the pending queue of the
  1513. // transaction pool.
  1514. func BenchmarkPendingDemotion100(b *testing.B) { benchmarkPendingDemotion(b, 100) }
  1515. func BenchmarkPendingDemotion1000(b *testing.B) { benchmarkPendingDemotion(b, 1000) }
  1516. func BenchmarkPendingDemotion10000(b *testing.B) { benchmarkPendingDemotion(b, 10000) }
  1517. func benchmarkPendingDemotion(b *testing.B, size int) {
  1518. // Add a batch of transactions to a pool one by one
  1519. pool, key := setupTxPool()
  1520. defer pool.Stop()
  1521. account, _ := deriveSender(transaction(0, 0, key))
  1522. pool.currentState.AddBalance(account, big.NewInt(1000000))
  1523. for i := 0; i < size; i++ {
  1524. tx := transaction(uint64(i), 100000, key)
  1525. pool.promoteTx(account, tx.Hash(), tx)
  1526. }
  1527. // Benchmark the speed of pool validation
  1528. b.ResetTimer()
  1529. for i := 0; i < b.N; i++ {
  1530. pool.demoteUnexecutables()
  1531. }
  1532. }
  1533. // Benchmarks the speed of scheduling the contents of the future queue of the
  1534. // transaction pool.
  1535. func BenchmarkFuturePromotion100(b *testing.B) { benchmarkFuturePromotion(b, 100) }
  1536. func BenchmarkFuturePromotion1000(b *testing.B) { benchmarkFuturePromotion(b, 1000) }
  1537. func BenchmarkFuturePromotion10000(b *testing.B) { benchmarkFuturePromotion(b, 10000) }
  1538. func benchmarkFuturePromotion(b *testing.B, size int) {
  1539. // Add a batch of transactions to a pool one by one
  1540. pool, key := setupTxPool()
  1541. defer pool.Stop()
  1542. account, _ := deriveSender(transaction(0, 0, key))
  1543. pool.currentState.AddBalance(account, big.NewInt(1000000))
  1544. for i := 0; i < size; i++ {
  1545. tx := transaction(uint64(1+i), 100000, key)
  1546. pool.enqueueTx(tx.Hash(), tx)
  1547. }
  1548. // Benchmark the speed of pool validation
  1549. b.ResetTimer()
  1550. for i := 0; i < b.N; i++ {
  1551. pool.promoteExecutables(nil)
  1552. }
  1553. }
  1554. // Benchmarks the speed of iterative transaction insertion.
  1555. func BenchmarkPoolInsert(b *testing.B) {
  1556. // Generate a batch of transactions to enqueue into the pool
  1557. pool, key := setupTxPool()
  1558. defer pool.Stop()
  1559. account, _ := deriveSender(transaction(0, 0, key))
  1560. pool.currentState.AddBalance(account, big.NewInt(1000000))
  1561. txs := make(types.Transactions, b.N)
  1562. for i := 0; i < b.N; i++ {
  1563. txs[i] = transaction(uint64(i), 100000, key)
  1564. }
  1565. // Benchmark importing the transactions into the queue
  1566. b.ResetTimer()
  1567. for _, tx := range txs {
  1568. pool.AddRemote(tx)
  1569. }
  1570. }
  1571. // Benchmarks the speed of batched transaction insertion.
  1572. func BenchmarkPoolBatchInsert100(b *testing.B) { benchmarkPoolBatchInsert(b, 100) }
  1573. func BenchmarkPoolBatchInsert1000(b *testing.B) { benchmarkPoolBatchInsert(b, 1000) }
  1574. func BenchmarkPoolBatchInsert10000(b *testing.B) { benchmarkPoolBatchInsert(b, 10000) }
  1575. func benchmarkPoolBatchInsert(b *testing.B, size int) {
  1576. // Generate a batch of transactions to enqueue into the pool
  1577. pool, key := setupTxPool()
  1578. defer pool.Stop()
  1579. account, _ := deriveSender(transaction(0, 0, key))
  1580. pool.currentState.AddBalance(account, big.NewInt(1000000))
  1581. batches := make([]types.Transactions, b.N)
  1582. for i := 0; i < b.N; i++ {
  1583. batches[i] = make(types.Transactions, size)
  1584. for j := 0; j < size; j++ {
  1585. batches[i][j] = transaction(uint64(size*i+j), 100000, key)
  1586. }
  1587. }
  1588. // Benchmark importing the transactions into the queue
  1589. b.ResetTimer()
  1590. for _, batch := range batches {
  1591. pool.AddRemotes(batch)
  1592. }
  1593. }