eth66_suite.go 14 KB

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