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