github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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 "github.com/ethereum/go-ethereum/cmd/devp2p/internal/ethtest" 24 "github.com/ethereum/go-ethereum/crypto" 25 "github.com/ethereum/go-ethereum/internal/utesting" 26 "github.com/ethereum/go-ethereum/p2p" 27 "github.com/ethereum/go-ethereum/p2p/rlpx" 28 "github.com/ethereum/go-ethereum/rlp" 29 "gopkg.in/urfave/cli.v1" 30 ) 31 32 var ( 33 rlpxCommand = cli.Command{ 34 Name: "rlpx", 35 Usage: "RLPx Commands", 36 Subcommands: []cli.Command{ 37 rlpxPingCommand, 38 rlpxEthTestCommand, 39 rlpxSnapTestCommand, 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 rlpxSnapTestCommand = cli.Command{ 58 Name: "snap-test", 59 Usage: "Runs tests against a node", 60 ArgsUsage: "<node> <chain.rlp> <genesis.json>", 61 Action: rlpxSnapTest, 62 Flags: []cli.Flag{ 63 testPatternFlag, 64 testTAPFlag, 65 }, 66 } 67 ) 68 69 func rlpxPing(ctx *cli.Context) error { 70 n := getNodeArg(ctx) 71 fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", n.IP(), n.TCP())) 72 if err != nil { 73 return err 74 } 75 conn := rlpx.NewConn(fd, n.Pubkey()) 76 ourKey, _ := crypto.GenerateKey() 77 _, err = conn.Handshake(ourKey) 78 if err != nil { 79 return err 80 } 81 code, data, _, err := conn.Read() 82 if err != nil { 83 return err 84 } 85 switch code { 86 case 0: 87 var h ethtest.Hello 88 if err := rlp.DecodeBytes(data, &h); err != nil { 89 return fmt.Errorf("invalid handshake: %v", err) 90 } 91 fmt.Printf("%+v\n", h) 92 case 1: 93 var msg []p2p.DiscReason 94 if rlp.DecodeBytes(data, &msg); len(msg) == 0 { 95 return fmt.Errorf("invalid disconnect message") 96 } 97 return fmt.Errorf("received disconnect message: %v", msg[0]) 98 default: 99 return fmt.Errorf("invalid message code %d, expected handshake (code zero)", code) 100 } 101 return nil 102 } 103 104 // rlpxEthTest runs the eth protocol test suite. 105 func rlpxEthTest(ctx *cli.Context) error { 106 if ctx.NArg() < 3 { 107 exit("missing path to chain.rlp as command-line argument") 108 } 109 suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args()[1], ctx.Args()[2]) 110 if err != nil { 111 exit(err) 112 } 113 // check if given node supports eth66, and if so, run eth66 protocol tests as well 114 is66Failed, _ := utesting.Run(utesting.Test{Name: "Is_66", Fn: suite.Is_66}) 115 if is66Failed { 116 return runTests(ctx, suite.EthTests()) 117 } 118 return runTests(ctx, suite.AllEthTests()) 119 } 120 121 // rlpxSnapTest runs the snap protocol test suite. 122 func rlpxSnapTest(ctx *cli.Context) error { 123 if ctx.NArg() < 3 { 124 exit("missing path to chain.rlp as command-line argument") 125 } 126 suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args()[1], ctx.Args()[2]) 127 if err != nil { 128 exit(err) 129 } 130 return runTests(ctx, suite.SnapTests()) 131 }