Skip to content

Commit

Permalink
fix: rootmulti's Restore don't return the next unknown item as expect…
Browse files Browse the repository at this point in the history
…ed (#11286)

## Description
Solution:
- return the next unknown item and add a unit test to ensure that.





---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
yihuang authored Feb 28, 2022
1 parent e657190 commit 41bd879
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1,496 deletions.
14 changes: 13 additions & 1 deletion store/rootmulti/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ func TestMultistoreSnapshotRestore(t *testing.T) {
target := newMultiStoreWithMixedMounts(dbm.NewMemDB())
version := uint64(source.LastCommitID().Version)
require.EqualValues(t, 3, version)
dummyExtensionItem := snapshottypes.SnapshotItem{
Item: &snapshottypes.SnapshotItem_Extension{
Extension: &snapshottypes.SnapshotExtensionMeta{
Name: "test",
Format: 1,
},
},
}

chunks := make(chan io.ReadCloser, 100)
go func() {
Expand All @@ -190,12 +198,16 @@ func TestMultistoreSnapshotRestore(t *testing.T) {
defer streamWriter.Close()
err := source.Snapshot(version, streamWriter)
require.NoError(t, err)
// write an extension metadata
err = streamWriter.WriteMsg(&dummyExtensionItem)
require.NoError(t, err)
}()

streamReader, err := snapshots.NewStreamReader(chunks)
require.NoError(t, err)
_, err = target.Restore(version, snapshottypes.CurrentFormat, streamReader)
nextItem, err := target.Restore(version, snapshottypes.CurrentFormat, streamReader)
require.NoError(t, err)
require.Equal(t, *dummyExtensionItem.GetExtension(), *nextItem.GetExtension())

assert.Equal(t, source.LastCommitID(), target.LastCommitID())
for key, sourceStore := range source.GetStores() {
Expand Down
11 changes: 6 additions & 5 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,9 +755,11 @@ func (rs *Store) Restore(
// a SnapshotStoreItem, telling us which store to import into. The following items will contain
// SnapshotNodeItem (i.e. ExportNode) until we reach the next SnapshotStoreItem or EOF.
var importer *iavltree.Importer
var snapshotItem snapshottypes.SnapshotItem
loop:
for {
snapshotItem := &snapshottypes.SnapshotItem{}
err := protoReader.ReadMsg(snapshotItem)
snapshotItem = snapshottypes.SnapshotItem{}
err := protoReader.ReadMsg(&snapshotItem)
if err == io.EOF {
break
} else if err != nil {
Expand Down Expand Up @@ -811,8 +813,7 @@ func (rs *Store) Restore(
}

default:
// pass back the unrecognized item.
return *snapshotItem, nil
break loop
}
}

Expand All @@ -825,7 +826,7 @@ func (rs *Store) Restore(
}

flushMetadata(rs.db, int64(height), rs.buildCommitInfo(int64(height)), []int64{})
return snapshottypes.SnapshotItem{}, rs.LoadLatestVersion()
return snapshotItem, rs.LoadLatestVersion()
}

func (rs *Store) loadCommitStoreFromParams(key types.StoreKey, id types.CommitID, params storeParams) (types.CommitKVStore, error) {
Expand Down
Loading

0 comments on commit 41bd879

Please sign in to comment.