-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
feat: Make extension snapshotter interface safer to use #11825
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f8ee15d
Make extension snapshotter interface safer to use
yihuang c659954
update changelog
yihuang ce63f95
Merge branch 'main' into safer-snapshotter-api
yihuang 3c2ff2b
Merge branch 'main' into safer-snapshotter-api
yihuang 9579e89
Update snapshots/types/util.go
yihuang 9c69a3e
changelog
yihuang 87a15ad
go linter
yihuang 16231ea
Update CHANGELOG.md
yihuang 008f6b7
Merge branch 'main' into safer-snapshotter-api
alexanderbez 14be33a
Merge branch 'main' into safer-snapshotter-api
alexanderbez 705e198
Merge branch 'main' into safer-snapshotter-api
alexanderbez bba13cf
Merge branch 'main' into safer-snapshotter-api
yihuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
"github.com/cosmos/cosmos-sdk/snapshots" | ||
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
|
@@ -62,7 +63,7 @@ func readChunks(chunks <-chan io.ReadCloser) [][]byte { | |
} | ||
|
||
// snapshotItems serialize a array of bytes as SnapshotItem_ExtensionPayload, and return the chunks. | ||
func snapshotItems(items [][]byte) [][]byte { | ||
func snapshotItems(items [][]byte, ext snapshottypes.ExtensionSnapshotter) [][]byte { | ||
// copy the same parameters from the code | ||
snapshotChunkSize := uint64(10e6) | ||
snapshotBufferSize := int(snapshotChunkSize) | ||
|
@@ -74,8 +75,20 @@ func snapshotItems(items [][]byte) [][]byte { | |
zWriter, _ := zlib.NewWriterLevel(bufWriter, 7) | ||
protoWriter := protoio.NewDelimitedWriter(zWriter) | ||
for _, item := range items { | ||
_ = snapshottypes.WriteExtensionItem(protoWriter, item) | ||
_ = snapshottypes.WriteExtensionPayload(protoWriter, item) | ||
} | ||
// write extension metadata | ||
_ = protoWriter.WriteMsg(&snapshottypes.SnapshotItem{ | ||
Item: &snapshottypes.SnapshotItem_Extension{ | ||
Extension: &snapshottypes.SnapshotExtensionMeta{ | ||
Name: ext.SnapshotName(), | ||
Format: ext.SnapshotFormat(), | ||
}, | ||
}, | ||
}) | ||
_ = ext.SnapshotExtension(0, func(payload []byte) error { | ||
return snapshottypes.WriteExtensionPayload(protoWriter, payload) | ||
}) | ||
_ = protoWriter.Close() | ||
_ = zWriter.Close() | ||
_ = bufWriter.Flush() | ||
|
@@ -110,28 +123,29 @@ func (m *mockSnapshotter) Restore( | |
return snapshottypes.SnapshotItem{}, errors.New("already has contents") | ||
} | ||
|
||
var item snapshottypes.SnapshotItem | ||
m.items = [][]byte{} | ||
for { | ||
item := &snapshottypes.SnapshotItem{} | ||
err := protoReader.ReadMsg(item) | ||
item.Reset() | ||
err := protoReader.ReadMsg(&item) | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(err, "invalid protobuf message") | ||
} | ||
payload := item.GetExtensionPayload() | ||
if payload == nil { | ||
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(err, "invalid protobuf message") | ||
break | ||
} | ||
m.items = append(m.items, payload.Payload) | ||
} | ||
|
||
return snapshottypes.SnapshotItem{}, nil | ||
return item, nil | ||
} | ||
|
||
func (m *mockSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) error { | ||
for _, item := range m.items { | ||
if err := snapshottypes.WriteExtensionItem(protoWriter, item); err != nil { | ||
if err := snapshottypes.WriteExtensionPayload(protoWriter, item); err != nil { | ||
return err | ||
} | ||
} | ||
|
@@ -216,3 +230,52 @@ func (m *hungSnapshotter) Restore( | |
) (snapshottypes.SnapshotItem, error) { | ||
panic("not implemented") | ||
} | ||
|
||
type extSnapshotter struct { | ||
state []uint64 | ||
} | ||
|
||
func newExtSnapshotter(count int) *extSnapshotter { | ||
state := make([]uint64, 0, count) | ||
for i := 0; i < count; i++ { | ||
state = append(state, uint64(i)) | ||
} | ||
return &extSnapshotter{ | ||
state, | ||
} | ||
} | ||
|
||
func (s *extSnapshotter) SnapshotName() string { | ||
return "mock" | ||
} | ||
|
||
func (s *extSnapshotter) SnapshotFormat() uint32 { | ||
return 1 | ||
} | ||
|
||
func (s *extSnapshotter) SupportedFormats() []uint32 { | ||
return []uint32{1} | ||
} | ||
|
||
func (s *extSnapshotter) SnapshotExtension(height uint64, payloadWriter snapshottypes.ExtensionPayloadWriter) error { | ||
for _, i := range s.state { | ||
if err := payloadWriter(sdk.Uint64ToBigEndian(uint64(i))); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *extSnapshotter) RestoreExtension(height uint64, format uint32, payloadReader snapshottypes.ExtensionPayloadReader) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An typical extension implementation. |
||
for { | ||
payload, err := payloadReader() | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return err | ||
} | ||
s.state = append(s.state, sdk.BigEndianToUint64(payload)) | ||
} | ||
// finalize restoration | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a test case to test this in handled correctly? I recently ran into an issue where we used .Reset like this and the api is different than what we expected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks straightforward enough to me, what issue did you have? Do you have a concern that after
ReadMsg
, some fields still keep the values from the last one?