Skip to content

Commit

Permalink
feat: extend gov config backword compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Nov 10, 2023
1 parent 665d61b commit 866ee14
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 165 deletions.
236 changes: 118 additions & 118 deletions api/cosmos/gov/module/v1/module.pulsar.go

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions proto/cosmos/gov/module/v1/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ message Module {
go_import: "cosmossdk.io/x/gov"
};

// max_title_len defines the maximum proposal title length.
// Defaults to 100 if not explicitly set.
int64 max_title_len = 1;

// max_metadata_len defines the maximum proposal metadata length.
// Defaults to 255 if not explicitly set.
int64 max_metadata_len = 2;
uint64 max_metadata_len = 1;

// 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.
int64 max_summary_len = 3;

// authority defines the custom module authority. If not set, defaults to the governance module.
string authority = 4;
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 100 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
19 changes: 16 additions & 3 deletions x/gov/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,23 @@ func (k Keeper) ModuleAccountAddress() sdk.AccAddress {
return k.authKeeper.GetModuleAddress(types.ModuleName)
}

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 != "" && int64(len(title)) > k.config.MaxTitleLen {
if title != "" && uint64(len(title)) > k.config.MaxTitleLen {
return types.ErrMetadataTooLong.Wrapf("got title with length %d", len(title))
}
return nil
Expand All @@ -202,7 +215,7 @@ func (k Keeper) assertTitleLength(title string) error {
// assertMetadataLength returns an error if given metadata length
// is greater than a pre-defined MaxMetadataLen.
func (k Keeper) assertMetadataLength(metadata string) error {
if metadata != "" && int64(len(metadata)) > k.config.MaxMetadataLen {
if metadata != "" && uint64(len(metadata)) > k.config.MaxMetadataLen {
return types.ErrMetadataTooLong.Wrapf("got metadata with length %d", len(metadata))
}
return nil
Expand All @@ -211,7 +224,7 @@ func (k Keeper) assertMetadataLength(metadata string) error {
// assertSummaryLength returns an error if given summary length
// is greater than a pre-defined MaxSummaryLen.
func (k Keeper) assertSummaryLength(summary string) error {
if summary != "" && int64(len(summary)) > k.config.MaxSummaryLen {
if summary != "" && uint64(len(summary)) > k.config.MaxSummaryLen {
return types.ErrSummaryTooLong.Wrapf("got summary with length %d", len(summary))
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion x/gov/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (suite *KeeperTestSuite) TestMsgSubmitProposal() {
initialDeposit,
proposer.String(),
"Metadata",
strings.Repeat("1", 101),
strings.Repeat("1", 256),
"description of proposal",
false,
)
Expand Down
24 changes: 5 additions & 19 deletions x/gov/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,11 @@ 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
}

// check that the proposal has
// the expected summary length
err = keeper.assertSummaryLength(summary)
if err != nil {
return v1.Proposal{}, err
}

// check that the proposal has
// the expected title length
err = keeper.assertTitleLength(title)
if err != nil {
return v1.Proposal{}, err

// This method checks that all the 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.
Expand Down
12 changes: 3 additions & 9 deletions x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,9 @@ 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
}
defaultConfig.MaxTitleLen = max(in.Config.MaxTitleLen, defaultConfig.MaxTitleLen)
defaultConfig.MaxMetadataLen = max(in.Config.MaxMetadataLen, defaultConfig.MaxMetadataLen)
defaultConfig.MaxSummaryLen = max(in.Config.MaxSummaryLen, defaultConfig.MaxSummaryLen)

// default to governance authority if not provided
authority := authtypes.NewModuleAddress(govtypes.ModuleName)
Expand Down
8 changes: 4 additions & 4 deletions x/gov/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package types
// Config is a config struct used for intialising the gov module to avoid using globals.
type Config struct {
// MaxTitleLen defines the amount of characters that can be used for proposal title
MaxTitleLen int64
MaxTitleLen uint64
// MaxMetadataLen defines the amount of characters that can be used for proposal metadata.
MaxMetadataLen int64
MaxMetadataLen uint64
// MaxSummaryLen defines the amount of characters that can be used for proposal summary
MaxSummaryLen int64
MaxSummaryLen uint64
}

// DefaultConfig returns the default config for gov.
func DefaultConfig() Config {
return Config{
MaxTitleLen: 100,
MaxTitleLen: 255,
MaxMetadataLen: 255,
MaxSummaryLen: 10200,
}
Expand Down

0 comments on commit 866ee14

Please sign in to comment.