OSDN Git Service

Nodeinfo handshake information modification (#68)
[bytom/vapor.git] / cmd / bytomd / commands / run_node.go
1 package commands
2
3 import (
4         "strings"
5         "time"
6
7         log "github.com/sirupsen/logrus"
8         "github.com/spf13/cobra"
9
10         "github.com/vapor/node"
11 )
12
13 const logModule = "cmd"
14
15 var runNodeCmd = &cobra.Command{
16         Use:   "node",
17         Short: "Run the bytomd",
18         RunE:  runNode,
19 }
20
21 func init() {
22         runNodeCmd.Flags().String("prof_laddr", config.ProfListenAddress, "Use http to profile bytomd programs")
23         runNodeCmd.Flags().Bool("mining", config.Mining, "Enable mining")
24
25         runNodeCmd.Flags().Bool("auth.disable", config.Auth.Disable, "Disable rpc access authenticate")
26
27         runNodeCmd.Flags().Bool("wallet.disable", config.Wallet.Disable, "Disable wallet")
28         runNodeCmd.Flags().Bool("wallet.rescan", config.Wallet.Rescan, "Rescan wallet")
29         runNodeCmd.Flags().Bool("wallet.txindex", config.Wallet.TxIndex, "Save global tx index")
30         runNodeCmd.Flags().Bool("vault_mode", config.VaultMode, "Run in the offline enviroment")
31         runNodeCmd.Flags().Bool("web.closed", config.Web.Closed, "Lanch web browser or not")
32         runNodeCmd.Flags().String("chain_id", config.ChainID, "Select network type")
33
34         // log level
35         runNodeCmd.Flags().String("log_level", config.LogLevel, "Select log level(debug, info, warn, error or fatal")
36
37         // p2p flags
38         runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)")
39         runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes")
40         runNodeCmd.Flags().String("p2p.node_key", config.P2P.PrivateKey, "Node key for p2p communication")
41         runNodeCmd.Flags().Bool("p2p.skip_upnp", config.P2P.SkipUPNP, "Skip UPNP configuration")
42         runNodeCmd.Flags().Bool("p2p.lan_discoverable", config.P2P.LANDiscover, "Whether the node can be discovered by nodes in the LAN")
43         runNodeCmd.Flags().Int("p2p.max_num_peers", config.P2P.MaxNumPeers, "Set max num peers")
44         runNodeCmd.Flags().Int("p2p.handshake_timeout", config.P2P.HandshakeTimeout, "Set handshake timeout")
45         runNodeCmd.Flags().Int("p2p.dial_timeout", config.P2P.DialTimeout, "Set dial timeout")
46         runNodeCmd.Flags().String("p2p.proxy_address", config.P2P.ProxyAddress, "Connect via SOCKS5 proxy (eg. 127.0.0.1:1086)")
47         runNodeCmd.Flags().String("p2p.proxy_username", config.P2P.ProxyUsername, "Username for proxy server")
48         runNodeCmd.Flags().String("p2p.proxy_password", config.P2P.ProxyPassword, "Password for proxy server")
49         runNodeCmd.Flags().String("p2p.keep_dial", config.P2P.KeepDial, "Peers addresses try keeping connecting to, separated by ',' (for example \"1.1.1.1:46657;2.2.2.2:46658\")")
50
51         // log flags
52         runNodeCmd.Flags().String("log_file", config.LogFile, "Log output file")
53
54         // websocket flags
55         runNodeCmd.Flags().Int("ws.max_num_websockets", config.Websocket.MaxNumWebsockets, "Max number of websocket connections")
56         runNodeCmd.Flags().Int("ws.max_num_concurrent_reqs", config.Websocket.MaxNumConcurrentReqs, "Max number of concurrent websocket requests that may be processed concurrently")
57
58         RootCmd.AddCommand(runNodeCmd)
59 }
60
61 func setLogLevel(level string) {
62         switch strings.ToLower(level) {
63         case "debug":
64                 log.SetLevel(log.DebugLevel)
65         case "info":
66                 log.SetLevel(log.InfoLevel)
67         case "warn":
68                 log.SetLevel(log.WarnLevel)
69         case "error":
70                 log.SetLevel(log.ErrorLevel)
71         case "fatal":
72                 log.SetLevel(log.FatalLevel)
73         default:
74                 log.SetLevel(log.InfoLevel)
75         }
76 }
77
78 func runNode(cmd *cobra.Command, args []string) error {
79         startTime := time.Now()
80         setLogLevel(config.LogLevel)
81
82         // Create & start node
83         n := node.NewNode(config)
84         if _, err := n.Start(); err != nil {
85                 log.WithFields(log.Fields{"module": logModule, "err": err}).Fatal("failed to start node")
86         }
87
88         log.WithFields(log.Fields{
89                 "module":   logModule,
90                 "duration": time.Since(startTime),
91         }).Info("start node complete")
92
93         // Trap signal, run forever.
94         n.RunForever()
95         return nil
96 }