Skip to content

Commit a434d3d

Browse files
authored
Add validation for restore (#47)
1 parent d09b588 commit a434d3d

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

x/wasm/keeper/snapshotter.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package keeper
22

33
import (
44
"encoding/hex"
5+
"errors"
6+
"fmt"
57
"io"
68

79
snapshot "github.com/cosmos/cosmos-sdk/snapshots/types"
@@ -98,37 +100,53 @@ func (ws *WasmSnapshotter) Restore(
98100
return snapshot.SnapshotItem{}, snapshot.ErrUnknownFormat
99101
}
100102

101-
func restoreV1(ctx sdk.Context, k *Keeper, compressedCode []byte) error {
103+
func restoreV1(ctx sdk.Context, k *Keeper, compressedCode []byte, num int) error {
102104
wasmCode, err := ioutils.Uncompress(compressedCode, uint64(types.MaxWasmSize))
103105
if err != nil {
104106
return sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
105107
}
106108

107109
// FIXME: check which codeIDs the checksum matches??
108-
_, err = k.wasmVM.Create(wasmCode)
110+
checkSum, err := k.wasmVM.Create(wasmCode)
109111
if err != nil {
110112
return sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
111113
}
114+
ctx.Logger().Info(fmt.Sprintf("Restored %d WASM code with checksum %X", num, checkSum))
112115
return nil
113116
}
114117

115118
func finalizeV1(ctx sdk.Context, k *Keeper) error {
116-
// FIXME: ensure all codes have been uploaded?
119+
var errCheckingExistence error
120+
k.IterateCodeInfos(ctx, func(id uint64, info types.CodeInfo) bool {
121+
_, err := k.GetByteCode(ctx, id)
122+
if err != nil {
123+
e := fmt.Sprintf("Could not find byte code for ID %d hash %X: %s", id, info.CodeHash, err)
124+
ctx.Logger().Error(e)
125+
errCheckingExistence = errors.New(e)
126+
}
127+
128+
return false
129+
})
130+
if errCheckingExistence != nil {
131+
return errCheckingExistence
132+
}
117133
return k.InitializePinnedCodes(ctx)
118134
}
119135

120136
func (ws *WasmSnapshotter) processAllItems(
121137
height uint64,
122138
protoReader protoio.Reader,
123-
cb func(sdk.Context, *Keeper, []byte) error,
139+
cb func(sdk.Context, *Keeper, []byte, int) error,
124140
finalize func(sdk.Context, *Keeper) error,
125141
) (snapshot.SnapshotItem, error) {
126142
ctx := sdk.NewContext(ws.cms, tmproto.Header{Height: int64(height)}, false, log.NewNopLogger())
127143

128144
// keep the last item here... if we break, it will either be empty (if we hit io.EOF)
129145
// or contain the last item (if we hit payload == nil)
130146
var item snapshot.SnapshotItem
147+
itemNum := 0
131148
for {
149+
itemNum++
132150
item = snapshot.SnapshotItem{}
133151
err := protoReader.ReadMsg(&item)
134152
if err == io.EOF {
@@ -144,7 +162,7 @@ func (ws *WasmSnapshotter) processAllItems(
144162
break
145163
}
146164

147-
if err := cb(ctx, ws.wasm, payload.Payload); err != nil {
165+
if err := cb(ctx, ws.wasm, payload.Payload, itemNum); err != nil {
148166
return snapshot.SnapshotItem{}, sdkerrors.Wrap(err, "processing snapshot item")
149167
}
150168
}

0 commit comments

Comments
 (0)