OSDN Git Service

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