Skip to content

Commit

Permalink
Change to StoreCodeUnchecked in Genesis and snapshotter (#5167)
Browse files Browse the repository at this point in the history
* change to store code unchecked

* separate storeWasmCode and importWasmCode

* add parameter to storeWasmCode to pass the store function

* store code for msg server

* fix: typos in godocs

* Update modules/light-clients/08-wasm/internal/ibcwasm/expected_interfaces.go

Co-authored-by: Damian Nolan <damiannolan@gmail.com>

* linting business

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
Co-authored-by: Damian Nolan <damiannolan@gmail.com>
  • Loading branch information
4 people authored Nov 28, 2023
1 parent 82c5021 commit 10bb80b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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) {
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

0 comments on commit 10bb80b

Please sign in to comment.