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

Merge storeWasmCode and importWasmCode functions #4088

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -10,7 +10,7 @@ import (
// state.
func (k Keeper) InitGenesis(ctx sdk.Context, gs types.GenesisState) error {
for _, contract := range gs.Contracts {
err := k.importWasmCode(ctx, contract.ContractCode)
_, err := k.storeWasmCode(ctx, contract.ContractCode, false)
if err != nil {
return err
}
Expand Down
31 changes: 8 additions & 23 deletions modules/light-clients/08-wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ func generateWasmCodeHash(code []byte) []byte {
return hash[:]
}

func (k Keeper) storeWasmCode(ctx sdk.Context, code []byte) ([]byte, error) {
func (k Keeper) storeWasmCode(ctx sdk.Context, code []byte, consumeGas bool) ([]byte, error) {
damiannolan marked this conversation as resolved.
Show resolved Hide resolved
store := ctx.KVStore(k.storeKey)

var err error
if types.IsGzip(code) {
ctx.GasMeter().ConsumeGas(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode")
if consumeGas {
ctx.GasMeter().ConsumeGas(types.VMGasRegister.UncompressCosts(len(code)), "Uncompress gzip bytecode")
}
code, err = types.Uncompress(code, types.MaxWasmByteSize())
if err != nil {
return nil, errorsmod.Wrap(err, "failed to store contract")
Expand All @@ -92,7 +94,10 @@ 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")
if consumeGas {
ctx.GasMeter().ConsumeGas(types.VMGasRegister.CompileCosts(len(code)), "Compiling wasm bytecode")
}

codeHash, err := k.wasmVM.StoreCode(code)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to store contract")
Expand All @@ -106,23 +111,3 @@ func (k Keeper) storeWasmCode(ctx sdk.Context, code []byte) ([]byte, error) {
store.Set(codeIDKey, code)
return codeHash, nil
}

func (k Keeper) importWasmCode(ctx sdk.Context, wasmCode []byte) error {
store := ctx.KVStore(k.storeKey)
if types.IsGzip(wasmCode) {
var err error
wasmCode, err = types.Uncompress(wasmCode, types.MaxWasmByteSize())
if err != nil {
return errorsmod.Wrap(err, "failed to store contract")
}
}

codeID, err := k.wasmVM.Create(wasmCode)
if err != nil {
return errorsmod.Wrap(err, "failed to store contract")
}
codeIDKey := types.CodeIDKey(codeID)

store.Set(codeIDKey, wasmCode)
return nil
}
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 @@ -20,7 +20,7 @@ func (k Keeper) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*type
}

ctx := sdk.UnwrapSDKContext(goCtx)
codeID, err := k.storeWasmCode(ctx, msg.Code)
codeID, err := k.storeWasmCode(ctx, msg.Code, true)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to store wasm bytecode")
}
Expand Down