mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 14:34:17 +00:00
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.
187 lines
5.2 KiB
Go
187 lines
5.2 KiB
Go
package commands
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
tmos "github.com/tendermint/tendermint/libs/os"
|
|
"github.com/tendermint/tendermint/privval"
|
|
)
|
|
|
|
// ResetAllCmd removes the database of this Tendermint core
|
|
// instance.
|
|
var ResetAllCmd = &cobra.Command{
|
|
Use: "unsafe-reset-all",
|
|
Aliases: []string{"unsafe_reset_all"},
|
|
Short: "(unsafe) Remove all the data and WAL, reset this node's validator to genesis state",
|
|
RunE: resetAllCmd,
|
|
PreRun: deprecateSnakeCase,
|
|
}
|
|
|
|
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",
|
|
PreRun: deprecateSnakeCase,
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
config, err = ParseConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return resetState(config.DBDir(), logger)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
ResetAllCmd.Flags().BoolVar(&keepAddrBook, "keep-addr-book", false, "keep the address book intact")
|
|
}
|
|
|
|
// ResetPrivValidatorCmd resets the private validator files.
|
|
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",
|
|
PreRun: deprecateSnakeCase,
|
|
RunE: resetPrivValidator,
|
|
}
|
|
|
|
// XXX: this is totally unsafe.
|
|
// it's only suitable for testnets.
|
|
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(),
|
|
logger,
|
|
)
|
|
}
|
|
|
|
// XXX: this is totally unsafe.
|
|
// it's only suitable for testnets.
|
|
func resetPrivValidator(cmd *cobra.Command, args []string) (err error) {
|
|
config, err = ParseConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resetFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(), logger)
|
|
return nil
|
|
}
|
|
|
|
// resetAll removes address book files plus all data, and resets the privValdiator data.
|
|
func resetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile string, logger log.Logger) error {
|
|
if keepAddrBook {
|
|
logger.Info("The address book remains intact")
|
|
} else {
|
|
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)
|
|
}
|
|
|
|
if err := tmos.EnsureDir(dbDir, 0700); err != nil {
|
|
logger.Error("unable to recreate dbDir", "err", err)
|
|
}
|
|
|
|
// recreate the dbDir since the privVal state needs to live there
|
|
resetFilePV(privValKeyFile, privValStateFile, logger)
|
|
return nil
|
|
}
|
|
|
|
// resetState removes address book files plus all databases.
|
|
func resetState(dbDir string, logger log.Logger) 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")
|
|
|
|
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 err := tmos.EnsureDir(dbDir, 0700); err != nil {
|
|
logger.Error("unable to recreate dbDir", "err", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func resetFilePV(privValKeyFile, privValStateFile string, logger log.Logger) {
|
|
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,
|
|
)
|
|
} else {
|
|
pv := privval.GenFilePV(privValKeyFile, privValStateFile)
|
|
pv.Save()
|
|
logger.Info(
|
|
"Generated private validator file",
|
|
"keyFile", privValKeyFile,
|
|
"stateFile", privValStateFile,
|
|
)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|