Skip to content

Commit

Permalink
cli: fix reset command for v0.34 (#8258)
Browse files Browse the repository at this point in the history
Co-authored-by: Callum Waters <cmwaters19@gmail.com>
  • Loading branch information
tnasu and cmwaters committed Apr 13, 2022
1 parent a2c183f commit b4e7b30
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
33 changes: 0 additions & 33 deletions cmd/ostracon/commands/reset_priv_validator_test.go

This file was deleted.

53 changes: 53 additions & 0 deletions cmd/ostracon/commands/reset_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b4e7b30

Please sign in to comment.