-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.go
117 lines (101 loc) · 3.68 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cli
import (
"encoding/json"
"fmt"
"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
"os"
"path/filepath"
cfg "github.com/fibonacci-chain/fbc/libs/tendermint/config"
"github.com/fibonacci-chain/fbc/libs/tendermint/libs/cli"
tmos "github.com/fibonacci-chain/fbc/libs/tendermint/libs/os"
"github.com/fibonacci-chain/fbc/libs/tendermint/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/server"
sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module"
"github.com/fibonacci-chain/fbc/x/genutil"
)
const (
flagOverwrite = "overwrite"
flagClientHome = "home-client"
)
type printInfo struct {
Moniker string `json:"moniker" yaml:"moniker"`
ChainID string `json:"chain_id" yaml:"chain_id"`
NodeID string `json:"node_id" yaml:"node_id"`
GenTxsDir string `json:"gentxs_dir" yaml:"gentxs_dir"`
AppMessage json.RawMessage `json:"app_message" yaml:"app_message"`
}
func newPrintInfo(moniker, chainID, nodeID, genTxsDir string,
appMessage json.RawMessage) printInfo {
return printInfo{
Moniker: moniker,
ChainID: chainID,
NodeID: nodeID,
GenTxsDir: genTxsDir,
AppMessage: appMessage,
}
}
func displayInfo(cdc *codec.Codec, info printInfo) error {
out, err := codec.MarshalJSONIndent(cdc, info)
if err != nil {
return err
}
_, err = fmt.Fprintf(os.Stderr, "%s\n", string(sdk.MustSortJSON(out)))
return err
}
// InitCmd returns a command that initializes all files needed for Tendermint
// and the respective application.
func InitCmd(ctx *server.Context, cdc *codec.Codec, mbm module.BasicManager,
defaultNodeHome string) *cobra.Command { // nolint: golint
cmd := &cobra.Command{
Use: "init [moniker]",
Short: "Initialize private validator, p2p, genesis, and application configuration files",
Long: `Initialize validators' and node's configuration files.`,
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
config := ctx.Config
config.SetRoot(viper.GetString(cli.HomeFlag))
chainID := viper.GetString(flags.FlagChainID)
index := viper.GetInt(flags.FlagNodeIndex)
nodeID, _, err := genutil.InitializeNodeValidatorFilesByIndex(config, index)
if err != nil {
return err
}
config.Moniker = args[0]
genFile := config.GenesisFile()
if !viper.GetBool(flagOverwrite) && tmos.FileExists(genFile) {
return fmt.Errorf("genesis.json file already exists: %v", genFile)
}
appState, err := codec.MarshalJSONIndent(cdc, mbm.DefaultGenesis())
if err != nil {
return err
}
genDoc := &types.GenesisDoc{}
if _, err := os.Stat(genFile); err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
genDoc, err = types.GenesisDocFromFile(genFile)
if err != nil {
return err
}
}
genDoc.ChainID, genDoc.Validators, genDoc.AppState = chainID, nil, appState
if err = genutil.ExportGenesisFile(genDoc, genFile); err != nil {
return err
}
toPrint := newPrintInfo(config.Moniker, chainID, nodeID, "", appState)
cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config)
return displayInfo(cdc, toPrint)
},
}
cmd.Flags().String(cli.HomeFlag, defaultNodeHome, "node's home directory")
cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file")
cmd.Flags().String(flags.FlagChainID, "testchain-1", "genesis file chain-id, it's necessary to be provided in the format like \"[chain name]-[positive integer]\"")
cmd.Flags().String(flags.FlagNodeIndex, "-1", "init by node index.")
return cmd
}