From 67c3af3bf8617a38dbae6f6ff916093335f37098 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Wed, 20 Dec 2017 11:50:27 -0700 Subject: [PATCH] cmd/tendermint: fix initialization file creation checks (#991) * cmd/tendermint: fix initialization file creation checks Fixes #989. The original initialization sequence started to inexplicably fail ```shell tendermint unsafe_reset_all tendermint init tendermint node --proxy_app=dummy ``` used to fail with ```shell ERROR: Failed to create node: Couldn't read GenesisDoc file: open /Users/emmanuelodeke/.tendermint/genesis.json: no such file or directory ``` because the initialization sequence always assumed that the genesisDoc would only be set if the privValidator was generated. However, `tendermint unsafe_reset_all` only created the `priv_validator.json` file which would mean that then running `tendermint init` would never create the `genesis.json` file which if following the recommended sequence would then fail since the `genesis.json` was absent. * cmd/tendermint: Load PrivValidatorFS if existent, lest generate it Feedback from @melekes * change logging messages for init cmd Refs #989 --- cmd/tendermint/commands/init.go | 51 +++++++++++++++++---------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/cmd/tendermint/commands/init.go b/cmd/tendermint/commands/init.go index e8f22eb1..0bc9e626 100644 --- a/cmd/tendermint/commands/init.go +++ b/cmd/tendermint/commands/init.go @@ -1,8 +1,6 @@ package commands import ( - "os" - "github.com/spf13/cobra" "github.com/tendermint/tendermint/types" @@ -17,29 +15,34 @@ var InitFilesCmd = &cobra.Command{ } func initFiles(cmd *cobra.Command, args []string) { + // private validator privValFile := config.PrivValidatorFile() - if _, err := os.Stat(privValFile); os.IsNotExist(err) { - privValidator := types.GenPrivValidatorFS(privValFile) - privValidator.Save() - - genFile := config.GenesisFile() - - if _, err := os.Stat(genFile); os.IsNotExist(err) { - genDoc := types.GenesisDoc{ - ChainID: cmn.Fmt("test-chain-%v", cmn.RandStr(6)), - } - genDoc.Validators = []types.GenesisValidator{{ - PubKey: privValidator.GetPubKey(), - Power: 10, - }} - - if err := genDoc.SaveAs(genFile); err != nil { - panic(err) - } - } - - logger.Info("Initialized tendermint", "genesis", config.GenesisFile(), "priv_validator", config.PrivValidatorFile()) + var privValidator *types.PrivValidatorFS + if cmn.FileExists(privValFile) { + privValidator = types.LoadPrivValidatorFS(privValFile) + logger.Info("Found private validator", "path", privValFile) } else { - logger.Info("Already initialized", "priv_validator", config.PrivValidatorFile()) + privValidator = types.GenPrivValidatorFS(privValFile) + privValidator.Save() + logger.Info("Genetated private validator", "path", privValFile) + } + + // 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)), + } + genDoc.Validators = []types.GenesisValidator{{ + PubKey: privValidator.GetPubKey(), + Power: 10, + }} + + if err := genDoc.SaveAs(genFile); err != nil { + panic(err) + } + logger.Info("Genetated genesis file", "path", genFile) } }