diff --git a/modules/light-clients/08-wasm/internal/ibcwasm/expected_interfaces.go b/modules/light-clients/08-wasm/internal/ibcwasm/expected_interfaces.go index cac51178d36..5b57a2f0d0b 100644 --- a/modules/light-clients/08-wasm/internal/ibcwasm/expected_interfaces.go +++ b/modules/light-clients/08-wasm/internal/ibcwasm/expected_interfaces.go @@ -8,13 +8,21 @@ import ( var _ WasmEngine = (*wasmvm.VM)(nil) type WasmEngine interface { - // Create will compile the wasm code, and store the resulting pre-compile + // StoreCode will compile the wasm code, and store the resulting pre-compile // as well as the original code. Both can be referenced later via checksum // This must be done one time for given code, after which it can be - // instatitated many times, and each instance called many times. + // instantiated many times, and each instance called many times. // It does the same as StoreCodeUnchecked plus the static checks. StoreCode(code wasmvm.WasmCode) (wasmvm.Checksum, error) + // StoreCodeUnchecked will compile the wasm code, and store the resulting pre-compile + // as well as the original code. Both can be referenced later via checksum + // This must be done one time for given code, after which it can be + // instantiated many times, and each instance called many times. + // It does the same as StoreCode but without the static checks. + // This allows restoring previous contract code in genesis and state-sync that may have been initially stored under different configuration constraints. + StoreCodeUnchecked(code wasmvm.WasmCode) (wasmvm.Checksum, error) + // Instantiate will create a new contract based on the given checksum. // We can set the initMsg (contract "genesis") here, and it then receives // an account and address and can be invoked (Execute) many times. diff --git a/modules/light-clients/08-wasm/keeper/genesis.go b/modules/light-clients/08-wasm/keeper/genesis.go index c09dd96c254..6ab642097fb 100644 --- a/modules/light-clients/08-wasm/keeper/genesis.go +++ b/modules/light-clients/08-wasm/keeper/genesis.go @@ -11,7 +11,7 @@ import ( // state. func (k Keeper) InitGenesis(ctx sdk.Context, gs types.GenesisState) error { for _, contract := range gs.Contracts { - _, err := k.storeWasmCode(ctx, contract.CodeBytes) + _, err := k.storeWasmCode(ctx, contract.CodeBytes, ibcwasm.GetVM().StoreCodeUnchecked) if err != nil { return err } diff --git a/modules/light-clients/08-wasm/keeper/keeper.go b/modules/light-clients/08-wasm/keeper/keeper.go index d994b1d4c96..72672f6dfd7 100644 --- a/modules/light-clients/08-wasm/keeper/keeper.go +++ b/modules/light-clients/08-wasm/keeper/keeper.go @@ -91,7 +91,7 @@ func (k Keeper) GetAuthority() string { return k.authority } -func (Keeper) storeWasmCode(ctx sdk.Context, code []byte) ([]byte, error) { +func (Keeper) storeWasmCode(ctx sdk.Context, code []byte, storeFn func(code wasmvm.WasmCode) (wasmvm.Checksum, error)) ([]byte, error) { var err error if types.IsGzip(code) { ctx.GasMeter().ConsumeGas(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode") @@ -118,7 +118,7 @@ func (Keeper) storeWasmCode(ctx sdk.Context, code []byte) ([]byte, error) { // create the code in the vm ctx.GasMeter().ConsumeGas(types.VMGasRegister.CompileCosts(len(code)), "Compiling wasm bytecode") - vmChecksum, err := ibcwasm.GetVM().StoreCode(code) + vmChecksum, err := storeFn(code) if err != nil { return nil, errorsmod.Wrap(err, "failed to store contract") } diff --git a/modules/light-clients/08-wasm/keeper/msg_server.go b/modules/light-clients/08-wasm/keeper/msg_server.go index 83ea4879202..46d55f397b5 100644 --- a/modules/light-clients/08-wasm/keeper/msg_server.go +++ b/modules/light-clients/08-wasm/keeper/msg_server.go @@ -22,7 +22,7 @@ func (k Keeper) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*type } ctx := sdk.UnwrapSDKContext(goCtx) - checksum, err := k.storeWasmCode(ctx, msg.WasmByteCode) + checksum, err := k.storeWasmCode(ctx, msg.WasmByteCode, ibcwasm.GetVM().StoreCode) if err != nil { return nil, errorsmod.Wrap(err, "failed to store wasm bytecode") } diff --git a/modules/light-clients/08-wasm/keeper/snapshotter.go b/modules/light-clients/08-wasm/keeper/snapshotter.go index adc16490290..2d474386e49 100644 --- a/modules/light-clients/08-wasm/keeper/snapshotter.go +++ b/modules/light-clients/08-wasm/keeper/snapshotter.go @@ -112,7 +112,7 @@ func restoreV1(ctx sdk.Context, k *Keeper, compressedCode []byte) error { return errorsmod.Wrap(err, "failed to uncompress wasm code") } - checksum, err := ibcwasm.GetVM().StoreCode(wasmCode) + checksum, err := ibcwasm.GetVM().StoreCodeUnchecked(wasmCode) if err != nil { return errorsmod.Wrap(err, "failed to store wasm code") } diff --git a/modules/light-clients/08-wasm/testing/mock_engine.go b/modules/light-clients/08-wasm/testing/mock_engine.go index 6bc94cec775..e15d0ca0cd6 100644 --- a/modules/light-clients/08-wasm/testing/mock_engine.go +++ b/modules/light-clients/08-wasm/testing/mock_engine.go @@ -63,6 +63,13 @@ func NewMockWasmEngine() *MockWasmEngine { return checkSum, nil } + m.StoreCodeUncheckedFn = func(code wasmvm.WasmCode) (wasmvm.Checksum, error) { + checkSum, _ := types.CreateChecksum(code) + + m.storedContracts[binary.LittleEndian.Uint32(checkSum)] = code + return checkSum, nil + } + m.PinFn = func(checksum wasmvm.Checksum) error { return nil } @@ -104,12 +111,13 @@ func (m *MockWasmEngine) RegisterSudoCallback(sudoMessage any, fn sudoFn) { // Without a stub function a panic is thrown. // ref: https://github.com/CosmWasm/wasmd/blob/v0.42.0/x/wasm/keeper/wasmtesting/mock_engine.go#L19 type MockWasmEngine struct { - StoreCodeFn func(code wasmvm.WasmCode) (wasmvm.Checksum, error) - InstantiateFn func(checksum wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, initMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) - MigrateFn func(checksum wasmvm.Checksum, env wasmvmtypes.Env, migrateMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) - GetCodeFn func(checksum wasmvm.Checksum) (wasmvm.WasmCode, error) - PinFn func(checksum wasmvm.Checksum) error - UnpinFn func(checksum wasmvm.Checksum) error + StoreCodeFn func(code wasmvm.WasmCode) (wasmvm.Checksum, error) + StoreCodeUncheckedFn func(code wasmvm.WasmCode) (wasmvm.Checksum, error) + InstantiateFn func(checksum wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, initMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) + MigrateFn func(checksum wasmvm.Checksum, env wasmvmtypes.Env, migrateMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) + GetCodeFn func(checksum wasmvm.Checksum) (wasmvm.WasmCode, error) + PinFn func(checksum wasmvm.Checksum) error + UnpinFn func(checksum wasmvm.Checksum) error // queryCallbacks contains a mapping of queryMsg field type name to callback function. queryCallbacks map[string]queryFn @@ -127,6 +135,14 @@ func (m *MockWasmEngine) StoreCode(code wasmvm.WasmCode) (wasmvm.Checksum, error return m.StoreCodeFn(code) } +// StoreCode implements the WasmEngine interface. +func (m *MockWasmEngine) StoreCodeUnchecked(code wasmvm.WasmCode) (wasmvm.Checksum, error) { + if m.StoreCodeUncheckedFn == nil { + panic(errors.New("mock engine is not properly initialized: StoreCodeUncheckedFn is nil")) + } + return m.StoreCodeUncheckedFn(code) +} + // Instantiate implements the WasmEngine interface. func (m *MockWasmEngine) Instantiate(checksum wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, initMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) { if m.InstantiateFn == nil {