OSDN Git Service

3e8993b1fb4a7d8dff6be150ae0f2f2ca2574a97
[bytom/vapor.git] / cmd / vapord / 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 vapord",
18         RunE:  runNode,
19 }
20
21 func init() {
22         runNodeCmd.Flags().String("prof_laddr", config.ProfListenAddress, "Use http to profile vapord 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().Bool("p2p.skip_upnp", config.P2P.SkipUPNP, "Skip UPNP configuration")
41         runNodeCmd.Flags().Bool("p2p.lan_discoverable", config.P2P.LANDiscover, "Whether the node can be discovered by nodes in the LAN")
42         runNodeCmd.Flags().Int("p2p.max_num_peers", config.P2P.MaxNumPeers, "Set max num peers")
43         runNodeCmd.Flags().Int("p2p.handshake_timeout", config.P2P.HandshakeTimeout, "Set handshake timeout")
44         runNodeCmd.Flags().Int("p2p.dial_timeout", config.P2P.DialTimeout, "Set dial timeout")
45         runNodeCmd.Flags().String("p2p.proxy_address", config.P2P.ProxyAddress, "Connect via SOCKS5 proxy (eg. 127.0.0.1:1086)")
46         runNodeCmd.Flags().String("p2p.proxy_username", config.P2P.ProxyUsername, "Username for proxy server")
47         runNodeCmd.Flags().String("p2p.proxy_password", config.P2P.ProxyPassword, "Password for proxy server")
48         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\")")
49
50         // log flags
51         runNodeCmd.Flags().String("log_file", config.LogFile, "Log output file")
52
53         // websocket flags
54         runNodeCmd.Flags().Int("ws.max_num_websockets", config.Websocket.MaxNumWebsockets, "Max number of websocket connections")
55         runNodeCmd.Flags().Int("ws.max_num_concurrent_reqs", config.Websocket.MaxNumConcurrentReqs, "Max number of concurrent websocket requests that may be processed concurrently")
56
57         RootCmd.AddCommand(runNodeCmd)
58 }
59
60 func setLogLevel(level string) {
61         switch strings.ToLower(level) {
62         case "debug":
63                 log.SetLevel(log.DebugLevel)
64         case "info":
65                 log.SetLevel(log.InfoLevel)
66         case "warn":
67                 log.SetLevel(log.WarnLevel)
68         case "error":
69                 log.SetLevel(log.ErrorLevel)
70         case "fatal":
71                 log.SetLevel(log.FatalLevel)
72         default:
73                 log.SetLevel(log.InfoLevel)
74         }
75 }
76
77 func runNode(cmd *cobra.Command, args []string) error {
78         startTime := time.Now()
79         setLogLevel(config.LogLevel)
80
81         // Create & start node
82         n := node.NewNode(config)
83         if _, err := n.Start(); err != nil {
84                 log.WithFields(log.Fields{"module": logModule, "err": err}).Fatal("failed to start node")
85         }
86
87         log.WithFields(log.Fields{
88                 "module":   logModule,
89                 "duration": time.Since(startTime),
90         }).Info("start node complete")
91
92         // Trap signal, run forever.
93         n.RunForever()
94         return nil
95 }