diff --git a/app/app.go b/app/app.go index 8080a39..1bae25c 100644 --- a/app/app.go +++ b/app/app.go @@ -1,6 +1,7 @@ package app import ( + "fmt" "io" "os" "path/filepath" @@ -38,8 +39,6 @@ import ( ) const appName = "WasmApp" -const flagLRUCacheSize = "lru_size" -const flagQueryGasLimit = "query_gas_limit" var ( // DefaultCLIHome default home directories for wasmcli @@ -184,13 +183,13 @@ func NewWasmApp( homeDir := viper.GetString(cli.HomeFlag) wasmDir := filepath.Join(homeDir, "wasm") - // Read LRU cache size from config (app.toml), defaults to `0` - cacheSize := uint64(viper.GetInt64(flagLRUCacheSize)) - // Read query gas limit from config - smartQueryGasLimit := uint64(viper.GetInt64(flagQueryGasLimit)) + wasmConfig := wasm.DefaultWasmConfig() + err := viper.Unmarshal(&wasmConfig) + if err != nil { + fmt.Println("error while reading wasm config:", err.Error()) + } - app.wasmKeeper = wasm.NewKeeper(app.cdc, keys[wasm.StoreKey], app.accountKeeper, app.bankKeeper, wasmRouter, - wasmDir, cacheSize, smartQueryGasLimit) + app.wasmKeeper = wasm.NewKeeper(app.cdc, keys[wasm.StoreKey], app.accountKeeper, app.bankKeeper, wasmRouter, wasmDir, wasmConfig) // create evidence keeper with evidence router app.evidenceKeeper = evidence.NewKeeper( diff --git a/x/wasm/alias.go b/x/wasm/alias.go index 3ffd16d..4bfaaa1 100644 --- a/x/wasm/alias.go +++ b/x/wasm/alias.go @@ -54,6 +54,7 @@ var ( NewWasmCoins = types.NewWasmCoins NewContractInfo = types.NewContractInfo CosmosResult = types.CosmosResult + DefaultWasmConfig = types.DefaultWasmConfig // genesis aliases ValidateGenesis = types.ValidateGenesis @@ -77,6 +78,7 @@ type ( MsgExecuteContract = types.MsgExecuteContract CodeInfo = types.CodeInfo ContractInfo = types.ContractInfo + WasmConfig = types.WasmConfig GenesisState = types.GenesisState ) diff --git a/x/wasm/internal/keeper/keeper.go b/x/wasm/internal/keeper/keeper.go index c889e16..0a6cd05 100644 --- a/x/wasm/internal/keeper/keeper.go +++ b/x/wasm/internal/keeper/keeper.go @@ -43,8 +43,8 @@ type Keeper struct { // NewKeeper creates a new contract Keeper instance func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, accountKeeper auth.AccountKeeper, bankKeeper bank.Keeper, - router sdk.Router, homeDir string, cacheSize uint64, smartQueryGasLimit uint64) Keeper { - wasmer, err := wasm.NewWasmer(filepath.Join(homeDir, "wasm"), cacheSize) + router sdk.Router, homeDir string, wasmConfig types.WasmConfig) Keeper { + wasmer, err := wasm.NewWasmer(filepath.Join(homeDir, "wasm"), wasmConfig.CacheSize) if err != nil { panic(err) } @@ -56,7 +56,7 @@ func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, accountKeeper auth.Accou accountKeeper: accountKeeper, bankKeeper: bankKeeper, router: router, - queryGasLimit: smartQueryGasLimit, + queryGasLimit: wasmConfig.SmartQueryGasLimit, } } diff --git a/x/wasm/internal/keeper/test_common.go b/x/wasm/internal/keeper/test_common.go index 304391b..f22eef0 100644 --- a/x/wasm/internal/keeper/test_common.go +++ b/x/wasm/internal/keeper/test_common.go @@ -16,6 +16,8 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" + + wasmTypes "github.com/cosmwasm/wasmd/x/wasm/internal/types" ) const flagLRUCacheSize = "lru_size" @@ -72,11 +74,10 @@ func CreateTestInput(t *testing.T, isCheckTx bool, tempDir string) (sdk.Context, h := bank.NewHandler(bk) router.AddRoute(bank.RouterKey, h) - // TODO Read LRU cache size, query gas limit from app.toml - cacheSize := uint64(3) - smartQueryGasLimit := uint64(3000000) + // Load default wasm config + wasmConfig := wasmTypes.DefaultWasmConfig() - keeper := NewKeeper(cdc, keyContract, accountKeeper, bk, router, tempDir, cacheSize, smartQueryGasLimit) + keeper := NewKeeper(cdc, keyContract, accountKeeper, bk, router, tempDir, wasmConfig) return ctx, accountKeeper, keeper } diff --git a/x/wasm/internal/types/types.go b/x/wasm/internal/types/types.go index 7e5ffe7..f56392e 100644 --- a/x/wasm/internal/types/types.go +++ b/x/wasm/internal/types/types.go @@ -6,6 +6,9 @@ import ( auth "github.com/cosmos/cosmos-sdk/x/auth/exported" ) +const defaultLRUCacheSize = uint64(0) +const defaultQueryGasLimit = uint64(3000000) + // Model is a struct that holds a KV pair type Model struct { Key string `json:"key"` @@ -81,3 +84,15 @@ func CosmosResult(wasmResult wasmTypes.Result) sdk.Result { GasUsed: wasmResult.GasUsed, } } + +type WasmConfig struct { + SmartQueryGasLimit uint64 `mapstructure:"query_gas_limit"` + CacheSize uint64 `mapstructure:"lru_size"` +} + +func DefaultWasmConfig() WasmConfig { + return WasmConfig{ + SmartQueryGasLimit: defaultQueryGasLimit, + CacheSize: defaultLRUCacheSize, + } +}