github.com/theQRL/go-zond@v0.2.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/crypto"
    25  	"github.com/theQRL/go-zond/p2p"
    26  	"github.com/theQRL/go-zond/p2p/rlpx"
    27  	"github.com/theQRL/go-zond/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  			rlpxZondTestCommand,
    38  			rlpxSnapTestCommand,
    39  		},
    40  	}
    41  	rlpxPingCommand = &cli.Command{
    42  		Name:   "ping",
    43  		Usage:  "ping <node>",
    44  		Action: rlpxPing,
    45  	}
    46  	rlpxZondTestCommand = &cli.Command{
    47  		Name:      "zond-test",
    48  		Usage:     "Runs tests against a node",
    49  		ArgsUsage: "<node> <chain.rlp> <genesis.json>",
    50  		Action:    rlpxZondTest,
    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  		// TODO(now.youtrack.cloud/issue/TGZ-6)
    87  		// var h zondtest.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  // rlpxZondTest runs the zond protocol test suite.
   105  func rlpxZondTest(ctx *cli.Context) error {
   106  	if ctx.NArg() < 3 {
   107  		exit("missing path to chain.rlp as command-line argument")
   108  	}
   109  	// TODO(now.youtrack.cloud/issue/TGZ-6)
   110  	// suite, err := zondtest.NewSuite(getNodeArg(ctx), ctx.Args().Get(1), ctx.Args().Get(2))
   111  	// if err != nil {
   112  	// 	exit(err)
   113  	// }
   114  	// return runTests(ctx, suite.ZondTests())
   115  	return nil
   116  }
   117  
   118  // rlpxSnapTest runs the snap protocol test suite.
   119  func rlpxSnapTest(ctx *cli.Context) error {
   120  	if ctx.NArg() < 3 {
   121  		exit("missing path to chain.rlp as command-line argument")
   122  	}
   123  	// TODO(now.youtrack.cloud/issue/TGZ-6)
   124  	// suite, err := zondtest.NewSuite(getNodeArg(ctx), ctx.Args().Get(1), ctx.Args().Get(2))
   125  	// if err != nil {
   126  	// 	exit(err)
   127  	// }
   128  	// return runTests(ctx, suite.SnapTests())
   129  	return nil
   130  }