Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(storev2): update snapshot manager and migration manager tests #20441

Merged
merged 25 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
53e4b18
add tests
sontrinh16 May 23, 2024
12a569e
Merge branch 'main' into son/add_store_v2_tests
sontrinh16 May 23, 2024
214605b
change initial version to 0
sontrinh16 May 31, 2024
aefee6a
Revert "change initial version to 0"
sontrinh16 Jun 1, 2024
495ab79
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk
sontrinh16 Jun 1, 2024
f761eb1
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk
sontrinh16 Jun 3, 2024
13cd0df
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk
sontrinh16 Jun 3, 2024
ea4edd5
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk
sontrinh16 Jun 6, 2024
be64070
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk
sontrinh16 Jun 12, 2024
b882b6b
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk
sontrinh16 Jun 12, 2024
9d9a5ab
Merge branch 'main' of https://github.com/cosmos/cosmos-sdk
sontrinh16 Jun 13, 2024
88b5176
Merge branch 'main' into son/add_store_v2_tests
sontrinh16 Jun 13, 2024
dce2bc7
modify test
sontrinh16 Jun 16, 2024
411aac9
Merge branch 'main' into son/add_store_v2_tests
sontrinh16 Jun 23, 2024
820138f
add snapshot take and prune test
sontrinh16 Jun 26, 2024
32b8c4e
update snapshot_take_restore test
sontrinh16 Jun 27, 2024
3bda63a
Merge branch 'son/add_store_v2_tests' of https://github.com/cosmos/co…
sontrinh16 Jun 27, 2024
efa0806
Merge branch 'main' into son/add_store_v2_tests
sontrinh16 Jul 4, 2024
d9d5bd2
address comments
sontrinh16 Jul 9, 2024
909bc02
Merge branch 'son/add_store_v2_tests' of https://github.com/cosmos/co…
sontrinh16 Jul 9, 2024
51c8d84
Merge branch 'main' into son/add_store_v2_tests
sontrinh16 Jul 15, 2024
5ae1ca6
Merge branch 'main' into son/add_store_v2_tests
sontrinh16 Jul 22, 2024
56ecd5d
lint
sontrinh16 Jul 22, 2024
384ac63
fix test
sontrinh16 Jul 22, 2024
71b7180
Merge branch 'main' into son/add_store_v2_tests
sontrinh16 Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions store/v2/migration/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func TestMigrateState(t *testing.T) {
err := m.Migrate(toVersion - 1)
require.NoError(t, err)

// expecting error for conflicting process, since Migrate trigger snapshotter create migration,
// which start a snapshot process already.
_, err = m.snapshotsManager.Create(toVersion - 1)
require.Error(t, err)

if m.stateCommitment != nil {
// check the migrated state
for version := uint64(1); version < toVersion; version++ {
Expand Down
140 changes: 140 additions & 0 deletions store/v2/snapshots/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,143 @@ func TestManager_TakeError(t *testing.T) {
_, err = manager.Create(1)
require.Error(t, err)
}

func TestSnapshot_Take_Restore(t *testing.T) {
store := setupStore(t)
items := [][]byte{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
commitSnapshotter := &mockCommitSnapshotter{
items: items,
}
extSnapshotter := newExtSnapshotter(10)

expectChunks := snapshotItems(items, extSnapshotter)
manager := snapshots.NewManager(store, opts, commitSnapshotter, &mockStorageSnapshotter{}, nil, log.NewNopLogger())
err := manager.RegisterExtensions(extSnapshotter)
require.NoError(t, err)

// creating a snapshot at a higher height should be fine, and should return it
snapshot, err := manager.Create(5)
require.NoError(t, err)

assert.Equal(t, &types.Snapshot{
Height: 5,
Format: commitSnapshotter.SnapshotFormat(),
Chunks: 1,
Hash: []uint8{0xc5, 0xf7, 0xfe, 0xea, 0xd3, 0x4d, 0x3e, 0x87, 0xff, 0x41, 0xa2, 0x27, 0xfa, 0xcb, 0x38, 0x17, 0xa, 0x5, 0xeb, 0x27, 0x4e, 0x16, 0x5e, 0xf3, 0xb2, 0x8b, 0x47, 0xd1, 0xe6, 0x94, 0x7e, 0x8b},
Metadata: types.Metadata{
ChunkHashes: checksums(expectChunks),
},
}, snapshot)

storeSnapshot, chunks, err := store.Load(snapshot.Height, snapshot.Format)
require.NoError(t, err)
assert.Equal(t, snapshot, storeSnapshot)
assert.Equal(t, expectChunks, readChunks(chunks))

err = manager.Restore(*snapshot)
require.NoError(t, err)

// Feeding the chunks should work
for i, chunk := range readChunks(chunks) {
done, err := manager.RestoreChunk(chunk)
require.NoError(t, err)
if i == len(chunks)-1 {
assert.True(t, done)
} else {
assert.False(t, done)
}
}

// The snapshot is saved in local snapshot store
snapshots, err := store.List()
require.NoError(t, err)
require.Equal(t, uint64(5), snapshots[0].Height)
require.Equal(t, types.CurrentFormat, snapshots[0].Format)

// Starting a new restore should fail now, because the target already has contents.
err = manager.Restore(*snapshot)
require.Error(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to check if the restore is done? maybe add more confirmation after restoring

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh this is the check for error when performing restore, i already added a confirmation after restoring above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I mean is, should we compare the restored data?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah gotcha

}

func TestSnapshot_Take_Prune(t *testing.T) {
store := setupStore(t)

// Prune should error while a snapshot is being taken
manager := setupBusyManager(t)
_, err := manager.Prune(2)
require.Error(t, err)

items := [][]byte{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
commitSnapshotter := &mockCommitSnapshotter{
items: items,
}
extSnapshotter := newExtSnapshotter(10)

expectChunks := snapshotItems(items, extSnapshotter)
manager = snapshots.NewManager(store, opts, commitSnapshotter, &mockStorageSnapshotter{}, nil, log.NewNopLogger())
err = manager.RegisterExtensions(extSnapshotter)
require.NoError(t, err)

// creating a snapshot at height 4
snapshot, err := manager.Create(4)
require.NoError(t, err)

assert.Equal(t, &types.Snapshot{
Height: 4,
Format: commitSnapshotter.SnapshotFormat(),
Chunks: 1,
Hash: []uint8{0xc5, 0xf7, 0xfe, 0xea, 0xd3, 0x4d, 0x3e, 0x87, 0xff, 0x41, 0xa2, 0x27, 0xfa, 0xcb, 0x38, 0x17, 0xa, 0x5, 0xeb, 0x27, 0x4e, 0x16, 0x5e, 0xf3, 0xb2, 0x8b, 0x47, 0xd1, 0xe6, 0x94, 0x7e, 0x8b},
Metadata: types.Metadata{
ChunkHashes: checksums(expectChunks),
},
}, snapshot)

pruned, err := manager.Prune(1)
require.NoError(t, err)
assert.EqualValues(t, 4, pruned)

// creating a snapshot at a same height 4, should be error
// since we prune all the previous snapshot except the latest at height 4
_, err = manager.Create(4)
require.Error(t, err)

// prune all
pruned, err = manager.Prune(0)
require.NoError(t, err)
assert.EqualValues(t, 1, pruned)

// creating a snapshot at a same height 4, should be true since we prune all the previous snapshot
snapshot, err = manager.Create(4)
require.NoError(t, err)

assert.Equal(t, &types.Snapshot{
Height: 4,
Format: commitSnapshotter.SnapshotFormat(),
Chunks: 1,
Hash: []uint8{0xc5, 0xf7, 0xfe, 0xea, 0xd3, 0x4d, 0x3e, 0x87, 0xff, 0x41, 0xa2, 0x27, 0xfa, 0xcb, 0x38, 0x17, 0xa, 0x5, 0xeb, 0x27, 0x4e, 0x16, 0x5e, 0xf3, 0xb2, 0x8b, 0x47, 0xd1, 0xe6, 0x94, 0x7e, 0x8b},
Metadata: types.Metadata{
ChunkHashes: checksums(expectChunks),
},
}, snapshot)

storeSnapshot, chunks, err := store.Load(snapshot.Height, snapshot.Format)
require.NoError(t, err)
assert.Equal(t, snapshot, storeSnapshot)
assert.Equal(t, expectChunks, readChunks(chunks))

pruned, err = manager.Prune(2)
require.NoError(t, err)
assert.EqualValues(t, 0, pruned)

list, err := manager.List()
require.NoError(t, err)
assert.Len(t, list, 1)
}
Loading