github.com/theQRL/go-zond@v0.1.1/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/theQRL/go-zond/cmd/devp2p/internal/v5test"
    25  	"github.com/theQRL/go-zond/common"
    26  	"github.com/theQRL/go-zond/internal/flags"
    27  	"github.com/theQRL/go-zond/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  	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), ctx.Int(crawlParallelismFlag.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(ctx, ln)
   141  	disc, err := discover.ListenV5(socket, ln, config)
   142  	if err != nil {
   143  		exit(err)
   144  	}
   145  	return disc
   146  }