github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/cmd/devp2p/discv4cmd.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 "net" 22 "strings" 23 "time" 24 25 "gopkg.in/urfave/cli.v1" 26 27 "github.com/scroll-tech/go-ethereum/cmd/devp2p/internal/v4test" 28 "github.com/scroll-tech/go-ethereum/common" 29 "github.com/scroll-tech/go-ethereum/crypto" 30 "github.com/scroll-tech/go-ethereum/p2p/discover" 31 "github.com/scroll-tech/go-ethereum/p2p/enode" 32 "github.com/scroll-tech/go-ethereum/params" 33 ) 34 35 var ( 36 discv4Command = cli.Command{ 37 Name: "discv4", 38 Usage: "Node Discovery v4 tools", 39 Subcommands: []cli.Command{ 40 discv4PingCommand, 41 discv4RequestRecordCommand, 42 discv4ResolveCommand, 43 discv4ResolveJSONCommand, 44 discv4CrawlCommand, 45 discv4TestCommand, 46 }, 47 } 48 discv4PingCommand = cli.Command{ 49 Name: "ping", 50 Usage: "Sends ping to a node", 51 Action: discv4Ping, 52 ArgsUsage: "<node>", 53 } 54 discv4RequestRecordCommand = cli.Command{ 55 Name: "requestenr", 56 Usage: "Requests a node record using EIP-868 enrRequest", 57 Action: discv4RequestRecord, 58 ArgsUsage: "<node>", 59 } 60 discv4ResolveCommand = cli.Command{ 61 Name: "resolve", 62 Usage: "Finds a node in the DHT", 63 Action: discv4Resolve, 64 ArgsUsage: "<node>", 65 Flags: []cli.Flag{bootnodesFlag}, 66 } 67 discv4ResolveJSONCommand = cli.Command{ 68 Name: "resolve-json", 69 Usage: "Re-resolves nodes in a nodes.json file", 70 Action: discv4ResolveJSON, 71 Flags: []cli.Flag{bootnodesFlag}, 72 ArgsUsage: "<nodes.json file>", 73 } 74 discv4CrawlCommand = cli.Command{ 75 Name: "crawl", 76 Usage: "Updates a nodes.json file with random nodes found in the DHT", 77 Action: discv4Crawl, 78 Flags: []cli.Flag{bootnodesFlag, crawlTimeoutFlag}, 79 } 80 discv4TestCommand = cli.Command{ 81 Name: "test", 82 Usage: "Runs tests against a node", 83 Action: discv4Test, 84 Flags: []cli.Flag{ 85 remoteEnodeFlag, 86 testPatternFlag, 87 testTAPFlag, 88 testListen1Flag, 89 testListen2Flag, 90 }, 91 } 92 ) 93 94 var ( 95 bootnodesFlag = cli.StringFlag{ 96 Name: "bootnodes", 97 Usage: "Comma separated nodes used for bootstrapping", 98 } 99 nodekeyFlag = cli.StringFlag{ 100 Name: "nodekey", 101 Usage: "Hex-encoded node key", 102 } 103 nodedbFlag = cli.StringFlag{ 104 Name: "nodedb", 105 Usage: "Nodes database location", 106 } 107 listenAddrFlag = cli.StringFlag{ 108 Name: "addr", 109 Usage: "Listening address", 110 } 111 crawlTimeoutFlag = cli.DurationFlag{ 112 Name: "timeout", 113 Usage: "Time limit for the crawl.", 114 Value: 30 * time.Minute, 115 } 116 remoteEnodeFlag = cli.StringFlag{ 117 Name: "remote", 118 Usage: "Enode of the remote node under test", 119 EnvVar: "REMOTE_ENODE", 120 } 121 ) 122 123 func discv4Ping(ctx *cli.Context) error { 124 n := getNodeArg(ctx) 125 disc := startV4(ctx) 126 defer disc.Close() 127 128 start := time.Now() 129 if err := disc.Ping(n); err != nil { 130 return fmt.Errorf("node didn't respond: %v", err) 131 } 132 fmt.Printf("node responded to ping (RTT %v).\n", time.Since(start)) 133 return nil 134 } 135 136 func discv4RequestRecord(ctx *cli.Context) error { 137 n := getNodeArg(ctx) 138 disc := startV4(ctx) 139 defer disc.Close() 140 141 respN, err := disc.RequestENR(n) 142 if err != nil { 143 return fmt.Errorf("can't retrieve record: %v", err) 144 } 145 fmt.Println(respN.String()) 146 return nil 147 } 148 149 func discv4Resolve(ctx *cli.Context) error { 150 n := getNodeArg(ctx) 151 disc := startV4(ctx) 152 defer disc.Close() 153 154 fmt.Println(disc.Resolve(n).String()) 155 return nil 156 } 157 158 func discv4ResolveJSON(ctx *cli.Context) error { 159 if ctx.NArg() < 1 { 160 return fmt.Errorf("need nodes file as argument") 161 } 162 nodesFile := ctx.Args().Get(0) 163 inputSet := make(nodeSet) 164 if common.FileExist(nodesFile) { 165 inputSet = loadNodesJSON(nodesFile) 166 } 167 168 // Add extra nodes from command line arguments. 169 var nodeargs []*enode.Node 170 for i := 1; i < ctx.NArg(); i++ { 171 n, err := parseNode(ctx.Args().Get(i)) 172 if err != nil { 173 exit(err) 174 } 175 nodeargs = append(nodeargs, n) 176 } 177 178 // Run the crawler. 179 disc := startV4(ctx) 180 defer disc.Close() 181 c := newCrawler(inputSet, disc, enode.IterNodes(nodeargs)) 182 c.revalidateInterval = 0 183 output := c.run(0) 184 writeNodesJSON(nodesFile, output) 185 return nil 186 } 187 188 func discv4Crawl(ctx *cli.Context) error { 189 if ctx.NArg() < 1 { 190 return fmt.Errorf("need nodes file as argument") 191 } 192 nodesFile := ctx.Args().First() 193 var inputSet nodeSet 194 if common.FileExist(nodesFile) { 195 inputSet = loadNodesJSON(nodesFile) 196 } 197 198 disc := startV4(ctx) 199 defer disc.Close() 200 c := newCrawler(inputSet, disc, disc.RandomNodes()) 201 c.revalidateInterval = 10 * time.Minute 202 output := c.run(ctx.Duration(crawlTimeoutFlag.Name)) 203 writeNodesJSON(nodesFile, output) 204 return nil 205 } 206 207 // discv4Test runs the protocol test suite. 208 func discv4Test(ctx *cli.Context) error { 209 // Configure test package globals. 210 if !ctx.IsSet(remoteEnodeFlag.Name) { 211 return fmt.Errorf("Missing -%v", remoteEnodeFlag.Name) 212 } 213 v4test.Remote = ctx.String(remoteEnodeFlag.Name) 214 v4test.Listen1 = ctx.String(testListen1Flag.Name) 215 v4test.Listen2 = ctx.String(testListen2Flag.Name) 216 return runTests(ctx, v4test.AllTests) 217 } 218 219 // startV4 starts an ephemeral discovery V4 node. 220 func startV4(ctx *cli.Context) *discover.UDPv4 { 221 ln, config := makeDiscoveryConfig(ctx) 222 socket := listen(ln, ctx.String(listenAddrFlag.Name)) 223 disc, err := discover.ListenV4(socket, ln, config) 224 if err != nil { 225 exit(err) 226 } 227 return disc 228 } 229 230 func makeDiscoveryConfig(ctx *cli.Context) (*enode.LocalNode, discover.Config) { 231 var cfg discover.Config 232 233 if ctx.IsSet(nodekeyFlag.Name) { 234 key, err := crypto.HexToECDSA(ctx.String(nodekeyFlag.Name)) 235 if err != nil { 236 exit(fmt.Errorf("-%s: %v", nodekeyFlag.Name, err)) 237 } 238 cfg.PrivateKey = key 239 } else { 240 cfg.PrivateKey, _ = crypto.GenerateKey() 241 } 242 243 if commandHasFlag(ctx, bootnodesFlag) { 244 bn, err := parseBootnodes(ctx) 245 if err != nil { 246 exit(err) 247 } 248 cfg.Bootnodes = bn 249 } 250 251 dbpath := ctx.String(nodedbFlag.Name) 252 db, err := enode.OpenDB(dbpath) 253 if err != nil { 254 exit(err) 255 } 256 ln := enode.NewLocalNode(db, cfg.PrivateKey) 257 return ln, cfg 258 } 259 260 func listen(ln *enode.LocalNode, addr string) *net.UDPConn { 261 if addr == "" { 262 addr = "0.0.0.0:0" 263 } 264 socket, err := net.ListenPacket("udp4", addr) 265 if err != nil { 266 exit(err) 267 } 268 usocket := socket.(*net.UDPConn) 269 uaddr := socket.LocalAddr().(*net.UDPAddr) 270 if uaddr.IP.IsUnspecified() { 271 ln.SetFallbackIP(net.IP{127, 0, 0, 1}) 272 } else { 273 ln.SetFallbackIP(uaddr.IP) 274 } 275 ln.SetFallbackUDP(uaddr.Port) 276 return usocket 277 } 278 279 func parseBootnodes(ctx *cli.Context) ([]*enode.Node, error) { 280 s := params.RinkebyBootnodes 281 if ctx.IsSet(bootnodesFlag.Name) { 282 input := ctx.String(bootnodesFlag.Name) 283 if input == "" { 284 return nil, nil 285 } 286 s = strings.Split(input, ",") 287 } 288 nodes := make([]*enode.Node, len(s)) 289 var err error 290 for i, record := range s { 291 nodes[i], err = parseNode(record) 292 if err != nil { 293 return nil, fmt.Errorf("invalid bootstrap node: %v", err) 294 } 295 } 296 return nodes, nil 297 }