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>
(cherry picked from commit 10bb80b)

# Conflicts:
#	modules/light-clients/08-wasm/keeper/keeper.go
#	modules/light-clients/08-wasm/keeper/snapshotter.go
  • Loading branch information
GNaD13 authored and mergify[bot] committed Nov 29, 2023
1 parent e84470f commit e7a2823
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 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 @@ -12,7 +12,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
8 changes: 8 additions & 0 deletions modules/light-clients/08-wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ func (k Keeper) GetAuthority() string {
return k.authority
}

<<<<<<< HEAD
func generateWasmChecksum(code []byte) []byte {
hash := sha256.Sum256(code)
return hash[:]
}

func (k 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) {
>>>>>>> 10bb80b7 (Change to StoreCodeUnchecked in Genesis and snapshotter (#5167))
var err error
if types.IsGzip(code) {
ctx.GasMeter().ConsumeGas(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode")
Expand All @@ -118,7 +122,11 @@ func (k 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")
<<<<<<< HEAD
vmChecksum, err := k.wasmVM.StoreCode(code)
=======
vmChecksum, err := storeFn(code)
>>>>>>> 10bb80b7 (Change to StoreCodeUnchecked in Genesis and snapshotter (#5167))
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
4 changes: 4 additions & 0 deletions modules/light-clients/08-wasm/keeper/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ func restoreV1(_ sdk.Context, k *Keeper, compressedCode []byte) error {
return errorsmod.Wrap(err, "failed to uncompress wasm code")
}

<<<<<<< HEAD
checksum, err := k.wasmVM.StoreCode(wasmCode)
=======
checksum, err := ibcwasm.GetVM().StoreCodeUnchecked(wasmCode)
>>>>>>> 10bb80b7 (Change to StoreCodeUnchecked in Genesis and snapshotter (#5167))
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 @@ -65,6 +65,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 @@ -106,12 +113,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 @@ -129,6 +137,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 e7a2823

Please sign in to comment.