github.com/phillinzzz/newBsc@v1.1.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/phillinzzz/newBsc/cmd/devp2p/internal/ethtest"
    24  	"github.com/phillinzzz/newBsc/crypto"
    25  	"github.com/phillinzzz/newBsc/internal/utesting"
    26  	"github.com/phillinzzz/newBsc/p2p"
    27  	"github.com/phillinzzz/newBsc/p2p/rlpx"
    28  	"github.com/phillinzzz/newBsc/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  		},
    40  	}
    41  	rlpxPingCommand = cli.Command{
    42  		Name:   "ping",
    43  		Usage:  "ping <node>",
    44  		Action: rlpxPing,
    45  	}
    46  	rlpxEthTestCommand = cli.Command{
    47  		Name:      "eth-test",
    48  		Usage:     "Runs tests against a node",
    49  		ArgsUsage: "<node> <chain.rlp> <genesis.json>",
    50  		Action:    rlpxEthTest,
    51  		Flags: []cli.Flag{
    52  			testPatternFlag,
    53  			testTAPFlag,
    54  		},
    55  	}
    56  )
    57  
    58  func rlpxPing(ctx *cli.Context) error {
    59  	n := getNodeArg(ctx)
    60  	fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", n.IP(), n.TCP()))
    61  	if err != nil {
    62  		return err
    63  	}
    64  	conn := rlpx.NewConn(fd, n.Pubkey())
    65  	ourKey, _ := crypto.GenerateKey()
    66  	_, err = conn.Handshake(ourKey)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	code, data, _, err := conn.Read()
    71  	if err != nil {
    72  		return err
    73  	}
    74  	switch code {
    75  	case 0:
    76  		var h ethtest.Hello
    77  		if err := rlp.DecodeBytes(data, &h); err != nil {
    78  			return fmt.Errorf("invalid handshake: %v", err)
    79  		}
    80  		fmt.Printf("%+v\n", h)
    81  	case 1:
    82  		var msg []p2p.DiscReason
    83  		if rlp.DecodeBytes(data, &msg); len(msg) == 0 {
    84  			return fmt.Errorf("invalid disconnect message")
    85  		}
    86  		return fmt.Errorf("received disconnect message: %v", msg[0])
    87  	default:
    88  		return fmt.Errorf("invalid message code %d, expected handshake (code zero)", code)
    89  	}
    90  	return nil
    91  }
    92  
    93  // rlpxEthTest runs the eth protocol test suite.
    94  func rlpxEthTest(ctx *cli.Context) error {
    95  	if ctx.NArg() < 3 {
    96  		exit("missing path to chain.rlp as command-line argument")
    97  	}
    98  	suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args()[1], ctx.Args()[2])
    99  	if err != nil {
   100  		exit(err)
   101  	}
   102  	// check if given node supports eth66, and if so, run eth66 protocol tests as well
   103  	is66Failed, _ := utesting.Run(utesting.Test{Name: "Is_66", Fn: suite.Is_66})
   104  	if is66Failed {
   105  		return runTests(ctx, suite.EthTests())
   106  	}
   107  	return runTests(ctx, suite.AllEthTests())
   108  }