OSDN Git Service

Hulk did something
[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("simd.enable", config.Simd.Enable, "Enable SIMD mechan for tensority")
26
27         runNodeCmd.Flags().Bool("auth.disable", config.Auth.Disable, "Disable rpc access authenticate")
28
29         runNodeCmd.Flags().Bool("wallet.disable", config.Wallet.Disable, "Disable wallet")
30         runNodeCmd.Flags().Bool("wallet.rescan", config.Wallet.Rescan, "Rescan wallet")
31         runNodeCmd.Flags().Bool("wallet.txindex", config.Wallet.TxIndex, "Save global tx index")
32         runNodeCmd.Flags().Bool("vault_mode", config.VaultMode, "Run in the offline enviroment")
33         runNodeCmd.Flags().Bool("web.closed", config.Web.Closed, "Lanch web browser or not")
34         runNodeCmd.Flags().String("chain_id", config.ChainID, "Select network type")
35
36         // log level
37         runNodeCmd.Flags().String("log_level", config.LogLevel, "Select log level(debug, info, warn, error or fatal")
38
39         // p2p flags
40         runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)")
41         runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes")
42         runNodeCmd.Flags().String("p2p.node_key", config.P2P.PrivateKey, "Node key for p2p communication")
43         runNodeCmd.Flags().Bool("p2p.skip_upnp", config.P2P.SkipUPNP, "Skip UPNP configuration")
44         runNodeCmd.Flags().Bool("p2p.lan_discoverable", config.P2P.LANDiscover, "Whether the node can be discovered by nodes in the LAN")
45         runNodeCmd.Flags().Int("p2p.max_num_peers", config.P2P.MaxNumPeers, "Set max num peers")
46         runNodeCmd.Flags().Int("p2p.handshake_timeout", config.P2P.HandshakeTimeout, "Set handshake timeout")
47         runNodeCmd.Flags().Int("p2p.dial_timeout", config.P2P.DialTimeout, "Set dial timeout")
48         runNodeCmd.Flags().String("p2p.proxy_address", config.P2P.ProxyAddress, "Connect via SOCKS5 proxy (eg. 127.0.0.1:1086)")
49         runNodeCmd.Flags().String("p2p.proxy_username", config.P2P.ProxyUsername, "Username for proxy server")
50         runNodeCmd.Flags().String("p2p.proxy_password", config.P2P.ProxyPassword, "Password for proxy server")
51         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\")")
52
53         // log flags
54         runNodeCmd.Flags().String("log_file", config.LogFile, "Log output file")
55
56         // websocket flags
57         runNodeCmd.Flags().Int("ws.max_num_websockets", config.Websocket.MaxNumWebsockets, "Max number of websocket connections")
58         runNodeCmd.Flags().Int("ws.max_num_concurrent_reqs", config.Websocket.MaxNumConcurrentReqs, "Max number of concurrent websocket requests that may be processed concurrently")
59
60         RootCmd.AddCommand(runNodeCmd)
61 }
62
63 func setLogLevel(level string) {
64         switch strings.ToLower(level) {
65         case "debug":
66                 log.SetLevel(log.DebugLevel)
67         case "info":
68                 log.SetLevel(log.InfoLevel)
69         case "warn":
70                 log.SetLevel(log.WarnLevel)
71         case "error":
72                 log.SetLevel(log.ErrorLevel)
73         case "fatal":
74                 log.SetLevel(log.FatalLevel)
75         default:
76                 log.SetLevel(log.InfoLevel)
77         }
78 }
79
80 func runNode(cmd *cobra.Command, args []string) error {
81         startTime := time.Now()
82         setLogLevel(config.LogLevel)
83
84         // Create & start node
85         n := node.NewNode(config)
86         if _, err := n.Start(); err != nil {
87                 log.WithFields(log.Fields{"module": logModule, "err": err}).Fatal("failed to start node")
88         }
89
90         nodeInfo := n.NodeInfo()
91         log.WithFields(log.Fields{
92                 "module":   logModule,
93                 "version":  nodeInfo.Version,
94                 "network":  nodeInfo.Network,
95                 "duration": time.Since(startTime),
96         }).Info("start node complete")
97
98         // Trap signal, run forever.
99         n.RunForever()
100         return nil
101 }