Skip to content

Commit

Permalink
Add WasmLimits to WasmConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Sep 27, 2024
1 parent ace4aa5 commit a50e8d7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type Keeper struct {
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string

// wasmLimits contains the limits sent to wasmvm on init
wasmLimits wasmvmtypes.WasmLimits
}

func (k Keeper) getUploadAccessConfig(ctx context.Context) types.AccessConfig {
Expand All @@ -119,6 +122,10 @@ func (k Keeper) getInstantiateAccessConfig(ctx context.Context) types.AccessType
return k.GetParams(ctx).InstantiateDefaultPermission
}

func (k Keeper) GetWasmLimits() wasmvmtypes.WasmLimits {
return k.wasmLimits
}

// GetParams returns the total set of wasm parameters.
func (k Keeper) GetParams(ctx context.Context) types.Params {
p, err := k.params.Get(ctx)
Expand Down
14 changes: 12 additions & 2 deletions x/wasm/keeper/keeper_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"

wasmvm "github.com/CosmWasm/wasmvm/v2"
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"

"cosmossdk.io/collections"
corestoretypes "cosmossdk.io/core/store"
Expand Down Expand Up @@ -56,7 +57,8 @@ func NewKeeper(
propagateGovAuthorization: map[types.AuthorizationPolicyAction]struct{}{
types.AuthZActionInstantiate: {},
},
authority: authority,
authority: authority,
wasmLimits: wasmConfig.WasmLimits,
}
keeper.messenger = NewDefaultMessageHandler(keeper, router, ics4Wrapper, channelKeeper, capabilityKeeper, bankKeeper, cdc, portSource)
keeper.wasmVMQueryHandler = DefaultQueryPlugins(bankKeeper, stakingKeeper, distrKeeper, channelKeeper, keeper)
Expand All @@ -70,7 +72,15 @@ func NewKeeper(
// NewVM does a lot, so better not to create it and silently drop it.
if keeper.wasmVM == nil {
var err error
keeper.wasmVM, err = wasmvm.NewVM(filepath.Join(homeDir, "wasm"), availableCapabilities, contractMemoryLimit, wasmConfig.ContractDebugMode, wasmConfig.MemoryCacheSize)
keeper.wasmVM, err = wasmvm.NewVMWithConfig(wasmvmtypes.VMConfig{
Cache: wasmvmtypes.CacheOptions{
BaseDir: filepath.Join(homeDir, "wasm"),
AvailableCapabilities: availableCapabilities,
MemoryCacheSize: wasmvmtypes.NewSizeMebi(contractMemoryLimit),
InstanceMemoryLimit: wasmvmtypes.NewSizeMebi(wasmConfig.MemoryCacheSize),
},
WasmLimits: wasmConfig.WasmLimits,
}, wasmConfig.ContractDebugMode)
if err != nil {
panic(err)
}
Expand Down
8 changes: 7 additions & 1 deletion x/wasm/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"runtime/debug"

Expand Down Expand Up @@ -440,8 +441,13 @@ func ensurePaginationParams(req *query.PageRequest) (*query.PageRequest, error)
}

func (q GrpcQuerier) WasmLimitsConfig(c context.Context, req *types.QueryWasmLimitsConfigRequest) (*types.QueryWasmLimitsConfigResponse, error) {
json, err := json.Marshal(q.keeper.GetWasmLimits())
if err != nil {
return nil, err
}

return &types.QueryWasmLimitsConfigResponse{
Config: "{\"todo\": \"put serialized limits here\"}", // TODO: implement
Config: string(json),
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions x/wasm/types/exported_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ViewKeeper interface {
GetByteCode(ctx context.Context, codeID uint64) ([]byte, error)
IsPinnedCode(ctx context.Context, codeID uint64) bool
GetParams(ctx context.Context) Params
GetWasmLimits() wasmvmtypes.WasmLimits
}

// ContractOpsKeeper contains mutable operations on a contract.
Expand Down
2 changes: 2 additions & 0 deletions x/wasm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ type WasmConfig struct {
MemoryCacheSize uint32 `mapstructure:"memory_cache_size"`
// ContractDebugMode log what contract print
ContractDebugMode bool
// WasmLimits are the limits that are used for static validation of Wasm binaries.
WasmLimits wasmvmtypes.WasmLimits
}

// DefaultWasmConfig returns the default settings for WasmConfig
Expand Down

0 comments on commit a50e8d7

Please sign in to comment.