tx_pool_test.go 66 KB

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