Skip to content

Commit

Permalink
cli: fix reset commands #8270
Browse files Browse the repository at this point in the history
Some applications use the command-line implementations directly,
rather than through the root command. Because the implentations
obtained config from an unexported global, this would not work.

Instead, have each command that needs the config parse it where
needed.

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
  • Loading branch information
tnasu and alexanderbez committed Apr 13, 2022
1 parent b4e7b30 commit 3ac561c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
67 changes: 44 additions & 23 deletions cmd/ostracon/commands/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ var keepAddrBook bool

// ResetStateCmd removes the database of the specified Tendermint core instance.
var ResetStateCmd = &cobra.Command{
Use: "reset-state",
Short: "Remove all the data and WAL",
RunE: func(cmd *cobra.Command, args []string) error {
Use: "reset-state",
Short: "Remove all the data and WAL",
PreRun: deprecateSnakeCase,
RunE: func(cmd *cobra.Command, args []string) (err error) {
config, err = ParseConfig()
if err != nil {
return err
}

return resetState(config.DBDir(), logger)
},
PreRun: deprecateSnakeCase,
}

func init() {
Expand All @@ -46,25 +51,39 @@ var ResetPrivValidatorCmd = &cobra.Command{
Use: "unsafe-reset-priv-validator",
Aliases: []string{"unsafe_reset_priv_validator"},
Short: "(unsafe) Reset this node's validator to genesis state",
Run: resetPrivValidator,
PreRun: deprecateSnakeCase,
RunE: resetPrivValidator,
}

// XXX: this is totally unsafe.
// it's only suitable for testnets.
func resetAllCmd(cmd *cobra.Command, args []string) error {
return resetAll(config.DBDir(), config.P2P.AddrBookFile(), config.PrivValidatorKeyFile(),
config.PrivValidatorStateFile(), config.PrivValidatorKeyType(), logger)
func resetAllCmd(cmd *cobra.Command, args []string) (err error) {
config, err = ParseConfig()
if err != nil {
return err
}

return resetAll(
config.DBDir(),
config.P2P.AddrBookFile(),
config.PrivValidatorKeyFile(),
config.PrivValidatorStateFile(),
config.PrivValidatorKeyType(),
logger,
)
}

// XXX: this is totally unsafe.
// it's only suitable for testnets.
func resetPrivValidator(cmd *cobra.Command, args []string) {
err := resetFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(),
config.PrivValidatorKeyType(), logger)
func resetPrivValidator(cmd *cobra.Command, args []string) (err error) {
config, err = ParseConfig()
if err != nil {
panic(err)
return err
}

resetFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(),
config.PrivValidatorKeyType(), logger)
return nil
}

// resetAll removes address book files plus all data, and resets the privValdiator data.
Expand All @@ -74,6 +93,7 @@ func resetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile, privKeyType
} else {
removeAddrBook(addrBookFile, logger)
}

if err := os.RemoveAll(dbDir); err == nil {
logger.Info("Removed all blockchain history", "dir", dbDir)
} else {
Expand Down Expand Up @@ -147,18 +167,19 @@ func resetFilePV(privValKeyFile, privValStateFile, privKeyType string, logger lo
if _, err := os.Stat(privValKeyFile); err == nil {
pv := privval.LoadFilePVEmptyState(privValKeyFile, privValStateFile)
pv.Reset()
logger.Info("Reset private validator file to genesis state", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
logger.Info(
"Reset private validator file to genesis state",
"keyFile", privValKeyFile,
"stateFile", privValStateFile,
)
} else {
pv, err := privval.GenFilePV(privValKeyFile, privValStateFile, privKeyType)
if err != nil {
return err
}
if pv != nil {
pv.Save()
}
logger.Info("Generated private validator file", "keyFile", privValKeyFile,
"stateFile", privValStateFile)
pv, _ := privval.GenFilePV(privValKeyFile, privValStateFile, privKeyType)
pv.Save()
logger.Info(
"Generated private validator file",
"keyFile", privValKeyFile,
"stateFile", privValStateFile,
)
}
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions privval/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ func (pvKey FilePVKey) Save() {
if err != nil {
panic(err)
}
err = tempfile.WriteFileAtomic(outFile, jsonBytes, 0600)
if err != nil {

if err := tempfile.WriteFileAtomic(outFile, jsonBytes, 0600); err != nil {
panic(err)
}

}

//-------------------------------------------------------------------------------
Expand Down

0 comments on commit 3ac561c

Please sign in to comment.