OSDN Git Service

Merge pull request #309 from Bytom/dev
[bytom/bytom.git] / cmd / bytomd / commands / run_node.go
1 package commands
2
3 import (
4         "fmt"
5         "io/ioutil"
6
7         log "github.com/sirupsen/logrus"
8         "github.com/spf13/cobra"
9         cmn "github.com/tendermint/tmlibs/common"
10
11         "github.com/bytom/node"
12         "github.com/bytom/types"
13 )
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
29         runNodeCmd.Flags().Bool("web.closed", config.Web.Closed, "Lanch web browser or not")
30
31         // p2p flags
32         runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)")
33         runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes")
34         runNodeCmd.Flags().Bool("p2p.skip_upnp", config.P2P.SkipUPNP, "Skip UPNP configuration")
35         runNodeCmd.Flags().Bool("p2p.pex", config.P2P.PexReactor, "Enable Peer-Exchange ")
36         runNodeCmd.Flags().Int("p2p.max_num_peers", config.P2P.MaxNumPeers, "Set max num peers")
37         runNodeCmd.Flags().Int("p2p.handshake_timeout", config.P2P.HandshakeTimeout, "Set handshake timeout")
38         runNodeCmd.Flags().Int("p2p.dial_timeout", config.P2P.DialTimeout, "Set dial timeout")
39
40         RootCmd.AddCommand(runNodeCmd)
41 }
42
43 func runNode(cmd *cobra.Command, args []string) error {
44         genDocFile := config.GenesisFile()
45         if cmn.FileExists(genDocFile) {
46                 jsonBlob, err := ioutil.ReadFile(genDocFile)
47                 if err != nil {
48                         return fmt.Errorf("Couldn't read GenesisDoc file: %v ", err)
49                 }
50                 genDoc, err := types.GenesisDocFromJSON(jsonBlob)
51                 if err != nil {
52                         return fmt.Errorf("Error reading GenesisDoc: %v ", err)
53                 }
54                 if genDoc.ChainID == "" {
55                         return fmt.Errorf("Genesis doc %v must include non-empty chain_id ", genDocFile)
56                 }
57
58                 config.ChainID = genDoc.ChainID
59                 config.PrivateKey = genDoc.PrivateKey
60                 config.Time = genDoc.GenesisTime
61         } else {
62                 return fmt.Errorf("not find genesis.json")
63         }
64
65         // Create & start node
66         n := node.NewNodeDefault(config)
67         if _, err := n.Start(); err != nil {
68                 return fmt.Errorf("Failed to start node: %v", err)
69         } else {
70                 log.WithField("nodeInfo", n.Switch().NodeInfo()).Info("Started node")
71         }
72
73         // Trap signal, run forever.
74         n.RunForever()
75
76         return nil
77 }