github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/cmd/p2psim/main.go (about) 1 // Copyright 2017 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 // p2psim provides a command-line client for a simulation HTTP API. 18 // 19 // Here is an example of creating a 2 node network with the first node 20 // connected to the second: 21 // 22 // $ p2psim node create 23 // Created node01 24 // 25 // $ p2psim node start node01 26 // Started node01 27 // 28 // $ p2psim node create 29 // Created node02 30 // 31 // $ p2psim node start node02 32 // Started node02 33 // 34 // $ p2psim node connect node01 node02 35 // Connected node01 to node02 36 package main 37 38 import ( 39 "context" 40 "encoding/json" 41 "fmt" 42 "io" 43 "os" 44 "strings" 45 "text/tabwriter" 46 47 "github.com/electroneum/electroneum-sc/crypto" 48 "github.com/electroneum/electroneum-sc/p2p" 49 "github.com/electroneum/electroneum-sc/p2p/enode" 50 "github.com/electroneum/electroneum-sc/p2p/simulations" 51 "github.com/electroneum/electroneum-sc/p2p/simulations/adapters" 52 "github.com/electroneum/electroneum-sc/rpc" 53 "gopkg.in/urfave/cli.v1" 54 ) 55 56 var client *simulations.Client 57 58 var ( 59 // global command flags 60 apiFlag = cli.StringFlag{ 61 Name: "api", 62 Value: "http://localhost:8888", 63 Usage: "simulation API URL", 64 EnvVar: "P2PSIM_API_URL", 65 } 66 67 // events subcommand flags 68 currentFlag = cli.BoolFlag{ 69 Name: "current", 70 Usage: "get existing nodes and conns first", 71 } 72 filterFlag = cli.StringFlag{ 73 Name: "filter", 74 Value: "", 75 Usage: "message filter", 76 } 77 78 // node create subcommand flags 79 nameFlag = cli.StringFlag{ 80 Name: "name", 81 Value: "", 82 Usage: "node name", 83 } 84 servicesFlag = cli.StringFlag{ 85 Name: "services", 86 Value: "", 87 Usage: "node services (comma separated)", 88 } 89 keyFlag = cli.StringFlag{ 90 Name: "key", 91 Value: "", 92 Usage: "node private key (hex encoded)", 93 } 94 95 // node rpc subcommand flags 96 subscribeFlag = cli.BoolFlag{ 97 Name: "subscribe", 98 Usage: "method is a subscription", 99 } 100 ) 101 102 func main() { 103 app := cli.NewApp() 104 app.Usage = "devp2p simulation command-line client" 105 app.Flags = []cli.Flag{ 106 apiFlag, 107 } 108 app.Before = func(ctx *cli.Context) error { 109 client = simulations.NewClient(ctx.GlobalString(apiFlag.Name)) 110 return nil 111 } 112 app.Commands = []cli.Command{ 113 { 114 Name: "show", 115 Usage: "show network information", 116 Action: showNetwork, 117 }, 118 { 119 Name: "events", 120 Usage: "stream network events", 121 Action: streamNetwork, 122 Flags: []cli.Flag{ 123 currentFlag, 124 filterFlag, 125 }, 126 }, 127 { 128 Name: "snapshot", 129 Usage: "create a network snapshot to stdout", 130 Action: createSnapshot, 131 }, 132 { 133 Name: "load", 134 Usage: "load a network snapshot from stdin", 135 Action: loadSnapshot, 136 }, 137 { 138 Name: "node", 139 Usage: "manage simulation nodes", 140 Action: listNodes, 141 Subcommands: []cli.Command{ 142 { 143 Name: "list", 144 Usage: "list nodes", 145 Action: listNodes, 146 }, 147 { 148 Name: "create", 149 Usage: "create a node", 150 Action: createNode, 151 Flags: []cli.Flag{ 152 nameFlag, 153 servicesFlag, 154 keyFlag, 155 }, 156 }, 157 { 158 Name: "show", 159 ArgsUsage: "<node>", 160 Usage: "show node information", 161 Action: showNode, 162 }, 163 { 164 Name: "start", 165 ArgsUsage: "<node>", 166 Usage: "start a node", 167 Action: startNode, 168 }, 169 { 170 Name: "stop", 171 ArgsUsage: "<node>", 172 Usage: "stop a node", 173 Action: stopNode, 174 }, 175 { 176 Name: "connect", 177 ArgsUsage: "<node> <peer>", 178 Usage: "connect a node to a peer node", 179 Action: connectNode, 180 }, 181 { 182 Name: "disconnect", 183 ArgsUsage: "<node> <peer>", 184 Usage: "disconnect a node from a peer node", 185 Action: disconnectNode, 186 }, 187 { 188 Name: "rpc", 189 ArgsUsage: "<node> <method> [<args>]", 190 Usage: "call a node RPC method", 191 Action: rpcNode, 192 Flags: []cli.Flag{ 193 subscribeFlag, 194 }, 195 }, 196 }, 197 }, 198 } 199 if err := app.Run(os.Args); err != nil { 200 fmt.Fprintln(os.Stderr, err) 201 os.Exit(1) 202 } 203 } 204 205 func showNetwork(ctx *cli.Context) error { 206 if len(ctx.Args()) != 0 { 207 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 208 } 209 network, err := client.GetNetwork() 210 if err != nil { 211 return err 212 } 213 w := tabwriter.NewWriter(ctx.App.Writer, 1, 2, 2, ' ', 0) 214 defer w.Flush() 215 fmt.Fprintf(w, "NODES\t%d\n", len(network.Nodes)) 216 fmt.Fprintf(w, "CONNS\t%d\n", len(network.Conns)) 217 return nil 218 } 219 220 func streamNetwork(ctx *cli.Context) error { 221 if len(ctx.Args()) != 0 { 222 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 223 } 224 events := make(chan *simulations.Event) 225 sub, err := client.SubscribeNetwork(events, simulations.SubscribeOpts{ 226 Current: ctx.Bool(currentFlag.Name), 227 Filter: ctx.String(filterFlag.Name), 228 }) 229 if err != nil { 230 return err 231 } 232 defer sub.Unsubscribe() 233 enc := json.NewEncoder(ctx.App.Writer) 234 for { 235 select { 236 case event := <-events: 237 if err := enc.Encode(event); err != nil { 238 return err 239 } 240 case err := <-sub.Err(): 241 return err 242 } 243 } 244 } 245 246 func createSnapshot(ctx *cli.Context) error { 247 if len(ctx.Args()) != 0 { 248 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 249 } 250 snap, err := client.CreateSnapshot() 251 if err != nil { 252 return err 253 } 254 return json.NewEncoder(os.Stdout).Encode(snap) 255 } 256 257 func loadSnapshot(ctx *cli.Context) error { 258 if len(ctx.Args()) != 0 { 259 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 260 } 261 snap := &simulations.Snapshot{} 262 if err := json.NewDecoder(os.Stdin).Decode(snap); err != nil { 263 return err 264 } 265 return client.LoadSnapshot(snap) 266 } 267 268 func listNodes(ctx *cli.Context) error { 269 if len(ctx.Args()) != 0 { 270 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 271 } 272 nodes, err := client.GetNodes() 273 if err != nil { 274 return err 275 } 276 w := tabwriter.NewWriter(ctx.App.Writer, 1, 2, 2, ' ', 0) 277 defer w.Flush() 278 fmt.Fprintf(w, "NAME\tPROTOCOLS\tID\n") 279 for _, node := range nodes { 280 fmt.Fprintf(w, "%s\t%s\t%s\n", node.Name, strings.Join(protocolList(node), ","), node.ID) 281 } 282 return nil 283 } 284 285 func protocolList(node *p2p.NodeInfo) []string { 286 protos := make([]string, 0, len(node.Protocols)) 287 for name := range node.Protocols { 288 protos = append(protos, name) 289 } 290 return protos 291 } 292 293 func createNode(ctx *cli.Context) error { 294 if len(ctx.Args()) != 0 { 295 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 296 } 297 config := adapters.RandomNodeConfig() 298 config.Name = ctx.String(nameFlag.Name) 299 if key := ctx.String(keyFlag.Name); key != "" { 300 privKey, err := crypto.HexToECDSA(key) 301 if err != nil { 302 return err 303 } 304 config.ID = enode.PubkeyToIDV4(&privKey.PublicKey) 305 config.PrivateKey = privKey 306 } 307 if services := ctx.String(servicesFlag.Name); services != "" { 308 config.Lifecycles = strings.Split(services, ",") 309 } 310 node, err := client.CreateNode(config) 311 if err != nil { 312 return err 313 } 314 fmt.Fprintln(ctx.App.Writer, "Created", node.Name) 315 return nil 316 } 317 318 func showNode(ctx *cli.Context) error { 319 args := ctx.Args() 320 if len(args) != 1 { 321 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 322 } 323 nodeName := args[0] 324 node, err := client.GetNode(nodeName) 325 if err != nil { 326 return err 327 } 328 w := tabwriter.NewWriter(ctx.App.Writer, 1, 2, 2, ' ', 0) 329 defer w.Flush() 330 fmt.Fprintf(w, "NAME\t%s\n", node.Name) 331 fmt.Fprintf(w, "PROTOCOLS\t%s\n", strings.Join(protocolList(node), ",")) 332 fmt.Fprintf(w, "ID\t%s\n", node.ID) 333 fmt.Fprintf(w, "ENODE\t%s\n", node.Enode) 334 for name, proto := range node.Protocols { 335 fmt.Fprintln(w) 336 fmt.Fprintf(w, "--- PROTOCOL INFO: %s\n", name) 337 fmt.Fprintf(w, "%v\n", proto) 338 fmt.Fprintf(w, "---\n") 339 } 340 return nil 341 } 342 343 func startNode(ctx *cli.Context) error { 344 args := ctx.Args() 345 if len(args) != 1 { 346 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 347 } 348 nodeName := args[0] 349 if err := client.StartNode(nodeName); err != nil { 350 return err 351 } 352 fmt.Fprintln(ctx.App.Writer, "Started", nodeName) 353 return nil 354 } 355 356 func stopNode(ctx *cli.Context) error { 357 args := ctx.Args() 358 if len(args) != 1 { 359 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 360 } 361 nodeName := args[0] 362 if err := client.StopNode(nodeName); err != nil { 363 return err 364 } 365 fmt.Fprintln(ctx.App.Writer, "Stopped", nodeName) 366 return nil 367 } 368 369 func connectNode(ctx *cli.Context) error { 370 args := ctx.Args() 371 if len(args) != 2 { 372 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 373 } 374 nodeName := args[0] 375 peerName := args[1] 376 if err := client.ConnectNode(nodeName, peerName); err != nil { 377 return err 378 } 379 fmt.Fprintln(ctx.App.Writer, "Connected", nodeName, "to", peerName) 380 return nil 381 } 382 383 func disconnectNode(ctx *cli.Context) error { 384 args := ctx.Args() 385 if len(args) != 2 { 386 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 387 } 388 nodeName := args[0] 389 peerName := args[1] 390 if err := client.DisconnectNode(nodeName, peerName); err != nil { 391 return err 392 } 393 fmt.Fprintln(ctx.App.Writer, "Disconnected", nodeName, "from", peerName) 394 return nil 395 } 396 397 func rpcNode(ctx *cli.Context) error { 398 args := ctx.Args() 399 if len(args) < 2 { 400 return cli.ShowCommandHelp(ctx, ctx.Command.Name) 401 } 402 nodeName := args[0] 403 method := args[1] 404 rpcClient, err := client.RPCClient(context.Background(), nodeName) 405 if err != nil { 406 return err 407 } 408 if ctx.Bool(subscribeFlag.Name) { 409 return rpcSubscribe(rpcClient, ctx.App.Writer, method, args[3:]...) 410 } 411 var result interface{} 412 params := make([]interface{}, len(args[3:])) 413 for i, v := range args[3:] { 414 params[i] = v 415 } 416 if err := rpcClient.Call(&result, method, params...); err != nil { 417 return err 418 } 419 return json.NewEncoder(ctx.App.Writer).Encode(result) 420 } 421 422 func rpcSubscribe(client *rpc.Client, out io.Writer, method string, args ...string) error { 423 parts := strings.SplitN(method, "_", 2) 424 namespace := parts[0] 425 method = parts[1] 426 ch := make(chan interface{}) 427 subArgs := make([]interface{}, len(args)+1) 428 subArgs[0] = method 429 for i, v := range args { 430 subArgs[i+1] = v 431 } 432 sub, err := client.Subscribe(context.Background(), namespace, ch, subArgs...) 433 if err != nil { 434 return err 435 } 436 defer sub.Unsubscribe() 437 enc := json.NewEncoder(out) 438 for { 439 select { 440 case v := <-ch: 441 if err := enc.Encode(v); err != nil { 442 return err 443 } 444 case err := <-sub.Err(): 445 return err 446 } 447 } 448 }