-
Notifications
You must be signed in to change notification settings - Fork 30
/
genesis.go
230 lines (191 loc) · 5.8 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package foundation
import (
"github.com/gogo/protobuf/proto"
codectypes "github.com/Finschia/finschia-sdk/codec/types"
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
authtypes "github.com/Finschia/finschia-sdk/x/auth/types"
)
// DefaultGenesisState creates a default GenesisState object
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
Foundation: DefaultFoundation(),
}
}
func DefaultFoundation() FoundationInfo {
return *FoundationInfo{
Version: 1,
TotalWeight: sdk.ZeroDec(),
}.WithDecisionPolicy(DefaultDecisionPolicy())
}
func DefaultDecisionPolicy() DecisionPolicy {
return &OutsourcingDecisionPolicy{
Description: "using x/group",
}
}
func DefaultAuthority() sdk.AccAddress {
return authtypes.NewModuleAddress(ModuleName)
}
func DefaultParams() Params {
return Params{
FoundationTax: sdk.ZeroDec(),
}
}
func (data GenesisState) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if err := data.Foundation.UnpackInterfaces(unpacker); err != nil {
return err
}
for _, p := range data.Proposals {
if err := p.UnpackInterfaces(unpacker); err != nil {
return err
}
}
for _, ga := range data.Authorizations {
if err := ga.UnpackInterfaces(unpacker); err != nil {
return err
}
}
return nil
}
func (i FoundationInfo) ValidateBasic() error {
if i.Version == 0 {
return sdkerrors.ErrInvalidVersion.Wrap("version must be > 0")
}
if i.TotalWeight.IsNil() || i.TotalWeight.IsNegative() {
return sdkerrors.ErrInvalidRequest.Wrap("total weight must be >= 0")
}
policy := i.GetDecisionPolicy()
if policy == nil {
return sdkerrors.ErrInvalidRequest.Wrap("must provide decision policy")
}
if err := policy.ValidateBasic(); err != nil {
return err
}
// Is foundation outsourcing the proposal feature
_, isOutsourcing := i.GetDecisionPolicy().(*OutsourcingDecisionPolicy)
memberNotExists := i.TotalWeight.IsZero()
if isOutsourcing && !memberNotExists {
return sdkerrors.ErrInvalidRequest.Wrap("outsourcing policy not allows members")
}
if !isOutsourcing && memberNotExists {
return sdkerrors.ErrInvalidRequest.Wrap("one member must exist at least")
}
return nil
}
// ValidateGenesis validates the provided genesis state to ensure the
// expected invariants holds.
func ValidateGenesis(data GenesisState) error {
if err := data.Params.ValidateBasic(); err != nil {
return err
}
info := data.Foundation
if err := info.ValidateBasic(); err != nil {
return err
}
// Is x/foundation outsourcing the proposal feature
isOutsourcing := info.TotalWeight.IsZero()
if realWeight := sdk.NewDec(int64(len(data.Members))); !info.TotalWeight.Equal(realWeight) {
return sdkerrors.ErrInvalidRequest.Wrapf("total weight not match, %s != %s", info.TotalWeight, realWeight)
}
members := Members{Members: data.Members}
if err := members.ValidateBasic(); err != nil {
return err
}
if isOutsourcing && len(data.Proposals) != 0 {
return sdkerrors.ErrInvalidRequest.Wrap("outsourcing policy not allows proposals")
}
proposalIDs := map[uint64]bool{}
for _, proposal := range data.Proposals {
id := proposal.Id
if id > data.PreviousProposalId {
return sdkerrors.ErrInvalidRequest.Wrapf("proposal %d has not yet been submitted", id)
}
if proposalIDs[id] {
return sdkerrors.ErrInvalidRequest.Wrapf("duplicated proposal id of %d", id)
}
proposalIDs[id] = true
if err := proposal.ValidateBasic(); err != nil {
return err
}
if proposal.FoundationVersion > info.Version {
return sdkerrors.ErrInvalidRequest.Wrapf("invalid foundation version of proposal %d", id)
}
}
for _, vote := range data.Votes {
if !proposalIDs[vote.ProposalId] {
return sdkerrors.ErrInvalidRequest.Wrapf("vote for a proposal which does not exist: id %d", vote.ProposalId)
}
if _, err := sdk.AccAddressFromBech32(vote.Voter); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid voter address: %s", vote.Voter)
}
if err := validateVoteOption(vote.Option); err != nil {
return err
}
}
seenURLs := map[string]bool{}
for _, censorship := range data.Censorships {
if err := censorship.ValidateBasic(); err != nil {
return err
}
if censorship.Authority == CensorshipAuthorityUnspecified {
return sdkerrors.ErrInvalidRequest.Wrap("authority unspecified")
}
url := censorship.MsgTypeUrl
if seenURLs[url] {
return sdkerrors.ErrInvalidRequest.Wrapf("duplicate censorship over %s", url)
}
seenURLs[url] = true
}
for _, ga := range data.Authorizations {
auth := ga.GetAuthorization()
if auth == nil {
return sdkerrors.ErrInvalidType.Wrap("invalid authorization")
}
url := auth.MsgTypeURL()
if !seenURLs[url] {
return sdkerrors.ErrInvalidRequest.Wrapf("no censorship over %s", url)
}
if _, err := sdk.AccAddressFromBech32(ga.Grantee); err != nil {
return err
}
}
if err := data.Pool.ValidateBasic(); err != nil {
return err
}
return nil
}
func (g GrantAuthorization) GetAuthorization() Authorization {
if g.Authorization == nil {
return nil
}
a, ok := g.Authorization.GetCachedValue().(Authorization)
if !ok {
return nil
}
return a
}
func (g *GrantAuthorization) SetAuthorization(a Authorization) error {
msg, ok := a.(proto.Message)
if !ok {
return sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg)
}
any, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return err
}
g.Authorization = any
return nil
}
// for the tests
func (g GrantAuthorization) WithAuthorization(authorization Authorization) *GrantAuthorization {
grant := g
if err := grant.SetAuthorization(authorization); err != nil {
return nil
}
return &grant
}
func (g GrantAuthorization) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var authorization Authorization
return unpacker.UnpackAny(g.Authorization, &authorization)
}