From ac80b93b60c355d6f5eefce0fee8061cf1823958 Mon Sep 17 00:00:00 2001 From: Adrian Brink Date: Tue, 12 Jun 2018 01:55:10 -0700 Subject: [PATCH] `unsafe_reset_all` also resets addrbook.json (#1731) * `unsafe_reset_all` also resets addrbook.json When executing `unsafe_reset_all` it also clear all IP addresses from addrbook.json. This is the expected behaviour of `unsafe_reset_all`. * Fix tests * improve logging statements * use correct file --- .../commands/reset_priv_validator.go | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/cmd/tendermint/commands/reset_priv_validator.go b/cmd/tendermint/commands/reset_priv_validator.go index 78744db71..32d7b1433 100644 --- a/cmd/tendermint/commands/reset_priv_validator.go +++ b/cmd/tendermint/commands/reset_priv_validator.go @@ -24,21 +24,10 @@ var ResetPrivValidatorCmd = &cobra.Command{ Run: resetPrivValidator, } -// ResetAll removes the privValidator files. -// Exported so other CLI tools can use it. -func ResetAll(dbDir, privValFile string, logger log.Logger) { - resetFilePV(privValFile, logger) - if err := os.RemoveAll(dbDir); err != nil { - logger.Error("Error removing directory", "err", err) - return - } - logger.Info("Removed all blockchain history", "dir", dbDir) -} - // XXX: this is totally unsafe. // it's only suitable for testnets. func resetAll(cmd *cobra.Command, args []string) { - ResetAll(config.DBDir(), config.PrivValidatorFile(), logger) + ResetAll(config.DBDir(), config.P2P.AddrBookFile(), config.PrivValidatorFile(), logger) } // XXX: this is totally unsafe. @@ -47,15 +36,34 @@ func resetPrivValidator(cmd *cobra.Command, args []string) { resetFilePV(config.PrivValidatorFile(), logger) } +// ResetAll removes the privValidator and address book files plus all data. +// Exported so other CLI tools can use it. +func ResetAll(dbDir, addrBookFile, privValFile string, logger log.Logger) { + resetFilePV(privValFile, logger) + removeAddrBook(addrBookFile, logger) + if err := os.RemoveAll(dbDir); err == nil { + logger.Info("Removed all blockchain history", "dir", dbDir) + } else { + logger.Error("Error removing all blockchain history", "dir", dbDir, "err", err) + } +} + func resetFilePV(privValFile string, logger log.Logger) { - // Get PrivValidator if _, err := os.Stat(privValFile); err == nil { pv := privval.LoadFilePV(privValFile) pv.Reset() - logger.Info("Reset PrivValidator to genesis state", "file", privValFile) + logger.Info("Reset private validator file to genesis state", "file", privValFile) } else { pv := privval.GenFilePV(privValFile) pv.Save() - logger.Info("Generated PrivValidator", "file", privValFile) + logger.Info("Generated private validator file", "file", privValFile) + } +} + +func removeAddrBook(addrBookFile string, logger log.Logger) { + if err := os.Remove(addrBookFile); err == nil { + logger.Info("Removed existing address book", "file", addrBookFile) + } else if !os.IsNotExist(err) { + logger.Info("Error removing address book", "file", addrBookFile, "err", err) } }