OSDN Git Service

Mov (#518)
[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/bytom/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         runNodeCmd.Flags().String("cross_chain.asset_whitelist", config.CrossChain.AssetWhitelist, "Cross-chain-allowed asset whitelist")
25
26         runNodeCmd.Flags().Bool("auth.disable", config.Auth.Disable, "Disable rpc access authenticate")
27
28         runNodeCmd.Flags().Bool("wallet.disable", config.Wallet.Disable, "Disable wallet")
29         runNodeCmd.Flags().Bool("wallet.rescan", config.Wallet.Rescan, "Rescan wallet")
30         runNodeCmd.Flags().Bool("wallet.txindex", config.Wallet.TxIndex, "Save global tx index")
31         runNodeCmd.Flags().Bool("vault_mode", config.VaultMode, "Run in the offline enviroment")
32         runNodeCmd.Flags().Bool("web.closed", config.Web.Closed, "Lanch web browser or not")
33         runNodeCmd.Flags().String("chain_id", config.ChainID, "Select network type")
34
35         // log level
36         runNodeCmd.Flags().String("log_level", config.LogLevel, "Select log level(debug, info, warn, error or fatal)")
37
38         // p2p flags
39         runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)")
40         runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes")
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 }