2017-02-23 00:21:13 -05:00
|
|
|
package commands
|
2015-12-21 17:52:00 -05:00
|
|
|
|
|
|
|
import (
|
2017-02-23 00:21:13 -05:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-04-09 15:41:26 +02:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2015-12-21 17:52:00 -05:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2018-04-05 22:05:30 -07:00
|
|
|
pvm "github.com/tendermint/tendermint/types/priv_validator"
|
2017-05-01 20:09:29 -04:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
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",
|
|
|
|
Run: initFiles,
|
|
|
|
}
|
|
|
|
|
|
|
|
func initFiles(cmd *cobra.Command, args []string) {
|
2017-12-20 11:50:27 -07:00
|
|
|
// private validator
|
2017-05-04 21:57:58 +02:00
|
|
|
privValFile := config.PrivValidatorFile()
|
2018-04-05 22:05:30 -07:00
|
|
|
var pv *pvm.FilePV
|
2017-12-20 11:50:27 -07:00
|
|
|
if cmn.FileExists(privValFile) {
|
2018-04-05 22:05:30 -07:00
|
|
|
pv = pvm.LoadFilePV(privValFile)
|
2017-12-20 11:50:27 -07:00
|
|
|
logger.Info("Found private validator", "path", privValFile)
|
|
|
|
} else {
|
2018-04-05 22:05:30 -07:00
|
|
|
pv = pvm.GenFilePV(privValFile)
|
|
|
|
pv.Save()
|
2018-03-06 17:44:13 +01:00
|
|
|
logger.Info("Generated private validator", "path", privValFile)
|
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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
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{
|
|
|
|
ChainID: cmn.Fmt("test-chain-%v", cmn.RandStr(6)),
|
2017-02-20 11:13:44 +04:00
|
|
|
}
|
2017-12-20 11:50:27 -07:00
|
|
|
genDoc.Validators = []types.GenesisValidator{{
|
2018-04-05 22:05:30 -07:00
|
|
|
PubKey: pv.GetPubKey(),
|
2017-12-20 11:50:27 -07:00
|
|
|
Power: 10,
|
|
|
|
}}
|
2017-02-20 11:13:44 +04:00
|
|
|
|
2017-12-20 11:50:27 -07:00
|
|
|
if err := genDoc.SaveAs(genFile); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-06 17:44:13 +01:00
|
|
|
logger.Info("Generated genesis file", "path", genFile)
|
2017-02-20 11:13:44 +04:00
|
|
|
}
|
2015-12-21 17:52:00 -05:00
|
|
|
}
|