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