github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/cmd/devp2p/discv5cmd.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  	"time"
    23  
    24  	"github.com/ethereum/go-ethereum/cmd/devp2p/internal/v5test"
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/internal/flags"
    27  	"github.com/ethereum/go-ethereum/p2p/discover"
    28  	"github.com/urfave/cli/v2"
    29  )
    30  
    31  var (
    32  	discv5Command = &cli.Command{
    33  		Name:  "discv5",
    34  		Usage: "Node Discovery v5 tools",
    35  		Subcommands: []*cli.Command{
    36  			discv5PingCommand,
    37  			discv5ResolveCommand,
    38  			discv5CrawlCommand,
    39  			discv5TestCommand,
    40  			discv5ListenCommand,
    41  		},
    42  	}
    43  	discv5PingCommand = &cli.Command{
    44  		Name:   "ping",
    45  		Usage:  "Sends ping to a node",
    46  		Action: discv5Ping,
    47  		Flags:  discoveryNodeFlags,
    48  	}
    49  	discv5ResolveCommand = &cli.Command{
    50  		Name:   "resolve",
    51  		Usage:  "Finds a node in the DHT",
    52  		Action: discv5Resolve,
    53  		Flags:  discoveryNodeFlags,
    54  	}
    55  	discv5CrawlCommand = &cli.Command{
    56  		Name:   "crawl",
    57  		Usage:  "Updates a nodes.json file with random nodes found in the DHT",
    58  		Action: discv5Crawl,
    59  		Flags: flags.Merge(discoveryNodeFlags, []cli.Flag{
    60  			crawlTimeoutFlag,
    61  		}),
    62  	}
    63  	discv5TestCommand = &cli.Command{
    64  		Name:   "test",
    65  		Usage:  "Runs protocol tests against a node",
    66  		Action: discv5Test,
    67  		Flags: []cli.Flag{
    68  			testPatternFlag,
    69  			testTAPFlag,
    70  			testListen1Flag,
    71  			testListen2Flag,
    72  		},
    73  	}
    74  	discv5ListenCommand = &cli.Command{
    75  		Name:   "listen",
    76  		Usage:  "Runs a node",
    77  		Action: discv5Listen,
    78  		Flags:  discoveryNodeFlags,
    79  	}
    80  )
    81  
    82  func discv5Ping(ctx *cli.Context) error {
    83  	n := getNodeArg(ctx)
    84  	disc, _ := startV5(ctx)
    85  	defer disc.Close()
    86  
    87  	fmt.Println(disc.Ping(n))
    88  	return nil
    89  }
    90  
    91  func discv5Resolve(ctx *cli.Context) error {
    92  	n := getNodeArg(ctx)
    93  	disc, _ := startV5(ctx)
    94  	defer disc.Close()
    95  
    96  	fmt.Println(disc.Resolve(n))
    97  	return nil
    98  }
    99  
   100  func discv5Crawl(ctx *cli.Context) error {
   101  	if ctx.NArg() < 1 {
   102  		return errors.New("need nodes file as argument")
   103  	}
   104  	nodesFile := ctx.Args().First()
   105  	inputSet := make(nodeSet)
   106  	if common.FileExist(nodesFile) {
   107  		inputSet = loadNodesJSON(nodesFile)
   108  	}
   109  
   110  	disc, config := startV5(ctx)
   111  	defer disc.Close()
   112  
   113  	c, err := newCrawler(inputSet, config.Bootnodes, disc, disc.RandomNodes())
   114  	if err != nil {
   115  		return err
   116  	}
   117  	c.revalidateInterval = 10 * time.Minute
   118  	output := c.run(ctx.Duration(crawlTimeoutFlag.Name), ctx.Int(crawlParallelismFlag.Name))
   119  	writeNodesJSON(nodesFile, output)
   120  	return nil
   121  }
   122  
   123  // discv5Test runs the protocol test suite.
   124  func discv5Test(ctx *cli.Context) error {
   125  	suite := &v5test.Suite{
   126  		Dest:    getNodeArg(ctx),
   127  		Listen1: ctx.String(testListen1Flag.Name),
   128  		Listen2: ctx.String(testListen2Flag.Name),
   129  	}
   130  	return runTests(ctx, suite.AllTests())
   131  }
   132  
   133  func discv5Listen(ctx *cli.Context) error {
   134  	disc, _ := startV5(ctx)
   135  	defer disc.Close()
   136  
   137  	fmt.Println(disc.Self())
   138  	select {}
   139  }
   140  
   141  // startV5 starts an ephemeral discovery v5 node.
   142  func startV5(ctx *cli.Context) (*discover.UDPv5, discover.Config) {
   143  	ln, config := makeDiscoveryConfig(ctx)
   144  	socket := listen(ctx, ln)
   145  	disc, err := discover.ListenV5(socket, ln, config)
   146  	if err != nil {
   147  		exit(err)
   148  	}
   149  	return disc, config
   150  }