diff --git a/app/app.go b/app/app.go index 95270325..85d24afa 100644 --- a/app/app.go +++ b/app/app.go @@ -97,7 +97,6 @@ import ( "github.com/terra-money/core/v2/app/ante" terraappconfig "github.com/terra-money/core/v2/app/config" terraappparams "github.com/terra-money/core/v2/app/params" - "github.com/terra-money/core/v2/app/wasmconfig" // unnamed import of statik for swagger UI support _ "github.com/terra-money/core/v2/client/docs/statik" @@ -190,7 +189,7 @@ func NewTerraApp( invCheckPeriod uint, encodingConfig terraappparams.EncodingConfig, appOpts servertypes.AppOptions, - wasmConfig *wasmconfig.Config, + wasmConfig wasmtypes.WasmConfig, baseAppOptions ...func(*baseapp.BaseApp), ) *TerraApp { appCodec := encodingConfig.Marshaler @@ -273,7 +272,7 @@ func NewTerraApp( FeeShareKeeper: app.Keepers.FeeShareKeeper, IBCkeeper: app.Keepers.IBCKeeper, TxCounterStoreKey: app.keys[wasmtypes.StoreKey], - WasmConfig: wasmConfig.ToWasmConfig(), + WasmConfig: wasmConfig, PobBuilderKeeper: app.Keepers.BuilderKeeper, TxConfig: encodingConfig.TxConfig, PobMempool: pobMempool, diff --git a/app/app_test/app_test.go b/app/app_test/app_test.go index 23b99ad6..829ae49a 100644 --- a/app/app_test/app_test.go +++ b/app/app_test/app_test.go @@ -13,7 +13,6 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "github.com/terra-money/alliance/x/alliance" - "github.com/terra-money/core/v2/app/wasmconfig" "github.com/terra-money/core/v2/x/feeshare" "github.com/terra-money/core/v2/x/tokenfactory" @@ -54,6 +53,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/upgrade" "github.com/CosmWasm/wasmd/x/wasm" + wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" terra_app "github.com/terra-money/core/v2/app" ) @@ -78,7 +78,7 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) { app := terra_app.NewTerraApp( log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, terra_app.DefaultNodeHome, 0, encCfg, - simtestutil.EmptyAppOptions{}, wasmconfig.DefaultConfig()) + simtestutil.EmptyAppOptions{}, wasmtypes.DefaultWasmConfig()) // generate validator private/public key privVal := mocktestutils.NewPV() @@ -114,7 +114,7 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) { app2 := terra_app.NewTerraApp( log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, terra_app.DefaultNodeHome, 0, - encCfg, simtestutil.EmptyAppOptions{}, wasmconfig.DefaultConfig()) + encCfg, simtestutil.EmptyAppOptions{}, wasmtypes.DefaultWasmConfig()) _, err = app2.ExportAppStateAndValidators(false, []string{}, []string{}) require.NoError(t, err, "ExportAppStateAndValidators should not have an error") } @@ -125,7 +125,7 @@ func TestInitGenesisOnMigration(t *testing.T) { logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) app := terra_app.NewTerraApp( logger, db, nil, true, map[int64]bool{}, - terra_app.DefaultNodeHome, 0, encCfg, simtestutil.EmptyAppOptions{}, wasmconfig.DefaultConfig()) + terra_app.DefaultNodeHome, 0, encCfg, simtestutil.EmptyAppOptions{}, wasmtypes.DefaultWasmConfig()) ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()}) @@ -215,7 +215,7 @@ func TestLegacyAmino(t *testing.T) { app := terra_app.NewTerraApp( log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, terra_app.DefaultNodeHome, 0, - encCfg, simtestutil.EmptyAppOptions{}, wasmconfig.DefaultConfig()) + encCfg, simtestutil.EmptyAppOptions{}, wasmtypes.DefaultWasmConfig()) require.Equal(t, encCfg.Amino, app.LegacyAmino()) } @@ -226,7 +226,7 @@ func TestAppCodec(t *testing.T) { app := terra_app.NewTerraApp( log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, terra_app.DefaultNodeHome, 0, - encCfg, simtestutil.EmptyAppOptions{}, wasmconfig.DefaultConfig()) + encCfg, simtestutil.EmptyAppOptions{}, wasmtypes.DefaultWasmConfig()) require.Equal(t, encCfg.Marshaler, app.AppCodec()) } @@ -237,7 +237,7 @@ func TestInterfaceRegistry(t *testing.T) { app := terra_app.NewTerraApp( log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, terra_app.DefaultNodeHome, 0, - encCfg, simtestutil.EmptyAppOptions{}, wasmconfig.DefaultConfig()) + encCfg, simtestutil.EmptyAppOptions{}, wasmtypes.DefaultWasmConfig()) require.Equal(t, encCfg.InterfaceRegistry, app.InterfaceRegistry()) } @@ -248,7 +248,7 @@ func TestGetKey(t *testing.T) { app := terra_app.NewTerraApp( log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, terra_app.DefaultNodeHome, 0, - encCfg, simtestutil.EmptyAppOptions{}, wasmconfig.DefaultConfig()) + encCfg, simtestutil.EmptyAppOptions{}, wasmtypes.DefaultWasmConfig()) require.NotEmpty(t, app.GetKey(banktypes.StoreKey)) require.NotEmpty(t, app.GetTKey(paramstypes.TStoreKey)) @@ -261,7 +261,7 @@ func TestSimAppEnforceStakingForVestingTokens(t *testing.T) { app := terra_app.NewTerraApp( log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, terra_app.DefaultNodeHome, 0, encCfg, - simtestutil.EmptyAppOptions{}, wasmconfig.DefaultConfig(), + simtestutil.EmptyAppOptions{}, wasmtypes.DefaultWasmConfig(), ) genAccounts := authtypes.GenesisAccounts{ vestingtypes.NewContinuousVestingAccount( diff --git a/app/app_test/test_helpers.go b/app/app_test/test_helpers.go index 727c6f88..ad6ac4fe 100644 --- a/app/app_test/test_helpers.go +++ b/app/app_test/test_helpers.go @@ -14,10 +14,8 @@ import ( tmproto "github.com/cometbft/cometbft/proto/tendermint/types" tmtypes "github.com/cometbft/cometbft/types" "github.com/stretchr/testify/suite" - "github.com/terra-money/core/v2/app" terra_app "github.com/terra-money/core/v2/app" appparams "github.com/terra-money/core/v2/app/params" - "github.com/terra-money/core/v2/app/wasmconfig" feesharetypes "github.com/terra-money/core/v2/x/feeshare/types" tokenfactorytypes "github.com/terra-money/core/v2/x/tokenfactory/types" @@ -36,7 +34,7 @@ import ( type AppTestSuite struct { suite.Suite - App *app.TerraApp + App *terra_app.TerraApp Ctx sdk.Context QueryHelper *baseapp.QueryServiceTestHelper TestAccs []sdk.AccAddress @@ -47,7 +45,7 @@ type AppTestSuite struct { func (s *AppTestSuite) Setup() { appparams.RegisterAddressesConfig() encCfg := terra_app.MakeEncodingConfig() - genesisState := app.NewDefaultGenesisState(encCfg.Marshaler) + genesisState := terra_app.NewDefaultGenesisState(encCfg.Marshaler) genesisState.SetDefaultTerraConfig(encCfg.Marshaler) db := dbm.NewMemDB() @@ -61,7 +59,7 @@ func (s *AppTestSuite) Setup() { 0, encCfg, simtestutil.EmptyAppOptions{}, - wasmconfig.DefaultConfig(), + wasmtypes.DefaultWasmConfig(), ) s.EncodingConfig = encCfg diff --git a/app/simulation_test.go b/app/simulation_test.go index 11d92968..0a1906d2 100644 --- a/app/simulation_test.go +++ b/app/simulation_test.go @@ -10,8 +10,8 @@ import ( "github.com/stretchr/testify/require" "github.com/terra-money/core/v2/app" "github.com/terra-money/core/v2/app/keepers" - "github.com/terra-money/core/v2/app/wasmconfig" + wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" simulationtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/simulation" @@ -54,7 +54,7 @@ func BenchmarkSimulation(b *testing.B) { 0, encoding, simtestutil.EmptyAppOptions{}, - wasmconfig.DefaultConfig(), + wasmtypes.DefaultWasmConfig(), ) // Run randomized simulations @@ -94,7 +94,7 @@ func TestSimulationManager(t *testing.T) { 0, encoding, simtestutil.EmptyAppOptions{}, - wasmconfig.DefaultConfig(), + wasmtypes.DefaultWasmConfig(), ) sm := terraApp.SimulationManager() require.NotNil(t, sm) diff --git a/app/wasmconfig/config.go b/app/wasmconfig/config.go deleted file mode 100644 index 9dccc54d..00000000 --- a/app/wasmconfig/config.go +++ /dev/null @@ -1,97 +0,0 @@ -package wasmconfig - -import ( - "github.com/spf13/cast" - "github.com/spf13/cobra" - - wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" - - servertypes "github.com/cosmos/cosmos-sdk/server/types" -) - -// config default values -const ( - DefaultContractQueryGasLimit = uint64(3_000_000) - DefaultContractSimulationGasLimit = uint64(50_000_000) - DefaultContractDebugMode = false - DefaultContractMemoryCacheSize = uint32(2048) -) - -// Config is the extra config required for wasm -type Config struct { - // SimulationGasLimit is the max gas to be used in a smart query contract call - ContractQueryGasLimit uint64 `mapstructure:"contract-query-gas-limit"` - - // SimulationGasLimit is the max gas to be used in a tx simulation call. - // When not set the consensus max block gas is used instead - ContractSimulationGasLimit uint64 `mapstructure:"contract-query-gas-limit"` - - // ContractDebugMode log what contract print - ContractDebugMode bool `mapstructure:"contract-debug-mode"` - - // MemoryCacheSize in MiB not bytes - ContractMemoryCacheSize uint32 `mapstructure:"contract-memory-cache-size"` -} - -// ToWasmConfig convert config to wasmd's config -func (c Config) ToWasmConfig() wasmtypes.WasmConfig { - return wasmtypes.WasmConfig{ - SimulationGasLimit: &c.ContractSimulationGasLimit, - SmartQueryGasLimit: c.ContractQueryGasLimit, - MemoryCacheSize: c.ContractMemoryCacheSize, - ContractDebugMode: c.ContractDebugMode, - } -} - -// DefaultConfig returns the default settings for WasmConfig -func DefaultConfig() *Config { - return &Config{ - ContractQueryGasLimit: DefaultContractQueryGasLimit, - ContractSimulationGasLimit: DefaultContractSimulationGasLimit, - ContractDebugMode: DefaultContractDebugMode, - ContractMemoryCacheSize: DefaultContractMemoryCacheSize, - } -} - -// GetConfig load config values from the app options -func GetConfig(appOpts servertypes.AppOptions) *Config { - return &Config{ - ContractQueryGasLimit: cast.ToUint64(appOpts.Get("wasm.contract-query-gas-limit")), - ContractSimulationGasLimit: cast.ToUint64(appOpts.Get("wasm.contract-simulation-gas-limit")), - ContractDebugMode: cast.ToBool(appOpts.Get("wasm.contract-debug-mode")), - ContractMemoryCacheSize: cast.ToUint32(appOpts.Get("wasm.contract-memory-cache-size")), - } -} - -const ( - flagContractQueryGasLimit = "wasm.contract-query-gas-limit" - flagContractSimulationGasLimit = "wasm.contract-simulation-gas-limit" - flagContractDebugMode = "wasm.contract-debug-mode" - flagContractMemoryCacheSize = "wasm.contract-memory-cache-size" -) - -// AddConfigFlags implements servertypes.WasmConfigFlags interface. -func AddConfigFlags(startCmd *cobra.Command) { - startCmd.Flags().Uint64(flagContractQueryGasLimit, DefaultContractQueryGasLimit, "Set the max gas that can be spent on executing a query with a Wasm contract") - startCmd.Flags().Uint64(flagContractSimulationGasLimit, DefaultContractSimulationGasLimit, "Set the max gas that can be spent when executing a simulation TX") - startCmd.Flags().Bool(flagContractDebugMode, DefaultContractDebugMode, "The flag to specify whether print contract logs or not") - startCmd.Flags().Uint32(flagContractMemoryCacheSize, DefaultContractMemoryCacheSize, "Sets the size in MiB (NOT bytes) of an in-memory cache for Wasm modules. Set to 0 to disable.") -} - -// DefaultConfigTemplate default config template for wasm module -const DefaultConfigTemplate = ` -[wasm] -# The maximum gas amount can be spent for contract query. -# The contract query will invoke contract execution vm, -# so we need to restrict the max usage to prevent DoS attack -contract-query-gas-limit = "{{ .WASMConfig.ContractQueryGasLimit }}" - -# The maximum gas amount can be used in a tx simulation call. -contract-simulation-gas-limit= "{{ .WASMConfig.ContractSimulationGasLimit }}" - -# The flag to specify whether print contract logs or not -contract-debug-mode = "{{ .WASMConfig.ContractDebugMode }}" - -# The WASM VM memory cache size in MiB not bytes -contract-memory-cache-size = "{{ .WASMConfig.ContractMemoryCacheSize }}" -` diff --git a/cmd/terrad/config.go b/cmd/terrad/config.go index 386d99ae..3f1a6a22 100644 --- a/cmd/terrad/config.go +++ b/cmd/terrad/config.go @@ -1,8 +1,7 @@ package main import ( - "github.com/terra-money/core/v2/app/wasmconfig" - + wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" serverconfig "github.com/cosmos/cosmos-sdk/server/config" ) @@ -10,7 +9,7 @@ import ( type TerraAppConfig struct { serverconfig.Config - WASMConfig wasmconfig.Config `mapstructure:"wasm"` + WASMConfig wasmtypes.WasmConfig `mapstructure:"wasm"` } // initAppConfig helps to override default appConfig template and configs. @@ -40,10 +39,10 @@ func initAppConfig() (string, interface{}) { terraAppConfig := TerraAppConfig{ Config: *srvCfg, - WASMConfig: *wasmconfig.DefaultConfig(), + WASMConfig: wasmtypes.DefaultWasmConfig(), } - terraAppTemplate := serverconfig.DefaultConfigTemplate + wasmconfig.DefaultConfigTemplate + terraAppTemplate := serverconfig.DefaultConfigTemplate + wasmtypes.DefaultConfigTemplate() return terraAppTemplate, terraAppConfig } diff --git a/cmd/terrad/root.go b/cmd/terrad/root.go index 7eafd349..2a4e6f2e 100644 --- a/cmd/terrad/root.go +++ b/cmd/terrad/root.go @@ -6,6 +6,7 @@ import ( "os" rosettaCmd "cosmossdk.io/tools/rosetta/cmd" + wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" dbm "github.com/cometbft/cometbft-db" tmcli "github.com/cometbft/cometbft/libs/cli" "github.com/cometbft/cometbft/libs/log" @@ -32,9 +33,9 @@ import ( "github.com/cosmos/cosmos-sdk/x/crisis" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" + wasm "github.com/CosmWasm/wasmd/x/wasm" terraapp "github.com/terra-money/core/v2/app" "github.com/terra-money/core/v2/app/params" - "github.com/terra-money/core/v2/app/wasmconfig" ) // NewRootCmd creates a new root command for terrad. @@ -146,7 +147,7 @@ func genesisCommand(encodingConfig params.EncodingConfig, cmds ...*cobra.Command func addModuleInitFlags(startCmd *cobra.Command) { crisis.AddModuleInitFlags(startCmd) - wasmconfig.AddConfigFlags(startCmd) + wasm.AddModuleInitFlags(startCmd) } func queryCommand() *cobra.Command { @@ -216,6 +217,11 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a skipUpgradeHeights[int64(h)] = true } + wasmConfig, err := wasm.ReadWasmConfig(appOpts) + if err != nil { + panic(err) + } + return terraapp.NewTerraApp( logger, db, @@ -226,7 +232,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)), a.encodingConfig, appOpts, - wasmconfig.GetConfig(appOpts), + wasmConfig, baseappOptions..., ) } @@ -252,13 +258,13 @@ func (a appCreator) appExport( var terraApp *terraapp.TerraApp if height != -1 { - terraApp = terraapp.NewTerraApp(logger, db, traceStore, false, map[int64]bool{}, homePath, cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)), a.encodingConfig, appOpts, wasmconfig.DefaultConfig()) + terraApp = terraapp.NewTerraApp(logger, db, traceStore, false, map[int64]bool{}, homePath, cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)), a.encodingConfig, appOpts, wasmtypes.DefaultWasmConfig()) if err := terraApp.LoadHeight(height); err != nil { return servertypes.ExportedApp{}, err } } else { - terraApp = terraapp.NewTerraApp(logger, db, traceStore, true, map[int64]bool{}, homePath, cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)), a.encodingConfig, appOpts, wasmconfig.DefaultConfig()) + terraApp = terraapp.NewTerraApp(logger, db, traceStore, true, map[int64]bool{}, homePath, cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)), a.encodingConfig, appOpts, wasmtypes.DefaultWasmConfig()) } return terraApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport) diff --git a/x/tokenfactory/bindings/helpers_test.go b/x/tokenfactory/bindings/helpers_test.go index 76db133e..e82eb0c2 100644 --- a/x/tokenfactory/bindings/helpers_test.go +++ b/x/tokenfactory/bindings/helpers_test.go @@ -23,7 +23,6 @@ import ( "github.com/CosmWasm/wasmd/x/wasm/keeper" wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types" "github.com/terra-money/core/v2/app" - "github.com/terra-money/core/v2/app/wasmconfig" tokenfactorytypes "github.com/terra-money/core/v2/x/tokenfactory/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -45,7 +44,7 @@ func CreateTestInput() (*app.TerraApp, sdk.Context) { 0, encCfg, simtestutil.EmptyAppOptions{}, - wasmconfig.DefaultConfig(), + wasmtypes.DefaultWasmConfig(), ) ctx := terraApp.BaseApp.NewContext(true, tmproto.Header{Height: 1, ChainID: "phoenix-1", Time: time.Now()}) err := terraApp.Keepers.WasmKeeper.SetParams(ctx, wasmtypes.DefaultParams())