github.com/gilgames000/kcc-geth@v1.0.6/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/p2p" 26 "github.com/ethereum/go-ethereum/p2p/rlpx" 27 "github.com/ethereum/go-ethereum/rlp" 28 "gopkg.in/urfave/cli.v1" 29 ) 30 31 var ( 32 rlpxCommand = cli.Command{ 33 Name: "rlpx", 34 Usage: "RLPx Commands", 35 Subcommands: []cli.Command{ 36 rlpxPingCommand, 37 rlpxEthTestCommand, 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 ) 56 57 func rlpxPing(ctx *cli.Context) error { 58 n := getNodeArg(ctx) 59 fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", n.IP(), n.TCP())) 60 if err != nil { 61 return err 62 } 63 conn := rlpx.NewConn(fd, n.Pubkey()) 64 ourKey, _ := crypto.GenerateKey() 65 _, err = conn.Handshake(ourKey) 66 if err != nil { 67 return err 68 } 69 code, data, _, err := conn.Read() 70 if err != nil { 71 return err 72 } 73 switch code { 74 case 0: 75 var h ethtest.Hello 76 if err := rlp.DecodeBytes(data, &h); err != nil { 77 return fmt.Errorf("invalid handshake: %v", err) 78 } 79 fmt.Printf("%+v\n", h) 80 case 1: 81 var msg []p2p.DiscReason 82 if rlp.DecodeBytes(data, &msg); len(msg) == 0 { 83 return fmt.Errorf("invalid disconnect message") 84 } 85 return fmt.Errorf("received disconnect message: %v", msg[0]) 86 default: 87 return fmt.Errorf("invalid message code %d, expected handshake (code zero)", code) 88 } 89 return nil 90 } 91 92 // rlpxEthTest runs the eth protocol test suite. 93 func rlpxEthTest(ctx *cli.Context) error { 94 if ctx.NArg() < 3 { 95 exit("missing path to chain.rlp as command-line argument") 96 } 97 suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args()[1], ctx.Args()[2]) 98 if err != nil { 99 exit(err) 100 } 101 return runTests(ctx, suite.EthTests()) 102 }