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