-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
genesis.go
119 lines (97 loc) · 2.86 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package v1
import (
"errors"
"fmt"
"golang.org/x/sync/errgroup"
"github.com/cosmos/cosmos-sdk/codec/types"
)
// NewGenesisState creates a new genesis state for the governance module
func NewGenesisState(startingProposalID uint64, params Params) *GenesisState {
return &GenesisState{
StartingProposalId: startingProposalID,
Params: ¶ms,
}
}
// DefaultGenesisState defines the default governance genesis state
func DefaultGenesisState() *GenesisState {
return NewGenesisState(
DefaultStartingProposalID,
DefaultParams(),
)
}
// Empty returns true if a GenesisState is empty
func (data GenesisState) Empty() bool {
return data.StartingProposalId == 0 || data.Params == nil
}
// 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")
}
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
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
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{}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (data GenesisState) UnpackInterfaces(unpacker types.AnyUnpacker) error {
for _, p := range data.Proposals {
err := p.UnpackInterfaces(unpacker)
if err != nil {
return err
}
}
return nil
}