Skip to content

Commit 67c8c57

Browse files
committed
Correctly implement WasmSnapshotter
1 parent 244c929 commit 67c8c57

File tree

1 file changed

+17
-52
lines changed

1 file changed

+17
-52
lines changed

x/compute/internal/keeper/wasm_snapshotter.go

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,15 @@ import (
1313
storetypes "cosmossdk.io/store/types"
1414
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
1515
sdk "github.com/cosmos/cosmos-sdk/types"
16-
protoio "github.com/cosmos/gogoproto/io"
1716
"github.com/scrtlabs/SecretNetwork/x/compute/internal/types"
1817
)
1918

2019
/*
2120
API to implement:
2221
23-
// Snapshotter is something that can create and restore snapshots, consisting of streamed binary
24-
// chunks - all of which must be read from the channel and closed. If an unsupported format is
25-
// given, it must return ErrUnknownFormat (possibly wrapped with fmt.Errorf).
26-
type Snapshotter interface {
27-
// Snapshot writes snapshot items into the protobuf writer.
28-
Snapshot(height uint64, protoWriter protoio.Writer) error
29-
30-
// Restore restores a state snapshot from the protobuf items read from the reader.
31-
// If the ready channel is non-nil, it returns a ready signal (by being closed) once the
32-
// restorer is ready to accept chunks.
33-
Restore(height uint64, format uint32, protoReader protoio.Reader) (SnapshotItem, error)
34-
}
35-
3622
// ExtensionSnapshotter is an extension Snapshotter that is appended to the snapshot stream.
3723
// ExtensionSnapshotter has an unique name and manages it's own internal formats.
3824
type ExtensionSnapshotter interface {
39-
Snapshotter
40-
4125
// SnapshotName returns the name of snapshotter, it should be unique in the manager.
4226
SnapshotName() string
4327
@@ -48,6 +32,13 @@ type ExtensionSnapshotter interface {
4832
4933
// SupportedFormats returns a list of formats it can restore from.
5034
SupportedFormats() []uint32
35+
36+
// SnapshotExtension writes extension payloads into the underlying protobuf stream.
37+
SnapshotExtension(height uint64, payloadWriter ExtensionPayloadWriter) error
38+
39+
// RestoreExtension restores an extension state snapshot,
40+
// the payload reader returns `io.EOF` when reached the extension boundaries.
41+
RestoreExtension(height uint64, format uint32, payloadReader ExtensionPayloadReader) error
5142
}
5243
*/
5344

@@ -81,7 +72,7 @@ func (ws *WasmSnapshotter) SupportedFormats() []uint32 {
8172
return []uint32{1}
8273
}
8374

84-
func (ws *WasmSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) error {
75+
func (ws *WasmSnapshotter) SnapshotExtension(height uint64, payloadWriter snapshottypes.ExtensionPayloadWriter) error {
8576
// TODO: This seems more correct (historical info), but kills my tests
8677
// Since codeIDs and wasm are immutible, it is never wrong to return new wasm data than the
8778
// user requests
@@ -113,7 +104,7 @@ func (ws *WasmSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) e
113104
return true
114105
}
115106

116-
err = snapshottypes.WriteExtensionPayload(protoWriter, wasmBytes)
107+
err = payloadWriter(wasmBytes)
117108
if err != nil {
118109
rerr = err
119110
return true
@@ -125,54 +116,28 @@ func (ws *WasmSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) e
125116
return rerr
126117
}
127118

128-
func (ws *WasmSnapshotter) Restore(
129-
height uint64, format uint32, protoReader protoio.Reader, //nolint:all
130-
) (snapshottypes.SnapshotItem, error) {
119+
func (ws *WasmSnapshotter) RestoreExtension(
120+
height uint64, format uint32, payloadReader snapshottypes.ExtensionPayloadReader, //nolint:all
121+
) error {
131122
if format != 1 {
132-
return snapshottypes.SnapshotItem{}, snapshottypes.ErrUnknownFormat
123+
return snapshottypes.ErrUnknownFormat
133124
}
134125

135126
for {
136-
item := snapshottypes.SnapshotItem{}
137-
err := protoReader.ReadMsg(&item)
127+
wasmBytes, err := payloadReader()
138128
if err == io.EOF {
139-
return snapshottypes.SnapshotItem{}, nil
129+
return nil
140130
} else if err != nil {
141-
return snapshottypes.SnapshotItem{}, errorsmod.Wrap(err, "invalid protobuf message")
142-
}
143-
144-
// if it is not another ExtensionPayload message, then it is not for us.
145-
// we should return it an let the manager handle this one
146-
payload := item.GetExtensionPayload()
147-
if payload == nil {
148-
return item, nil
131+
return errorsmod.Wrap(err, "invalid protobuf message")
149132
}
150133

151-
wasmBytes := payload.Payload
152-
153134
codeHash := sha256.Sum256(wasmBytes)
154135
wasmFileName := hex.EncodeToString(codeHash[:])
155136
wasmFilePath := filepath.Join(ws.wasmDirectory, wasmFileName)
156137

157138
err = os.WriteFile(wasmFilePath, wasmBytes, 0o600 /* -rw------- */)
158139
if err != nil {
159-
return snapshottypes.SnapshotItem{}, errorsmod.Wrapf(err, "failed to write wasm file '%v' to disk", wasmFilePath)
140+
return errorsmod.Wrapf(err, "failed to write wasm file '%v' to disk", wasmFilePath)
160141
}
161142
}
162143
}
163-
164-
func (ws *WasmSnapshotter) PruneSnapshotHeight(_ int64) {
165-
panic("not implemented")
166-
}
167-
168-
func (ws *WasmSnapshotter) SetSnapshotInterval(_ uint64) {
169-
panic("not implemented")
170-
}
171-
172-
func (ws *WasmSnapshotter) RestoreExtension(_ uint64, _ uint32, _ snapshottypes.ExtensionPayloadReader) error {
173-
panic("not implemented")
174-
}
175-
176-
func (ws *WasmSnapshotter) SnapshotExtension(_ uint64, _ snapshottypes.ExtensionPayloadWriter) error {
177-
panic("not implemented")
178-
}

0 commit comments

Comments
 (0)