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