From 5472fb5995a0464207f60df5cfab5c6323acbc78 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Sat, 13 May 2023 09:10:01 +0000 Subject: [PATCH] fix: snapshot commands panic if snapshot don't exists (backport #16138) (#16140) Co-authored-by: yihuang Co-authored-by: marbar3778 Co-authored-by: Julien Robert --- .gitignore | 1 + client/snapshot/dump.go | 5 +++++ snapshots/manager.go | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/.gitignore b/.gitignore index 1ce4f5cfa123..1adf64472c42 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ dist tools-stamp buf-stamp artifacts +tools/ # Data - ideally these don't exist baseapp/data/* diff --git a/client/snapshot/dump.go b/client/snapshot/dump.go index 917dca071512..72dadbc57254 100644 --- a/client/snapshot/dump.go +++ b/client/snapshot/dump.go @@ -3,6 +3,7 @@ package snapshot import ( "archive/tar" "compress/gzip" + "errors" "fmt" "io" "os" @@ -48,6 +49,10 @@ func DumpArchiveCmd() *cobra.Command { return err } + if snapshot == nil { + return errors.New("snapshot doesn't exist") + } + bz, err := snapshot.Marshal() if err != nil { return err diff --git a/snapshots/manager.go b/snapshots/manager.go index 4e46116a9199..7bb5cd8abf5f 100644 --- a/snapshots/manager.go +++ b/snapshots/manager.go @@ -398,6 +398,10 @@ func (m *Manager) RestoreLocalSnapshot(height uint64, format uint32) error { return err } + if snapshot == nil { + return fmt.Errorf("snapshot doesn't exist, height: %d, format: %d", height, format) + } + m.mtx.Lock() defer m.mtx.Unlock()