Skip to content
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

Change to StoreCodeUnchecked in Genesis and snapshotter #5167

Merged
merged 10 commits into from
Nov 28, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ 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.
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
// 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
DimitrisJim marked this conversation as resolved.
Show resolved Hide resolved
// 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.
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
// It does the same as StoreCode but without the static checks.
StoreCodeUnchecked(code wasmvm.WasmCode) (wasmvm.Checksum, error)
DimitrisJim marked this conversation as resolved.
Show resolved Hide resolved

// 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.
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions modules/light-clients/08-wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I'd be down for a slick alias for storefn but I think this might be a code hygiene thing we can do later (as it can be done elsewhere too).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would support the idea of an alias 👍🏻

var err error
if types.IsGzip(code) {
ctx.GasMeter().ConsumeGas(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode")
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion modules/light-clients/08-wasm/keeper/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
28 changes: 22 additions & 6 deletions modules/light-clients/08-wasm/testing/mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Loading