Browse Source

cmd: make reset more safe (backport #8081) (#8090)

Backport notes:

- Revert command declaration to the old explicit format.
- Update threading of the keyType argument.
- Fix function naming collision.
pull/8092/head
mergify[bot] 3 years ago
committed by GitHub
parent
commit
aface5f9b8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 10 deletions
  1. +78
    -10
      cmd/tendermint/commands/reset_priv_validator.go
  2. +1
    -0
      cmd/tendermint/main.go

+ 78
- 10
cmd/tendermint/commands/reset_priv_validator.go View File

@ -2,6 +2,7 @@ package commands
import ( import (
"os" "os"
"path/filepath"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -16,11 +17,20 @@ import (
var ResetAllCmd = &cobra.Command{ var ResetAllCmd = &cobra.Command{
Use: "unsafe-reset-all", Use: "unsafe-reset-all",
Short: "(unsafe) Remove all the data and WAL, reset this node's validator to genesis state", Short: "(unsafe) Remove all the data and WAL, reset this node's validator to genesis state",
RunE: resetAll,
RunE: resetAllCmd,
} }
var keepAddrBook bool 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 {
return resetState(config.DBDir(), logger, keyType)
},
}
func init() { func init() {
ResetAllCmd.Flags().BoolVar(&keepAddrBook, "keep-addr-book", false, "keep the address book intact") ResetAllCmd.Flags().BoolVar(&keepAddrBook, "keep-addr-book", false, "keep the address book intact")
ResetPrivValidatorCmd.Flags().StringVar(&keyType, "key", types.ABCIPubKeyTypeEd25519, ResetPrivValidatorCmd.Flags().StringVar(&keyType, "key", types.ABCIPubKeyTypeEd25519,
@ -36,20 +46,19 @@ var ResetPrivValidatorCmd = &cobra.Command{
// XXX: this is totally unsafe. // XXX: this is totally unsafe.
// it's only suitable for testnets. // it's only suitable for testnets.
func resetAll(cmd *cobra.Command, args []string) error {
return ResetAll(config.DBDir(), config.P2P.AddrBookFile(), config.PrivValidator.KeyFile(),
func resetAllCmd(cmd *cobra.Command, args []string) error {
return resetAll(config.DBDir(), config.P2P.AddrBookFile(), config.PrivValidator.KeyFile(),
config.PrivValidator.StateFile(), logger) config.PrivValidator.StateFile(), logger)
} }
// XXX: this is totally unsafe. // XXX: this is totally unsafe.
// it's only suitable for testnets. // it's only suitable for testnets.
func resetPrivValidator(cmd *cobra.Command, args []string) error { func resetPrivValidator(cmd *cobra.Command, args []string) error {
return resetFilePV(config.PrivValidator.KeyFile(), config.PrivValidator.StateFile(), logger)
return resetFilePV(config.PrivValidator.KeyFile(), config.PrivValidator.StateFile(), logger, keyType)
} }
// 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) error {
// resetAllCmd removes address book files plus all data, and resets the privValidator data.
func resetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile string, logger log.Logger) error {
if keepAddrBook { if keepAddrBook {
logger.Info("The address book remains intact") logger.Info("The address book remains intact")
} else { } else {
@ -60,14 +69,73 @@ func ResetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile string, logg
} else { } else {
logger.Error("Error removing all blockchain history", "dir", dbDir, "err", err) 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 { if err := tmos.EnsureDir(dbDir, 0700); err != nil {
logger.Error("unable to recreate dbDir", "err", err) logger.Error("unable to recreate dbDir", "err", err)
} }
return resetFilePV(privValKeyFile, privValStateFile, logger)
return nil
} }
func resetFilePV(privValKeyFile, privValStateFile string, logger log.Logger) error {
func resetFilePV(privValKeyFile, privValStateFile string, logger log.Logger, keyType string) error {
if _, err := os.Stat(privValKeyFile); err == nil { if _, err := os.Stat(privValKeyFile); err == nil {
pv, err := privval.LoadFilePVEmptyState(privValKeyFile, privValStateFile) pv, err := privval.LoadFilePVEmptyState(privValKeyFile, privValStateFile)
if err != nil { if err != nil {


+ 1
- 0
cmd/tendermint/main.go View File

@ -23,6 +23,7 @@ func main() {
cmd.ReplayConsoleCmd, cmd.ReplayConsoleCmd,
cmd.ResetAllCmd, cmd.ResetAllCmd,
cmd.ResetPrivValidatorCmd, cmd.ResetPrivValidatorCmd,
cmd.ResetStateCmd,
cmd.ShowValidatorCmd, cmd.ShowValidatorCmd,
cmd.TestnetFilesCmd, cmd.TestnetFilesCmd,
cmd.ShowNodeIDCmd, cmd.ShowNodeIDCmd,


Loading…
Cancel
Save