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