OSDN Git Service

change bytom to bytomd
[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 p2p node",
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 (dev feature)")
26
27         RootCmd.AddCommand(runNodeCmd)
28 }
29
30 func runNode(cmd *cobra.Command, args []string) error {
31         genDocFile := config.GenesisFile()
32         if cmn.FileExists(genDocFile) {
33                 jsonBlob, err := ioutil.ReadFile(genDocFile)
34                 if err != nil {
35                         return fmt.Errorf("Couldn't read GenesisDoc file: %v", err)
36                 }
37                 genDoc, err := types.GenesisDocFromJSON(jsonBlob)
38                 if err != nil {
39                         return fmt.Errorf("Error reading GenesisDoc: %v", err)
40                 }
41                 if genDoc.ChainID == "" {
42                         return fmt.Errorf("Genesis doc %v must include non-empty chain_id", genDocFile)
43                 }
44                 config.ChainID = genDoc.ChainID
45                 config.PrivateKey = genDoc.PrivateKey
46                 config.Time = genDoc.GenesisTime
47         } else {
48                 return fmt.Errorf("not find genesis.json")
49         }
50
51         // Create & start node
52         n := node.NewNodeDefault(config, logger.With("module", "node_p2p"))
53         if _, err := n.Start(); err != nil {
54                 return fmt.Errorf("Failed to start node: %v", err)
55         } else {
56                 logger.Info("Started node", "nodeInfo", n.Switch().NodeInfo())
57         }
58
59         // Trap signal, run forever.
60         n.RunForever()
61
62         return nil
63 }