eth66_suite.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/core/types"
  20. "github.com/ethereum/go-ethereum/crypto"
  21. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  22. "github.com/ethereum/go-ethereum/internal/utesting"
  23. "github.com/ethereum/go-ethereum/p2p"
  24. )
  25. // Is_66 checks if the node supports the eth66 protocol version,
  26. // and if not, exists the test suite
  27. func (s *Suite) Is_66(t *utesting.T) {
  28. conn := s.dial66(t)
  29. conn.handshake(t)
  30. if conn.negotiatedProtoVersion < 66 {
  31. t.Fail()
  32. }
  33. }
  34. // TestStatus_66 attempts to connect to the given node and exchange
  35. // a status message with it on the eth66 protocol, and then check to
  36. // make sure the chain head is correct.
  37. func (s *Suite) TestStatus_66(t *utesting.T) {
  38. conn := s.dial66(t)
  39. // get protoHandshake
  40. conn.handshake(t)
  41. // get status
  42. switch msg := conn.statusExchange66(t, s.chain).(type) {
  43. case *Status:
  44. status := *msg
  45. if status.ProtocolVersion != uint32(66) {
  46. t.Fatalf("mismatch in version: wanted 66, got %d", status.ProtocolVersion)
  47. }
  48. t.Logf("got status message: %s", pretty.Sdump(msg))
  49. default:
  50. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  51. }
  52. }
  53. // TestGetBlockHeaders_66 tests whether the given node can respond to
  54. // an eth66 `GetBlockHeaders` request and that the response is accurate.
  55. func (s *Suite) TestGetBlockHeaders_66(t *utesting.T) {
  56. conn := s.setupConnection66(t)
  57. // get block headers
  58. req := &eth.GetBlockHeadersPacket66{
  59. RequestId: 3,
  60. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  61. Origin: eth.HashOrNumber{
  62. Hash: s.chain.blocks[1].Hash(),
  63. },
  64. Amount: 2,
  65. Skip: 1,
  66. Reverse: false,
  67. },
  68. }
  69. // write message
  70. headers := s.getBlockHeaders66(t, conn, req, req.RequestId)
  71. // check for correct headers
  72. headersMatch(t, s.chain, headers)
  73. }
  74. // TestSimultaneousRequests_66 sends two simultaneous `GetBlockHeader` requests
  75. // with different request IDs and checks to make sure the node responds with the correct
  76. // headers per request.
  77. func (s *Suite) TestSimultaneousRequests_66(t *utesting.T) {
  78. // create two connections
  79. conn1, conn2 := s.setupConnection66(t), s.setupConnection66(t)
  80. // create two requests
  81. req1 := &eth.GetBlockHeadersPacket66{
  82. RequestId: 111,
  83. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  84. Origin: eth.HashOrNumber{
  85. Hash: s.chain.blocks[1].Hash(),
  86. },
  87. Amount: 2,
  88. Skip: 1,
  89. Reverse: false,
  90. },
  91. }
  92. req2 := &eth.GetBlockHeadersPacket66{
  93. RequestId: 222,
  94. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  95. Origin: eth.HashOrNumber{
  96. Hash: s.chain.blocks[1].Hash(),
  97. },
  98. Amount: 4,
  99. Skip: 1,
  100. Reverse: false,
  101. },
  102. }
  103. // wait for headers for first request
  104. headerChan := make(chan BlockHeaders, 1)
  105. go func(headers chan BlockHeaders) {
  106. headers <- s.getBlockHeaders66(t, conn1, req1, req1.RequestId)
  107. }(headerChan)
  108. // check headers of second request
  109. headersMatch(t, s.chain, s.getBlockHeaders66(t, conn2, req2, req2.RequestId))
  110. // check headers of first request
  111. headersMatch(t, s.chain, <-headerChan)
  112. }
  113. // TestBroadcast_66 tests whether a block announcement is correctly
  114. // propagated to the given node's peer(s) on the eth66 protocol.
  115. func (s *Suite) TestBroadcast_66(t *utesting.T) {
  116. sendConn, receiveConn := s.setupConnection66(t), s.setupConnection66(t)
  117. nextBlock := len(s.chain.blocks)
  118. blockAnnouncement := &NewBlock{
  119. Block: s.fullChain.blocks[nextBlock],
  120. TD: s.fullChain.TD(nextBlock + 1),
  121. }
  122. s.testAnnounce66(t, sendConn, receiveConn, blockAnnouncement)
  123. // update test suite chain
  124. s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
  125. // wait for client to update its chain
  126. if err := receiveConn.waitForBlock66(s.chain.Head()); err != nil {
  127. t.Fatal(err)
  128. }
  129. }
  130. // TestGetBlockBodies_66 tests whether the given node can respond to
  131. // a `GetBlockBodies` request and that the response is accurate over
  132. // the eth66 protocol.
  133. func (s *Suite) TestGetBlockBodies_66(t *utesting.T) {
  134. conn := s.setupConnection66(t)
  135. // create block bodies request
  136. id := uint64(55)
  137. req := &eth.GetBlockBodiesPacket66{
  138. RequestId: id,
  139. GetBlockBodiesPacket: eth.GetBlockBodiesPacket{
  140. s.chain.blocks[54].Hash(),
  141. s.chain.blocks[75].Hash(),
  142. },
  143. }
  144. if err := conn.write66(req, GetBlockBodies{}.Code()); err != nil {
  145. t.Fatalf("could not write to connection: %v", err)
  146. }
  147. reqID, msg := conn.readAndServe66(s.chain, timeout)
  148. switch msg := msg.(type) {
  149. case BlockBodies:
  150. if reqID != req.RequestId {
  151. t.Fatalf("request ID mismatch: wanted %d, got %d", req.RequestId, reqID)
  152. }
  153. t.Logf("received %d block bodies", len(msg))
  154. default:
  155. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  156. }
  157. }
  158. // TestLargeAnnounce_66 tests the announcement mechanism with a large block.
  159. func (s *Suite) TestLargeAnnounce_66(t *utesting.T) {
  160. nextBlock := len(s.chain.blocks)
  161. blocks := []*NewBlock{
  162. {
  163. Block: largeBlock(),
  164. TD: s.fullChain.TD(nextBlock + 1),
  165. },
  166. {
  167. Block: s.fullChain.blocks[nextBlock],
  168. TD: largeNumber(2),
  169. },
  170. {
  171. Block: largeBlock(),
  172. TD: largeNumber(2),
  173. },
  174. {
  175. Block: s.fullChain.blocks[nextBlock],
  176. TD: s.fullChain.TD(nextBlock + 1),
  177. },
  178. }
  179. for i, blockAnnouncement := range blocks[0:3] {
  180. t.Logf("Testing malicious announcement: %v\n", i)
  181. sendConn := s.setupConnection66(t)
  182. if err := sendConn.Write(blockAnnouncement); err != nil {
  183. t.Fatalf("could not write to connection: %v", err)
  184. }
  185. // Invalid announcement, check that peer disconnected
  186. switch msg := sendConn.ReadAndServe(s.chain, timeout).(type) {
  187. case *Disconnect:
  188. case *Error:
  189. break
  190. default:
  191. t.Fatalf("unexpected: %s wanted disconnect", pretty.Sdump(msg))
  192. }
  193. }
  194. // Test the last block as a valid block
  195. sendConn := s.setupConnection66(t)
  196. receiveConn := s.setupConnection66(t)
  197. s.testAnnounce66(t, sendConn, receiveConn, blocks[3])
  198. // update test suite chain
  199. s.chain.blocks = append(s.chain.blocks, s.fullChain.blocks[nextBlock])
  200. // wait for client to update its chain
  201. if err := receiveConn.waitForBlock66(s.fullChain.blocks[nextBlock]); err != nil {
  202. t.Fatal(err)
  203. }
  204. }
  205. func (s *Suite) TestOldAnnounce_66(t *utesting.T) {
  206. s.oldAnnounce(t, s.setupConnection66(t), s.setupConnection66(t))
  207. }
  208. // TestMaliciousHandshake_66 tries to send malicious data during the handshake.
  209. func (s *Suite) TestMaliciousHandshake_66(t *utesting.T) {
  210. conn := s.dial66(t)
  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. // get protoHandshake
  284. conn.handshake(t)
  285. status := &Status{
  286. ProtocolVersion: uint32(66),
  287. NetworkID: s.chain.chainConfig.ChainID.Uint64(),
  288. TD: largeNumber(2),
  289. Head: s.chain.blocks[s.chain.Len()-1].Hash(),
  290. Genesis: s.chain.blocks[0].Hash(),
  291. ForkID: s.chain.ForkID(),
  292. }
  293. // get status
  294. switch msg := conn.statusExchange(t, s.chain, status).(type) {
  295. case *Status:
  296. t.Logf("%+v\n", msg)
  297. default:
  298. t.Fatalf("expected status, got: %#v ", msg)
  299. }
  300. // wait for disconnect
  301. switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
  302. case *Disconnect:
  303. case *Error:
  304. return
  305. default:
  306. t.Fatalf("expected disconnect, got: %s", pretty.Sdump(msg))
  307. }
  308. }
  309. func (s *Suite) TestTransaction_66(t *utesting.T) {
  310. tests := []*types.Transaction{
  311. getNextTxFromChain(t, s),
  312. unknownTx(t, s),
  313. }
  314. for i, tx := range tests {
  315. t.Logf("Testing tx propagation: %v\n", i)
  316. sendSuccessfulTx66(t, s, tx)
  317. }
  318. }
  319. func (s *Suite) TestMaliciousTx_66(t *utesting.T) {
  320. tests := []*types.Transaction{
  321. getOldTxFromChain(t, s),
  322. invalidNonceTx(t, s),
  323. hugeAmount(t, s),
  324. hugeGasPrice(t, s),
  325. hugeData(t, s),
  326. }
  327. for i, tx := range tests {
  328. t.Logf("Testing malicious tx propagation: %v\n", i)
  329. sendFailingTx66(t, s, tx)
  330. }
  331. }
  332. // TestZeroRequestID_66 checks that a request ID of zero is still handled
  333. // by the node.
  334. func (s *Suite) TestZeroRequestID_66(t *utesting.T) {
  335. conn := s.setupConnection66(t)
  336. req := &eth.GetBlockHeadersPacket66{
  337. RequestId: 0,
  338. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  339. Origin: eth.HashOrNumber{
  340. Number: 0,
  341. },
  342. Amount: 2,
  343. },
  344. }
  345. headersMatch(t, s.chain, s.getBlockHeaders66(t, conn, req, req.RequestId))
  346. }
  347. // TestSameRequestID_66 sends two requests with the same request ID
  348. // concurrently to a single node.
  349. func (s *Suite) TestSameRequestID_66(t *utesting.T) {
  350. conn := s.setupConnection66(t)
  351. // create two separate requests with same ID
  352. reqID := uint64(1234)
  353. req1 := &eth.GetBlockHeadersPacket66{
  354. RequestId: reqID,
  355. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  356. Origin: eth.HashOrNumber{
  357. Number: 0,
  358. },
  359. Amount: 2,
  360. },
  361. }
  362. req2 := &eth.GetBlockHeadersPacket66{
  363. RequestId: reqID,
  364. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  365. Origin: eth.HashOrNumber{
  366. Number: 33,
  367. },
  368. Amount: 2,
  369. },
  370. }
  371. // send requests concurrently
  372. go func() {
  373. headersMatch(t, s.chain, s.getBlockHeaders66(t, conn, req2, reqID))
  374. }()
  375. // check response from first request
  376. headersMatch(t, s.chain, s.getBlockHeaders66(t, conn, req1, reqID))
  377. }