gitee.com/liu-zhao234568/cntest@v1.0.0/cmd/devp2p/internal/ethtest/suite.go (about) 1 // Copyright 2020 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 17 package ethtest 18 19 import ( 20 "time" 21 22 "gitee.com/liu-zhao234568/cntest/common" 23 "gitee.com/liu-zhao234568/cntest/eth/protocols/eth" 24 "gitee.com/liu-zhao234568/cntest/internal/utesting" 25 "gitee.com/liu-zhao234568/cntest/p2p/enode" 26 ) 27 28 // Suite represents a structure used to test a node's conformance 29 // to the eth protocol. 30 type Suite struct { 31 Dest *enode.Node 32 33 chain *Chain 34 fullChain *Chain 35 } 36 37 // NewSuite creates and returns a new eth-test suite that can 38 // be used to test the given node against the given blockchain 39 // data. 40 func NewSuite(dest *enode.Node, chainfile string, genesisfile string) (*Suite, error) { 41 chain, err := loadChain(chainfile, genesisfile) 42 if err != nil { 43 return nil, err 44 } 45 return &Suite{ 46 Dest: dest, 47 chain: chain.Shorten(1000), 48 fullChain: chain, 49 }, nil 50 } 51 52 func (s *Suite) AllEthTests() []utesting.Test { 53 return []utesting.Test{ 54 // status 55 {Name: "TestStatus", Fn: s.TestStatus}, 56 {Name: "TestStatus66", Fn: s.TestStatus66}, 57 // get block headers 58 {Name: "TestGetBlockHeaders", Fn: s.TestGetBlockHeaders}, 59 {Name: "TestGetBlockHeaders66", Fn: s.TestGetBlockHeaders66}, 60 {Name: "TestSimultaneousRequests66", Fn: s.TestSimultaneousRequests66}, 61 {Name: "TestSameRequestID66", Fn: s.TestSameRequestID66}, 62 {Name: "TestZeroRequestID66", Fn: s.TestZeroRequestID66}, 63 // get block bodies 64 {Name: "TestGetBlockBodies", Fn: s.TestGetBlockBodies}, 65 {Name: "TestGetBlockBodies66", Fn: s.TestGetBlockBodies66}, 66 // broadcast 67 {Name: "TestBroadcast", Fn: s.TestBroadcast}, 68 {Name: "TestBroadcast66", Fn: s.TestBroadcast66}, 69 {Name: "TestLargeAnnounce", Fn: s.TestLargeAnnounce}, 70 {Name: "TestLargeAnnounce66", Fn: s.TestLargeAnnounce66}, 71 {Name: "TestOldAnnounce", Fn: s.TestOldAnnounce}, 72 {Name: "TestOldAnnounce66", Fn: s.TestOldAnnounce66}, 73 {Name: "TestBlockHashAnnounce", Fn: s.TestBlockHashAnnounce}, 74 {Name: "TestBlockHashAnnounce66", Fn: s.TestBlockHashAnnounce66}, 75 // malicious handshakes + status 76 {Name: "TestMaliciousHandshake", Fn: s.TestMaliciousHandshake}, 77 {Name: "TestMaliciousStatus", Fn: s.TestMaliciousStatus}, 78 {Name: "TestMaliciousHandshake66", Fn: s.TestMaliciousHandshake66}, 79 {Name: "TestMaliciousStatus66", Fn: s.TestMaliciousStatus66}, 80 // test transactions 81 {Name: "TestTransaction", Fn: s.TestTransaction}, 82 {Name: "TestTransaction66", Fn: s.TestTransaction66}, 83 {Name: "TestMaliciousTx", Fn: s.TestMaliciousTx}, 84 {Name: "TestMaliciousTx66", Fn: s.TestMaliciousTx66}, 85 {Name: "TestLargeTxRequest66", Fn: s.TestLargeTxRequest66}, 86 {Name: "TestNewPooledTxs66", Fn: s.TestNewPooledTxs66}, 87 } 88 } 89 90 func (s *Suite) EthTests() []utesting.Test { 91 return []utesting.Test{ 92 {Name: "TestStatus", Fn: s.TestStatus}, 93 {Name: "TestGetBlockHeaders", Fn: s.TestGetBlockHeaders}, 94 {Name: "TestGetBlockBodies", Fn: s.TestGetBlockBodies}, 95 {Name: "TestBroadcast", Fn: s.TestBroadcast}, 96 {Name: "TestLargeAnnounce", Fn: s.TestLargeAnnounce}, 97 {Name: "TestOldAnnounce", Fn: s.TestOldAnnounce}, 98 {Name: "TestBlockHashAnnounce", Fn: s.TestBlockHashAnnounce}, 99 {Name: "TestMaliciousHandshake", Fn: s.TestMaliciousHandshake}, 100 {Name: "TestMaliciousStatus", Fn: s.TestMaliciousStatus}, 101 {Name: "TestTransaction", Fn: s.TestTransaction}, 102 {Name: "TestMaliciousTx", Fn: s.TestMaliciousTx}, 103 } 104 } 105 106 func (s *Suite) Eth66Tests() []utesting.Test { 107 return []utesting.Test{ 108 // only proceed with eth66 test suite if node supports eth 66 protocol 109 {Name: "TestStatus66", Fn: s.TestStatus66}, 110 {Name: "TestGetBlockHeaders66", Fn: s.TestGetBlockHeaders66}, 111 {Name: "TestSimultaneousRequests66", Fn: s.TestSimultaneousRequests66}, 112 {Name: "TestSameRequestID66", Fn: s.TestSameRequestID66}, 113 {Name: "TestZeroRequestID66", Fn: s.TestZeroRequestID66}, 114 {Name: "TestGetBlockBodies66", Fn: s.TestGetBlockBodies66}, 115 {Name: "TestBroadcast66", Fn: s.TestBroadcast66}, 116 {Name: "TestLargeAnnounce66", Fn: s.TestLargeAnnounce66}, 117 {Name: "TestOldAnnounce66", Fn: s.TestOldAnnounce66}, 118 {Name: "TestBlockHashAnnounce66", Fn: s.TestBlockHashAnnounce66}, 119 {Name: "TestMaliciousHandshake66", Fn: s.TestMaliciousHandshake66}, 120 {Name: "TestMaliciousStatus66", Fn: s.TestMaliciousStatus66}, 121 {Name: "TestTransaction66", Fn: s.TestTransaction66}, 122 {Name: "TestMaliciousTx66", Fn: s.TestMaliciousTx66}, 123 {Name: "TestLargeTxRequest66", Fn: s.TestLargeTxRequest66}, 124 {Name: "TestNewPooledTxs66", Fn: s.TestNewPooledTxs66}, 125 } 126 } 127 128 var ( 129 eth66 = true // indicates whether suite should negotiate eth66 connection 130 eth65 = false // indicates whether suite should negotiate eth65 connection or below. 131 ) 132 133 // TestStatus attempts to connect to the given node and exchange 134 // a status message with it. 135 func (s *Suite) TestStatus(t *utesting.T) { 136 conn, err := s.dial() 137 if err != nil { 138 t.Fatalf("dial failed: %v", err) 139 } 140 defer conn.Close() 141 if err := conn.peer(s.chain, nil); err != nil { 142 t.Fatalf("peering failed: %v", err) 143 } 144 } 145 146 // TestStatus66 attempts to connect to the given node and exchange 147 // a status message with it on the eth66 protocol. 148 func (s *Suite) TestStatus66(t *utesting.T) { 149 conn, err := s.dial66() 150 if err != nil { 151 t.Fatalf("dial failed: %v", err) 152 } 153 defer conn.Close() 154 if err := conn.peer(s.chain, nil); err != nil { 155 t.Fatalf("peering failed: %v", err) 156 } 157 } 158 159 // TestGetBlockHeaders tests whether the given node can respond to 160 // a `GetBlockHeaders` request accurately. 161 func (s *Suite) TestGetBlockHeaders(t *utesting.T) { 162 conn, err := s.dial() 163 if err != nil { 164 t.Fatalf("dial failed: %v", err) 165 } 166 defer conn.Close() 167 if err := conn.peer(s.chain, nil); err != nil { 168 t.Fatalf("handshake(s) failed: %v", err) 169 } 170 // write request 171 req := &GetBlockHeaders{ 172 Origin: eth.HashOrNumber{ 173 Hash: s.chain.blocks[1].Hash(), 174 }, 175 Amount: 2, 176 Skip: 1, 177 Reverse: false, 178 } 179 headers, err := conn.headersRequest(req, s.chain, eth65, 0) 180 if err != nil { 181 t.Fatalf("GetBlockHeaders request failed: %v", err) 182 } 183 // check for correct headers 184 expected, err := s.chain.GetHeaders(*req) 185 if err != nil { 186 t.Fatalf("failed to get headers for given request: %v", err) 187 } 188 if !headersMatch(expected, headers) { 189 t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected, headers) 190 } 191 } 192 193 // TestGetBlockHeaders66 tests whether the given node can respond to 194 // an eth66 `GetBlockHeaders` request and that the response is accurate. 195 func (s *Suite) TestGetBlockHeaders66(t *utesting.T) { 196 conn, err := s.dial66() 197 if err != nil { 198 t.Fatalf("dial failed: %v", err) 199 } 200 defer conn.Close() 201 if err = conn.peer(s.chain, nil); err != nil { 202 t.Fatalf("peering failed: %v", err) 203 } 204 // write request 205 req := &GetBlockHeaders{ 206 Origin: eth.HashOrNumber{ 207 Hash: s.chain.blocks[1].Hash(), 208 }, 209 Amount: 2, 210 Skip: 1, 211 Reverse: false, 212 } 213 headers, err := conn.headersRequest(req, s.chain, eth66, 33) 214 if err != nil { 215 t.Fatalf("could not get block headers: %v", err) 216 } 217 // check for correct headers 218 expected, err := s.chain.GetHeaders(*req) 219 if err != nil { 220 t.Fatalf("failed to get headers for given request: %v", err) 221 } 222 if !headersMatch(expected, headers) { 223 t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected, headers) 224 } 225 } 226 227 // TestSimultaneousRequests66 sends two simultaneous `GetBlockHeader` requests from 228 // the same connection with different request IDs and checks to make sure the node 229 // responds with the correct headers per request. 230 func (s *Suite) TestSimultaneousRequests66(t *utesting.T) { 231 // create a connection 232 conn, err := s.dial66() 233 if err != nil { 234 t.Fatalf("dial failed: %v", err) 235 } 236 defer conn.Close() 237 if err := conn.peer(s.chain, nil); err != nil { 238 t.Fatalf("peering failed: %v", err) 239 } 240 // create two requests 241 req1 := ð.GetBlockHeadersPacket66{ 242 RequestId: uint64(111), 243 GetBlockHeadersPacket: ð.GetBlockHeadersPacket{ 244 Origin: eth.HashOrNumber{ 245 Hash: s.chain.blocks[1].Hash(), 246 }, 247 Amount: 2, 248 Skip: 1, 249 Reverse: false, 250 }, 251 } 252 req2 := ð.GetBlockHeadersPacket66{ 253 RequestId: uint64(222), 254 GetBlockHeadersPacket: ð.GetBlockHeadersPacket{ 255 Origin: eth.HashOrNumber{ 256 Hash: s.chain.blocks[1].Hash(), 257 }, 258 Amount: 4, 259 Skip: 1, 260 Reverse: false, 261 }, 262 } 263 // write the first request 264 if err := conn.Write66(req1, GetBlockHeaders{}.Code()); err != nil { 265 t.Fatalf("failed to write to connection: %v", err) 266 } 267 // write the second request 268 if err := conn.Write66(req2, GetBlockHeaders{}.Code()); err != nil { 269 t.Fatalf("failed to write to connection: %v", err) 270 } 271 // wait for responses 272 msg := conn.waitForResponse(s.chain, timeout, req1.RequestId) 273 headers1, ok := msg.(BlockHeaders) 274 if !ok { 275 t.Fatalf("unexpected %s", pretty.Sdump(msg)) 276 } 277 msg = conn.waitForResponse(s.chain, timeout, req2.RequestId) 278 headers2, ok := msg.(BlockHeaders) 279 if !ok { 280 t.Fatalf("unexpected %s", pretty.Sdump(msg)) 281 } 282 // check received headers for accuracy 283 expected1, err := s.chain.GetHeaders(GetBlockHeaders(*req1.GetBlockHeadersPacket)) 284 if err != nil { 285 t.Fatalf("failed to get expected headers for request 1: %v", err) 286 } 287 expected2, err := s.chain.GetHeaders(GetBlockHeaders(*req2.GetBlockHeadersPacket)) 288 if err != nil { 289 t.Fatalf("failed to get expected headers for request 2: %v", err) 290 } 291 if !headersMatch(expected1, headers1) { 292 t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected1, headers1) 293 } 294 if !headersMatch(expected2, headers2) { 295 t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected2, headers2) 296 } 297 } 298 299 // TestSameRequestID66 sends two requests with the same request ID to a 300 // single node. 301 func (s *Suite) TestSameRequestID66(t *utesting.T) { 302 conn, err := s.dial66() 303 if err != nil { 304 t.Fatalf("dial failed: %v", err) 305 } 306 defer conn.Close() 307 if err := conn.peer(s.chain, nil); err != nil { 308 t.Fatalf("peering failed: %v", err) 309 } 310 // create requests 311 reqID := uint64(1234) 312 request1 := ð.GetBlockHeadersPacket66{ 313 RequestId: reqID, 314 GetBlockHeadersPacket: ð.GetBlockHeadersPacket{ 315 Origin: eth.HashOrNumber{ 316 Number: 1, 317 }, 318 Amount: 2, 319 }, 320 } 321 request2 := ð.GetBlockHeadersPacket66{ 322 RequestId: reqID, 323 GetBlockHeadersPacket: ð.GetBlockHeadersPacket{ 324 Origin: eth.HashOrNumber{ 325 Number: 33, 326 }, 327 Amount: 2, 328 }, 329 } 330 // write the requests 331 if err = conn.Write66(request1, GetBlockHeaders{}.Code()); err != nil { 332 t.Fatalf("failed to write to connection: %v", err) 333 } 334 if err = conn.Write66(request2, GetBlockHeaders{}.Code()); err != nil { 335 t.Fatalf("failed to write to connection: %v", err) 336 } 337 // wait for responses 338 msg := conn.waitForResponse(s.chain, timeout, reqID) 339 headers1, ok := msg.(BlockHeaders) 340 if !ok { 341 t.Fatalf("unexpected %s", pretty.Sdump(msg)) 342 } 343 msg = conn.waitForResponse(s.chain, timeout, reqID) 344 headers2, ok := msg.(BlockHeaders) 345 if !ok { 346 t.Fatalf("unexpected %s", pretty.Sdump(msg)) 347 } 348 // check if headers match 349 expected1, err := s.chain.GetHeaders(GetBlockHeaders(*request1.GetBlockHeadersPacket)) 350 if err != nil { 351 t.Fatalf("failed to get expected block headers: %v", err) 352 } 353 expected2, err := s.chain.GetHeaders(GetBlockHeaders(*request2.GetBlockHeadersPacket)) 354 if err != nil { 355 t.Fatalf("failed to get expected block headers: %v", err) 356 } 357 if !headersMatch(expected1, headers1) { 358 t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected1, headers1) 359 } 360 if !headersMatch(expected2, headers2) { 361 t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected2, headers2) 362 } 363 } 364 365 // TestZeroRequestID_66 checks that a message with a request ID of zero is still handled 366 // by the node. 367 func (s *Suite) TestZeroRequestID66(t *utesting.T) { 368 conn, err := s.dial66() 369 if err != nil { 370 t.Fatalf("dial failed: %v", err) 371 } 372 defer conn.Close() 373 if err := conn.peer(s.chain, nil); err != nil { 374 t.Fatalf("peering failed: %v", err) 375 } 376 req := &GetBlockHeaders{ 377 Origin: eth.HashOrNumber{ 378 Number: 0, 379 }, 380 Amount: 2, 381 } 382 headers, err := conn.headersRequest(req, s.chain, eth66, 0) 383 if err != nil { 384 t.Fatalf("failed to get block headers: %v", err) 385 } 386 expected, err := s.chain.GetHeaders(*req) 387 if err != nil { 388 t.Fatalf("failed to get expected block headers: %v", err) 389 } 390 if !headersMatch(expected, headers) { 391 t.Fatalf("header mismatch: \nexpected %v \ngot %v", expected, headers) 392 } 393 } 394 395 // TestGetBlockBodies tests whether the given node can respond to 396 // a `GetBlockBodies` request and that the response is accurate. 397 func (s *Suite) TestGetBlockBodies(t *utesting.T) { 398 conn, err := s.dial() 399 if err != nil { 400 t.Fatalf("dial failed: %v", err) 401 } 402 defer conn.Close() 403 if err := conn.peer(s.chain, nil); err != nil { 404 t.Fatalf("peering failed: %v", err) 405 } 406 // create block bodies request 407 req := &GetBlockBodies{ 408 s.chain.blocks[54].Hash(), 409 s.chain.blocks[75].Hash(), 410 } 411 if err := conn.Write(req); err != nil { 412 t.Fatalf("could not write to connection: %v", err) 413 } 414 // wait for response 415 switch msg := conn.readAndServe(s.chain, timeout).(type) { 416 case *BlockBodies: 417 t.Logf("received %d block bodies", len(*msg)) 418 if len(*msg) != len(*req) { 419 t.Fatalf("wrong bodies in response: expected %d bodies, "+ 420 "got %d", len(*req), len(*msg)) 421 } 422 default: 423 t.Fatalf("unexpected: %s", pretty.Sdump(msg)) 424 } 425 } 426 427 // TestGetBlockBodies66 tests whether the given node can respond to 428 // a `GetBlockBodies` request and that the response is accurate over 429 // the eth66 protocol. 430 func (s *Suite) TestGetBlockBodies66(t *utesting.T) { 431 conn, err := s.dial66() 432 if err != nil { 433 t.Fatalf("dial failed: %v", err) 434 } 435 defer conn.Close() 436 if err := conn.peer(s.chain, nil); err != nil { 437 t.Fatalf("peering failed: %v", err) 438 } 439 // create block bodies request 440 req := ð.GetBlockBodiesPacket66{ 441 RequestId: uint64(55), 442 GetBlockBodiesPacket: eth.GetBlockBodiesPacket{ 443 s.chain.blocks[54].Hash(), 444 s.chain.blocks[75].Hash(), 445 }, 446 } 447 if err := conn.Write66(req, GetBlockBodies{}.Code()); err != nil { 448 t.Fatalf("could not write to connection: %v", err) 449 } 450 // wait for block bodies response 451 msg := conn.waitForResponse(s.chain, timeout, req.RequestId) 452 blockBodies, ok := msg.(BlockBodies) 453 if !ok { 454 t.Fatalf("unexpected: %s", pretty.Sdump(msg)) 455 } 456 t.Logf("received %d block bodies", len(blockBodies)) 457 if len(blockBodies) != len(req.GetBlockBodiesPacket) { 458 t.Fatalf("wrong bodies in response: expected %d bodies, "+ 459 "got %d", len(req.GetBlockBodiesPacket), len(blockBodies)) 460 } 461 } 462 463 // TestBroadcast tests whether a block announcement is correctly 464 // propagated to the given node's peer(s). 465 func (s *Suite) TestBroadcast(t *utesting.T) { 466 if err := s.sendNextBlock(eth65); err != nil { 467 t.Fatalf("block broadcast failed: %v", err) 468 } 469 } 470 471 // TestBroadcast66 tests whether a block announcement is correctly 472 // propagated to the given node's peer(s) on the eth66 protocol. 473 func (s *Suite) TestBroadcast66(t *utesting.T) { 474 if err := s.sendNextBlock(eth66); err != nil { 475 t.Fatalf("block broadcast failed: %v", err) 476 } 477 } 478 479 // TestLargeAnnounce tests the announcement mechanism with a large block. 480 func (s *Suite) TestLargeAnnounce(t *utesting.T) { 481 nextBlock := len(s.chain.blocks) 482 blocks := []*NewBlock{ 483 { 484 Block: largeBlock(), 485 TD: s.fullChain.TotalDifficultyAt(nextBlock), 486 }, 487 { 488 Block: s.fullChain.blocks[nextBlock], 489 TD: largeNumber(2), 490 }, 491 { 492 Block: largeBlock(), 493 TD: largeNumber(2), 494 }, 495 } 496 497 for i, blockAnnouncement := range blocks { 498 t.Logf("Testing malicious announcement: %v\n", i) 499 conn, err := s.dial() 500 if err != nil { 501 t.Fatalf("dial failed: %v", err) 502 } 503 if err = conn.peer(s.chain, nil); err != nil { 504 t.Fatalf("peering failed: %v", err) 505 } 506 if err = conn.Write(blockAnnouncement); err != nil { 507 t.Fatalf("could not write to connection: %v", err) 508 } 509 // Invalid announcement, check that peer disconnected 510 switch msg := conn.readAndServe(s.chain, time.Second*8).(type) { 511 case *Disconnect: 512 case *Error: 513 break 514 default: 515 t.Fatalf("unexpected: %s wanted disconnect", pretty.Sdump(msg)) 516 } 517 conn.Close() 518 } 519 // Test the last block as a valid block 520 if err := s.sendNextBlock(eth65); err != nil { 521 t.Fatalf("failed to broadcast next block: %v", err) 522 } 523 } 524 525 // TestLargeAnnounce66 tests the announcement mechanism with a large 526 // block over the eth66 protocol. 527 func (s *Suite) TestLargeAnnounce66(t *utesting.T) { 528 nextBlock := len(s.chain.blocks) 529 blocks := []*NewBlock{ 530 { 531 Block: largeBlock(), 532 TD: s.fullChain.TotalDifficultyAt(nextBlock), 533 }, 534 { 535 Block: s.fullChain.blocks[nextBlock], 536 TD: largeNumber(2), 537 }, 538 { 539 Block: largeBlock(), 540 TD: largeNumber(2), 541 }, 542 } 543 544 for i, blockAnnouncement := range blocks[0:3] { 545 t.Logf("Testing malicious announcement: %v\n", i) 546 conn, err := s.dial66() 547 if err != nil { 548 t.Fatalf("dial failed: %v", err) 549 } 550 if err := conn.peer(s.chain, nil); err != nil { 551 t.Fatalf("peering failed: %v", err) 552 } 553 if err := conn.Write(blockAnnouncement); err != nil { 554 t.Fatalf("could not write to connection: %v", err) 555 } 556 // Invalid announcement, check that peer disconnected 557 switch msg := conn.readAndServe(s.chain, time.Second*8).(type) { 558 case *Disconnect: 559 case *Error: 560 break 561 default: 562 t.Fatalf("unexpected: %s wanted disconnect", pretty.Sdump(msg)) 563 } 564 conn.Close() 565 } 566 // Test the last block as a valid block 567 if err := s.sendNextBlock(eth66); err != nil { 568 t.Fatalf("failed to broadcast next block: %v", err) 569 } 570 } 571 572 // TestOldAnnounce tests the announcement mechanism with an old block. 573 func (s *Suite) TestOldAnnounce(t *utesting.T) { 574 if err := s.oldAnnounce(eth65); err != nil { 575 t.Fatal(err) 576 } 577 } 578 579 // TestOldAnnounce66 tests the announcement mechanism with an old block, 580 // over the eth66 protocol. 581 func (s *Suite) TestOldAnnounce66(t *utesting.T) { 582 if err := s.oldAnnounce(eth66); err != nil { 583 t.Fatal(err) 584 } 585 } 586 587 // TestBlockHashAnnounce sends a new block hash announcement and expects 588 // the node to perform a `GetBlockHeaders` request. 589 func (s *Suite) TestBlockHashAnnounce(t *utesting.T) { 590 if err := s.hashAnnounce(eth65); err != nil { 591 t.Fatalf("block hash announcement failed: %v", err) 592 } 593 } 594 595 // TestBlockHashAnnounce66 sends a new block hash announcement and expects 596 // the node to perform a `GetBlockHeaders` request. 597 func (s *Suite) TestBlockHashAnnounce66(t *utesting.T) { 598 if err := s.hashAnnounce(eth66); err != nil { 599 t.Fatalf("block hash announcement failed: %v", err) 600 } 601 } 602 603 // TestMaliciousHandshake tries to send malicious data during the handshake. 604 func (s *Suite) TestMaliciousHandshake(t *utesting.T) { 605 if err := s.maliciousHandshakes(t, eth65); err != nil { 606 t.Fatal(err) 607 } 608 } 609 610 // TestMaliciousHandshake66 tries to send malicious data during the handshake. 611 func (s *Suite) TestMaliciousHandshake66(t *utesting.T) { 612 if err := s.maliciousHandshakes(t, eth66); err != nil { 613 t.Fatal(err) 614 } 615 } 616 617 // TestMaliciousStatus sends a status package with a large total difficulty. 618 func (s *Suite) TestMaliciousStatus(t *utesting.T) { 619 conn, err := s.dial() 620 if err != nil { 621 t.Fatalf("dial failed: %v", err) 622 } 623 defer conn.Close() 624 625 if err := s.maliciousStatus(conn); err != nil { 626 t.Fatal(err) 627 } 628 } 629 630 // TestMaliciousStatus66 sends a status package with a large total 631 // difficulty over the eth66 protocol. 632 func (s *Suite) TestMaliciousStatus66(t *utesting.T) { 633 conn, err := s.dial66() 634 if err != nil { 635 t.Fatalf("dial failed: %v", err) 636 } 637 defer conn.Close() 638 639 if err := s.maliciousStatus(conn); err != nil { 640 t.Fatal(err) 641 } 642 } 643 644 // TestTransaction sends a valid transaction to the node and 645 // checks if the transaction gets propagated. 646 func (s *Suite) TestTransaction(t *utesting.T) { 647 if err := s.sendSuccessfulTxs(t, eth65); err != nil { 648 t.Fatal(err) 649 } 650 } 651 652 // TestTransaction66 sends a valid transaction to the node and 653 // checks if the transaction gets propagated. 654 func (s *Suite) TestTransaction66(t *utesting.T) { 655 if err := s.sendSuccessfulTxs(t, eth66); err != nil { 656 t.Fatal(err) 657 } 658 } 659 660 // TestMaliciousTx sends several invalid transactions and tests whether 661 // the node will propagate them. 662 func (s *Suite) TestMaliciousTx(t *utesting.T) { 663 if err := s.sendMaliciousTxs(t, eth65); err != nil { 664 t.Fatal(err) 665 } 666 } 667 668 // TestMaliciousTx66 sends several invalid transactions and tests whether 669 // the node will propagate them. 670 func (s *Suite) TestMaliciousTx66(t *utesting.T) { 671 if err := s.sendMaliciousTxs(t, eth66); err != nil { 672 t.Fatal(err) 673 } 674 } 675 676 // TestLargeTxRequest66 tests whether a node can fulfill a large GetPooledTransactions 677 // request. 678 func (s *Suite) TestLargeTxRequest66(t *utesting.T) { 679 // send the next block to ensure the node is no longer syncing and 680 // is able to accept txs 681 if err := s.sendNextBlock(eth66); err != nil { 682 t.Fatalf("failed to send next block: %v", err) 683 } 684 // send 2000 transactions to the node 685 hashMap, txs, err := generateTxs(s, 2000) 686 if err != nil { 687 t.Fatalf("failed to generate transactions: %v", err) 688 } 689 if err = sendMultipleSuccessfulTxs(t, s, txs); err != nil { 690 t.Fatalf("failed to send multiple txs: %v", err) 691 } 692 // set up connection to receive to ensure node is peered with the receiving connection 693 // before tx request is sent 694 conn, err := s.dial66() 695 if err != nil { 696 t.Fatalf("dial failed: %v", err) 697 } 698 defer conn.Close() 699 if err = conn.peer(s.chain, nil); err != nil { 700 t.Fatalf("peering failed: %v", err) 701 } 702 // create and send pooled tx request 703 hashes := make([]common.Hash, 0) 704 for _, hash := range hashMap { 705 hashes = append(hashes, hash) 706 } 707 getTxReq := ð.GetPooledTransactionsPacket66{ 708 RequestId: 1234, 709 GetPooledTransactionsPacket: hashes, 710 } 711 if err = conn.Write66(getTxReq, GetPooledTransactions{}.Code()); err != nil { 712 t.Fatalf("could not write to conn: %v", err) 713 } 714 // check that all received transactions match those that were sent to node 715 switch msg := conn.waitForResponse(s.chain, timeout, getTxReq.RequestId).(type) { 716 case PooledTransactions: 717 for _, gotTx := range msg { 718 if _, exists := hashMap[gotTx.Hash()]; !exists { 719 t.Fatalf("unexpected tx received: %v", gotTx.Hash()) 720 } 721 } 722 default: 723 t.Fatalf("unexpected %s", pretty.Sdump(msg)) 724 } 725 } 726 727 // TestNewPooledTxs_66 tests whether a node will do a GetPooledTransactions 728 // request upon receiving a NewPooledTransactionHashes announcement. 729 func (s *Suite) TestNewPooledTxs66(t *utesting.T) { 730 // send the next block to ensure the node is no longer syncing and 731 // is able to accept txs 732 if err := s.sendNextBlock(eth66); err != nil { 733 t.Fatalf("failed to send next block: %v", err) 734 } 735 736 // generate 50 txs 737 hashMap, _, err := generateTxs(s, 50) 738 if err != nil { 739 t.Fatalf("failed to generate transactions: %v", err) 740 } 741 742 // create new pooled tx hashes announcement 743 hashes := make([]common.Hash, 0) 744 for _, hash := range hashMap { 745 hashes = append(hashes, hash) 746 } 747 announce := NewPooledTransactionHashes(hashes) 748 749 // send announcement 750 conn, err := s.dial66() 751 if err != nil { 752 t.Fatalf("dial failed: %v", err) 753 } 754 defer conn.Close() 755 if err = conn.peer(s.chain, nil); err != nil { 756 t.Fatalf("peering failed: %v", err) 757 } 758 if err = conn.Write(announce); err != nil { 759 t.Fatalf("failed to write to connection: %v", err) 760 } 761 762 // wait for GetPooledTxs request 763 for { 764 _, msg := conn.readAndServe66(s.chain, timeout) 765 switch msg := msg.(type) { 766 case GetPooledTransactions: 767 if len(msg) != len(hashes) { 768 t.Fatalf("unexpected number of txs requested: wanted %d, got %d", len(hashes), len(msg)) 769 } 770 return 771 // ignore propagated txs from previous tests 772 case *NewPooledTransactionHashes: 773 continue 774 // ignore block announcements from previous tests 775 case *NewBlockHashes: 776 continue 777 case *NewBlock: 778 continue 779 default: 780 t.Fatalf("unexpected %s", pretty.Sdump(msg)) 781 } 782 } 783 }