github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/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/tirogen/go-ethereum/cmd/devp2p/internal/v5test" 24 "github.com/tirogen/go-ethereum/common" 25 "github.com/tirogen/go-ethereum/internal/flags" 26 "github.com/tirogen/go-ethereum/p2p/discover" 27 "github.com/urfave/cli/v2" 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 Flags: discoveryNodeFlags, 47 } 48 discv5ResolveCommand = &cli.Command{ 49 Name: "resolve", 50 Usage: "Finds a node in the DHT", 51 Action: discv5Resolve, 52 Flags: discoveryNodeFlags, 53 } 54 discv5CrawlCommand = &cli.Command{ 55 Name: "crawl", 56 Usage: "Updates a nodes.json file with random nodes found in the DHT", 57 Action: discv5Crawl, 58 Flags: flags.Merge(discoveryNodeFlags, []cli.Flag{ 59 crawlTimeoutFlag, 60 }), 61 } 62 discv5TestCommand = &cli.Command{ 63 Name: "test", 64 Usage: "Runs protocol tests against a node", 65 Action: discv5Test, 66 Flags: []cli.Flag{ 67 testPatternFlag, 68 testTAPFlag, 69 testListen1Flag, 70 testListen2Flag, 71 }, 72 } 73 discv5ListenCommand = &cli.Command{ 74 Name: "listen", 75 Usage: "Runs a node", 76 Action: discv5Listen, 77 Flags: discoveryNodeFlags, 78 } 79 ) 80 81 func discv5Ping(ctx *cli.Context) error { 82 n := getNodeArg(ctx) 83 disc := startV5(ctx) 84 defer disc.Close() 85 86 fmt.Println(disc.Ping(n)) 87 return nil 88 } 89 90 func discv5Resolve(ctx *cli.Context) error { 91 n := getNodeArg(ctx) 92 disc := startV5(ctx) 93 defer disc.Close() 94 95 fmt.Println(disc.Resolve(n)) 96 return nil 97 } 98 99 func discv5Crawl(ctx *cli.Context) error { 100 if ctx.NArg() < 1 { 101 return fmt.Errorf("need nodes file as argument") 102 } 103 nodesFile := ctx.Args().First() 104 var inputSet nodeSet 105 if common.FileExist(nodesFile) { 106 inputSet = loadNodesJSON(nodesFile) 107 } 108 109 disc := startV5(ctx) 110 defer disc.Close() 111 c := newCrawler(inputSet, disc, disc.RandomNodes()) 112 c.revalidateInterval = 10 * time.Minute 113 output := c.run(ctx.Duration(crawlTimeoutFlag.Name)) 114 writeNodesJSON(nodesFile, output) 115 return nil 116 } 117 118 // discv5Test runs the protocol test suite. 119 func discv5Test(ctx *cli.Context) error { 120 suite := &v5test.Suite{ 121 Dest: getNodeArg(ctx), 122 Listen1: ctx.String(testListen1Flag.Name), 123 Listen2: ctx.String(testListen2Flag.Name), 124 } 125 return runTests(ctx, suite.AllTests()) 126 } 127 128 func discv5Listen(ctx *cli.Context) error { 129 disc := startV5(ctx) 130 defer disc.Close() 131 132 fmt.Println(disc.Self()) 133 select {} 134 } 135 136 // startV5 starts an ephemeral discovery v5 node. 137 func startV5(ctx *cli.Context) *discover.UDPv5 { 138 ln, config := makeDiscoveryConfig(ctx) 139 socket := listen(ctx, ln) 140 disc, err := discover.ListenV5(socket, ln, config) 141 if err != nil { 142 exit(err) 143 } 144 return disc 145 }