Add logic to app.go to configure enabled proposals

This commit is contained in:
Ethan Frey
2020-07-27 19:20:25 +02:00
parent c9cf9a8bbf
commit 7b1a4de59d
5 changed files with 50 additions and 6 deletions

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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

View File

@@ -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))