github.com/516108736/tendermint@v0.36.0/cmd/tendermint/commands/reset_priv_validator.go (about) 1 package commands 2 3 import ( 4 "os" 5 6 "github.com/spf13/cobra" 7 8 "github.com/tendermint/tendermint/libs/log" 9 tmos "github.com/tendermint/tendermint/libs/os" 10 "github.com/tendermint/tendermint/privval" 11 ) 12 13 // ResetAllCmd removes the database of this Tendermint core 14 // instance. 15 var ResetAllCmd = &cobra.Command{ 16 Use: "unsafe-reset-all", 17 Aliases: []string{"unsafe_reset_all"}, 18 Short: "(unsafe) Remove all the data and WAL, reset this node's validator to genesis state", 19 Run: resetAll, 20 PreRun: deprecateSnakeCase, 21 } 22 23 var keepAddrBook bool 24 25 func init() { 26 ResetAllCmd.Flags().BoolVar(&keepAddrBook, "keep-addr-book", false, "keep the address book intact") 27 } 28 29 // ResetPrivValidatorCmd resets the private validator files. 30 var ResetPrivValidatorCmd = &cobra.Command{ 31 Use: "unsafe-reset-priv-validator", 32 Aliases: []string{"unsafe_reset_priv_validator"}, 33 Short: "(unsafe) Reset this node's validator to genesis state", 34 Run: resetPrivValidator, 35 PreRun: deprecateSnakeCase, 36 } 37 38 // XXX: this is totally unsafe. 39 // it's only suitable for testnets. 40 func resetAll(cmd *cobra.Command, args []string) { 41 ResetAll(config.DBDir(), config.P2P.AddrBookFile(), config.PrivValidatorKeyFile(), 42 config.PrivValidatorStateFile(), logger) 43 } 44 45 // XXX: this is totally unsafe. 46 // it's only suitable for testnets. 47 func resetPrivValidator(cmd *cobra.Command, args []string) { 48 resetFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(), logger) 49 } 50 51 // ResetAll removes address book files plus all data, and resets the privValdiator data. 52 // Exported so other CLI tools can use it. 53 func ResetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile string, logger log.Logger) { 54 if keepAddrBook { 55 logger.Info("The address book remains intact") 56 } else { 57 removeAddrBook(addrBookFile, logger) 58 } 59 if err := os.RemoveAll(dbDir); err == nil { 60 logger.Info("Removed all blockchain history", "dir", dbDir) 61 } else { 62 logger.Error("Error removing all blockchain history", "dir", dbDir, "err", err) 63 } 64 // recreate the dbDir since the privVal state needs to live there 65 if err := tmos.EnsureDir(dbDir, 0700); err != nil { 66 logger.Error("unable to recreate dbDir", "err", err) 67 } 68 resetFilePV(privValKeyFile, privValStateFile, logger) 69 } 70 71 func resetFilePV(privValKeyFile, privValStateFile string, logger log.Logger) { 72 if _, err := os.Stat(privValKeyFile); err == nil { 73 pv := privval.LoadFilePVEmptyState(privValKeyFile, privValStateFile) 74 pv.Reset() 75 logger.Info("Reset private validator file to genesis state", "keyFile", privValKeyFile, 76 "stateFile", privValStateFile) 77 } else { 78 pv := privval.GenFilePV(privValKeyFile, privValStateFile) 79 pv.Save() 80 logger.Info("Generated private validator file", "keyFile", privValKeyFile, 81 "stateFile", privValStateFile) 82 } 83 } 84 85 func removeAddrBook(addrBookFile string, logger log.Logger) { 86 if err := os.Remove(addrBookFile); err == nil { 87 logger.Info("Removed existing address book", "file", addrBookFile) 88 } else if !os.IsNotExist(err) { 89 logger.Info("Error removing address book", "file", addrBookFile, "err", err) 90 } 91 }