From b4e7b3002fdce0af22164dd9fbbd939ecdbdbd5e Mon Sep 17 00:00:00 2001 From: tnasu Date: Wed, 13 Apr 2022 15:41:53 +0900 Subject: [PATCH] cli: fix reset command for v0.34 (#8258) Co-authored-by: Callum Waters --- .../{reset_priv_validator.go => reset.go} | 14 ++--- .../commands/reset_priv_validator_test.go | 33 ------------ cmd/ostracon/commands/reset_test.go | 53 +++++++++++++++++++ 3 files changed, 57 insertions(+), 43 deletions(-) rename cmd/ostracon/commands/{reset_priv_validator.go => reset.go} (93%) delete mode 100644 cmd/ostracon/commands/reset_priv_validator_test.go create mode 100644 cmd/ostracon/commands/reset_test.go diff --git a/cmd/ostracon/commands/reset_priv_validator.go b/cmd/ostracon/commands/reset.go similarity index 93% rename from cmd/ostracon/commands/reset_priv_validator.go rename to cmd/ostracon/commands/reset.go index 12ee3d96b..3e97bb43d 100644 --- a/cmd/ostracon/commands/reset_priv_validator.go +++ b/cmd/ostracon/commands/reset.go @@ -80,11 +80,13 @@ func resetAll(dbDir, addrBookFile, privValKeyFile, privValStateFile, privKeyType logger.Error("Error removing all blockchain history", "dir", dbDir, "err", err) } - // recreate the dbDir since the privVal state needs to live there if err := tmos.EnsureDir(dbDir, 0700); err != nil { logger.Error("unable to recreate dbDir", "err", err) } - return resetFilePV(privValKeyFile, privValStateFile, privKeyType, logger) + + // recreate the dbDir since the privVal state needs to live there + resetFilePV(privValKeyFile, privValStateFile, privKeyType, logger) + return nil } // resetState removes address book files plus all databases. @@ -94,7 +96,6 @@ func resetState(dbDir string, logger log.Logger) error { 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 { @@ -136,13 +137,6 @@ func resetState(dbDir string, logger log.Logger) error { } } - 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) } diff --git a/cmd/ostracon/commands/reset_priv_validator_test.go b/cmd/ostracon/commands/reset_priv_validator_test.go deleted file mode 100644 index 84694d4c2..000000000 --- a/cmd/ostracon/commands/reset_priv_validator_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package commands - -import ( - "os" - "path/filepath" - "testing" - - "github.com/stretchr/testify/require" -) - -func setupResetCmd(t *testing.T) { - clearConfig(defaultRoot) - config.SetRoot(defaultRoot) - require.NoError(t, os.MkdirAll(filepath.Dir(config.PrivValidatorKeyFile()), 0755)) - require.NoError(t, os.MkdirAll(filepath.Dir(config.PrivValidatorStateFile()), 0755)) -} - -func TestResetAllCmd(t *testing.T) { - setupResetCmd(t) - err := ResetAllCmd.RunE(ResetAllCmd, nil) - require.NoError(t, err) -} - -func TestResetStateCmd(t *testing.T) { - setupResetCmd(t) - err := ResetStateCmd.RunE(ResetStateCmd, nil) - require.NoError(t, err) -} - -func TestResetPrivValidatorCmd(t *testing.T) { - setupResetCmd(t) - ResetPrivValidatorCmd.Run(ResetPrivValidatorCmd, nil) -} diff --git a/cmd/ostracon/commands/reset_test.go b/cmd/ostracon/commands/reset_test.go new file mode 100644 index 000000000..5faaca00c --- /dev/null +++ b/cmd/ostracon/commands/reset_test.go @@ -0,0 +1,53 @@ +package commands + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + cfg "github.com/line/ostracon/config" + "github.com/line/ostracon/privval" +) + +func Test_ResetAll(t *testing.T) { + config := cfg.TestConfig() + dir := t.TempDir() + config.SetRoot(dir) + cfg.EnsureRoot(dir) + require.NoError(t, initFilesWithConfig(config)) + pv := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) + pv.LastSignState.Height = 10 + pv.Save() + require.NoError(t, resetAll(config.DBDir(), config.P2P.AddrBookFile(), config.PrivValidatorKeyFile(), + config.PrivValidatorStateFile(), config.PrivKeyType, logger)) + require.DirExists(t, config.DBDir()) + require.NoFileExists(t, filepath.Join(config.DBDir(), "block.db")) + require.NoFileExists(t, filepath.Join(config.DBDir(), "state.db")) + require.NoFileExists(t, filepath.Join(config.DBDir(), "evidence.db")) + require.NoFileExists(t, filepath.Join(config.DBDir(), "tx_index.db")) + require.FileExists(t, config.PrivValidatorStateFile()) + pv = privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) + require.Equal(t, int64(0), pv.LastSignState.Height) +} + +func Test_ResetState(t *testing.T) { + config := cfg.TestConfig() + dir := t.TempDir() + config.SetRoot(dir) + cfg.EnsureRoot(dir) + require.NoError(t, initFilesWithConfig(config)) + pv := privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) + pv.LastSignState.Height = 10 + pv.Save() + require.NoError(t, resetState(config.DBDir(), logger)) + require.DirExists(t, config.DBDir()) + require.NoFileExists(t, filepath.Join(config.DBDir(), "block.db")) + require.NoFileExists(t, filepath.Join(config.DBDir(), "state.db")) + require.NoFileExists(t, filepath.Join(config.DBDir(), "evidence.db")) + require.NoFileExists(t, filepath.Join(config.DBDir(), "tx_index.db")) + require.FileExists(t, config.PrivValidatorStateFile()) + pv = privval.LoadFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()) + // private validator state should still be in tact. + require.Equal(t, int64(10), pv.LastSignState.Height) +}