Skip to content

Commit

Permalink
blockchain: Reject params with blank choice id.
Browse files Browse the repository at this point in the history
This adds an additional validation check to the chain initialization
process to validate that none of the IDs of the choices in the given
chain params are blank.

It also adds a test for the new validation logic.
  • Loading branch information
davecgh committed Mar 13, 2023
1 parent 491d178 commit e22c50a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,13 @@ func validateDeploymentChoices(voteParams *chaincfg.Vote) error {
var numAbstain, numNo int
dups := make(map[string]int)
for choiceIdx, choice := range voteParams.Choices {
// Ensure the id is not empty.
if choice.Id == "" {
str := fmt.Sprintf("deployment ID %s choice index %d does not "+
"have an ID", voteParams.Id, choiceIdx)
return contextError(ErrDeploymentMissingChoiceID, str)
}

// Ensure that the choice bits are not zero for all choices except the
// abstain choice.
if choice.Bits == 0 && !choice.IsAbstain {
Expand Down
7 changes: 7 additions & 0 deletions internal/blockchain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ func TestDeploymentParamsValidation(t *testing.T) {
})
},
err: ErrDeploymentTooManyChoices,
}, {
name: "reject choices without an id",
munger: func(params *chaincfg.Params) {
vote := &params.Deployments[0][0].Vote
vote.Choices[1].Id = ""
},
err: ErrDeploymentMissingChoiceID,
}, {
name: "reject choice with abstain bits but no abstain flag",
munger: func(params *chaincfg.Params) {
Expand Down
3 changes: 3 additions & 0 deletions internal/blockchain/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,9 @@ const (
// a deployment that its mask can represent.
ErrDeploymentTooManyChoices = ErrorKind("ErrDeploymentTooManyChoices")

// ErrDeploymentMissingChoiceID indicates a choice id is not set.
ErrDeploymentMissingChoiceID = ErrorKind("ErrDeploymentMissingChoiceID")

// ErrDeploymentBadChoiceBits indicates the choice bits that represent a
// deployment vote choice are invalid in some way. For example, when they
// are all zero and the choice is not also marked as abstain or they are not
Expand Down
1 change: 1 addition & 0 deletions internal/blockchain/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func TestErrorKindStringer(t *testing.T) {
{ErrUnknownDeploymentChoice, "ErrUnknownDeploymentChoice"},
{ErrDeploymentBadMask, "ErrDeploymentBadMask"},
{ErrDeploymentTooManyChoices, "ErrDeploymentTooManyChoices"},
{ErrDeploymentMissingChoiceID, "ErrDeploymentMissingChoiceID"},
{ErrDeploymentBadChoiceBits, "ErrDeploymentBadChoiceBits"},
{ErrDeploymentNonExclusiveFlags, "ErrDeploymentNonExclusiveFlags"},
{ErrDeploymentDuplicateChoice, "ErrDeploymentDuplicateChoice"},
Expand Down

0 comments on commit e22c50a

Please sign in to comment.