github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/cmd/devp2p/rlpxcmd.go (about) 1 // Copyright 2020 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU 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 // go-ethereum 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 General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "fmt" 21 "net" 22 23 "gopkg.in/urfave/cli.v1" 24 25 "github.com/scroll-tech/go-ethereum/cmd/devp2p/internal/ethtest" 26 "github.com/scroll-tech/go-ethereum/crypto" 27 "github.com/scroll-tech/go-ethereum/internal/utesting" 28 "github.com/scroll-tech/go-ethereum/p2p" 29 "github.com/scroll-tech/go-ethereum/p2p/rlpx" 30 "github.com/scroll-tech/go-ethereum/rlp" 31 ) 32 33 var ( 34 rlpxCommand = cli.Command{ 35 Name: "rlpx", 36 Usage: "RLPx Commands", 37 Subcommands: []cli.Command{ 38 rlpxPingCommand, 39 rlpxEthTestCommand, 40 }, 41 } 42 rlpxPingCommand = cli.Command{ 43 Name: "ping", 44 Usage: "ping <node>", 45 Action: rlpxPing, 46 } 47 rlpxEthTestCommand = cli.Command{ 48 Name: "eth-test", 49 Usage: "Runs tests against a node", 50 ArgsUsage: "<node> <chain.rlp> <genesis.json>", 51 Action: rlpxEthTest, 52 Flags: []cli.Flag{ 53 testPatternFlag, 54 testTAPFlag, 55 }, 56 } 57 ) 58 59 func rlpxPing(ctx *cli.Context) error { 60 n := getNodeArg(ctx) 61 fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", n.IP(), n.TCP())) 62 if err != nil { 63 return err 64 } 65 conn := rlpx.NewConn(fd, n.Pubkey()) 66 ourKey, _ := crypto.GenerateKey() 67 _, err = conn.Handshake(ourKey) 68 if err != nil { 69 return err 70 } 71 code, data, _, err := conn.Read() 72 if err != nil { 73 return err 74 } 75 switch code { 76 case 0: 77 var h ethtest.Hello 78 if err := rlp.DecodeBytes(data, &h); err != nil { 79 return fmt.Errorf("invalid handshake: %v", err) 80 } 81 fmt.Printf("%+v\n", h) 82 case 1: 83 var msg []p2p.DiscReason 84 if rlp.DecodeBytes(data, &msg); len(msg) == 0 { 85 return fmt.Errorf("invalid disconnect message") 86 } 87 return fmt.Errorf("received disconnect message: %v", msg[0]) 88 default: 89 return fmt.Errorf("invalid message code %d, expected handshake (code zero)", code) 90 } 91 return nil 92 } 93 94 // rlpxEthTest runs the eth protocol test suite. 95 func rlpxEthTest(ctx *cli.Context) error { 96 if ctx.NArg() < 3 { 97 exit("missing path to chain.rlp as command-line argument") 98 } 99 suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args()[1], ctx.Args()[2]) 100 if err != nil { 101 exit(err) 102 } 103 // check if given node supports eth66, and if so, run eth66 protocol tests as well 104 is66Failed, _ := utesting.Run(utesting.Test{Name: "Is_66", Fn: suite.Is_66}) 105 if is66Failed { 106 return runTests(ctx, suite.EthTests()) 107 } 108 return runTests(ctx, suite.AllEthTests()) 109 }