tx_pool_test.go 60 KB

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