diff --git a/store/snapshots/manager.go b/store/snapshots/manager.go index 663b534e57..a61e272479 100644 --- a/store/snapshots/manager.go +++ b/store/snapshots/manager.go @@ -8,6 +8,7 @@ import ( "io" "math" "os" + "slices" "sort" "sync" @@ -493,12 +494,7 @@ func (m *Manager) sortedExtensionNames() []string { // IsFormatSupported returns if the snapshotter supports restoration from given format. func IsFormatSupported(snapshotter types.ExtensionSnapshotter, format uint32) bool { - for _, i := range snapshotter.SupportedFormats() { - if i == format { - return true - } - } - return false + return slices.Contains(snapshotter.SupportedFormats(), format) } // SnapshotIfApplicable takes a snapshot of the current state if we are on a snapshot height. diff --git a/store/snapshots/manager_test.go b/store/snapshots/manager_test.go index 49f31e8627..e5a38a9e6e 100644 --- a/store/snapshots/manager_test.go +++ b/store/snapshots/manager_test.go @@ -256,3 +256,30 @@ func TestManager_TakeError(t *testing.T) { _, err = manager.Create(1) require.Error(t, err) } + +type mockExtensionSnapshotter struct { + types.ExtensionSnapshotter + formats []uint32 +} + +func (m *mockExtensionSnapshotter) SnapshotName() string { return "mock" } +func (m *mockExtensionSnapshotter) SupportedFormats() []uint32 { return m.formats } + +func TestIsFormatSupported(t *testing.T) { + mockExtension := &mockExtensionSnapshotter{ + formats: []uint32{1, 2}, + } + + t.Run("supported format", func(t *testing.T) { + require.True(t, snapshots.IsFormatSupported(mockExtension, 1)) + }) + + t.Run("unsupported format", func(t *testing.T) { + require.False(t, snapshots.IsFormatSupported(mockExtension, 3)) + }) + + t.Run("empty supported formats", func(t *testing.T) { + emptyExtension := &mockExtensionSnapshotter{formats: []uint32{}} + require.False(t, snapshots.IsFormatSupported(emptyExtension, 1)) + }) +}