-
Notifications
You must be signed in to change notification settings - Fork 13
/
init.go
145 lines (125 loc) · 4.52 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cli
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/spf13/cobra"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/cli"
tmos "github.com/tendermint/tendermint/libs/os"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/types"
"github.com/tyler-smith/go-bip39"
)
const (
// FlagOverwrite defines a flag to overwrite an existing genesis JSON file.
FlagOverwrite = "overwrite"
// FlagRecover defines a flag to initialize the private validator key from a specific seed.
FlagRecover = "recover"
)
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,
}
}
// InitCmd returns a command that initializes all files needed for Tendermint
// and the respective application.
func InitCmd(nodeHome string, genesisState map[string]json.RawMessage, consensusParams *tmproto.ConsensusParams) *cobra.Command {
cmd := &cobra.Command{
Use: "init [moniker]",
Short: "Initialize private validator, p2p, genesis, application and client configuration files",
Long: `Initialize validators's and node's configuration files.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
serverCtx := server.GetServerContextFromCmd(cmd)
config := serverCtx.Config
config.SetRoot(clientCtx.HomeDir)
chainID, err := cmd.Flags().GetString(flags.FlagChainID)
if err != nil {
return err
}
// Get bip39 mnemonic
var mnemonic string
flagRecover, err := cmd.Flags().GetBool(FlagRecover)
if err != nil {
return err
}
if flagRecover {
inBuf := bufio.NewReader(cmd.InOrStdin())
mnemonic, err = input.GetString("Enter your bip39 mnemonic", inBuf)
if err != nil {
return err
}
if !bip39.IsMnemonicValid(mnemonic) {
return errors.New("invalid mnemonic")
}
}
nodeID, _, err := genutil.InitializeNodeValidatorFilesFromMnemonic(config, mnemonic)
if err != nil {
return err
}
config.Moniker = args[0]
genFile := config.GenesisFile()
overwrite, _ := cmd.Flags().GetBool(FlagOverwrite)
if !overwrite && tmos.FileExists(genFile) {
return fmt.Errorf("genesis.json file already exists: %v", genFile)
}
appState, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
return fmt.Errorf("failed to marshall default genesis state: %s", err.Error())
}
genDoc := &types.GenesisDoc{}
if _, err := os.Stat(genFile); err != nil {
if !os.IsNotExist(err) {
return err
}
genDoc.ConsensusParams = consensusParams
} else {
genDoc, err = types.GenesisDocFromFile(genFile)
if err != nil {
return fmt.Errorf("failed to read genesis doc from file: %s", err.Error())
}
}
genDoc.ChainID = chainID
genDoc.Validators = nil
genDoc.AppState = appState
if err = genutil.ExportGenesisFile(genDoc, genFile); err != nil {
return fmt.Errorf("failed to export gensis file: %s", err.Error())
}
cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config)
toPrint := NewPrintInfo(config.Moniker, chainID, nodeID, "", appState)
out, err := json.MarshalIndent(toPrint, "", " ")
if err != nil {
return err
}
return clientCtx.PrintString(string(sdk.MustSortJSON(out)) + "\n")
},
}
cmd.Flags().String(cli.HomeFlag, nodeHome, "node's home directory")
cmd.Flags().Bool(FlagOverwrite, false, "overwrite the genesis.json file")
cmd.Flags().Bool(FlagRecover, false, "provide seed phrase to recover existing key instead of creating")
cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().StringP(cli.OutputFlag, "o", "json", "Output format (text|json)")
return cmd
}