OSDN Git Service

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