Skip to content

Commit

Permalink
Add length caps for governance proposal titles and descriptions (cosm…
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 authored and jackzampolin committed Jan 29, 2019
1 parent 14dcaa6 commit 172a451
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ IMPROVEMENTS
* Gaia
* [\#3418](https://github.com/cosmos/cosmos-sdk/issues/3418) Add vesting account
genesis validation checks to `GaiaValidateGenesisState`.
* [\#3420](https://github.com/cosmos/cosmos-sdk/issues/3420) Added maximum length to governance proposal descriptions and titles

* SDK
* \#3435 Test that store implementations do not allow nil values
Expand Down
8 changes: 4 additions & 4 deletions x/gov/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func ErrAddressNotStaked(codespace sdk.CodespaceType, address sdk.AccAddress) sd
return sdk.NewError(codespace, CodeAddressNotStaked, fmt.Sprintf("Address %s is not staked and is thus ineligible to vote", address))
}

func ErrInvalidTitle(codespace sdk.CodespaceType, title string) sdk.Error {
return sdk.NewError(codespace, CodeInvalidTitle, fmt.Sprintf("Proposal Title '%s' is not valid", title))
func ErrInvalidTitle(codespace sdk.CodespaceType, errorMsg string) sdk.Error {
return sdk.NewError(codespace, CodeInvalidTitle, errorMsg)
}

func ErrInvalidDescription(codespace sdk.CodespaceType, description string) sdk.Error {
return sdk.NewError(codespace, CodeInvalidDescription, fmt.Sprintf("Proposal Desciption '%s' is not valid", description))
func ErrInvalidDescription(codespace sdk.CodespaceType, errorMsg string) sdk.Error {
return sdk.NewError(codespace, CodeInvalidDescription, errorMsg)
}

func ErrInvalidProposalType(codespace sdk.CodespaceType, proposalType ProposalKind) sdk.Error {
Expand Down
13 changes: 11 additions & 2 deletions x/gov/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const (
TypeMsgDeposit = "deposit"
TypeMsgVote = "vote"
TypeMsgSubmitProposal = "submit_proposal"

MaxDescriptionLength int = 5000
MaxTitleLength int = 140
)

var _, _, _ sdk.Msg = MsgSubmitProposal{}, MsgDeposit{}, MsgVote{}
Expand Down Expand Up @@ -42,10 +45,16 @@ func (msg MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal }
// Implements Msg.
func (msg MsgSubmitProposal) ValidateBasic() sdk.Error {
if len(msg.Title) == 0 {
return ErrInvalidTitle(DefaultCodespace, msg.Title) // TODO: Proper Error
return ErrInvalidTitle(DefaultCodespace, "No title present in proposal")
}
if len(msg.Title) > MaxTitleLength {
return ErrInvalidTitle(DefaultCodespace, fmt.Sprintf("Proposal title is longer than max length of %d", MaxTitleLength))
}
if len(msg.Description) == 0 {
return ErrInvalidDescription(DefaultCodespace, msg.Description) // TODO: Proper Error
return ErrInvalidDescription(DefaultCodespace, "No description present in proposal")
}
if len(msg.Description) > MaxDescriptionLength {
return ErrInvalidDescription(DefaultCodespace, fmt.Sprintf("Proposal description is longer than max length of %d", MaxDescriptionLength))
}
if !validProposalType(msg.ProposalType) {
return ErrInvalidProposalType(DefaultCodespace, msg.ProposalType)
Expand Down
3 changes: 3 additions & 0 deletions x/gov/msgs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -40,6 +41,8 @@ func TestMsgSubmitProposal(t *testing.T) {
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, sdk.AccAddress{}, coinsPos, false},
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsZero, true},
{"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, true},
{strings.Repeat("#", MaxTitleLength*2), "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, false},
{"Test Proposal", strings.Repeat("#", MaxDescriptionLength*2), ProposalTypeText, addrs[0], coinsMulti, false},
}

for i, tc := range tests {
Expand Down

0 comments on commit 172a451

Please sign in to comment.