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