From 6bad0ce785767c4116d87ebbc057aa9d4eeac10b Mon Sep 17 00:00:00 2001 From: Marko Date: Tue, 8 Mar 2022 15:32:29 +0100 Subject: [PATCH] cmd: make reset more safe (#8081) * add safe reset * undo change * remove unsafe * Update cmd/tendermint/commands/reset_priv_validator.go Co-authored-by: Thane Thomson * Update cmd/tendermint/commands/reset_priv_validator.go Co-authored-by: M. J. Fromberger * remove export comment Co-authored-by: Thane Thomson Co-authored-by: M. J. Fromberger (cherry picked from commit 7c03e7dbfb25fea4c980b490c6572bd3ec48a9e4) # Conflicts: # cmd/tendermint/commands/reset_priv_validator.go # cmd/tendermint/main.go --- .../commands/reset_priv_validator.go | 90 ++++++++++++++++++- cmd/tendermint/main.go | 37 ++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/cmd/tendermint/commands/reset_priv_validator.go b/cmd/tendermint/commands/reset_priv_validator.go index 45b86d44c..ce9610f23 100644 --- a/cmd/tendermint/commands/reset_priv_validator.go +++ b/cmd/tendermint/commands/reset_priv_validator.go @@ -2,6 +2,7 @@ package commands import ( "os" + "path/filepath" "github.com/spf13/cobra" @@ -20,7 +21,26 @@ var ResetAllCmd = &cobra.Command{ PreRun: deprecateSnakeCase, } +<<<<<<< HEAD var keepAddrBook bool +======= +// MakeResetStateCommand constructs a command that removes the database of +// the specified Tendermint core instance. +func MakeResetStateCommand(conf *config.Config, logger log.Logger) *cobra.Command { + var keyType string + + return &cobra.Command{ + Use: "reset-state", + Short: "Remove all the data and WAL", + RunE: func(cmd *cobra.Command, args []string) error { + return resetState(conf.DBDir(), logger, keyType) + }, + } +} + +func MakeResetPrivateValidatorCommand(conf *config.Config, logger log.Logger) *cobra.Command { + var keyType string +>>>>>>> 7c03e7dbf (cmd: make reset more safe (#8081)) func init() { ResetAllCmd.Flags().BoolVar(&keepAddrBook, "keep-addr-book", false, "keep the address book intact") @@ -48,6 +68,7 @@ func resetPrivValidator(cmd *cobra.Command, args []string) { resetFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(), logger) } +<<<<<<< HEAD // ResetAll removes address book files plus all data, and resets the privValdiator data. // Exported so other CLI tools can use it. func ResetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile string, logger log.Logger) { @@ -56,16 +77,83 @@ func ResetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile string, logg } else { removeAddrBook(addrBookFile, logger) } +======= +// resetAll removes address book files plus all data, and resets the privValdiator data. +func resetAll(dbDir, privValKeyFile, privValStateFile string, logger log.Logger, keyType string) error { +>>>>>>> 7c03e7dbf (cmd: make reset more safe (#8081)) 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) } - // recreate the dbDir since the privVal state needs to live there + + return resetFilePV(privValKeyFile, privValStateFile, logger, keyType) +} + +// resetState removes address book files plus all databases. +func resetState(dbDir string, logger log.Logger, keyType string) error { + blockdb := filepath.Join(dbDir, "blockstore.db") + state := filepath.Join(dbDir, "state.db") + wal := filepath.Join(dbDir, "cs.wal") + evidence := filepath.Join(dbDir, "evidence.db") + txIndex := filepath.Join(dbDir, "tx_index.db") + peerstore := filepath.Join(dbDir, "peerstore.db") + + if tmos.FileExists(blockdb) { + if err := os.RemoveAll(blockdb); err == nil { + logger.Info("Removed all blockstore.db", "dir", blockdb) + } else { + logger.Error("error removing all blockstore.db", "dir", blockdb, "err", err) + } + } + + if tmos.FileExists(state) { + if err := os.RemoveAll(state); err == nil { + logger.Info("Removed all state.db", "dir", state) + } else { + logger.Error("error removing all state.db", "dir", state, "err", err) + } + } + + if tmos.FileExists(wal) { + if err := os.RemoveAll(wal); err == nil { + logger.Info("Removed all cs.wal", "dir", wal) + } else { + logger.Error("error removing all cs.wal", "dir", wal, "err", err) + } + } + + if tmos.FileExists(evidence) { + if err := os.RemoveAll(evidence); err == nil { + logger.Info("Removed all evidence.db", "dir", evidence) + } else { + logger.Error("error removing all evidence.db", "dir", evidence, "err", err) + } + } + + if tmos.FileExists(txIndex) { + if err := os.RemoveAll(txIndex); err == nil { + logger.Info("Removed tx_index.db", "dir", txIndex) + } else { + logger.Error("error removing tx_index.db", "dir", txIndex, "err", err) + } + } + + if tmos.FileExists(peerstore) { + if err := os.RemoveAll(peerstore); err == nil { + logger.Info("Removed peerstore.db", "dir", peerstore) + } else { + logger.Error("error removing peerstore.db", "dir", peerstore, "err", err) + } + } if err := tmos.EnsureDir(dbDir, 0700); err != nil { logger.Error("unable to recreate dbDir", "err", err) } +<<<<<<< HEAD resetFilePV(privValKeyFile, privValStateFile, logger) +======= + return nil +>>>>>>> 7c03e7dbf (cmd: make reset more safe (#8081)) } func resetFilePV(privValKeyFile, privValStateFile string, logger log.Logger) { diff --git a/cmd/tendermint/main.go b/cmd/tendermint/main.go index 730946aa9..d0a7affc9 100644 --- a/cmd/tendermint/main.go +++ b/cmd/tendermint/main.go @@ -12,6 +12,7 @@ import ( ) func main() { +<<<<<<< HEAD rootCmd := cmd.RootCmd rootCmd.AddCommand( cmd.GenValidatorCmd, @@ -30,6 +31,42 @@ func main() { cmd.RollbackStateCmd, debug.DebugCmd, cli.NewCompletionCmd(rootCmd, true), +======= + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + conf, err := commands.ParseConfig(config.DefaultConfig()) + if err != nil { + panic(err) + } + + logger, err := log.NewDefaultLogger(conf.LogFormat, conf.LogLevel) + if err != nil { + panic(err) + } + + rcmd := commands.RootCommand(conf, logger) + rcmd.AddCommand( + commands.MakeGenValidatorCommand(), + commands.MakeReindexEventCommand(conf, logger), + commands.MakeInitFilesCommand(conf, logger), + commands.MakeLightCommand(conf, logger), + commands.MakeReplayCommand(conf, logger), + commands.MakeReplayConsoleCommand(conf, logger), + commands.MakeResetAllCommand(conf, logger), + commands.MakeResetStateCommand(conf, logger), + commands.MakeResetPrivateValidatorCommand(conf, logger), + commands.MakeShowValidatorCommand(conf, logger), + commands.MakeTestnetFilesCommand(conf, logger), + commands.MakeShowNodeIDCommand(conf), + commands.GenNodeKeyCmd, + commands.VersionCmd, + commands.MakeInspectCommand(conf, logger), + commands.MakeRollbackStateCommand(conf), + commands.MakeKeyMigrateCommand(conf, logger), + debug.GetDebugCommand(logger), + commands.NewCompletionCmd(rcmd, true), +>>>>>>> 7c03e7dbf (cmd: make reset more safe (#8081)) ) // NOTE: