eth66_suite.go 11 KB

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