feat(x/gov): extend governance config (#18428)

This commit is contained in:
emidev98
2023-11-10 15:17:56 +00:00
committed by GitHub
parent 5741c320b9
commit 89296ccdd3
12 changed files with 228 additions and 47 deletions
+2 -2
View File
@@ -308,8 +308,8 @@ the following `JSON` template:
This makes it far easier for clients to support multiple networks.
The metadata has a maximum length that is chosen by the app developer, and
passed into the gov keeper as a config. The default maximum length in the SDK is 255 characters.
Fields metadata, title and summary have a maximum length that is chosen by the app developer, and
passed into the gov keeper as a config. The default maximum length are: for the title 255 characters, for the metadata 255 characters and for summary 10200 characters (40 times the one of the title).
#### Writing a module that uses governance
+1
View File
@@ -163,6 +163,7 @@ require (
replace github.com/cosmos/cosmos-sdk => ../../.
replace (
cosmossdk.io/api => ../../api
cosmossdk.io/x/auth => ../auth
cosmossdk.io/x/bank => ../bank
cosmossdk.io/x/distribution => ../distribution
-2
View File
@@ -35,8 +35,6 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
cosmossdk.io/api v0.7.3-0.20231029200940-6af7f30bfd54 h1:c7kl5S1ME0q2g/7cdxngOAZr6N/5L7vVibmrmQZrQ0U=
cosmossdk.io/api v0.7.3-0.20231029200940-6af7f30bfd54/go.mod h1:7B/5XWh1HYwJk3DzWeNoxOSI+nGx1m5UyYfHLFuKzkw=
cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s=
cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0=
cosmossdk.io/core v0.12.0 h1:aFuvkG6eDv0IQC+UDjx86wxNWVAxdCFk7OABJ1Vh4RU=
+37 -4
View File
@@ -91,9 +91,18 @@ func NewKeeper(
panic(fmt.Sprintf("invalid authority address: %s", authority))
}
defaultConfig := types.DefaultConfig()
// If MaxMetadataLen not set by app developer, set to default value.
if config.MaxTitleLen == 0 {
config.MaxTitleLen = defaultConfig.MaxTitleLen
}
// If MaxMetadataLen not set by app developer, set to default value.
if config.MaxMetadataLen == 0 {
config.MaxMetadataLen = types.DefaultConfig().MaxMetadataLen
config.MaxMetadataLen = defaultConfig.MaxMetadataLen
}
// If MaxMetadataLen not set by app developer, set to default value.
if config.MaxSummaryLen == 0 {
config.MaxSummaryLen = defaultConfig.MaxSummaryLen
}
sb := collections.NewSchemaBuilder(storeService)
@@ -181,6 +190,30 @@ func (k Keeper) ModuleAccountAddress() sdk.AccAddress {
return k.authKeeper.GetModuleAddress(types.ModuleName)
}
// validateProposalLengths checks message metadata, summary and title
// to have the expected length otherwise returns an error.
func (k Keeper) validateProposalLengths(metadata, title, summary string) error {
if err := k.assertMetadataLength(metadata); err != nil {
return err
}
if err := k.assertSummaryLength(summary); err != nil {
return err
}
if err := k.assertTitleLength(title); err != nil {
return err
}
return nil
}
// assertTitleLength returns an error if given title length
// is greater than a pre-defined MaxTitleLen.
func (k Keeper) assertTitleLength(title string) error {
if title != "" && uint64(len(title)) > k.config.MaxTitleLen {
return types.ErrTitleTooLong.Wrapf("got title with length %d", len(title))
}
return nil
}
// assertMetadataLength returns an error if given metadata length
// is greater than a pre-defined MaxMetadataLen.
func (k Keeper) assertMetadataLength(metadata string) error {
@@ -191,9 +224,9 @@ func (k Keeper) assertMetadataLength(metadata string) error {
}
// assertSummaryLength returns an error if given summary length
// is greater than a pre-defined 40*MaxMetadataLen.
func (keeper Keeper) assertSummaryLength(summary string) error {
if summary != "" && uint64(len(summary)) > 40*keeper.config.MaxMetadataLen {
// is greater than a pre-defined MaxSummaryLen.
func (k Keeper) assertSummaryLength(summary string) error {
if summary != "" && uint64(len(summary)) > k.config.MaxSummaryLen {
return types.ErrSummaryTooLong.Wrapf("got summary with length %d", len(summary))
}
return nil
+18 -3
View File
@@ -48,7 +48,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitProposal() {
[]sdk.Msg{bankMsg},
initialDeposit,
"",
strings.Repeat("1", 300),
strings.Repeat("1", 100),
"Proposal",
"description of proposal",
false,
@@ -132,13 +132,28 @@ func (suite *KeeperTestSuite) TestMsgSubmitProposal() {
expErr: true,
expErrMsg: "metadata summary '' must equal proposal summary 'description'",
},
"title too long": {
preRun: func() (*v1.MsgSubmitProposal, error) {
return v1.NewMsgSubmitProposal(
[]sdk.Msg{bankMsg},
initialDeposit,
proposer.String(),
"Metadata",
strings.Repeat("1", 256),
"description of proposal",
false,
)
},
expErr: true,
expErrMsg: "title too long",
},
"metadata too long": {
preRun: func() (*v1.MsgSubmitProposal, error) {
return v1.NewMsgSubmitProposal(
[]sdk.Msg{bankMsg},
initialDeposit,
proposer.String(),
strings.Repeat("1", 300),
strings.Repeat("1", 256),
"Proposal",
"description of proposal",
false,
@@ -155,7 +170,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitProposal() {
proposer.String(),
"",
"Proposal",
strings.Repeat("1", 300*40),
strings.Repeat("1", 10201),
false,
)
},
+5 -14
View File
@@ -19,21 +19,12 @@ import (
// SubmitProposal creates a new proposal given an array of messages
func (keeper Keeper) SubmitProposal(ctx context.Context, messages []sdk.Msg, metadata, title, summary string, proposer sdk.AccAddress, expedited bool) (v1.Proposal, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
err := keeper.assertMetadataLength(metadata)
if err != nil {
return v1.Proposal{}, err
}
// assert summary is no longer than predefined max length of metadata
err = keeper.assertSummaryLength(summary)
if err != nil {
return v1.Proposal{}, err
}
// assert title is no longer than predefined max length of metadata
err = keeper.assertMetadataLength(title)
if err != nil {
return v1.Proposal{}, err
// This method checks that all message metadata, summary and title
// has te expected length defined in the module configuration.
if err := keeper.validateProposalLengths(metadata, title, summary); err != nil {
return v1.Proposal{}, err
}
// Will hold a string slice of all Msg type URLs.
+6
View File
@@ -182,9 +182,15 @@ type ModuleOutputs struct {
func ProvideModule(in ModuleInputs) ModuleOutputs {
defaultConfig := govtypes.DefaultConfig()
if in.Config.MaxTitleLen != 0 {
defaultConfig.MaxTitleLen = in.Config.MaxTitleLen
}
if in.Config.MaxMetadataLen != 0 {
defaultConfig.MaxMetadataLen = in.Config.MaxMetadataLen
}
if in.Config.MaxSummaryLen != 0 {
defaultConfig.MaxSummaryLen = in.Config.MaxSummaryLen
}
// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
+7 -1
View File
@@ -2,13 +2,19 @@ package types
// Config is a config struct used for intialising the gov module to avoid using globals.
type Config struct {
// MaxMetadataLen defines the maximum proposal metadata length.
// MaxTitleLen defines the amount of characters that can be used for proposal title
MaxTitleLen uint64
// MaxMetadataLen defines the amount of characters that can be used for proposal metadata.
MaxMetadataLen uint64
// MaxSummaryLen defines the amount of characters that can be used for proposal summary
MaxSummaryLen uint64
}
// DefaultConfig returns the default config for gov.
func DefaultConfig() Config {
return Config{
MaxTitleLen: 255,
MaxMetadataLen: 255,
MaxSummaryLen: 10200,
}
}
+1
View File
@@ -25,4 +25,5 @@ var (
ErrInvalidProposal = errors.Register(ModuleName, 21, "invalid proposal")
ErrSummaryTooLong = errors.Register(ModuleName, 22, "summary too long")
ErrInvalidDepositDenom = errors.Register(ModuleName, 23, "invalid deposit denom")
ErrTitleTooLong = errors.Register(ModuleName, 24, "title too long")
)