From 7b1a4de59dcbd62cd15b6c384a72f9eadf8ddef8 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 27 Jul 2020 19:20:25 +0200 Subject: [PATCH] Add logic to app.go to configure enabled proposals --- app/app.go | 26 ++++++++++++++++++++++++++ cmd/wasmd/main.go | 8 ++++---- cmd/wasmd/replay.go | 3 +-- x/wasm/alias.go | 1 + x/wasm/internal/types/proposal.go | 18 ++++++++++++++++++ 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/app.go b/app/app.go index 9126471d..cc45f80f 100644 --- a/app/app.go +++ b/app/app.go @@ -4,6 +4,7 @@ import ( "io" "os" "path/filepath" + "strings" "github.com/CosmWasm/wasmd/x/wasm" wasmclient "github.com/CosmWasm/wasmd/x/wasm/client" @@ -44,8 +45,33 @@ var ( CLIDir = ".wasmcli" NodeDir = ".wasmd" Bech32Prefix = sdk.Bech32MainPrefix + + // If EnabledSpecificProposals is "", then we check this to decide to enable all or disable all x/wasm proposals + ProposalsEnabled = false + // If set to non-empty string it must be comma-separated list of values that are all a subset + // of "EnableAllProposals" (takes precedence over ProposalsEnabled) + // https://github.com/CosmWasm/wasmd/blob/02a54d33ff2c064f3539ae12d75d027d9c665f05/x/wasm/internal/types/proposal.go#L28-L34 + EnableSpecificProposals = "" ) +// GetEnabledProposals parses the ProposalsEnabled / EnableSpecificProposals values to +// produce a list of enabled proposals to pass into wasmd app. +func GetEnabledProposals() []wasm.ProposalType { + if EnableSpecificProposals == "" { + if ProposalsEnabled { + return wasm.EnableAllProposals + } else { + return wasm.DisableAllProposals + } + } + chunks := strings.Split(EnableSpecificProposals, ",") + proposals, err := wasm.ConvertToProposals(chunks) + if err != nil { + panic(err) + } + return proposals +} + // These constants are derived from the above variables. // These are the ones we will want to use in the code, based on // any overrides above diff --git a/cmd/wasmd/main.go b/cmd/wasmd/main.go index 99b75db4..91493f54 100644 --- a/cmd/wasmd/main.go +++ b/cmd/wasmd/main.go @@ -6,7 +6,6 @@ import ( "io" "github.com/CosmWasm/wasmd/app" - "github.com/CosmWasm/wasmd/x/wasm" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client/debug" "github.com/cosmos/cosmos-sdk/client/flags" @@ -90,7 +89,8 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application } return app.NewWasmApp(logger, db, traceStore, true, invCheckPeriod, - wasm.DisableAllProposals, skipUpgradeHeights, + app.GetEnabledProposals(), + skipUpgradeHeights, baseapp.SetPruning(pruningOpts), baseapp.SetMinGasPrices(viper.GetString(server.FlagMinGasPrices)), baseapp.SetHaltHeight(viper.GetUint64(server.FlagHaltHeight)), @@ -103,7 +103,7 @@ func exportAppStateAndTMValidators( ) (json.RawMessage, []tmtypes.GenesisValidator, error) { if height != -1 { - gapp := app.NewWasmApp(logger, db, traceStore, false, uint(1), wasm.DisableAllProposals, nil) + gapp := app.NewWasmApp(logger, db, traceStore, false, uint(1), app.GetEnabledProposals(), nil) err := gapp.LoadHeight(height) if err != nil { return nil, nil, err @@ -111,6 +111,6 @@ func exportAppStateAndTMValidators( return gapp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) } - gapp := app.NewWasmApp(logger, db, traceStore, true, uint(1), wasm.DisableAllProposals, nil) + gapp := app.NewWasmApp(logger, db, traceStore, true, uint(1), app.GetEnabledProposals(), nil) return gapp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList) } diff --git a/cmd/wasmd/replay.go b/cmd/wasmd/replay.go index 3b7f25bc..c877fcc9 100644 --- a/cmd/wasmd/replay.go +++ b/cmd/wasmd/replay.go @@ -8,7 +8,6 @@ import ( "time" "github.com/CosmWasm/wasmd/app" - "github.com/CosmWasm/wasmd/x/wasm" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/server" storetypes "github.com/cosmos/cosmos-sdk/store/types" @@ -92,7 +91,7 @@ func replayTxs(rootDir string) error { fmt.Fprintln(os.Stderr, "Creating application") gapp := app.NewWasmApp( // TODO: do we want to set skipUpgradeHieghts here? - ctx.Logger, appDB, traceStoreWriter, true, uint(1), wasm.DisableAllProposals, nil, + ctx.Logger, appDB, traceStoreWriter, true, uint(1), app.GetEnabledProposals(), nil, baseapp.SetPruning(storetypes.PruneEverything)) // Genesis diff --git a/x/wasm/alias.go b/x/wasm/alias.go index e0755f2a..edf77ee8 100644 --- a/x/wasm/alias.go +++ b/x/wasm/alias.go @@ -39,6 +39,7 @@ var ( // functions aliases RegisterCodec = types.RegisterCodec ValidateGenesis = types.ValidateGenesis + ConvertToProposals = types.ConvertToProposals GetCodeKey = types.GetCodeKey GetContractAddressKey = types.GetContractAddressKey GetContractStorePrefixKey = types.GetContractStorePrefixKey diff --git a/x/wasm/internal/types/proposal.go b/x/wasm/internal/types/proposal.go index 363effa2..3db50c7a 100644 --- a/x/wasm/internal/types/proposal.go +++ b/x/wasm/internal/types/proposal.go @@ -33,6 +33,24 @@ var EnableAllProposals = []ProposalType{ ProposalTypeClearAdmin, } +// ConvertToProposals maps each key to a ProposalType and returns a typed list. +// If any string is not a valid type (in this file), then return an error +func ConvertToProposals(keys []string) ([]ProposalType, error) { + valid := make(map[string]bool, len(EnableAllProposals)) + for _, key := range EnableAllProposals { + valid[string(key)] = true + } + + proposals := make([]ProposalType, len(keys)) + for i, key := range keys { + if _, ok := valid[key]; !ok { + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "'%s' is not a valid ProposalType", key) + } + proposals[i] = ProposalType(key) + } + return proposals, nil +} + func init() { // register new content types with the sdk govtypes.RegisterProposalType(string(ProposalTypeStoreCode)) govtypes.RegisterProposalType(string(ProposalTypeStoreInstantiateContract))