2017-02-23 00:21:13 -05:00
|
|
|
package commands
|
2015-12-21 17:52:00 -05:00
|
|
|
|
|
|
|
import (
|
2018-08-10 00:25:57 -05:00
|
|
|
"fmt"
|
2018-05-08 12:04:20 +04:00
|
|
|
|
2017-02-23 00:21:13 -05:00
|
|
|
"github.com/spf13/cobra"
|
2018-04-11 19:40:53 +02:00
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2018-08-10 00:25:57 -05:00
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
2018-04-09 15:41:26 +02:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2018-06-01 19:17:37 +02:00
|
|
|
"github.com/tendermint/tendermint/privval"
|
2015-12-21 17:52:00 -05:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2018-09-01 01:33:51 +02:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
2015-12-21 17:52:00 -05:00
|
|
|
)
|
|
|
|
|
2017-09-13 00:18:07 +02:00
|
|
|
// InitFilesCmd initialises a fresh Tendermint Core instance.
|
2017-08-30 22:21:32 +02:00
|
|
|
var InitFilesCmd = &cobra.Command{
|
2017-02-23 00:21:13 -05:00
|
|
|
Use: "init",
|
|
|
|
Short: "Initialize Tendermint",
|
2018-04-11 19:40:53 +02:00
|
|
|
RunE: initFiles,
|
2017-02-23 00:21:13 -05:00
|
|
|
}
|
|
|
|
|
2018-04-11 19:40:53 +02:00
|
|
|
func initFiles(cmd *cobra.Command, args []string) error {
|
|
|
|
return initFilesWithConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initFilesWithConfig(config *cfg.Config) error {
|
2017-12-20 11:50:27 -07:00
|
|
|
// private validator
|
2018-12-22 05:58:27 +08:00
|
|
|
privValKeyFile := config.PrivValidatorKeyFile()
|
|
|
|
privValStateFile := config.PrivValidatorStateFile()
|
2018-06-01 19:17:37 +02:00
|
|
|
var pv *privval.FilePV
|
2018-12-22 05:58:27 +08:00
|
|
|
if cmn.FileExists(privValKeyFile) {
|
|
|
|
pv = privval.LoadFilePV(privValKeyFile, privValStateFile)
|
|
|
|
logger.Info("Found private validator", "keyFile", privValKeyFile,
|
|
|
|
"stateFile", privValStateFile)
|
2017-12-20 11:50:27 -07:00
|
|
|
} else {
|
2018-12-22 05:58:27 +08:00
|
|
|
pv = privval.GenFilePV(privValKeyFile, privValStateFile)
|
2018-04-05 22:05:30 -07:00
|
|
|
pv.Save()
|
2018-12-22 05:58:27 +08:00
|
|
|
logger.Info("Generated private validator", "keyFile", privValKeyFile,
|
|
|
|
"stateFile", privValStateFile)
|
2017-12-20 11:50:27 -07:00
|
|
|
}
|
2015-12-21 17:52:00 -05:00
|
|
|
|
2018-04-09 15:41:26 +02:00
|
|
|
nodeKeyFile := config.NodeKeyFile()
|
|
|
|
if cmn.FileExists(nodeKeyFile) {
|
|
|
|
logger.Info("Found node key", "path", nodeKeyFile)
|
|
|
|
} else {
|
|
|
|
if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil {
|
2018-04-11 19:40:53 +02:00
|
|
|
return err
|
2018-04-09 15:41:26 +02:00
|
|
|
}
|
|
|
|
logger.Info("Generated node key", "path", nodeKeyFile)
|
|
|
|
}
|
|
|
|
|
2017-12-20 11:50:27 -07:00
|
|
|
// genesis file
|
|
|
|
genFile := config.GenesisFile()
|
|
|
|
if cmn.FileExists(genFile) {
|
|
|
|
logger.Info("Found genesis file", "path", genFile)
|
|
|
|
} else {
|
|
|
|
genDoc := types.GenesisDoc{
|
2018-08-10 00:25:57 -05:00
|
|
|
ChainID: fmt.Sprintf("test-chain-%v", cmn.RandStr(6)),
|
2018-09-01 01:33:51 +02:00
|
|
|
GenesisTime: tmtime.Now(),
|
2018-06-27 14:12:52 +04:00
|
|
|
ConsensusParams: types.DefaultConsensusParams(),
|
2017-02-20 11:13:44 +04:00
|
|
|
}
|
2018-12-22 06:36:45 +01:00
|
|
|
key := pv.GetPubKey()
|
2017-12-20 11:50:27 -07:00
|
|
|
genDoc.Validators = []types.GenesisValidator{{
|
2018-12-22 06:36:45 +01:00
|
|
|
Address: key.Address(),
|
|
|
|
PubKey: key,
|
2018-09-21 20:36:48 +02:00
|
|
|
Power: 10,
|
2017-12-20 11:50:27 -07:00
|
|
|
}}
|
2017-02-20 11:13:44 +04:00
|
|
|
|
2017-12-20 11:50:27 -07:00
|
|
|
if err := genDoc.SaveAs(genFile); err != nil {
|
2018-04-11 19:40:53 +02:00
|
|
|
return err
|
2017-12-20 11:50:27 -07:00
|
|
|
}
|
2018-03-06 17:44:13 +01:00
|
|
|
logger.Info("Generated genesis file", "path", genFile)
|
2017-02-20 11:13:44 +04:00
|
|
|
}
|
2018-04-11 19:40:53 +02:00
|
|
|
|
|
|
|
return nil
|
2015-12-21 17:52:00 -05:00
|
|
|
}
|