handler_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. package eth
  2. import (
  3. "fmt"
  4. "math/big"
  5. "math/rand"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/core"
  9. "github.com/ethereum/go-ethereum/core/state"
  10. "github.com/ethereum/go-ethereum/core/types"
  11. "github.com/ethereum/go-ethereum/crypto"
  12. "github.com/ethereum/go-ethereum/eth/downloader"
  13. "github.com/ethereum/go-ethereum/ethdb"
  14. "github.com/ethereum/go-ethereum/p2p"
  15. "github.com/ethereum/go-ethereum/params"
  16. )
  17. // Tests that hashes can be retrieved from a remote chain by hashes in reverse
  18. // order.
  19. func TestGetBlockHashes61(t *testing.T) { testGetBlockHashes(t, 61) }
  20. func testGetBlockHashes(t *testing.T, protocol int) {
  21. pm := newTestProtocolManager(downloader.MaxHashFetch+15, nil, nil)
  22. peer, _ := newTestPeer("peer", protocol, pm, true)
  23. defer peer.close()
  24. // Create a batch of tests for various scenarios
  25. limit := downloader.MaxHashFetch
  26. tests := []struct {
  27. origin common.Hash
  28. number int
  29. result int
  30. }{
  31. {common.Hash{}, 1, 0}, // Make sure non existent hashes don't return results
  32. {pm.blockchain.Genesis().Hash(), 1, 0}, // There are no hashes to retrieve up from the genesis
  33. {pm.blockchain.GetBlockByNumber(5).Hash(), 5, 5}, // All the hashes including the genesis requested
  34. {pm.blockchain.GetBlockByNumber(5).Hash(), 10, 5}, // More hashes than available till the genesis requested
  35. {pm.blockchain.GetBlockByNumber(100).Hash(), 10, 10}, // All hashes available from the middle of the chain
  36. {pm.blockchain.CurrentBlock().Hash(), 10, 10}, // All hashes available from the head of the chain
  37. {pm.blockchain.CurrentBlock().Hash(), limit, limit}, // Request the maximum allowed hash count
  38. {pm.blockchain.CurrentBlock().Hash(), limit + 1, limit}, // Request more than the maximum allowed hash count
  39. }
  40. // Run each of the tests and verify the results against the chain
  41. for i, tt := range tests {
  42. // Assemble the hash response we would like to receive
  43. resp := make([]common.Hash, tt.result)
  44. if len(resp) > 0 {
  45. from := pm.blockchain.GetBlock(tt.origin).NumberU64() - 1
  46. for j := 0; j < len(resp); j++ {
  47. resp[j] = pm.blockchain.GetBlockByNumber(uint64(int(from) - j)).Hash()
  48. }
  49. }
  50. // Send the hash request and verify the response
  51. p2p.Send(peer.app, 0x03, getBlockHashesData{tt.origin, uint64(tt.number)})
  52. if err := p2p.ExpectMsg(peer.app, 0x04, resp); err != nil {
  53. t.Errorf("test %d: block hashes mismatch: %v", i, err)
  54. }
  55. }
  56. }
  57. // Tests that hashes can be retrieved from a remote chain by numbers in forward
  58. // order.
  59. func TestGetBlockHashesFromNumber61(t *testing.T) { testGetBlockHashesFromNumber(t, 61) }
  60. func testGetBlockHashesFromNumber(t *testing.T, protocol int) {
  61. pm := newTestProtocolManager(downloader.MaxHashFetch+15, nil, nil)
  62. peer, _ := newTestPeer("peer", protocol, pm, true)
  63. defer peer.close()
  64. // Create a batch of tests for various scenarios
  65. limit := downloader.MaxHashFetch
  66. tests := []struct {
  67. origin uint64
  68. number int
  69. result int
  70. }{
  71. {pm.blockchain.CurrentBlock().NumberU64() + 1, 1, 0}, // Out of bounds requests should return empty
  72. {pm.blockchain.CurrentBlock().NumberU64(), 1, 1}, // Make sure the head hash can be retrieved
  73. {pm.blockchain.CurrentBlock().NumberU64() - 4, 5, 5}, // All hashes, including the head hash requested
  74. {pm.blockchain.CurrentBlock().NumberU64() - 4, 10, 5}, // More hashes requested than available till the head
  75. {pm.blockchain.CurrentBlock().NumberU64() - 100, 10, 10}, // All hashes available from the middle of the chain
  76. {0, 10, 10}, // All hashes available from the root of the chain
  77. {0, limit, limit}, // Request the maximum allowed hash count
  78. {0, limit + 1, limit}, // Request more than the maximum allowed hash count
  79. {0, 1, 1}, // Make sure the genesis hash can be retrieved
  80. }
  81. // Run each of the tests and verify the results against the chain
  82. for i, tt := range tests {
  83. // Assemble the hash response we would like to receive
  84. resp := make([]common.Hash, tt.result)
  85. for j := 0; j < len(resp); j++ {
  86. resp[j] = pm.blockchain.GetBlockByNumber(tt.origin + uint64(j)).Hash()
  87. }
  88. // Send the hash request and verify the response
  89. p2p.Send(peer.app, 0x08, getBlockHashesFromNumberData{tt.origin, uint64(tt.number)})
  90. if err := p2p.ExpectMsg(peer.app, 0x04, resp); err != nil {
  91. t.Errorf("test %d: block hashes mismatch: %v", i, err)
  92. }
  93. }
  94. }
  95. // Tests that blocks can be retrieved from a remote chain based on their hashes.
  96. func TestGetBlocks61(t *testing.T) { testGetBlocks(t, 61) }
  97. func testGetBlocks(t *testing.T, protocol int) {
  98. pm := newTestProtocolManager(downloader.MaxHashFetch+15, nil, nil)
  99. peer, _ := newTestPeer("peer", protocol, pm, true)
  100. defer peer.close()
  101. // Create a batch of tests for various scenarios
  102. limit := downloader.MaxBlockFetch
  103. tests := []struct {
  104. random int // Number of blocks to fetch randomly from the chain
  105. explicit []common.Hash // Explicitly requested blocks
  106. available []bool // Availability of explicitly requested blocks
  107. expected int // Total number of existing blocks to expect
  108. }{
  109. {1, nil, nil, 1}, // A single random block should be retrievable
  110. {10, nil, nil, 10}, // Multiple random blocks should be retrievable
  111. {limit, nil, nil, limit}, // The maximum possible blocks should be retrievable
  112. {limit + 1, nil, nil, limit}, // No more than the possible block count should be returned
  113. {0, []common.Hash{pm.blockchain.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable
  114. {0, []common.Hash{pm.blockchain.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable
  115. {0, []common.Hash{common.Hash{}}, []bool{false}, 0}, // A non existent block should not be returned
  116. // Existing and non-existing blocks interleaved should not cause problems
  117. {0, []common.Hash{
  118. common.Hash{},
  119. pm.blockchain.GetBlockByNumber(1).Hash(),
  120. common.Hash{},
  121. pm.blockchain.GetBlockByNumber(10).Hash(),
  122. common.Hash{},
  123. pm.blockchain.GetBlockByNumber(100).Hash(),
  124. common.Hash{},
  125. }, []bool{false, true, false, true, false, true, false}, 3},
  126. }
  127. // Run each of the tests and verify the results against the chain
  128. for i, tt := range tests {
  129. // Collect the hashes to request, and the response to expect
  130. hashes, seen := []common.Hash{}, make(map[int64]bool)
  131. blocks := []*types.Block{}
  132. for j := 0; j < tt.random; j++ {
  133. for {
  134. num := rand.Int63n(int64(pm.blockchain.CurrentBlock().NumberU64()))
  135. if !seen[num] {
  136. seen[num] = true
  137. block := pm.blockchain.GetBlockByNumber(uint64(num))
  138. hashes = append(hashes, block.Hash())
  139. if len(blocks) < tt.expected {
  140. blocks = append(blocks, block)
  141. }
  142. break
  143. }
  144. }
  145. }
  146. for j, hash := range tt.explicit {
  147. hashes = append(hashes, hash)
  148. if tt.available[j] && len(blocks) < tt.expected {
  149. blocks = append(blocks, pm.blockchain.GetBlock(hash))
  150. }
  151. }
  152. // Send the hash request and verify the response
  153. p2p.Send(peer.app, 0x05, hashes)
  154. if err := p2p.ExpectMsg(peer.app, 0x06, blocks); err != nil {
  155. t.Errorf("test %d: blocks mismatch: %v", i, err)
  156. }
  157. }
  158. }
  159. // Tests that block headers can be retrieved from a remote chain based on user queries.
  160. func TestGetBlockHeaders62(t *testing.T) { testGetBlockHeaders(t, 62) }
  161. func TestGetBlockHeaders63(t *testing.T) { testGetBlockHeaders(t, 63) }
  162. func TestGetBlockHeaders64(t *testing.T) { testGetBlockHeaders(t, 64) }
  163. func testGetBlockHeaders(t *testing.T, protocol int) {
  164. pm := newTestProtocolManager(downloader.MaxHashFetch+15, nil, nil)
  165. peer, _ := newTestPeer("peer", protocol, pm, true)
  166. defer peer.close()
  167. // Create a "random" unknown hash for testing
  168. var unknown common.Hash
  169. for i, _ := range unknown {
  170. unknown[i] = byte(i)
  171. }
  172. // Create a batch of tests for various scenarios
  173. limit := uint64(downloader.MaxHeaderFetch)
  174. tests := []struct {
  175. query *getBlockHeadersData // The query to execute for header retrieval
  176. expect []common.Hash // The hashes of the block whose headers are expected
  177. }{
  178. // A single random block should be retrievable by hash and number too
  179. {
  180. &getBlockHeadersData{Origin: hashOrNumber{Hash: pm.blockchain.GetBlockByNumber(limit / 2).Hash()}, Amount: 1},
  181. []common.Hash{pm.blockchain.GetBlockByNumber(limit / 2).Hash()},
  182. }, {
  183. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 1},
  184. []common.Hash{pm.blockchain.GetBlockByNumber(limit / 2).Hash()},
  185. },
  186. // Multiple headers should be retrievable in both directions
  187. {
  188. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3},
  189. []common.Hash{
  190. pm.blockchain.GetBlockByNumber(limit / 2).Hash(),
  191. pm.blockchain.GetBlockByNumber(limit/2 + 1).Hash(),
  192. pm.blockchain.GetBlockByNumber(limit/2 + 2).Hash(),
  193. },
  194. }, {
  195. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Amount: 3, Reverse: true},
  196. []common.Hash{
  197. pm.blockchain.GetBlockByNumber(limit / 2).Hash(),
  198. pm.blockchain.GetBlockByNumber(limit/2 - 1).Hash(),
  199. pm.blockchain.GetBlockByNumber(limit/2 - 2).Hash(),
  200. },
  201. },
  202. // Multiple headers with skip lists should be retrievable
  203. {
  204. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3},
  205. []common.Hash{
  206. pm.blockchain.GetBlockByNumber(limit / 2).Hash(),
  207. pm.blockchain.GetBlockByNumber(limit/2 + 4).Hash(),
  208. pm.blockchain.GetBlockByNumber(limit/2 + 8).Hash(),
  209. },
  210. }, {
  211. &getBlockHeadersData{Origin: hashOrNumber{Number: limit / 2}, Skip: 3, Amount: 3, Reverse: true},
  212. []common.Hash{
  213. pm.blockchain.GetBlockByNumber(limit / 2).Hash(),
  214. pm.blockchain.GetBlockByNumber(limit/2 - 4).Hash(),
  215. pm.blockchain.GetBlockByNumber(limit/2 - 8).Hash(),
  216. },
  217. },
  218. // The chain endpoints should be retrievable
  219. {
  220. &getBlockHeadersData{Origin: hashOrNumber{Number: 0}, Amount: 1},
  221. []common.Hash{pm.blockchain.GetBlockByNumber(0).Hash()},
  222. }, {
  223. &getBlockHeadersData{Origin: hashOrNumber{Number: pm.blockchain.CurrentBlock().NumberU64()}, Amount: 1},
  224. []common.Hash{pm.blockchain.CurrentBlock().Hash()},
  225. },
  226. // Ensure protocol limits are honored
  227. {
  228. &getBlockHeadersData{Origin: hashOrNumber{Number: pm.blockchain.CurrentBlock().NumberU64() - 1}, Amount: limit + 10, Reverse: true},
  229. pm.blockchain.GetBlockHashesFromHash(pm.blockchain.CurrentBlock().Hash(), limit),
  230. },
  231. // Check that requesting more than available is handled gracefully
  232. {
  233. &getBlockHeadersData{Origin: hashOrNumber{Number: pm.blockchain.CurrentBlock().NumberU64() - 4}, Skip: 3, Amount: 3},
  234. []common.Hash{
  235. pm.blockchain.GetBlockByNumber(pm.blockchain.CurrentBlock().NumberU64() - 4).Hash(),
  236. pm.blockchain.GetBlockByNumber(pm.blockchain.CurrentBlock().NumberU64()).Hash(),
  237. },
  238. }, {
  239. &getBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 3, Amount: 3, Reverse: true},
  240. []common.Hash{
  241. pm.blockchain.GetBlockByNumber(4).Hash(),
  242. pm.blockchain.GetBlockByNumber(0).Hash(),
  243. },
  244. },
  245. // Check that requesting more than available is handled gracefully, even if mid skip
  246. {
  247. &getBlockHeadersData{Origin: hashOrNumber{Number: pm.blockchain.CurrentBlock().NumberU64() - 4}, Skip: 2, Amount: 3},
  248. []common.Hash{
  249. pm.blockchain.GetBlockByNumber(pm.blockchain.CurrentBlock().NumberU64() - 4).Hash(),
  250. pm.blockchain.GetBlockByNumber(pm.blockchain.CurrentBlock().NumberU64() - 1).Hash(),
  251. },
  252. }, {
  253. &getBlockHeadersData{Origin: hashOrNumber{Number: 4}, Skip: 2, Amount: 3, Reverse: true},
  254. []common.Hash{
  255. pm.blockchain.GetBlockByNumber(4).Hash(),
  256. pm.blockchain.GetBlockByNumber(1).Hash(),
  257. },
  258. },
  259. // Check that non existing headers aren't returned
  260. {
  261. &getBlockHeadersData{Origin: hashOrNumber{Hash: unknown}, Amount: 1},
  262. []common.Hash{},
  263. }, {
  264. &getBlockHeadersData{Origin: hashOrNumber{Number: pm.blockchain.CurrentBlock().NumberU64() + 1}, Amount: 1},
  265. []common.Hash{},
  266. },
  267. }
  268. // Run each of the tests and verify the results against the chain
  269. for i, tt := range tests {
  270. // Collect the headers to expect in the response
  271. headers := []*types.Header{}
  272. for _, hash := range tt.expect {
  273. headers = append(headers, pm.blockchain.GetBlock(hash).Header())
  274. }
  275. // Send the hash request and verify the response
  276. p2p.Send(peer.app, 0x03, tt.query)
  277. if err := p2p.ExpectMsg(peer.app, 0x04, headers); err != nil {
  278. t.Errorf("test %d: headers mismatch: %v", i, err)
  279. }
  280. }
  281. }
  282. // Tests that block contents can be retrieved from a remote chain based on their hashes.
  283. func TestGetBlockBodies62(t *testing.T) { testGetBlockBodies(t, 62) }
  284. func TestGetBlockBodies63(t *testing.T) { testGetBlockBodies(t, 63) }
  285. func TestGetBlockBodies64(t *testing.T) { testGetBlockBodies(t, 64) }
  286. func testGetBlockBodies(t *testing.T, protocol int) {
  287. pm := newTestProtocolManager(downloader.MaxBlockFetch+15, nil, nil)
  288. peer, _ := newTestPeer("peer", protocol, pm, true)
  289. defer peer.close()
  290. // Create a batch of tests for various scenarios
  291. limit := downloader.MaxBlockFetch
  292. tests := []struct {
  293. random int // Number of blocks to fetch randomly from the chain
  294. explicit []common.Hash // Explicitly requested blocks
  295. available []bool // Availability of explicitly requested blocks
  296. expected int // Total number of existing blocks to expect
  297. }{
  298. {1, nil, nil, 1}, // A single random block should be retrievable
  299. {10, nil, nil, 10}, // Multiple random blocks should be retrievable
  300. {limit, nil, nil, limit}, // The maximum possible blocks should be retrievable
  301. {limit + 1, nil, nil, limit}, // No more than the possible block count should be returned
  302. {0, []common.Hash{pm.blockchain.Genesis().Hash()}, []bool{true}, 1}, // The genesis block should be retrievable
  303. {0, []common.Hash{pm.blockchain.CurrentBlock().Hash()}, []bool{true}, 1}, // The chains head block should be retrievable
  304. {0, []common.Hash{common.Hash{}}, []bool{false}, 0}, // A non existent block should not be returned
  305. // Existing and non-existing blocks interleaved should not cause problems
  306. {0, []common.Hash{
  307. common.Hash{},
  308. pm.blockchain.GetBlockByNumber(1).Hash(),
  309. common.Hash{},
  310. pm.blockchain.GetBlockByNumber(10).Hash(),
  311. common.Hash{},
  312. pm.blockchain.GetBlockByNumber(100).Hash(),
  313. common.Hash{},
  314. }, []bool{false, true, false, true, false, true, false}, 3},
  315. }
  316. // Run each of the tests and verify the results against the chain
  317. for i, tt := range tests {
  318. // Collect the hashes to request, and the response to expect
  319. hashes, seen := []common.Hash{}, make(map[int64]bool)
  320. bodies := []*blockBody{}
  321. for j := 0; j < tt.random; j++ {
  322. for {
  323. num := rand.Int63n(int64(pm.blockchain.CurrentBlock().NumberU64()))
  324. if !seen[num] {
  325. seen[num] = true
  326. block := pm.blockchain.GetBlockByNumber(uint64(num))
  327. hashes = append(hashes, block.Hash())
  328. if len(bodies) < tt.expected {
  329. bodies = append(bodies, &blockBody{Transactions: block.Transactions(), Uncles: block.Uncles()})
  330. }
  331. break
  332. }
  333. }
  334. }
  335. for j, hash := range tt.explicit {
  336. hashes = append(hashes, hash)
  337. if tt.available[j] && len(bodies) < tt.expected {
  338. block := pm.blockchain.GetBlock(hash)
  339. bodies = append(bodies, &blockBody{Transactions: block.Transactions(), Uncles: block.Uncles()})
  340. }
  341. }
  342. // Send the hash request and verify the response
  343. p2p.Send(peer.app, 0x05, hashes)
  344. if err := p2p.ExpectMsg(peer.app, 0x06, bodies); err != nil {
  345. t.Errorf("test %d: bodies mismatch: %v", i, err)
  346. }
  347. }
  348. }
  349. // Tests that the node state database can be retrieved based on hashes.
  350. func TestGetNodeData63(t *testing.T) { testGetNodeData(t, 63) }
  351. func TestGetNodeData64(t *testing.T) { testGetNodeData(t, 64) }
  352. func testGetNodeData(t *testing.T, protocol int) {
  353. // Define three accounts to simulate transactions with
  354. acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
  355. acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
  356. acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey)
  357. acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey)
  358. // Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_makerts_test)
  359. generator := func(i int, block *core.BlockGen) {
  360. switch i {
  361. case 0:
  362. // In block 1, the test bank sends account #1 some ether.
  363. tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey)
  364. block.AddTx(tx)
  365. case 1:
  366. // In block 2, the test bank sends some more ether to account #1.
  367. // acc1Addr passes it on to account #2.
  368. tx1, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(testBankKey)
  369. tx2, _ := types.NewTransaction(block.TxNonce(acc1Addr), acc2Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(acc1Key)
  370. block.AddTx(tx1)
  371. block.AddTx(tx2)
  372. case 2:
  373. // Block 3 is empty but was mined by account #2.
  374. block.SetCoinbase(acc2Addr)
  375. block.SetExtra([]byte("yeehaw"))
  376. case 3:
  377. // Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data).
  378. b2 := block.PrevBlock(1).Header()
  379. b2.Extra = []byte("foo")
  380. block.AddUncle(b2)
  381. b3 := block.PrevBlock(2).Header()
  382. b3.Extra = []byte("foo")
  383. block.AddUncle(b3)
  384. }
  385. }
  386. // Assemble the test environment
  387. pm := newTestProtocolManager(4, generator, nil)
  388. peer, _ := newTestPeer("peer", protocol, pm, true)
  389. defer peer.close()
  390. // Fetch for now the entire chain db
  391. hashes := []common.Hash{}
  392. for _, key := range pm.chaindb.(*ethdb.MemDatabase).Keys() {
  393. hashes = append(hashes, common.BytesToHash(key))
  394. }
  395. p2p.Send(peer.app, 0x0d, hashes)
  396. msg, err := peer.app.ReadMsg()
  397. if err != nil {
  398. t.Fatalf("failed to read node data response: %v", err)
  399. }
  400. if msg.Code != 0x0e {
  401. t.Fatalf("response packet code mismatch: have %x, want %x", msg.Code, 0x0c)
  402. }
  403. var data [][]byte
  404. if err := msg.Decode(&data); err != nil {
  405. t.Fatalf("failed to decode response node data: %v", err)
  406. }
  407. // Verify that all hashes correspond to the requested data, and reconstruct a state tree
  408. for i, want := range hashes {
  409. if hash := crypto.Sha3Hash(data[i]); hash != want {
  410. fmt.Errorf("data hash mismatch: have %x, want %x", hash, want)
  411. }
  412. }
  413. statedb, _ := ethdb.NewMemDatabase()
  414. for i := 0; i < len(data); i++ {
  415. statedb.Put(hashes[i].Bytes(), data[i])
  416. }
  417. accounts := []common.Address{testBankAddress, acc1Addr, acc2Addr}
  418. for i := uint64(0); i <= pm.blockchain.CurrentBlock().NumberU64(); i++ {
  419. trie, _ := state.New(pm.blockchain.GetBlockByNumber(i).Root(), statedb)
  420. for j, acc := range accounts {
  421. state, _ := pm.blockchain.State()
  422. bw := state.GetBalance(acc)
  423. bh := trie.GetBalance(acc)
  424. if (bw != nil && bh == nil) || (bw == nil && bh != nil) {
  425. t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
  426. }
  427. if bw != nil && bh != nil && bw.Cmp(bw) != 0 {
  428. t.Errorf("test %d, account %d: balance mismatch: have %v, want %v", i, j, bh, bw)
  429. }
  430. }
  431. }
  432. }
  433. // Tests that the transaction receipts can be retrieved based on hashes.
  434. func TestGetReceipt63(t *testing.T) { testGetReceipt(t, 63) }
  435. func TestGetReceipt64(t *testing.T) { testGetReceipt(t, 64) }
  436. func testGetReceipt(t *testing.T, protocol int) {
  437. // Define three accounts to simulate transactions with
  438. acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
  439. acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
  440. acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey)
  441. acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey)
  442. // Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_makerts_test)
  443. generator := func(i int, block *core.BlockGen) {
  444. switch i {
  445. case 0:
  446. // In block 1, the test bank sends account #1 some ether.
  447. tx, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil).SignECDSA(testBankKey)
  448. block.AddTx(tx)
  449. case 1:
  450. // In block 2, the test bank sends some more ether to account #1.
  451. // acc1Addr passes it on to account #2.
  452. tx1, _ := types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(testBankKey)
  453. tx2, _ := types.NewTransaction(block.TxNonce(acc1Addr), acc2Addr, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(acc1Key)
  454. block.AddTx(tx1)
  455. block.AddTx(tx2)
  456. case 2:
  457. // Block 3 is empty but was mined by account #2.
  458. block.SetCoinbase(acc2Addr)
  459. block.SetExtra([]byte("yeehaw"))
  460. case 3:
  461. // Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data).
  462. b2 := block.PrevBlock(1).Header()
  463. b2.Extra = []byte("foo")
  464. block.AddUncle(b2)
  465. b3 := block.PrevBlock(2).Header()
  466. b3.Extra = []byte("foo")
  467. block.AddUncle(b3)
  468. }
  469. }
  470. // Assemble the test environment
  471. pm := newTestProtocolManager(4, generator, nil)
  472. peer, _ := newTestPeer("peer", protocol, pm, true)
  473. defer peer.close()
  474. // Collect the hashes to request, and the response to expect
  475. hashes := []common.Hash{}
  476. for i := uint64(0); i <= pm.blockchain.CurrentBlock().NumberU64(); i++ {
  477. for _, tx := range pm.blockchain.GetBlockByNumber(i).Transactions() {
  478. hashes = append(hashes, tx.Hash())
  479. }
  480. }
  481. receipts := make([]*types.Receipt, len(hashes))
  482. for i, hash := range hashes {
  483. receipts[i] = core.GetReceipt(pm.chaindb, hash)
  484. }
  485. // Send the hash request and verify the response
  486. p2p.Send(peer.app, 0x0f, hashes)
  487. if err := p2p.ExpectMsg(peer.app, 0x10, receipts); err != nil {
  488. t.Errorf("receipts mismatch: %v", err)
  489. }
  490. }