eth66_suite.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. // TestMaliciousHandshake_66 tries to send malicious data during the handshake.
  206. func (s *Suite) TestMaliciousHandshake_66(t *utesting.T) {
  207. conn := s.dial66(t)
  208. // write hello to client
  209. pub0 := crypto.FromECDSAPub(&conn.ourKey.PublicKey)[1:]
  210. handshakes := []*Hello{
  211. {
  212. Version: 5,
  213. Caps: []p2p.Cap{
  214. {Name: largeString(2), Version: 66},
  215. },
  216. ID: pub0,
  217. },
  218. {
  219. Version: 5,
  220. Caps: []p2p.Cap{
  221. {Name: "eth", Version: 64},
  222. {Name: "eth", Version: 65},
  223. {Name: "eth", Version: 66},
  224. },
  225. ID: append(pub0, byte(0)),
  226. },
  227. {
  228. Version: 5,
  229. Caps: []p2p.Cap{
  230. {Name: "eth", Version: 64},
  231. {Name: "eth", Version: 65},
  232. {Name: "eth", Version: 66},
  233. },
  234. ID: append(pub0, pub0...),
  235. },
  236. {
  237. Version: 5,
  238. Caps: []p2p.Cap{
  239. {Name: "eth", Version: 64},
  240. {Name: "eth", Version: 65},
  241. {Name: "eth", Version: 66},
  242. },
  243. ID: largeBuffer(2),
  244. },
  245. {
  246. Version: 5,
  247. Caps: []p2p.Cap{
  248. {Name: largeString(2), Version: 66},
  249. },
  250. ID: largeBuffer(2),
  251. },
  252. }
  253. for i, handshake := range handshakes {
  254. t.Logf("Testing malicious handshake %v\n", i)
  255. // Init the handshake
  256. if err := conn.Write(handshake); err != nil {
  257. t.Fatalf("could not write to connection: %v", err)
  258. }
  259. // check that the peer disconnected
  260. timeout := 20 * time.Second
  261. // Discard one hello
  262. for i := 0; i < 2; i++ {
  263. switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
  264. case *Disconnect:
  265. case *Error:
  266. case *Hello:
  267. // Hello's are sent concurrently, so ignore them
  268. continue
  269. default:
  270. t.Fatalf("unexpected: %s", pretty.Sdump(msg))
  271. }
  272. }
  273. // Dial for the next round
  274. conn = s.dial66(t)
  275. }
  276. }
  277. // TestMaliciousStatus_66 sends a status package with a large total difficulty.
  278. func (s *Suite) TestMaliciousStatus_66(t *utesting.T) {
  279. conn := s.dial66(t)
  280. // get protoHandshake
  281. conn.handshake(t)
  282. status := &Status{
  283. ProtocolVersion: uint32(66),
  284. NetworkID: s.chain.chainConfig.ChainID.Uint64(),
  285. TD: largeNumber(2),
  286. Head: s.chain.blocks[s.chain.Len()-1].Hash(),
  287. Genesis: s.chain.blocks[0].Hash(),
  288. ForkID: s.chain.ForkID(),
  289. }
  290. // get status
  291. switch msg := conn.statusExchange(t, s.chain, status).(type) {
  292. case *Status:
  293. t.Logf("%+v\n", msg)
  294. default:
  295. t.Fatalf("expected status, got: %#v ", msg)
  296. }
  297. // wait for disconnect
  298. switch msg := conn.ReadAndServe(s.chain, timeout).(type) {
  299. case *Disconnect:
  300. case *Error:
  301. return
  302. default:
  303. t.Fatalf("expected disconnect, got: %s", pretty.Sdump(msg))
  304. }
  305. }
  306. func (s *Suite) TestTransaction_66(t *utesting.T) {
  307. tests := []*types.Transaction{
  308. getNextTxFromChain(t, s),
  309. unknownTx(t, s),
  310. }
  311. for i, tx := range tests {
  312. t.Logf("Testing tx propagation: %v\n", i)
  313. sendSuccessfulTx66(t, s, tx)
  314. }
  315. }
  316. func (s *Suite) TestMaliciousTx_66(t *utesting.T) {
  317. tests := []*types.Transaction{
  318. getOldTxFromChain(t, s),
  319. invalidNonceTx(t, s),
  320. hugeAmount(t, s),
  321. hugeGasPrice(t, s),
  322. hugeData(t, s),
  323. }
  324. for i, tx := range tests {
  325. t.Logf("Testing malicious tx propagation: %v\n", i)
  326. sendFailingTx66(t, s, tx)
  327. }
  328. }
  329. // TestZeroRequestID_66 checks that a request ID of zero is still handled
  330. // by the node.
  331. func (s *Suite) TestZeroRequestID_66(t *utesting.T) {
  332. conn := s.setupConnection66(t)
  333. req := &eth.GetBlockHeadersPacket66{
  334. RequestId: 0,
  335. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  336. Origin: eth.HashOrNumber{
  337. Number: 0,
  338. },
  339. Amount: 2,
  340. },
  341. }
  342. headersMatch(t, s.chain, s.getBlockHeaders66(t, conn, req, req.RequestId))
  343. }
  344. // TestSameRequestID_66 sends two requests with the same request ID
  345. // concurrently to a single node.
  346. func (s *Suite) TestSameRequestID_66(t *utesting.T) {
  347. conn := s.setupConnection66(t)
  348. // create two separate requests with same ID
  349. reqID := uint64(1234)
  350. req1 := &eth.GetBlockHeadersPacket66{
  351. RequestId: reqID,
  352. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  353. Origin: eth.HashOrNumber{
  354. Number: 0,
  355. },
  356. Amount: 2,
  357. },
  358. }
  359. req2 := &eth.GetBlockHeadersPacket66{
  360. RequestId: reqID,
  361. GetBlockHeadersPacket: &eth.GetBlockHeadersPacket{
  362. Origin: eth.HashOrNumber{
  363. Number: 33,
  364. },
  365. Amount: 2,
  366. },
  367. }
  368. // send requests concurrently
  369. go func() {
  370. headersMatch(t, s.chain, s.getBlockHeaders66(t, conn, req2, reqID))
  371. }()
  372. // check response from first request
  373. headersMatch(t, s.chain, s.getBlockHeaders66(t, conn, req1, reqID))
  374. }