eth66_suite.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. // Copyright 2021 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 ethtest
  17. import (
  18. "time"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  23. "github.com/ethereum/go-ethereum/internal/utesting"
  24. "github.com/ethereum/go-ethereum/p2p"
  25. )
  26. // Is_66 checks if the node supports the eth66 protocol version,
  27. // and if not, exists the test suite
  28. func (s *Suite) Is_66(t *utesting.T) {
  29. conn := s.dial66(t)
  30. conn.handshake(t)
  31. if conn.negotiatedProtoVersion < 66 {
  32. t.Fail()
  33. }
  34. }
  35. // TestStatus_66 attempts to connect to the given node and exchange
  36. // a status message with it on the eth66 protocol, and then check to
  37. // make sure the chain head is correct.
  38. func (s *Suite) TestStatus_66(t *utesting.T) {
  39. conn := s.dial66(t)
  40. defer conn.Close()
  41. // get protoHandshake
  42. conn.handshake(t)
  43. // get status
  44. switch msg := conn.statusExchange66(t, s.chain).(type) {
  45. case *Status:
  46. status := *msg
  47. if status.ProtocolVersion != uint32(66) {
  48. t.Fatalf("mismatch in version: wanted 66, got %d", status.ProtocolVersion)
  49. }
  50. t.Logf("got status message: %s", pretty.Sdump(msg))
  51. default:
  52. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  53. }
  54. }
  55. // TestGetBlockHeaders_66 tests whether the given node can respond to
  56. // an eth66 `GetBlockHeaders` request and that the response is accurate.
  57. func (s *Suite) TestGetBlockHeaders_66(t *utesting.T) {
  58. conn := s.setupConnection66(t)
  59. defer conn.Close()
  60. // get block headers
  61. req := &eth.GetBlockHeadersPacket66{
  62. RequestId: 3,
  63. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  64. Origin: eth.HashOrNumber{
  65. Hash: s.chain.blocks[1].Hash(),
  66. },
  67. Amount: 2,
  68. Skip: 1,
  69. Reverse: false,
  70. },
  71. }
  72. // write message
  73. headers, err := s.getBlockHeaders66(conn, req, req.RequestId)
  74. if err != nil {
  75. t.Fatalf("could not get block headers: %v", err)
  76. }
  77. // check for correct headers
  78. if !headersMatch(t, s.chain, headers) {
  79. t.Fatal("received wrong header(s)")
  80. }
  81. }
  82. // TestSimultaneousRequests_66 sends two simultaneous `GetBlockHeader` requests
  83. // with different request IDs and checks to make sure the node responds with the correct
  84. // headers per request.
  85. func (s *Suite) TestSimultaneousRequests_66(t *utesting.T) {
  86. // create two connections
  87. conn := s.setupConnection66(t)
  88. defer conn.Close()
  89. // create two requests
  90. req1 := &eth.GetBlockHeadersPacket66{
  91. RequestId: 111,
  92. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  93. Origin: eth.HashOrNumber{
  94. Hash: s.chain.blocks[1].Hash(),
  95. },
  96. Amount: 2,
  97. Skip: 1,
  98. Reverse: false,
  99. },
  100. }
  101. req2 := &eth.GetBlockHeadersPacket66{
  102. RequestId: 222,
  103. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  104. Origin: eth.HashOrNumber{
  105. Hash: s.chain.blocks[1].Hash(),
  106. },
  107. Amount: 4,
  108. Skip: 1,
  109. Reverse: false,
  110. },
  111. }
  112. // write first request
  113. if err := conn.write66(req1, GetBlockHeaders{}.Code()); err != nil {
  114. t.Fatalf("failed to write to connection: %v", err)
  115. }
  116. // write second request
  117. if err := conn.write66(req2, GetBlockHeaders{}.Code()); err != nil {
  118. t.Fatalf("failed to write to connection: %v", err)
  119. }
  120. // wait for responses
  121. headers1, err := s.waitForBlockHeadersResponse66(conn, req1.RequestId)
  122. if err != nil {
  123. t.Fatalf("error while waiting for block headers: %v", err)
  124. }
  125. headers2, err := s.waitForBlockHeadersResponse66(conn, req2.RequestId)
  126. if err != nil {
  127. t.Fatalf("error while waiting for block headers: %v", err)
  128. }
  129. // check headers of both responses
  130. if !headersMatch(t, s.chain, headers1) {
  131. t.Fatalf("wrong header(s) in response to req1: got %v", headers1)
  132. }
  133. if !headersMatch(t, s.chain, headers2) {
  134. t.Fatalf("wrong header(s) in response to req2: got %v", headers2)
  135. }
  136. }
  137. // TestBroadcast_66 tests whether a block announcement is correctly
  138. // propagated to the given node's peer(s) on the eth66 protocol.
  139. func (s *Suite) TestBroadcast_66(t *utesting.T) {
  140. s.sendNextBlock66(t)
  141. }
  142. // TestGetBlockBodies_66 tests whether the given node can respond to
  143. // a `GetBlockBodies` request and that the response is accurate over
  144. // the eth66 protocol.
  145. func (s *Suite) TestGetBlockBodies_66(t *utesting.T) {
  146. conn := s.setupConnection66(t)
  147. defer conn.Close()
  148. // create block bodies request
  149. id := uint64(55)
  150. req := &eth.GetBlockBodiesPacket66{
  151. RequestId: id,
  152. GetBlockBodiesPacket: eth.GetBlockBodiesPacket{
  153. s.chain.blocks[54].Hash(),
  154. s.chain.blocks[75].Hash(),
  155. },
  156. }
  157. if err := conn.write66(req, GetBlockBodies{}.Code()); err != nil {
  158. t.Fatalf("could not write to connection: %v", err)
  159. }
  160. reqID, msg := conn.readAndServe66(s.chain, timeout)
  161. switch msg := msg.(type) {
  162. case BlockBodies:
  163. if reqID != req.RequestId {
  164. t.Fatalf("request ID mismatch: wanted %d, got %d", req.RequestId, reqID)
  165. }
  166. t.Logf("received %d block bodies", len(msg))
  167. default:
  168. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  169. }
  170. }
  171. // TestLargeAnnounce_66 tests the announcement mechanism with a large block.
  172. func (s *Suite) TestLargeAnnounce_66(t *utesting.T) {
  173. nextBlock := len(s.chain.blocks)
  174. blocks := []*NewBlock{
  175. {
  176. Block: largeBlock(),
  177. TD: s.fullChain.TD(nextBlock + 1),
  178. },
  179. {
  180. Block: s.fullChain.blocks[nextBlock],
  181. TD: largeNumber(2),
  182. },
  183. {
  184. Block: largeBlock(),
  185. TD: largeNumber(2),
  186. },
  187. {
  188. Block: s.fullChain.blocks[nextBlock],
  189. TD: s.fullChain.TD(nextBlock + 1),
  190. },
  191. }
  192. for i, blockAnnouncement := range blocks[0:3] {
  193. t.Logf("Testing malicious announcement: %v\n", i)
  194. sendConn := s.setupConnection66(t)
  195. if err := sendConn.Write(blockAnnouncement); err != nil {
  196. t.Fatalf("could not write to connection: %v", err)
  197. }
  198. // Invalid announcement, check that peer disconnected
  199. switch msg := sendConn.ReadAndServe(s.chain, time.Second*8).(type) {
  200. case *Disconnect:
  201. case *Error:
  202. break
  203. default:
  204. t.Fatalf("unexpected: %s wanted disconnect", pretty.Sdump(msg))
  205. }
  206. sendConn.Close()
  207. }
  208. // Test the last block as a valid block
  209. s.sendNextBlock66(t)
  210. }
  211. func (s *Suite) TestOldAnnounce_66(t *utesting.T) {
  212. sendConn, recvConn := s.setupConnection66(t), s.setupConnection66(t)
  213. defer sendConn.Close()
  214. defer recvConn.Close()
  215. s.oldAnnounce(t, sendConn, recvConn)
  216. }
  217. // TestMaliciousHandshake_66 tries to send malicious data during the handshake.
  218. func (s *Suite) TestMaliciousHandshake_66(t *utesting.T) {
  219. conn := s.dial66(t)
  220. defer conn.Close()
  221. // write hello to client
  222. pub0 := crypto.FromECDSAPub(&conn.ourKey.PublicKey)[1:]
  223. handshakes := []*Hello{
  224. {
  225. Version: 5,
  226. Caps: []p2p.Cap{
  227. {Name: largeString(2), Version: 66},
  228. },
  229. ID: pub0,
  230. },
  231. {
  232. Version: 5,
  233. Caps: []p2p.Cap{
  234. {Name: "eth", Version: 64},
  235. {Name: "eth", Version: 65},
  236. {Name: "eth", Version: 66},
  237. },
  238. ID: append(pub0, byte(0)),
  239. },
  240. {
  241. Version: 5,
  242. Caps: []p2p.Cap{
  243. {Name: "eth", Version: 64},
  244. {Name: "eth", Version: 65},
  245. {Name: "eth", Version: 66},
  246. },
  247. ID: append(pub0, pub0...),
  248. },
  249. {
  250. Version: 5,
  251. Caps: []p2p.Cap{
  252. {Name: "eth", Version: 64},
  253. {Name: "eth", Version: 65},
  254. {Name: "eth", Version: 66},
  255. },
  256. ID: largeBuffer(2),
  257. },
  258. {
  259. Version: 5,
  260. Caps: []p2p.Cap{
  261. {Name: largeString(2), Version: 66},
  262. },
  263. ID: largeBuffer(2),
  264. },
  265. }
  266. for i, handshake := range handshakes {
  267. t.Logf("Testing malicious handshake %v\n", i)
  268. // Init the handshake
  269. if err := conn.Write(handshake); err != nil {
  270. t.Fatalf("could not write to connection: %v", err)
  271. }
  272. // check that the peer disconnected
  273. timeout := 20 * time.Second
  274. // Discard one hello
  275. for i := 0; i < 2; i++ {
  276. switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
  277. case *Disconnect:
  278. case *Error:
  279. case *Hello:
  280. // Hello's are sent concurrently, so ignore them
  281. continue
  282. default:
  283. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  284. }
  285. }
  286. // Dial for the next round
  287. conn = s.dial66(t)
  288. }
  289. }
  290. // TestMaliciousStatus_66 sends a status package with a large total difficulty.
  291. func (s *Suite) TestMaliciousStatus_66(t *utesting.T) {
  292. conn := s.dial66(t)
  293. defer conn.Close()
  294. // get protoHandshake
  295. conn.handshake(t)
  296. status := &Status{
  297. ProtocolVersion: uint32(66),
  298. NetworkID: s.chain.chainConfig.ChainID.Uint64(),
  299. TD: largeNumber(2),
  300. Head: s.chain.blocks[s.chain.Len()-1].Hash(),
  301. Genesis: s.chain.blocks[0].Hash(),
  302. ForkID: s.chain.ForkID(),
  303. }
  304. // get status
  305. switch msg := conn.statusExchange(t, s.chain, status).(type) {
  306. case *Status:
  307. t.Logf("%+v\n", msg)
  308. default:
  309. t.Fatalf("expected status, got: %#v ", msg)
  310. }
  311. // wait for disconnect
  312. switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
  313. case *Disconnect:
  314. case *Error:
  315. return
  316. default:
  317. t.Fatalf("expected disconnect, got: %s", pretty.Sdump(msg))
  318. }
  319. }
  320. func (s *Suite) TestTransaction_66(t *utesting.T) {
  321. tests := []*types.Transaction{
  322. getNextTxFromChain(t, s),
  323. unknownTx(t, s),
  324. }
  325. for i, tx := range tests {
  326. t.Logf("Testing tx propagation: %v\n", i)
  327. sendSuccessfulTx66(t, s, tx)
  328. }
  329. }
  330. func (s *Suite) TestMaliciousTx_66(t *utesting.T) {
  331. badTxs := []*types.Transaction{
  332. getOldTxFromChain(t, s),
  333. invalidNonceTx(t, s),
  334. hugeAmount(t, s),
  335. hugeGasPrice(t, s),
  336. hugeData(t, s),
  337. }
  338. sendConn := s.setupConnection66(t)
  339. defer sendConn.Close()
  340. // set up receiving connection before sending txs to make sure
  341. // no announcements are missed
  342. recvConn := s.setupConnection66(t)
  343. defer recvConn.Close()
  344. for i, tx := range badTxs {
  345. t.Logf("Testing malicious tx propagation: %v\n", i)
  346. if err := sendConn.Write(&Transactions{tx}); err != nil {
  347. t.Fatalf("could not write to connection: %v", err)
  348. }
  349. }
  350. // check to make sure bad txs aren't propagated
  351. waitForTxPropagation(t, s, badTxs, recvConn)
  352. }
  353. // TestZeroRequestID_66 checks that a request ID of zero is still handled
  354. // by the node.
  355. func (s *Suite) TestZeroRequestID_66(t *utesting.T) {
  356. conn := s.setupConnection66(t)
  357. defer conn.Close()
  358. req := &eth.GetBlockHeadersPacket66{
  359. RequestId: 0,
  360. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  361. Origin: eth.HashOrNumber{
  362. Number: 0,
  363. },
  364. Amount: 2,
  365. },
  366. }
  367. headers, err := s.getBlockHeaders66(conn, req, req.RequestId)
  368. if err != nil {
  369. t.Fatalf("could not get block headers: %v", err)
  370. }
  371. if !headersMatch(t, s.chain, headers) {
  372. t.Fatal("received wrong header(s)")
  373. }
  374. }
  375. // TestSameRequestID_66 sends two requests with the same request ID
  376. // concurrently to a single node.
  377. func (s *Suite) TestSameRequestID_66(t *utesting.T) {
  378. conn := s.setupConnection66(t)
  379. // create two requests with the same request ID
  380. reqID := uint64(1234)
  381. request1 := &eth.GetBlockHeadersPacket66{
  382. RequestId: reqID,
  383. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  384. Origin: eth.HashOrNumber{
  385. Number: 1,
  386. },
  387. Amount: 2,
  388. },
  389. }
  390. request2 := &eth.GetBlockHeadersPacket66{
  391. RequestId: reqID,
  392. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  393. Origin: eth.HashOrNumber{
  394. Number: 33,
  395. },
  396. Amount: 2,
  397. },
  398. }
  399. // write the first request
  400. err := conn.write66(request1, GetBlockHeaders{}.Code())
  401. if err != nil {
  402. t.Fatalf("could not write to connection: %v", err)
  403. }
  404. // perform second request
  405. headers2, err := s.getBlockHeaders66(conn, request2, reqID)
  406. if err != nil {
  407. t.Fatalf("could not get block headers: %v", err)
  408. return
  409. }
  410. // wait for response to first request
  411. headers1, err := s.waitForBlockHeadersResponse66(conn, reqID)
  412. if err != nil {
  413. t.Fatalf("could not get BlockHeaders response: %v", err)
  414. }
  415. // check if headers match
  416. if !headersMatch(t, s.chain, headers1) || !headersMatch(t, s.chain, headers2) {
  417. t.Fatal("received wrong header(s)")
  418. }
  419. }
  420. // TestLargeTxRequest_66 tests whether a node can fulfill a large GetPooledTransactions
  421. // request.
  422. func (s *Suite) TestLargeTxRequest_66(t *utesting.T) {
  423. // send the next block to ensure the node is no longer syncing and is able to accept
  424. // txs
  425. s.sendNextBlock66(t)
  426. // send 2000 transactions to the node
  427. hashMap, txs := generateTxs(t, s, 2000)
  428. sendConn := s.setupConnection66(t)
  429. defer sendConn.Close()
  430. sendMultipleSuccessfulTxs(t, s, sendConn, txs)
  431. // set up connection to receive to ensure node is peered with the receiving connection
  432. // before tx request is sent
  433. recvConn := s.setupConnection66(t)
  434. defer recvConn.Close()
  435. // create and send pooled tx request
  436. hashes := make([]common.Hash, 0)
  437. for _, hash := range hashMap {
  438. hashes = append(hashes, hash)
  439. }
  440. getTxReq := &eth.GetPooledTransactionsPacket66{
  441. RequestId: 1234,
  442. GetPooledTransactionsPacket: hashes,
  443. }
  444. if err := recvConn.write66(getTxReq, GetPooledTransactions{}.Code()); err != nil {
  445. t.Fatalf("could not write to conn: %v", err)
  446. }
  447. // check that all received transactions match those that were sent to node
  448. switch msg := recvConn.waitForResponse(s.chain, timeout, getTxReq.RequestId).(type) {
  449. case PooledTransactions:
  450. for _, gotTx := range msg {
  451. if _, exists := hashMap[gotTx.Hash()]; !exists {
  452. t.Fatalf("unexpected tx received: %v", gotTx.Hash())
  453. }
  454. }
  455. default:
  456. t.Fatalf("unexpected %s", pretty.Sdump(msg))
  457. }
  458. }
  459. // TestNewPooledTxs_66 tests whether a node will do a GetPooledTransactions
  460. // request upon receiving a NewPooledTransactionHashes announcement.
  461. func (s *Suite) TestNewPooledTxs_66(t *utesting.T) {
  462. // send the next block to ensure the node is no longer syncing and is able to accept
  463. // txs
  464. s.sendNextBlock66(t)
  465. // generate 50 txs
  466. hashMap, _ := generateTxs(t, s, 50)
  467. // create new pooled tx hashes announcement
  468. hashes := make([]common.Hash, 0)
  469. for _, hash := range hashMap {
  470. hashes = append(hashes, hash)
  471. }
  472. announce := NewPooledTransactionHashes(hashes)
  473. // send announcement
  474. conn := s.setupConnection66(t)
  475. defer conn.Close()
  476. if err := conn.Write(announce); err != nil {
  477. t.Fatalf("could not write to connection: %v", err)
  478. }
  479. // wait for GetPooledTxs request
  480. for {
  481. _, msg := conn.readAndServe66(s.chain, timeout)
  482. switch msg := msg.(type) {
  483. case GetPooledTransactions:
  484. if len(msg) != len(hashes) {
  485. t.Fatalf("unexpected number of txs requested: wanted %d, got %d", len(hashes), len(msg))
  486. }
  487. return
  488. case *NewPooledTransactionHashes, *NewBlock, *NewBlockHashes:
  489. // ignore propagated txs and blocks from old tests
  490. continue
  491. default:
  492. t.Fatalf("unexpected %s", pretty.Sdump(msg))
  493. }
  494. }
  495. }