Skip to content

Commit

Permalink
🏃🏾
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Dec 12, 2023
1 parent 074b907 commit 8de4177
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
2 changes: 1 addition & 1 deletion x/gov/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ require (
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sync v0.5.0
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
75 changes: 46 additions & 29 deletions x/gov/types/v1/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"

"golang.org/x/sync/errgroup"

"github.com/cosmos/cosmos-sdk/codec/types"
)

Expand Down Expand Up @@ -37,6 +39,8 @@ func ValidateGenesis(data *GenesisState) error {
return errors.New("starting proposal id must be greater than 0")
}

var errGroup errgroup.Group

// verify proposal IDs
proposalIds := make(map[uint64]struct{})
for _, p := range data.Proposals {
Expand All @@ -48,44 +52,57 @@ func ValidateGenesis(data *GenesisState) error {
}

// 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)
errGroup.Go(func() error {
type depositKey struct {
ProposalId uint64
Depositor string
}

dk := depositKey{d.ProposalId, d.Depositor}
if _, ok := depositIds[dk]; ok {
return fmt.Errorf("duplicate deposit: %v", d)
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{}{}
}

depositIds[dk] = struct{}{}
}
return nil
})

// 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)
errGroup.Go(func() error {
type voteKey struct {
ProposalId uint64
Voter string
}

vk := voteKey{v.ProposalId, v.Voter}
if _, ok := voteIds[vk]; ok {
return fmt.Errorf("duplicate vote: %v", v)
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{}{}
}

voteIds[vk] = struct{}{}
}
return nil
})

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

return data.Params.ValidateBasic()
return errGroup.Wait()
}

var _ types.UnpackInterfacesMessage = GenesisState{}
Expand Down

0 comments on commit 8de4177

Please sign in to comment.