From 7328d032efa21f7d3adac12189202075b4fdf73b Mon Sep 17 00:00:00 2001 From: emidev98 Date: Fri, 10 Nov 2023 09:35:13 +0200 Subject: [PATCH] feat: extend governance config --- CHANGELOG.md | 1 + x/gov/keeper/keeper.go | 17 +++++++++++++---- x/gov/keeper/proposal.go | 10 +++++++--- x/gov/types/config.go | 10 ++++++++-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a01a7359e891..2a81ebc3f50b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ` 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. diff --git a/x/gov/keeper/keeper.go b/x/gov/keeper/keeper.go index 55d2a03b428d..663e19336e8a 100644 --- a/x/gov/keeper/keeper.go +++ b/x/gov/keeper/keeper.go @@ -181,19 +181,28 @@ func (k Keeper) ModuleAccountAddress() sdk.AccAddress { return k.authKeeper.GetModuleAddress(types.ModuleName) } +// assertTitleLength returns an error if given title length +// is greater than a pre-defined MaxTitleLen. +func (k Keeper) assertTitleLength(title string) error { + if title != "" && len(title) > k.config.MaxTitleLen { + return types.ErrMetadataTooLong.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 { - if metadata != "" && uint64(len(metadata)) > k.config.MaxMetadataLen { + if metadata != "" && len(metadata) > k.config.MaxMetadataLen { return types.ErrMetadataTooLong.Wrapf("got metadata with length %d", len(metadata)) } return nil } // 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 != "" && len(summary) > k.config.MaxSummaryLen { return types.ErrSummaryTooLong.Wrapf("got summary with length %d", len(summary)) } return nil diff --git a/x/gov/keeper/proposal.go b/x/gov/keeper/proposal.go index c4b18557b6d1..cc441b2af7ca 100644 --- a/x/gov/keeper/proposal.go +++ b/x/gov/keeper/proposal.go @@ -19,19 +19,23 @@ 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) + // check that the proposal has + // the expected metadata length err := keeper.assertMetadataLength(metadata) if err != nil { return v1.Proposal{}, err } - // assert summary is no longer than predefined max length of metadata + // check that the proposal has + // the expected summary length 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) + // check that the proposal has + // the expected title length + err = keeper.assertTitleLength(title) if err != nil { return v1.Proposal{}, err } diff --git a/x/gov/types/config.go b/x/gov/types/config.go index d19767f7d15c..a002ce6c4e8f 100644 --- a/x/gov/types/config.go +++ b/x/gov/types/config.go @@ -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. - MaxMetadataLen uint64 + // MaxTitleLen defines the amount of characters that can be used for proposal title + MaxTitleLen int + // MaxMetadataLen defines the amount of characters that can be used for proposal metadata. + MaxMetadataLen int + // MaxSummaryLen defines the amount of characters that can be used for proposal summary + MaxSummaryLen int } // DefaultConfig returns the default config for gov. func DefaultConfig() Config { return Config{ + MaxTitleLen: 100, MaxMetadataLen: 255, + MaxSummaryLen: 10200, } }