Skip to content

Commit

Permalink
feat(x/gov): better gov genesis validation (backport #18707) (#18711)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Dec 13, 2023
1 parent d8fb76d commit 14bc239
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Improvements

* (x/gov) [#18707](https://github.com/cosmos/cosmos-sdk/pull/18707) Improve genesis validation.

## [v0.50.2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.2) - 2023-12-11

### Features
Expand Down
73 changes: 71 additions & 2 deletions x/gov/types/v1/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package v1

import (
"errors"
"fmt"

"golang.org/x/sync/errgroup"

"github.com/cosmos/cosmos-sdk/codec/types"
)
Expand All @@ -27,13 +30,79 @@ func (data GenesisState) Empty() bool {
return data.StartingProposalId == 0 || data.Params == nil
}

// ValidateGenesis checks if parameters are within valid ranges
// ValidateGenesis checks if gov genesis state is valid ranges
// It checks if params are in valid ranges
// It also makes sure that the provided proposal IDs are unique and
// that there are no duplicate deposit or vote records and no vote or deposits for non-existent proposals
func ValidateGenesis(data *GenesisState) error {
if data.StartingProposalId == 0 {
return errors.New("starting proposal id must be greater than 0")
}

return data.Params.ValidateBasic()
var errGroup errgroup.Group

// weed out duplicate proposals
proposalIds := make(map[uint64]struct{})
for _, p := range data.Proposals {
if _, ok := proposalIds[p.Id]; ok {
return fmt.Errorf("duplicate proposal id: %d", p.Id)
}

proposalIds[p.Id] = struct{}{}
}

// weed out duplicate deposits
errGroup.Go(func() error {
type depositKey struct {
ProposalId uint64 //nolint:revive // staying consistent with main and v0.47
Depositor string
}
depositIds := make(map[depositKey]struct{})
for _, d := range data.Deposits {
if _, ok := proposalIds[d.ProposalId]; !ok {
return fmt.Errorf("deposit %v has non-existent proposal id: %d", d, d.ProposalId)
}

dk := depositKey{d.ProposalId, d.Depositor}
if _, ok := depositIds[dk]; ok {
return fmt.Errorf("duplicate deposit: %v", d)
}

depositIds[dk] = struct{}{}
}

return nil
})

// weed out duplicate votes
errGroup.Go(func() error {
type voteKey struct {
ProposalId uint64 //nolint:revive // staying consistent with main and v0.47
Voter string
}
voteIds := make(map[voteKey]struct{})
for _, v := range data.Votes {
if _, ok := proposalIds[v.ProposalId]; !ok {
return fmt.Errorf("vote %v has non-existent proposal id: %d", v, v.ProposalId)
}

vk := voteKey{v.ProposalId, v.Voter}
if _, ok := voteIds[vk]; ok {
return fmt.Errorf("duplicate vote: %v", v)
}

voteIds[vk] = struct{}{}
}

return nil
})

// verify params
errGroup.Go(func() error {
return data.Params.ValidateBasic()
})

return errGroup.Wait()
}

var _ types.UnpackInterfacesMessage = GenesisState{}
Expand Down
104 changes: 91 additions & 13 deletions x/gov/types/v1/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestValidateGenesis(t *testing.T) {
testCases := []struct {
name string
genesisState func() *v1.GenesisState
expErr bool
expErrMsg string
}{
{
name: "valid",
Expand All @@ -38,7 +38,7 @@ func TestValidateGenesis(t *testing.T) {
genesisState: func() *v1.GenesisState {
return v1.NewGenesisState(0, params)
},
expErr: true,
expErrMsg: "starting proposal id must be greater than 0",
},
{
name: "invalid min deposit",
Expand All @@ -49,58 +49,136 @@ func TestValidateGenesis(t *testing.T) {
Amount: sdkmath.NewInt(-100),
}}

return v1.NewGenesisState(0, params1)
return v1.NewGenesisState(v1.DefaultStartingProposalID, params1)
},
expErr: true,
expErrMsg: "invalid minimum deposit",
},
{
name: "invalid max deposit period",
genesisState: func() *v1.GenesisState {
params1 := params
params1.MaxDepositPeriod = nil

return v1.NewGenesisState(0, params1)
return v1.NewGenesisState(v1.DefaultStartingProposalID, params1)
},
expErr: true,
expErrMsg: "maximum deposit period must not be nil",
},
{
name: "invalid quorum",
genesisState: func() *v1.GenesisState {
params1 := params
params1.Quorum = "2"

return v1.NewGenesisState(0, params1)
return v1.NewGenesisState(v1.DefaultStartingProposalID, params1)
},
expErr: true,
expErrMsg: "quorom too large",
},
{
name: "invalid threshold",
genesisState: func() *v1.GenesisState {
params1 := params
params1.Threshold = "2"

return v1.NewGenesisState(0, params1)
return v1.NewGenesisState(v1.DefaultStartingProposalID, params1)
},
expErr: true,
expErrMsg: "vote threshold too large",
},
{
name: "invalid veto threshold",
genesisState: func() *v1.GenesisState {
params1 := params
params1.VetoThreshold = "2"

return v1.NewGenesisState(0, params1)
return v1.NewGenesisState(v1.DefaultStartingProposalID, params1)
},
expErr: true,
expErrMsg: "veto threshold too large",
},
{
name: "duplicate proposals",
genesisState: func() *v1.GenesisState {
state := v1.NewGenesisState(v1.DefaultStartingProposalID, params)
state.Proposals = append(state.Proposals, &v1.Proposal{Id: 1})
state.Proposals = append(state.Proposals, &v1.Proposal{Id: 1})

return state
},
expErrMsg: "duplicate proposal id: 1",
},
{
name: "duplicate votes",
genesisState: func() *v1.GenesisState {
state := v1.NewGenesisState(v1.DefaultStartingProposalID, params)
state.Proposals = append(state.Proposals, &v1.Proposal{Id: 1})
state.Votes = append(state.Votes,
&v1.Vote{
ProposalId: 1,
Voter: "voter",
},
&v1.Vote{
ProposalId: 1,
Voter: "voter",
})

return state
},
expErrMsg: "duplicate vote",
},
{
name: "duplicate deposits",
genesisState: func() *v1.GenesisState {
state := v1.NewGenesisState(v1.DefaultStartingProposalID, params)
state.Proposals = append(state.Proposals, &v1.Proposal{Id: 1})
state.Deposits = append(state.Deposits,
&v1.Deposit{
ProposalId: 1,
Depositor: "depositor",
},
&v1.Deposit{
ProposalId: 1,
Depositor: "depositor",
})

return state
},
expErrMsg: "duplicate deposit: proposal_id:1 depositor:\"depositor\"",
},
{
name: "non-existent proposal id in votes",
genesisState: func() *v1.GenesisState {
state := v1.NewGenesisState(v1.DefaultStartingProposalID, params)
state.Votes = append(state.Votes,
&v1.Vote{
ProposalId: 1,
Voter: "voter",
})

return state
},
expErrMsg: "vote proposal_id:1 voter:\"voter\" has non-existent proposal id: 1",
},
{
name: "non-existent proposal id in deposits",
genesisState: func() *v1.GenesisState {
state := v1.NewGenesisState(v1.DefaultStartingProposalID, params)
state.Deposits = append(state.Deposits,
&v1.Deposit{
ProposalId: 1,
Depositor: "depositor",
})

return state
},
expErrMsg: "deposit proposal_id:1 depositor:\"depositor\"",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := v1.ValidateGenesis(tc.genesisState())
if tc.expErr {
if tc.expErrMsg != "" {
require.Error(t, err)
require.ErrorContains(t, err, tc.expErrMsg)
} else {
require.NoError(t, err)
}
Expand Down

0 comments on commit 14bc239

Please sign in to comment.