github.com/ethereum/go-ethereum@v1.16.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 "slices" 23 "time" 24 25 "github.com/ethereum/go-ethereum/cmd/devp2p/internal/v5test" 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/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: slices.Concat(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 _, err := disc.Ping(n) 88 fmt.Println(err) 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 errors.New("need nodes file as argument") 104 } 105 nodesFile := ctx.Args().First() 106 inputSet := make(nodeSet) 107 if common.FileExist(nodesFile) { 108 inputSet = loadNodesJSON(nodesFile) 109 } 110 111 disc, config := startV5(ctx) 112 defer disc.Close() 113 114 c, err := newCrawler(inputSet, config.Bootnodes, disc, disc.RandomNodes()) 115 if err != nil { 116 return err 117 } 118 c.revalidateInterval = 10 * time.Minute 119 output := c.run(ctx.Duration(crawlTimeoutFlag.Name), ctx.Int(crawlParallelismFlag.Name)) 120 writeNodesJSON(nodesFile, output) 121 return nil 122 } 123 124 // discv5Test runs the protocol test suite. 125 func discv5Test(ctx *cli.Context) error { 126 suite := &v5test.Suite{ 127 Dest: getNodeArg(ctx), 128 Listen1: ctx.String(testListen1Flag.Name), 129 Listen2: ctx.String(testListen2Flag.Name), 130 } 131 return runTests(ctx, suite.AllTests()) 132 } 133 134 func discv5Listen(ctx *cli.Context) error { 135 disc, _ := startV5(ctx) 136 defer disc.Close() 137 138 fmt.Println(disc.Self()) 139 select {} 140 } 141 142 // startV5 starts an ephemeral discovery v5 node. 143 func startV5(ctx *cli.Context) (*discover.UDPv5, discover.Config) { 144 ln, config := makeDiscoveryConfig(ctx) 145 socket := listen(ctx, ln) 146 disc, err := discover.ListenV5(socket, ln, config) 147 if err != nil { 148 exit(err) 149 } 150 return disc, config 151 }