From c6fadc1e1fac09d2679217a1ea296f83ba0b6074 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 8 Dec 2021 12:09:47 +0100 Subject: [PATCH] cmd: cosmetic changes for errors and print statements (#7377) (#7408) --- cmd/tendermint/commands/reindex_event.go | 10 +++++++++ cmd/tendermint/commands/reindex_event_test.go | 22 +++++++++++++++++-- cmd/tendermint/commands/rollback.go | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/cmd/tendermint/commands/reindex_event.go b/cmd/tendermint/commands/reindex_event.go index 58f11657b..bd9577963 100644 --- a/cmd/tendermint/commands/reindex_event.go +++ b/cmd/tendermint/commands/reindex_event.go @@ -3,6 +3,7 @@ package commands import ( "errors" "fmt" + "path/filepath" "strings" "github.com/spf13/cobra" @@ -16,6 +17,7 @@ import ( "github.com/tendermint/tendermint/internal/state/indexer/sink/kv" "github.com/tendermint/tendermint/internal/state/indexer/sink/psql" "github.com/tendermint/tendermint/internal/store" + "github.com/tendermint/tendermint/libs/os" "github.com/tendermint/tendermint/rpc/coretypes" "github.com/tendermint/tendermint/types" ) @@ -132,6 +134,10 @@ func loadEventSinks(cfg *tmcfg.Config) ([]indexer.EventSink, error) { func loadStateAndBlockStore(cfg *tmcfg.Config) (*store.BlockStore, state.Store, error) { dbType := dbm.BackendType(cfg.DBBackend) + if !os.FileExists(filepath.Join(cfg.DBDir(), "blockstore.db")) { + return nil, nil, fmt.Errorf("no blockstore found in %v", cfg.DBDir()) + } + // Get BlockStore blockStoreDB, err := dbm.NewDB("blockstore", dbType, cfg.DBDir()) if err != nil { @@ -139,6 +145,10 @@ func loadStateAndBlockStore(cfg *tmcfg.Config) (*store.BlockStore, state.Store, } blockStore := store.NewBlockStore(blockStoreDB) + if !os.FileExists(filepath.Join(cfg.DBDir(), "state.db")) { + return nil, nil, fmt.Errorf("no blockstore found in %v", cfg.DBDir()) + } + // Get StateStore stateDB, err := dbm.NewDB("state", dbType, cfg.DBDir()) if err != nil { diff --git a/cmd/tendermint/commands/reindex_event_test.go b/cmd/tendermint/commands/reindex_event_test.go index c76ce6d9a..2008251bc 100644 --- a/cmd/tendermint/commands/reindex_event_test.go +++ b/cmd/tendermint/commands/reindex_event_test.go @@ -15,6 +15,7 @@ import ( "github.com/tendermint/tendermint/internal/state/mocks" prototmstate "github.com/tendermint/tendermint/proto/tendermint/state" "github.com/tendermint/tendermint/types" + dbm "github.com/tendermint/tm-db" _ "github.com/lib/pq" // for the psql sink ) @@ -109,12 +110,29 @@ func TestLoadEventSink(t *testing.T) { } func TestLoadBlockStore(t *testing.T) { - bs, ss, err := loadStateAndBlockStore(tmcfg.TestConfig()) + testCfg, err := tmcfg.ResetTestRoot(t.Name()) + require.NoError(t, err) + testCfg.DBBackend = "goleveldb" + _, _, err = loadStateAndBlockStore(testCfg) + // we should return an error because the state store and block store + // don't yet exist + require.Error(t, err) + + dbType := dbm.BackendType(testCfg.DBBackend) + bsdb, err := dbm.NewDB("blockstore", dbType, testCfg.DBDir()) + require.NoError(t, err) + bsdb.Close() + + ssdb, err := dbm.NewDB("state", dbType, testCfg.DBDir()) + require.NoError(t, err) + ssdb.Close() + + bs, ss, err := loadStateAndBlockStore(testCfg) require.NoError(t, err) require.NotNil(t, bs) require.NotNil(t, ss) - } + func TestReIndexEvent(t *testing.T) { mockBlockStore := &mocks.BlockStore{} mockStateStore := &mocks.Store{} diff --git a/cmd/tendermint/commands/rollback.go b/cmd/tendermint/commands/rollback.go index 5aff232be..b42725ee5 100644 --- a/cmd/tendermint/commands/rollback.go +++ b/cmd/tendermint/commands/rollback.go @@ -26,7 +26,7 @@ application. return fmt.Errorf("failed to rollback state: %w", err) } - fmt.Printf("Rolled back state to height %d and hash %v", height, hash) + fmt.Printf("Rolled back state to height %d and hash %X", height, hash) return nil }, }