github.com/core-coin/go-core/v2@v2.1.9/cmd/devp2p/discv5cmd.go (about)

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