OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / cmd / vapord / commands / run_node.go
1 package commands
2
3 import (
4         "fmt"
5         "strings"
6         "time"
7
8         log "github.com/sirupsen/logrus"
9         "github.com/spf13/cobra"
10
11         "github.com/bytom/vapor/node"
12         "github.com/bytom/vapor/toolbar/osssync/download"
13 )
14
15 const logModule = "cmd"
16
17 var runNodeCmd = &cobra.Command{
18         Use:   "node",
19         Short: "Run the vapord",
20         RunE:  runNode,
21 }
22
23 func init() {
24         runNodeCmd.Flags().String("prof_laddr", config.ProfListenAddress, "Use http to profile vapord programs")
25         runNodeCmd.Flags().Bool("mining", config.Mining, "Enable mining")
26         runNodeCmd.Flags().String("cross_chain.asset_whitelist", config.CrossChain.AssetWhitelist, "Cross-chain-allowed asset whitelist")
27
28         runNodeCmd.Flags().Bool("auth.disable", config.Auth.Disable, "Disable rpc access authenticate")
29
30         runNodeCmd.Flags().Bool("wallet.disable", config.Wallet.Disable, "Disable wallet")
31         runNodeCmd.Flags().Bool("wallet.rescan", config.Wallet.Rescan, "Rescan wallet")
32         runNodeCmd.Flags().Bool("wallet.txindex", config.Wallet.TxIndex, "Save global tx index")
33         runNodeCmd.Flags().Bool("vault_mode", config.VaultMode, "Run in the offline enviroment")
34         runNodeCmd.Flags().Bool("web.closed", config.Web.Closed, "Lanch web browser or not")
35         runNodeCmd.Flags().String("chain_id", config.ChainID, "Select network type")
36
37         // log level
38         runNodeCmd.Flags().String("log_level", config.LogLevel, "Select log level(debug, info, warn, error or fatal)")
39
40         // p2p flags
41         runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)")
42         runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes")
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         // OSS
61         runNodeCmd.Flags().String("oss.url", config.Oss.Url, "URL of OSS")
62
63         RootCmd.AddCommand(runNodeCmd)
64 }
65
66 func setLogLevel(level string) {
67         switch strings.ToLower(level) {
68         case "debug":
69                 log.SetLevel(log.DebugLevel)
70         case "info":
71                 log.SetLevel(log.InfoLevel)
72         case "warn":
73                 log.SetLevel(log.WarnLevel)
74         case "error":
75                 log.SetLevel(log.ErrorLevel)
76         case "fatal":
77                 log.SetLevel(log.FatalLevel)
78         default:
79                 log.SetLevel(log.InfoLevel)
80         }
81 }
82
83 func runNode(cmd *cobra.Command, args []string) error {
84         startTime := time.Now()
85         setLogLevel(config.LogLevel)
86
87         // Create & start node
88         n := node.NewNode(config)
89
90         // Get blocks from OSS
91         if err := download.Run(n, config.Oss.Url); err != nil {
92                 fmt.Println("Failed to get blocks from oss: ", err)
93         }
94
95         // Start node
96         if _, err := n.Start(); err != nil {
97                 log.WithFields(log.Fields{"module": logModule, "err": err}).Fatal("failed to start node")
98         }
99
100         log.WithFields(log.Fields{
101                 "module":   logModule,
102                 "duration": time.Since(startTime),
103         }).Info("start node complete")
104
105         // Trap signal, run forever.
106         n.RunForever()
107         return nil
108 }