Skip to content

Commit

Permalink
feat(x/gov): extend governance config (#18428)
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 authored Nov 10, 2023
1 parent 5741c32 commit 89296cc
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (x/gov) [#18428](https://github.com/cosmos/cosmos-sdk/pull/18428) Extend governance config
* (x/gov) [#18189](https://github.com/cosmos/cosmos-sdk/pull/18189) Limit the accepted deposit coins for a proposal to the minimum proposal deposit denoms.
* (x/gov) [#18025](https://github.com/cosmos/cosmos-sdk/pull/18025) Improve `<appd> q gov proposer` by querying directly a proposal instead of tx events. It is an alias of `q gov proposal` as the proposer is a field of the proposal.
* (client) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Add `client.Context{}.WithAddressCodec`, `WithValidatorAddressCodec`, `WithConsensusAddressCodec` to provide address codecs to the client context. See the [UPGRADING.md](./UPGRADING.md) for more details.
Expand Down
163 changes: 142 additions & 21 deletions api/cosmos/gov/module/v1/module.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions proto/cosmos/gov/module/v1/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ message Module {

// authority defines the custom module authority. If not set, defaults to the governance module.
string authority = 2;

// max_title_len defines the maximum proposal title length.
// Defaults to 255 if not explicitly set.
uint64 max_title_len = 3;

// max_summary_len defines the maximum proposal summary length.
// Defaults to 10200 if not explicitly set.
uint64 max_summary_len = 4;
}
4 changes: 2 additions & 2 deletions x/gov/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions x/gov/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions x/gov/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
41 changes: 37 additions & 4 deletions x/gov/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Loading

0 comments on commit 89296cc

Please sign in to comment.