github.com/JFJun/bsc@v1.0.0/cmd/devp2p/discv5cmd.go (about)

     1  // Copyright 2019 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/JFJun/bsc/common"
    24  	"github.com/JFJun/bsc/p2p/discover"
    25  	"gopkg.in/urfave/cli.v1"
    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  			discv5ListenCommand,
    37  		},
    38  	}
    39  	discv5PingCommand = cli.Command{
    40  		Name:   "ping",
    41  		Usage:  "Sends ping to a node",
    42  		Action: discv5Ping,
    43  	}
    44  	discv5ResolveCommand = cli.Command{
    45  		Name:   "resolve",
    46  		Usage:  "Finds a node in the DHT",
    47  		Action: discv5Resolve,
    48  		Flags:  []cli.Flag{bootnodesFlag},
    49  	}
    50  	discv5CrawlCommand = cli.Command{
    51  		Name:   "crawl",
    52  		Usage:  "Updates a nodes.json file with random nodes found in the DHT",
    53  		Action: discv5Crawl,
    54  		Flags:  []cli.Flag{bootnodesFlag, crawlTimeoutFlag},
    55  	}
    56  	discv5ListenCommand = cli.Command{
    57  		Name:   "listen",
    58  		Usage:  "Runs a node",
    59  		Action: discv5Listen,
    60  		Flags: []cli.Flag{
    61  			bootnodesFlag,
    62  			nodekeyFlag,
    63  			nodedbFlag,
    64  			listenAddrFlag,
    65  		},
    66  	}
    67  )
    68  
    69  func discv5Ping(ctx *cli.Context) error {
    70  	n := getNodeArg(ctx)
    71  	disc := startV5(ctx)
    72  	defer disc.Close()
    73  
    74  	fmt.Println(disc.Ping(n))
    75  	return nil
    76  }
    77  
    78  func discv5Resolve(ctx *cli.Context) error {
    79  	n := getNodeArg(ctx)
    80  	disc := startV5(ctx)
    81  	defer disc.Close()
    82  
    83  	fmt.Println(disc.Resolve(n))
    84  	return nil
    85  }
    86  
    87  func discv5Crawl(ctx *cli.Context) error {
    88  	if ctx.NArg() < 1 {
    89  		return fmt.Errorf("need nodes file as argument")
    90  	}
    91  	nodesFile := ctx.Args().First()
    92  	var inputSet nodeSet
    93  	if common.FileExist(nodesFile) {
    94  		inputSet = loadNodesJSON(nodesFile)
    95  	}
    96  
    97  	disc := startV5(ctx)
    98  	defer disc.Close()
    99  	c := newCrawler(inputSet, disc, disc.RandomNodes())
   100  	c.revalidateInterval = 10 * time.Minute
   101  	output := c.run(ctx.Duration(crawlTimeoutFlag.Name))
   102  	writeNodesJSON(nodesFile, output)
   103  	return nil
   104  }
   105  
   106  func discv5Listen(ctx *cli.Context) error {
   107  	disc := startV5(ctx)
   108  	defer disc.Close()
   109  
   110  	fmt.Println(disc.Self())
   111  	select {}
   112  }
   113  
   114  // startV5 starts an ephemeral discovery v5 node.
   115  func startV5(ctx *cli.Context) *discover.UDPv5 {
   116  	ln, config := makeDiscoveryConfig(ctx)
   117  	socket := listen(ln, ctx.String(listenAddrFlag.Name))
   118  	disc, err := discover.ListenV5(socket, ln, config)
   119  	if err != nil {
   120  		exit(err)
   121  	}
   122  	return disc
   123  }