OSDN Git Service

Merge pull request #47 from liuchengxu/ci
[bytom/bytom.git] / cmd / bytomd / commands / run_node.go
1 package commands
2
3 import (
4         "fmt"
5         "io/ioutil"
6
7         "github.com/spf13/cobra"
8
9         "github.com/bytom/node"
10         "github.com/bytom/types"
11         cmn "github.com/tendermint/tmlibs/common"
12 )
13
14 var runNodeCmd = &cobra.Command{
15         Use:   "node",
16         Short: "Run the bytomd",
17         RunE:  runNode,
18 }
19
20 func init() {
21         // p2p flags
22         runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)")
23         runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes")
24         runNodeCmd.Flags().Bool("p2p.skip_upnp", config.P2P.SkipUPNP, "Skip UPNP configuration")
25         runNodeCmd.Flags().Bool("p2p.pex", config.P2P.PexReactor, "Enable Peer-Exchange ")
26         runNodeCmd.Flags().Bool("wallet.enable", config.Wallet.Enable, "Enable wallet")
27
28         RootCmd.AddCommand(runNodeCmd)
29 }
30
31 func runNode(cmd *cobra.Command, args []string) error {
32         genDocFile := config.GenesisFile()
33         if cmn.FileExists(genDocFile) {
34                 jsonBlob, err := ioutil.ReadFile(genDocFile)
35                 if err != nil {
36                         return fmt.Errorf("Couldn't read GenesisDoc file: %v", err)
37                 }
38                 genDoc, err := types.GenesisDocFromJSON(jsonBlob)
39                 if err != nil {
40                         return fmt.Errorf("Error reading GenesisDoc: %v", err)
41                 }
42                 if genDoc.ChainID == "" {
43                         return fmt.Errorf("Genesis doc %v must include non-empty chain_id", genDocFile)
44                 }
45                 config.ChainID = genDoc.ChainID
46                 config.PrivateKey = genDoc.PrivateKey
47                 config.Time = genDoc.GenesisTime
48         } else {
49                 return fmt.Errorf("not find genesis.json")
50         }
51
52         // Create & start node
53         n := node.NewNodeDefault(config, logger.With("module", "node_p2p"))
54         if _, err := n.Start(); err != nil {
55                 return fmt.Errorf("Failed to start node: %v", err)
56         } else {
57                 logger.Info("Started node", "nodeInfo", n.Switch().NodeInfo())
58         }
59
60         // Trap signal, run forever.
61         n.RunForever()
62
63         return nil
64 }