Skip to content

Commit

Permalink
Add config template for wasm fields
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jan 9, 2023
1 parent 06b715e commit 125f719
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 27 deletions.
25 changes: 4 additions & 21 deletions cmd/wasmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,10 @@ func initTendermintConfig() *tmcfg.Config {
func initAppConfig() (string, interface{}) {
// The following code snippet is just for reference.

// WASMConfig defines configuration for the wasm module.
type WASMConfig struct {
// This is the maximum sdk gas (wasm and storage) that we allow for any x/wasm "smart" queries
QueryGasLimit uint64 `mapstructure:"query_gas_limit"`

// Address defines the gRPC-web server to listen on
LruSize uint64 `mapstructure:"lru_size"`
}

type CustomAppConfig struct {
serverconfig.Config

WASM WASMConfig `mapstructure:"wasm"`
Wasm wasmtypes.WasmConfig `mapstructure:"wasm"`
}

// Optionally allow the chain developer to overwrite the SDK's default
Expand All @@ -156,19 +147,11 @@ func initAppConfig() (string, interface{}) {

customAppConfig := CustomAppConfig{
Config: *srvCfg,
WASM: WASMConfig{
LruSize: 1,
QueryGasLimit: 300000,
},
Wasm: wasmtypes.DefaultWasmConfig(),
}

customAppTemplate := serverconfig.DefaultConfigTemplate + `
[wasm]
# This is the maximum sdk gas (wasm and storage) that we allow for any x/wasm "smart" queries
query_gas_limit = 300000
# This is the number of wasm vm instances we keep cached in memory for speed-up
# Warning: this is currently unstable and may lead to crashes, best to keep for 0 unless testing locally
lru_size = 0`
customAppTemplate := serverconfig.DefaultConfigTemplate +
wasmtypes.DefaultConfigTemplate()

return customAppTemplate, customAppConfig
}
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func ReadWasmConfig(opts servertypes.AppOptions) (types.WasmConfig, error) {
}
}
if v := opts.Get(flagWasmSimulationGasLimit); v != nil {
if raw, ok := v.(string); ok && raw != "" {
if raw, ok := v.(string); !ok || raw != "" {
limit, err := cast.ToUint64E(v) // non empty string set
if err != nil {
return cfg, err
Expand Down
32 changes: 31 additions & 1 deletion x/wasm/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"bytes"
"encoding/json"
"os"
"strings"
"testing"

servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -426,9 +430,17 @@ func TestHandleExecuteEscrow(t *testing.T) {
}

func TestReadWasmConfig(t *testing.T) {
withViper := func(s string) *viper.Viper {
v := viper.New()
v.SetConfigType("toml")
require.NoError(t, v.ReadConfig(strings.NewReader(s)))
return v
}
var one uint64 = 1
defaults := DefaultWasmConfig()

specs := map[string]struct {
src AppOptionsMock
src servertypes.AppOptions
exp types.WasmConfig
}{
"set query gas limit via opts": {
Expand Down Expand Up @@ -460,8 +472,26 @@ func TestReadWasmConfig(t *testing.T) {
},
},
"all defaults when no options set": {
src: AppOptionsMock{},
exp: defaults,
},
"default config template values": {
src: withViper(types.DefaultConfigTemplate()),
exp: defaults,
},
"custom config template values": {
src: withViper(types.ConfigTemplate(types.WasmConfig{
SimulationGasLimit: &one,
SmartQueryGasLimit: 2,
MemoryCacheSize: 3,
})),
exp: types.WasmConfig{
SimulationGasLimit: &one,
SmartQueryGasLimit: 2,
MemoryCacheSize: 3,
ContractDebugMode: false,
},
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
Expand Down
35 changes: 31 additions & 4 deletions x/wasm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ func NewWasmCoins(cosmosCoins sdk.Coins) (wasmCoins []wasmvmtypes.Coin) {
type WasmConfig struct {
// SimulationGasLimit is the max gas to be used in a tx simulation call.
// When not set the consensus max block gas is used instead
SimulationGasLimit *uint64
// SimulationGasLimit is the max gas to be used in a smart query contract call
SmartQueryGasLimit uint64
SimulationGasLimit *uint64 `mapstructure:"simulation_gas_limit"`
// SmartQueryGasLimit is the max gas to be used in a smart query contract call
SmartQueryGasLimit uint64 `mapstructure:"query_gas_limit"`
// MemoryCacheSize in MiB not bytes
MemoryCacheSize uint32
MemoryCacheSize uint32 `mapstructure:"memory_cache_size"`
// ContractDebugMode log what contract print
ContractDebugMode bool
}
Expand All @@ -333,6 +333,33 @@ func DefaultWasmConfig() WasmConfig {
}
}

// DefaultConfigTemplate toml snippet with default values for app.toml
func DefaultConfigTemplate() string {
return ConfigTemplate(DefaultWasmConfig())
}

// ConfigTemplate toml snippet for app.toml
func ConfigTemplate(c WasmConfig) string {
simGasLimit := `# simulation_gas_limit =`
if c.SimulationGasLimit != nil {
simGasLimit = fmt.Sprintf(`simulation_gas_limit = %d`, *c.SimulationGasLimit)
}

return fmt.Sprintf(`
[wasm]
# Smart query gas limit is the max gas to be used in a smart query contract call
query_gas_limit = %d
# in-memory cache for Wasm contracts. Set to 0 to disable.
# The value is in MiB not bytes
memory_cache_size = %d
# Simulation gas limit is the max gas to be used in a tx simulation call.
# When not set the consensus max block gas is used instead
%s
`, c.SmartQueryGasLimit, c.MemoryCacheSize, simGasLimit)
}

// VerifyAddressLen ensures that the address matches the expected length
func VerifyAddressLen() func(addr []byte) error {
return func(addr []byte) error {
Expand Down

0 comments on commit 125f719

Please sign in to comment.