OSDN Git Service

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