Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/gov): better gov genesis validation #18707

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion x/gov/types/v1/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/codec/types"
)
Expand All @@ -27,12 +28,63 @@ 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")
}

// verify proposal IDs
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
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)
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
}

julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
proposalIds[p.Id] = struct{}{}
}

// verify deposits
type depositKey struct {
ProposalId uint64
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)
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
}

depositIds[dk] = struct{}{}
}

// verify votes
type voteKey struct {
ProposalId uint64
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 data.Params.ValidateBasic()
}

Expand Down
77 changes: 77 additions & 0 deletions x/gov/types/v1/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,83 @@ func TestValidateGenesis(t *testing.T) {
},
expErr: true,
},
{
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
},
expErr: true,
},
{
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
},
expErr: true,
},
{
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
},
expErr: true,
},
{
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
},
expErr: true,
},
{
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
},
expErr: true,
},
}

for _, tc := range testCases {
Expand Down
Loading