Add length caps for governance proposal titles and descriptions (#3434)

This commit is contained in:
Sunny Aggarwal 2019-01-29 14:23:25 -08:00 committed by Jack Zampolin
parent 14dcaa6458
commit 172a4510c7
4 changed files with 19 additions and 6 deletions

View File

@ -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

View File

@ -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 {

View File

@ -11,6 +11,9 @@ const (
TypeMsgDeposit = "deposit"
TypeMsgVote = "vote"
TypeMsgSubmitProposal = "submit_proposal"
MaxDescriptionLength int = 5000
MaxTitleLength int = 140
)
var _, _, _ sdk.Msg = MsgSubmitProposal{}, MsgDeposit{}, MsgVote{}
@ -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)

View File

@ -1,6 +1,7 @@
package gov
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
@ -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 {