handler_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // Copyright 2016 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 les
  17. import (
  18. "bytes"
  19. "math/big"
  20. "math/rand"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/consensus/ethash"
  25. "github.com/ethereum/go-ethereum/core"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. "github.com/ethereum/go-ethereum/eth/downloader"
  29. "github.com/ethereum/go-ethereum/ethdb"
  30. "github.com/ethereum/go-ethereum/light"
  31. "github.com/ethereum/go-ethereum/p2p"
  32. "github.com/ethereum/go-ethereum/params"
  33. "github.com/ethereum/go-ethereum/rlp"
  34. "github.com/ethereum/go-ethereum/trie"
  35. )
  36. func expectResponse(r p2p.MsgReader, msgcode, reqID, bv uint64, data interface{}) error {
  37. type resp struct {
  38. ReqID, BV uint64
  39. Data interface{}
  40. }
  41. return p2p.ExpectMsg(r, msgcode, resp{reqID, bv, data})
  42. }
  43. func testCheckProof(t *testing.T, exp *light.NodeSet, got light.NodeList) {
  44. if exp.KeyCount() > len(got) {
  45. t.Errorf("proof has fewer nodes than expected")
  46. return
  47. }
  48. if exp.KeyCount() < len(got) {
  49. t.Errorf("proof has more nodes than expected")
  50. return
  51. }
  52. for _, node := range got {
  53. n, _ := exp.Get(crypto.Keccak256(node))
  54. if !bytes.Equal(n, node) {
  55. t.Errorf("proof contents mismatch")
  56. return
  57. }
  58. }
  59. }
  60. // Tests that block headers can be retrieved from a remote chain based on user queries.
  61. func TestGetBlockHeadersLes1(t *testing.T) { testGetBlockHeaders(t, 1) }
  62. func TestGetBlockHeadersLes2(t *testing.T) { testGetBlockHeaders(t, 2) }
  63. func testGetBlockHeaders(t *testing.T, protocol int) {
  64. db, _ := ethdb.NewMemDatabase()
  65. pm := newTestProtocolManagerMust(t, false, downloader.MaxHashFetch+15, nil, nil, nil, db)
  66. bc := pm.blockchain.(*core.BlockChain)
  67. peer, _ := newTestPeer(t, "peer", protocol, pm, true)
  68. defer peer.close()
  69. // Create a "random" unknown hash for testing
  70. var unknown common.Hash
  71. for i := range unknown {
  72. unknown[i] = byte(i)
  73. }
  74. // Create a batch of tests for various scenarios
  75. limit := uint64(MaxHeaderFetch)
  76. tests := []struct {
  77. query *getBlockHeadersData // The query to execute for header retrieval
  78. expect []common.Hash // The hashes of the block whose headers are expected
  79. }{
  80. // A single random block should be retrievable by hash and number too
  81. {
  82. &getBlockHeadersData{Origin: hashOrNumber{Hash: bc.GetBlockByNumber(limit / 2).Hash()}, Amount: 1},
  83. []common.Hash{bc.GetBlockByNumber(limit / 2).Hash()},
  84. }, {
  85. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 1},
  86. []common.Hash{bc.GetBlockByNumber(limit / 2).Hash()},
  87. },
  88. // Multiple headers should be retrievable in both directions
  89. {
  90. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3},
  91. []common.Hash{
  92. bc.GetBlockByNumber(limit / 2).Hash(),
  93. bc.GetBlockByNumber(limit/2 + 1).Hash(),
  94. bc.GetBlockByNumber(limit/2 + 2).Hash(),
  95. },
  96. }, {
  97. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3, Reverse: true},
  98. []common.Hash{
  99. bc.GetBlockByNumber(limit / 2).Hash(),
  100. bc.GetBlockByNumber(limit/2 - 1).Hash(),
  101. bc.GetBlockByNumber(limit/2 - 2).Hash(),
  102. },
  103. },
  104. // Multiple headers with skip lists should be retrievable
  105. {
  106. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3},
  107. []common.Hash{
  108. bc.GetBlockByNumber(limit / 2).Hash(),
  109. bc.GetBlockByNumber(limit/2 + 4).Hash(),
  110. bc.GetBlockByNumber(limit/2 + 8).Hash(),
  111. },
  112. }, {
  113. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3, Reverse: true},
  114. []common.Hash{
  115. bc.GetBlockByNumber(limit / 2).Hash(),
  116. bc.GetBlockByNumber(limit/2 - 4).Hash(),
  117. bc.GetBlockByNumber(limit/2 - 8).Hash(),
  118. },
  119. },
  120. // The chain endpoints should be retrievable
  121. {
  122. &getBlockHeadersData{Origin: hashOrNumber{Number: 0}, Amount: 1},
  123. []common.Hash{bc.GetBlockByNumber(0).Hash()},
  124. }, {
  125. &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64()}, Amount: 1},
  126. []common.Hash{bc.CurrentBlock().Hash()},
  127. },
  128. // Ensure protocol limits are honored
  129. /*{
  130. &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64() - 1}, Amount: limit + 10, Reverse: true},
  131. bc.GetBlockHashesFromHash(bc.CurrentBlock().Hash(), limit),
  132. },*/
  133. // Check that requesting more than available is handled gracefully
  134. {
  135. &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64() - 4}, Skip: 3, Amount: 3},
  136. []common.Hash{
  137. bc.GetBlockByNumber(bc.CurrentBlock().NumberU64() - 4).Hash(),
  138. bc.GetBlockByNumber(bc.CurrentBlock().NumberU64()).Hash(),
  139. },
  140. }, {
  141. &getBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 3, Amount: 3, Reverse: true},
  142. []common.Hash{
  143. bc.GetBlockByNumber(4).Hash(),
  144. bc.GetBlockByNumber(0).Hash(),
  145. },
  146. },
  147. // Check that requesting more than available is handled gracefully, even if mid skip
  148. {
  149. &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64() - 4}, Skip: 2, Amount: 3},
  150. []common.Hash{
  151. bc.GetBlockByNumber(bc.CurrentBlock().NumberU64() - 4).Hash(),
  152. bc.GetBlockByNumber(bc.CurrentBlock().NumberU64() - 1).Hash(),
  153. },
  154. }, {
  155. &getBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 2, Amount: 3, Reverse: true},
  156. []common.Hash{
  157. bc.GetBlockByNumber(4).Hash(),
  158. bc.GetBlockByNumber(1).Hash(),
  159. },
  160. },
  161. // Check that non existing headers aren't returned
  162. {
  163. &getBlockHeadersData{Origin: hashOrNumber{Hash: unknown}, Amount: 1},
  164. []common.Hash{},
  165. }, {
  166. &getBlockHeadersData{Origin: hashOrNumber{Number: bc.CurrentBlock().NumberU64() + 1}, Amount: 1},
  167. []common.Hash{},
  168. },
  169. }
  170. // Run each of the tests and verify the results against the chain
  171. var reqID uint64
  172. for i, tt := range tests {
  173. // Collect the headers to expect in the response
  174. headers := []*types.Header{}
  175. for _, hash := range tt.expect {
  176. headers = append(headers, bc.GetHeaderByHash(hash))
  177. }
  178. // Send the hash request and verify the response
  179. reqID++
  180. cost := peer.GetRequestCost(GetBlockHeadersMsg, int(tt.query.Amount))
  181. sendRequest(peer.app, GetBlockHeadersMsg, reqID, cost, tt.query)
  182. if err := expectResponse(peer.app, BlockHeadersMsg, reqID, testBufLimit, headers); err != nil {
  183. t.Errorf("test %d: headers mismatch: %v", i, err)
  184. }
  185. }
  186. }
  187. // Tests that block contents can be retrieved from a remote chain based on their hashes.
  188. func TestGetBlockBodiesLes1(t *testing.T) { testGetBlockBodies(t, 1) }
  189. func TestGetBlockBodiesLes2(t *testing.T) { testGetBlockBodies(t, 2) }
  190. func testGetBlockBodies(t *testing.T, protocol int) {
  191. db, _ := ethdb.NewMemDatabase()
  192. pm := newTestProtocolManagerMust(t, false, downloader.MaxBlockFetch+15, nil, nil, nil, db)
  193. bc := pm.blockchain.(*core.BlockChain)
  194. peer, _ := newTestPeer(t, "peer", protocol, pm, true)
  195. defer peer.close()
  196. // Create a batch of tests for various scenarios
  197. limit := MaxBodyFetch
  198. tests := []struct {
  199. random int // Number of blocks to fetch randomly from the chain
  200. explicit []common.Hash // Explicitly requested blocks
  201. available []bool // Availability of explicitly requested blocks
  202. expected int // Total number of existing blocks to expect
  203. }{
  204. {1, nil, nil, 1}, // A single random block should be retrievable
  205. {10, nil, nil, 10}, // Multiple random blocks should be retrievable
  206. {limit, nil, nil, limit}, // The maximum possible blocks should be retrievable
  207. //{limit + 1, nil, nil, limit}, // No more than the possible block count should be returned
  208. {0, []common.Hash{bc.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable
  209. {0, []common.Hash{bc.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable
  210. {0, []common.Hash{{}}, []bool{false}, 0}, // A non existent block should not be returned
  211. // Existing and non-existing blocks interleaved should not cause problems
  212. {0, []common.Hash{
  213. {},
  214. bc.GetBlockByNumber(1).Hash(),
  215. {},
  216. bc.GetBlockByNumber(10).Hash(),
  217. {},
  218. bc.GetBlockByNumber(100).Hash(),
  219. {},
  220. }, []bool{false, true, false, true, false, true, false}, 3},
  221. }
  222. // Run each of the tests and verify the results against the chain
  223. var reqID uint64
  224. for i, tt := range tests {
  225. // Collect the hashes to request, and the response to expect
  226. hashes, seen := []common.Hash{}, make(map[int64]bool)
  227. bodies := []*types.Body{}
  228. for j := 0; j < tt.random; j++ {
  229. for {
  230. num := rand.Int63n(int64(bc.CurrentBlock().NumberU64()))
  231. if !seen[num] {
  232. seen[num] = true
  233. block := bc.GetBlockByNumber(uint64(num))
  234. hashes = append(hashes, block.Hash())
  235. if len(bodies) < tt.expected {
  236. bodies = append(bodies, &types.Body{Transactions: block.Transactions(), Uncles: block.Uncles()})
  237. }
  238. break
  239. }
  240. }
  241. }
  242. for j, hash := range tt.explicit {
  243. hashes = append(hashes, hash)
  244. if tt.available[j] && len(bodies) < tt.expected {
  245. block := bc.GetBlockByHash(hash)
  246. bodies = append(bodies, &types.Body{Transactions: block.Transactions(), Uncles: block.Uncles()})
  247. }
  248. }
  249. reqID++
  250. // Send the hash request and verify the response
  251. cost := peer.GetRequestCost(GetBlockBodiesMsg, len(hashes))
  252. sendRequest(peer.app, GetBlockBodiesMsg, reqID, cost, hashes)
  253. if err := expectResponse(peer.app, BlockBodiesMsg, reqID, testBufLimit, bodies); err != nil {
  254. t.Errorf("test %d: bodies mismatch: %v", i, err)
  255. }
  256. }
  257. }
  258. // Tests that the contract codes can be retrieved based on account addresses.
  259. func TestGetCodeLes1(t *testing.T) { testGetCode(t, 1) }
  260. func TestGetCodeLes2(t *testing.T) { testGetCode(t, 2) }
  261. func testGetCode(t *testing.T, protocol int) {
  262. // Assemble the test environment
  263. db, _ := ethdb.NewMemDatabase()
  264. pm := newTestProtocolManagerMust(t, false, 4, testChainGen, nil, nil, db)
  265. bc := pm.blockchain.(*core.BlockChain)
  266. peer, _ := newTestPeer(t, "peer", protocol, pm, true)
  267. defer peer.close()
  268. var codereqs []*CodeReq
  269. var codes [][]byte
  270. for i := uint64(0); i <= bc.CurrentBlock().NumberU64(); i++ {
  271. header := bc.GetHeaderByNumber(i)
  272. req := &CodeReq{
  273. BHash: header.Hash(),
  274. AccKey: crypto.Keccak256(testContractAddr[:]),
  275. }
  276. codereqs = append(codereqs, req)
  277. if i >= testContractDeployed {
  278. codes = append(codes, testContractCodeDeployed)
  279. }
  280. }
  281. cost := peer.GetRequestCost(GetCodeMsg, len(codereqs))
  282. sendRequest(peer.app, GetCodeMsg, 42, cost, codereqs)
  283. if err := expectResponse(peer.app, CodeMsg, 42, testBufLimit, codes); err != nil {
  284. t.Errorf("codes mismatch: %v", err)
  285. }
  286. }
  287. // Tests that the transaction receipts can be retrieved based on hashes.
  288. func TestGetReceiptLes1(t *testing.T) { testGetReceipt(t, 1) }
  289. func TestGetReceiptLes2(t *testing.T) { testGetReceipt(t, 2) }
  290. func testGetReceipt(t *testing.T, protocol int) {
  291. // Assemble the test environment
  292. db, _ := ethdb.NewMemDatabase()
  293. pm := newTestProtocolManagerMust(t, false, 4, testChainGen, nil, nil, db)
  294. bc := pm.blockchain.(*core.BlockChain)
  295. peer, _ := newTestPeer(t, "peer", protocol, pm, true)
  296. defer peer.close()
  297. // Collect the hashes to request, and the response to expect
  298. hashes, receipts := []common.Hash{}, []types.Receipts{}
  299. for i := uint64(0); i <= bc.CurrentBlock().NumberU64(); i++ {
  300. block := bc.GetBlockByNumber(i)
  301. hashes = append(hashes, block.Hash())
  302. receipts = append(receipts, core.GetBlockReceipts(db, block.Hash(), block.NumberU64()))
  303. }
  304. // Send the hash request and verify the response
  305. cost := peer.GetRequestCost(GetReceiptsMsg, len(hashes))
  306. sendRequest(peer.app, GetReceiptsMsg, 42, cost, hashes)
  307. if err := expectResponse(peer.app, ReceiptsMsg, 42, testBufLimit, receipts); err != nil {
  308. t.Errorf("receipts mismatch: %v", err)
  309. }
  310. }
  311. // Tests that trie merkle proofs can be retrieved
  312. func TestGetProofsLes1(t *testing.T) { testGetProofs(t, 1) }
  313. func TestGetProofsLes2(t *testing.T) { testGetProofs(t, 2) }
  314. func testGetProofs(t *testing.T, protocol int) {
  315. // Assemble the test environment
  316. db, _ := ethdb.NewMemDatabase()
  317. pm := newTestProtocolManagerMust(t, false, 4, testChainGen, nil, nil, db)
  318. bc := pm.blockchain.(*core.BlockChain)
  319. peer, _ := newTestPeer(t, "peer", protocol, pm, true)
  320. defer peer.close()
  321. var (
  322. proofreqs []ProofReq
  323. proofsV1 [][]rlp.RawValue
  324. )
  325. proofsV2 := light.NewNodeSet()
  326. accounts := []common.Address{testBankAddress, acc1Addr, acc2Addr, {}}
  327. for i := uint64(0); i <= bc.CurrentBlock().NumberU64(); i++ {
  328. header := bc.GetHeaderByNumber(i)
  329. root := header.Root
  330. trie, _ := trie.New(root, db)
  331. for _, acc := range accounts {
  332. req := ProofReq{
  333. BHash: header.Hash(),
  334. Key: crypto.Keccak256(acc[:]),
  335. }
  336. proofreqs = append(proofreqs, req)
  337. switch protocol {
  338. case 1:
  339. var proof light.NodeList
  340. trie.Prove(crypto.Keccak256(acc[:]), 0, &proof)
  341. proofsV1 = append(proofsV1, proof)
  342. case 2:
  343. trie.Prove(crypto.Keccak256(acc[:]), 0, proofsV2)
  344. }
  345. }
  346. }
  347. // Send the proof request and verify the response
  348. switch protocol {
  349. case 1:
  350. cost := peer.GetRequestCost(GetProofsV1Msg, len(proofreqs))
  351. sendRequest(peer.app, GetProofsV1Msg, 42, cost, proofreqs)
  352. if err := expectResponse(peer.app, ProofsV1Msg, 42, testBufLimit, proofsV1); err != nil {
  353. t.Errorf("proofs mismatch: %v", err)
  354. }
  355. case 2:
  356. cost := peer.GetRequestCost(GetProofsV2Msg, len(proofreqs))
  357. sendRequest(peer.app, GetProofsV2Msg, 42, cost, proofreqs)
  358. msg, err := peer.app.ReadMsg()
  359. if err != nil {
  360. t.Errorf("Message read error: %v", err)
  361. }
  362. var resp struct {
  363. ReqID, BV uint64
  364. Data light.NodeList
  365. }
  366. if err := msg.Decode(&resp); err != nil {
  367. t.Errorf("reply decode error: %v", err)
  368. }
  369. if msg.Code != ProofsV2Msg {
  370. t.Errorf("Message code mismatch")
  371. }
  372. if resp.ReqID != 42 {
  373. t.Errorf("ReqID mismatch")
  374. }
  375. if resp.BV != testBufLimit {
  376. t.Errorf("BV mismatch")
  377. }
  378. testCheckProof(t, proofsV2, resp.Data)
  379. }
  380. }
  381. func TestTransactionStatusLes2(t *testing.T) {
  382. db, _ := ethdb.NewMemDatabase()
  383. pm := newTestProtocolManagerMust(t, false, 0, nil, nil, nil, db)
  384. chain := pm.blockchain.(*core.BlockChain)
  385. config := core.DefaultTxPoolConfig
  386. config.Journal = ""
  387. txpool := core.NewTxPool(config, params.TestChainConfig, chain)
  388. pm.txpool = txpool
  389. peer, _ := newTestPeer(t, "peer", 2, pm, true)
  390. defer peer.close()
  391. var reqID uint64
  392. test := func(tx *types.Transaction, send bool, expStatus txStatus) {
  393. reqID++
  394. if send {
  395. cost := peer.GetRequestCost(SendTxV2Msg, 1)
  396. sendRequest(peer.app, SendTxV2Msg, reqID, cost, types.Transactions{tx})
  397. } else {
  398. cost := peer.GetRequestCost(GetTxStatusMsg, 1)
  399. sendRequest(peer.app, GetTxStatusMsg, reqID, cost, []common.Hash{tx.Hash()})
  400. }
  401. if err := expectResponse(peer.app, TxStatusMsg, reqID, testBufLimit, []txStatus{expStatus}); err != nil {
  402. t.Errorf("transaction status mismatch")
  403. }
  404. }
  405. signer := types.HomesteadSigner{}
  406. // test error status by sending an underpriced transaction
  407. tx0, _ := types.SignTx(types.NewTransaction(0, acc1Addr, big.NewInt(10000), params.TxGas, nil, nil), signer, testBankKey)
  408. test(tx0, true, txStatus{Status: core.TxStatusUnknown, Error: core.ErrUnderpriced})
  409. tx1, _ := types.SignTx(types.NewTransaction(0, acc1Addr, big.NewInt(10000), params.TxGas, big.NewInt(100000000000), nil), signer, testBankKey)
  410. test(tx1, false, txStatus{Status: core.TxStatusUnknown}) // query before sending, should be unknown
  411. test(tx1, true, txStatus{Status: core.TxStatusPending}) // send valid processable tx, should return pending
  412. test(tx1, true, txStatus{Status: core.TxStatusPending}) // adding it again should not return an error
  413. tx2, _ := types.SignTx(types.NewTransaction(1, acc1Addr, big.NewInt(10000), params.TxGas, big.NewInt(100000000000), nil), signer, testBankKey)
  414. tx3, _ := types.SignTx(types.NewTransaction(2, acc1Addr, big.NewInt(10000), params.TxGas, big.NewInt(100000000000), nil), signer, testBankKey)
  415. // send transactions in the wrong order, tx3 should be queued
  416. test(tx3, true, txStatus{Status: core.TxStatusQueued})
  417. test(tx2, true, txStatus{Status: core.TxStatusPending})
  418. // query again, now tx3 should be pending too
  419. test(tx3, false, txStatus{Status: core.TxStatusPending})
  420. // generate and add a block with tx1 and tx2 included
  421. gchain, _ := core.GenerateChain(params.TestChainConfig, chain.GetBlockByNumber(0), ethash.NewFaker(), db, 1, func(i int, block *core.BlockGen) {
  422. block.AddTx(tx1)
  423. block.AddTx(tx2)
  424. })
  425. if _, err := chain.InsertChain(gchain); err != nil {
  426. panic(err)
  427. }
  428. // wait until TxPool processes the inserted block
  429. for i := 0; i < 10; i++ {
  430. if pending, _ := txpool.Stats(); pending == 1 {
  431. break
  432. }
  433. time.Sleep(100 * time.Millisecond)
  434. }
  435. if pending, _ := txpool.Stats(); pending != 1 {
  436. t.Fatalf("pending count mismatch: have %d, want 1", pending)
  437. }
  438. // check if their status is included now
  439. block1hash := core.GetCanonicalHash(db, 1)
  440. test(tx1, false, txStatus{Status: core.TxStatusIncluded, Lookup: &core.TxLookupEntry{BlockHash: block1hash, BlockIndex: 1, Index: 0}})
  441. test(tx2, false, txStatus{Status: core.TxStatusIncluded, Lookup: &core.TxLookupEntry{BlockHash: block1hash, BlockIndex: 1, Index: 1}})
  442. // create a reorg that rolls them back
  443. gchain, _ = core.GenerateChain(params.TestChainConfig, chain.GetBlockByNumber(0), ethash.NewFaker(), db, 2, func(i int, block *core.BlockGen) {})
  444. if _, err := chain.InsertChain(gchain); err != nil {
  445. panic(err)
  446. }
  447. // wait until TxPool processes the reorg
  448. for i := 0; i < 10; i++ {
  449. if pending, _ := txpool.Stats(); pending == 3 {
  450. break
  451. }
  452. time.Sleep(100 * time.Millisecond)
  453. }
  454. if pending, _ := txpool.Stats(); pending != 3 {
  455. t.Fatalf("pending count mismatch: have %d, want 3", pending)
  456. }
  457. // check if their status is pending again
  458. test(tx1, false, txStatus{Status: core.TxStatusPending})
  459. test(tx2, false, txStatus{Status: core.TxStatusPending})
  460. }