From 11bf51094773fc9777aa183d2e4cadac9ce18c2d Mon Sep 17 00:00:00 2001 From: mmsqe Date: Wed, 28 Sep 2022 02:16:13 +0800 Subject: [PATCH] fix: double close (#13400) (cherry picked from commit dcb0c9c04c5d8406bede521cde94335015185b2b) --- CHANGELOG.md | 1 + baseapp/baseapp_test.go | 2 +- baseapp/deliver_tx_test.go | 12 +++---- store/rootmulti/snapshot_test.go | 2 +- store/snapshots/helpers_test.go | 54 +++----------------------------- store/snapshots/manager_test.go | 6 ++-- store/snapshots/types/format.go | 2 +- 7 files changed, 18 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d8dabe686a3..460f023b78d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (deps) Bump Tendermint version to [v0.34.23](https://github.com/tendermint/tendermint/releases/tag/v0.34.23). * (deps) Bump IAVL version to [v0.19.4](https://github.com/cosmos/iavl/releases/tag/v0.19.4). +* (snapshot) [#13400](https://github.com/cosmos/cosmos-sdk/pull/13400) Fix snapshot checksum issue in golang 1.19. ### Bug Fixes diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 7cd9bab02007..174a0d378b8a 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -176,7 +176,7 @@ func TestListSnapshots(t *testing.T) { app, _ := setupBaseAppWithSnapshots(t, 2, 5) expected := abci.ResponseListSnapshots{Snapshots: []*abci.Snapshot{ - {Height: 2, Format: 1, Chunks: 2}, + {Height: 2, Format: 2, Chunks: 2}, }} resp := app.ListSnapshots(abci.RequestListSnapshots{}) diff --git a/baseapp/deliver_tx_test.go b/baseapp/deliver_tx_test.go index c0126f9964f4..df2dea33a8a7 100644 --- a/baseapp/deliver_tx_test.go +++ b/baseapp/deliver_tx_test.go @@ -39,13 +39,13 @@ func TestLoadSnapshotChunk(t *testing.T) { chunk uint32 expectEmpty bool }{ - "Existing snapshot": {2, 1, 1, false}, - "Missing height": {100, 1, 1, true}, - "Missing format": {2, 2, 1, true}, - "Missing chunk": {2, 1, 9, true}, - "Zero height": {0, 1, 1, true}, + "Existing snapshot": {2, 2, 1, false}, + "Missing height": {100, 2, 1, true}, + "Missing format": {2, 3, 1, true}, + "Missing chunk": {2, 2, 9, true}, + "Zero height": {0, 2, 1, true}, "Zero format": {2, 0, 1, true}, - "Zero chunk": {2, 1, 0, false}, + "Zero chunk": {2, 2, 0, false}, } for name, tc := range testcases { tc := tc diff --git a/store/rootmulti/snapshot_test.go b/store/rootmulti/snapshot_test.go index 7a3071940e99..48cd4ff8f4f1 100644 --- a/store/rootmulti/snapshot_test.go +++ b/store/rootmulti/snapshot_test.go @@ -128,7 +128,7 @@ func TestMultistoreSnapshot_Checksum(t *testing.T) { "aa048b4ee0f484965d7b3b06822cf0772cdcaad02f3b1b9055e69f2cb365ef3c", "7921eaa3ed4921341e504d9308a9877986a879fe216a099c86e8db66fcba4c63", "a4a864e6c02c9fca5837ec80dc84f650b25276ed7e4820cf7516ced9f9901b86", - "ca2879ac6e7205d257440131ba7e72bef784cd61642e32b847729e543c1928b9", + "980925390cc50f14998ecb1e87de719ca9dd7e72f5fefbe445397bf670f36c31", }}, } for _, tc := range testcases { diff --git a/store/snapshots/helpers_test.go b/store/snapshots/helpers_test.go index b34f90dd6ab8..b150378a1130 100644 --- a/store/snapshots/helpers_test.go +++ b/store/snapshots/helpers_test.go @@ -89,9 +89,9 @@ func snapshotItems(items [][]byte, ext snapshottypes.ExtensionSnapshotter) [][]b _ = ext.SnapshotExtension(0, func(payload []byte) error { return snapshottypes.WriteExtensionPayload(protoWriter, payload) }) - _ = protoWriter.Close() - _ = bufWriter.Flush() - _ = chunkWriter.Close() + protoWriter.Close() + bufWriter.Flush() + chunkWriter.Close() }() var chunks [][]byte @@ -152,55 +152,11 @@ func (m *mockSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) er } func (m *mockSnapshotter) SnapshotFormat() uint32 { - return snapshottypes.CurrentFormat + return 2 } func (m *mockSnapshotter) SupportedFormats() []uint32 { - return []uint32{snapshottypes.CurrentFormat} -} - -func (m *mockSnapshotter) PruneSnapshotHeight(height int64) { - m.prunedHeights[height] = struct{}{} -} - -func (m *mockSnapshotter) GetSnapshotInterval() uint64 { - return m.snapshotInterval -} - -func (m *mockSnapshotter) SetSnapshotInterval(snapshotInterval uint64) { - m.snapshotInterval = snapshotInterval -} - -type mockErrorSnapshotter struct{} - -var _ snapshottypes.Snapshotter = (*mockErrorSnapshotter)(nil) - -func (m *mockErrorSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) error { - return errors.New("mock snapshot error") -} - -func (m *mockErrorSnapshotter) Restore( - height uint64, format uint32, protoReader protoio.Reader, -) (snapshottypes.SnapshotItem, error) { - return snapshottypes.SnapshotItem{}, errors.New("mock restore error") -} - -func (m *mockErrorSnapshotter) SnapshotFormat() uint32 { - return snapshottypes.CurrentFormat -} - -func (m *mockErrorSnapshotter) SupportedFormats() []uint32 { - return []uint32{snapshottypes.CurrentFormat} -} - -func (m *mockErrorSnapshotter) PruneSnapshotHeight(height int64) { -} - -func (m *mockErrorSnapshotter) GetSnapshotInterval() uint64 { - return 0 -} - -func (m *mockErrorSnapshotter) SetSnapshotInterval(snapshotInterval uint64) { + return []uint32{2} } // setupBusyManager creates a manager with an empty store that is busy creating a snapshot at height 1. diff --git a/store/snapshots/manager_test.go b/store/snapshots/manager_test.go index 6264a0b70d47..d62b6117a795 100644 --- a/store/snapshots/manager_test.go +++ b/store/snapshots/manager_test.go @@ -97,7 +97,7 @@ func TestManager_Take(t *testing.T) { Height: 5, Format: snapshotter.SnapshotFormat(), Chunks: 1, - Hash: []uint8{0x89, 0xfa, 0x18, 0xbc, 0x5a, 0xe3, 0xdc, 0x36, 0xa6, 0x95, 0x5, 0x17, 0xf9, 0x2, 0x1a, 0x55, 0x36, 0x16, 0x5d, 0x4b, 0x8b, 0x2b, 0x3d, 0xfd, 0xe, 0x2f, 0xb6, 0x40, 0x6b, 0xc3, 0xbc, 0x23}, + 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), }, @@ -178,7 +178,7 @@ func TestManager_Restore(t *testing.T) { // Starting a restore works err = manager.Restore(types.Snapshot{ Height: 3, - Format: types.CurrentFormat, + Format: 2, Hash: []byte{1, 2, 3}, Chunks: 1, Metadata: types.Metadata{ChunkHashes: checksums(chunks)}, @@ -229,7 +229,7 @@ func TestManager_Restore(t *testing.T) { target.items = nil err = manager.Restore(types.Snapshot{ Height: 3, - Format: types.CurrentFormat, + Format: 2, Hash: []byte{1, 2, 3}, Chunks: 1, Metadata: types.Metadata{ChunkHashes: checksums(chunks)}, diff --git a/store/snapshots/types/format.go b/store/snapshots/types/format.go index 317b6a6e329e..d5e960660ac9 100644 --- a/store/snapshots/types/format.go +++ b/store/snapshots/types/format.go @@ -3,4 +3,4 @@ package types // CurrentFormat is the currently used format for snapshots. Snapshots using the same format // must be identical across all nodes for a given height, so this must be bumped when the binary // snapshot output changes. -const CurrentFormat uint32 = 3 +const CurrentFormat uint32 = 2