github.com/theQRL/go-zond@v0.1.1/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  	"errors"
    21  	"fmt"
    22  	"net"
    23  
    24  	"github.com/theQRL/go-zond/cmd/devp2p/internal/ethtest"
    25  	"github.com/theQRL/go-zond/crypto"
    26  	"github.com/theQRL/go-zond/p2p"
    27  	"github.com/theQRL/go-zond/p2p/rlpx"
    28  	"github.com/theQRL/go-zond/rlp"
    29  	"github.com/urfave/cli/v2"
    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:      "zond-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 errors.New("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 zond 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().Get(1), ctx.Args().Get(2))
   110  	if err != nil {
   111  		exit(err)
   112  	}
   113  	return runTests(ctx, suite.EthTests())
   114  }
   115  
   116  // rlpxSnapTest runs the snap protocol test suite.
   117  func rlpxSnapTest(ctx *cli.Context) error {
   118  	if ctx.NArg() < 3 {
   119  		exit("missing path to chain.rlp as command-line argument")
   120  	}
   121  	suite, err := ethtest.NewSuite(getNodeArg(ctx), ctx.Args().Get(1), ctx.Args().Get(2))
   122  	if err != nil {
   123  		exit(err)
   124  	}
   125  	return runTests(ctx, suite.SnapTests())
   126  }