-
Notifications
You must be signed in to change notification settings - Fork 29
/
foundation.go
534 lines (438 loc) · 14.3 KB
/
foundation.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package foundation
import (
"fmt"
"time"
"github.com/gogo/protobuf/proto"
"github.com/Finschia/finschia-sdk/codec"
codectypes "github.com/Finschia/finschia-sdk/codec/types"
sdk "github.com/Finschia/finschia-sdk/types"
sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
)
func validateProposers(proposers []string) error {
if len(proposers) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("no proposers")
}
addrs := map[string]bool{}
for _, proposer := range proposers {
if addrs[proposer] {
return sdkerrors.ErrInvalidRequest.Wrapf("duplicated proposer: %s", proposer)
}
addrs[proposer] = true
if _, err := sdk.AccAddressFromBech32(proposer); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid proposer address: %s", proposer)
}
}
return nil
}
func validateMsgs(msgs []sdk.Msg) error {
if len(msgs) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("no msgs")
}
for i, msg := range msgs {
if err := msg.ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "msg %d", i)
}
}
return nil
}
func validateVoteOption(option VoteOption) error {
if option == VOTE_OPTION_UNSPECIFIED {
return sdkerrors.ErrInvalidRequest.Wrap("empty vote option")
}
if _, ok := VoteOption_name[int32(option)]; !ok {
return sdkerrors.ErrInvalidRequest.Wrap("invalid vote option")
}
return nil
}
func (c Censorship) ValidateBasic() error {
url := c.MsgTypeUrl
if len(url) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("empty msg type url")
}
authority := c.Authority
if _, found := CensorshipAuthority_name[int32(authority)]; !found {
return sdkerrors.ErrInvalidRequest.Wrapf("censorship authority %s over %s", authority, url)
}
return nil
}
func (p Params) ValidateBasic() error {
if err := validateRatio(p.FoundationTax, "tax rate"); err != nil {
return err
}
return nil
}
func (m Member) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Address); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid member address: %s", m.Address)
}
return nil
}
// ValidateBasic performs stateless validation on a member.
func (m MemberRequest) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Address); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid member address: %s", m.Address)
}
return nil
}
type DecisionPolicyResult struct {
Allow bool
Final bool
}
type DecisionPolicy interface {
codec.ProtoMarshaler
// GetVotingPeriod returns the duration after proposal submission where
// votes are accepted.
GetVotingPeriod() time.Duration
// Allow defines policy-specific logic to allow a proposal to pass or not,
// based on its tally result, the foundation's total power and the time since
// the proposal was submitted.
Allow(tallyResult TallyResult, totalPower sdk.Dec, sinceSubmission time.Duration) (*DecisionPolicyResult, error)
ValidateBasic() error
Validate(info FoundationInfo, config Config) error
}
// DefaultTallyResult returns a TallyResult with all counts set to 0.
func DefaultTallyResult() TallyResult {
return NewTallyResult(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec())
}
func NewTallyResult(yes, abstain, no, veto sdk.Dec) TallyResult {
return TallyResult{
YesCount: yes,
AbstainCount: abstain,
NoCount: no,
NoWithVetoCount: veto,
}
}
func (t *TallyResult) Add(option VoteOption) error {
weight := sdk.OneDec()
switch option {
case VOTE_OPTION_YES:
t.YesCount = t.YesCount.Add(weight)
case VOTE_OPTION_NO:
t.NoCount = t.NoCount.Add(weight)
case VOTE_OPTION_ABSTAIN:
t.AbstainCount = t.AbstainCount.Add(weight)
case VOTE_OPTION_NO_WITH_VETO:
t.NoWithVetoCount = t.NoWithVetoCount.Add(weight)
default:
return sdkerrors.ErrInvalidRequest.Wrapf("unknown vote option %s", option)
}
return nil
}
// TotalCounts is the sum of all weights.
func (t TallyResult) TotalCounts() sdk.Dec {
totalCounts := sdk.ZeroDec()
totalCounts = totalCounts.Add(t.YesCount)
totalCounts = totalCounts.Add(t.NoCount)
totalCounts = totalCounts.Add(t.AbstainCount)
totalCounts = totalCounts.Add(t.NoWithVetoCount)
return totalCounts
}
var _ codectypes.UnpackInterfacesMessage = (*Proposal)(nil)
func (p Proposal) ValidateBasic() error {
if p.Id == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("id must be > 0")
}
if p.FoundationVersion == 0 {
return sdkerrors.ErrInvalidVersion.Wrap("foundation version must be > 0")
}
if err := validateProposers(p.Proposers); err != nil {
return err
}
if err := validateMsgs(p.GetMsgs()); err != nil {
return err
}
return nil
}
func (p *Proposal) GetMsgs() []sdk.Msg {
msgs, err := GetMsgs(p.Messages, "proposal")
if err != nil {
panic(err)
}
return msgs
}
func (p *Proposal) SetMsgs(msgs []sdk.Msg) error {
anys, err := SetMsgs(msgs)
if err != nil {
return err
}
p.Messages = anys
return nil
}
// for the tests
func (p Proposal) WithMsgs(msgs []sdk.Msg) *Proposal {
proposal := p
if err := proposal.SetMsgs(msgs); err != nil {
return nil
}
return &proposal
}
func (p Proposal) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
return UnpackInterfaces(unpacker, p.Messages)
}
// UnpackInterfaces unpacks Any's to sdk.Msg's.
func UnpackInterfaces(unpacker codectypes.AnyUnpacker, anys []*codectypes.Any) error {
for _, any := range anys {
var msg sdk.Msg
err := unpacker.UnpackAny(any, &msg)
if err != nil {
return err
}
}
return nil
}
// GetMsgs takes a slice of Any's and turn them into sdk.Msg's.
func GetMsgs(anys []*codectypes.Any, name string) ([]sdk.Msg, error) {
msgs := make([]sdk.Msg, len(anys))
for i, any := range anys {
cached := any.GetCachedValue()
if cached == nil {
return nil, fmt.Errorf("any cached value is nil, %s messages must be correctly packed any values", name)
}
msgs[i] = cached.(sdk.Msg)
}
return msgs, nil
}
// SetMsgs takes a slice of sdk.Msg's and turn them into Any's.
func SetMsgs(msgs []sdk.Msg) ([]*codectypes.Any, error) {
anys := make([]*codectypes.Any, len(msgs))
for i, msg := range msgs {
var err error
anys[i], err = codectypes.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
}
return anys, nil
}
func validateDecisionPolicyWindows(windows DecisionPolicyWindows, config Config) error {
if windows.MinExecutionPeriod >= windows.VotingPeriod+config.MaxExecutionPeriod {
return sdkerrors.ErrInvalidRequest.Wrap("min_execution_period should be smaller than voting_period + max_execution_period")
}
return nil
}
func validateDecisionPolicyWindowsBasic(windows *DecisionPolicyWindows) error {
if windows == nil || windows.VotingPeriod == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("voting period cannot be zero")
}
return nil
}
var _ DecisionPolicy = (*ThresholdDecisionPolicy)(nil)
func (p ThresholdDecisionPolicy) Allow(result TallyResult, totalWeight sdk.Dec, sinceSubmission time.Duration) (*DecisionPolicyResult, error) {
if sinceSubmission < p.Windows.MinExecutionPeriod {
return nil, sdkerrors.ErrUnauthorized.Wrapf("must wait %s after submission before execution, currently at %s", p.Windows.MinExecutionPeriod, sinceSubmission)
}
// the real threshold of the policy is `min(threshold,total_weight)`. If
// the foundation member weights changes (member leaving, member weight update)
// and the threshold doesn't, we can end up with threshold > total_weight.
// In this case, as long as everyone votes yes (in which case
// `yesCount`==`realThreshold`), then the proposal still passes.
realThreshold := sdk.MinDec(p.Threshold, totalWeight)
if result.YesCount.GTE(realThreshold) {
return &DecisionPolicyResult{Allow: true, Final: true}, nil
}
totalCounts := result.TotalCounts()
undecided := totalWeight.Sub(totalCounts)
// maxYesCount is the max potential number of yes count, i.e the current yes count
// plus all undecided count (supposing they all vote yes).
maxYesCount := result.YesCount.Add(undecided)
if maxYesCount.LT(realThreshold) {
return &DecisionPolicyResult{Final: true}, nil
}
return &DecisionPolicyResult{}, nil
}
func (p ThresholdDecisionPolicy) GetVotingPeriod() time.Duration {
return p.Windows.VotingPeriod
}
func (p ThresholdDecisionPolicy) ValidateBasic() error {
if p.Threshold.IsNil() || !p.Threshold.IsPositive() {
return sdkerrors.ErrInvalidRequest.Wrap("threshold must be a positive number")
}
if err := validateDecisionPolicyWindowsBasic(p.Windows); err != nil {
return err
}
return nil
}
func (p ThresholdDecisionPolicy) Validate(info FoundationInfo, config Config) error {
if !info.TotalWeight.IsPositive() {
return sdkerrors.ErrInvalidRequest.Wrapf("total weight must be positive")
}
if err := validateDecisionPolicyWindows(*p.Windows, config); err != nil {
return err
}
return nil
}
var _ DecisionPolicy = (*PercentageDecisionPolicy)(nil)
func (p PercentageDecisionPolicy) Allow(result TallyResult, totalWeight sdk.Dec, sinceSubmission time.Duration) (*DecisionPolicyResult, error) {
if sinceSubmission < p.Windows.MinExecutionPeriod {
return nil, sdkerrors.ErrUnauthorized.Wrapf("must wait %s after submission before execution, currently at %s", p.Windows.MinExecutionPeriod, sinceSubmission)
}
notAbstaining := totalWeight.Sub(result.AbstainCount)
// If no one votes (everyone abstains), proposal fails
if notAbstaining.IsZero() {
return &DecisionPolicyResult{Final: true}, nil
}
yesPercentage := result.YesCount.Quo(notAbstaining)
if yesPercentage.GTE(p.Percentage) {
return &DecisionPolicyResult{Allow: true, Final: true}, nil
}
totalCounts := result.TotalCounts()
undecided := totalWeight.Sub(totalCounts)
maxYesCount := result.YesCount.Add(undecided)
maxYesPercentage := maxYesCount.Quo(notAbstaining)
if maxYesPercentage.LT(p.Percentage) {
return &DecisionPolicyResult{Final: true}, nil
}
return &DecisionPolicyResult{}, nil
}
func (p PercentageDecisionPolicy) GetVotingPeriod() time.Duration {
return p.Windows.VotingPeriod
}
func (p PercentageDecisionPolicy) ValidateBasic() error {
if err := validateDecisionPolicyWindowsBasic(p.Windows); err != nil {
return err
}
if err := validateRatio(p.Percentage, "percentage"); err != nil {
return err
}
return nil
}
func (p PercentageDecisionPolicy) Validate(info FoundationInfo, config Config) error {
// total weight must be positive, because the admin is group policy
// (in x/group words)
if !info.TotalWeight.IsPositive() {
return sdkerrors.ErrInvalidRequest.Wrapf("total weight must be positive")
}
if err := validateDecisionPolicyWindows(*p.Windows, config); err != nil {
return err
}
return nil
}
func validateRatio(ratio sdk.Dec, name string) error {
if ratio.IsNil() {
return sdkerrors.ErrInvalidRequest.Wrapf("%s is nil", name)
}
if ratio.GT(sdk.OneDec()) || ratio.IsNegative() {
return sdkerrors.ErrInvalidRequest.Wrapf("%s must be >= 0 and <= 1", name)
}
return nil
}
var _ DecisionPolicy = (*OutsourcingDecisionPolicy)(nil)
func (p OutsourcingDecisionPolicy) Allow(result TallyResult, totalWeight sdk.Dec, sinceSubmission time.Duration) (*DecisionPolicyResult, error) {
return nil, sdkerrors.ErrInvalidRequest.Wrap(p.Description)
}
func (p OutsourcingDecisionPolicy) GetVotingPeriod() time.Duration {
return 0
}
func (p OutsourcingDecisionPolicy) ValidateBasic() error {
return nil
}
func (p OutsourcingDecisionPolicy) Validate(info FoundationInfo, config Config) error {
return sdkerrors.ErrInvalidRequest.Wrap(p.Description)
}
var _ codectypes.UnpackInterfacesMessage = (*FoundationInfo)(nil)
func (i FoundationInfo) GetDecisionPolicy() DecisionPolicy {
if i.DecisionPolicy == nil {
return nil
}
policy, ok := i.DecisionPolicy.GetCachedValue().(DecisionPolicy)
if !ok {
return nil
}
return policy
}
func (i *FoundationInfo) SetDecisionPolicy(policy DecisionPolicy) error {
msg, ok := policy.(proto.Message)
if !ok {
return sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg)
}
any, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return err
}
i.DecisionPolicy = any
return nil
}
// for the tests
func (i FoundationInfo) WithDecisionPolicy(policy DecisionPolicy) *FoundationInfo {
info := i
if err := info.SetDecisionPolicy(policy); err != nil {
return nil
}
return &info
}
func (i *FoundationInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var policy DecisionPolicy
return unpacker.UnpackAny(i.DecisionPolicy, &policy)
}
func GetAuthorization(any *codectypes.Any, name string) (Authorization, error) {
cached := any.GetCachedValue()
if cached == nil {
return nil, fmt.Errorf("any cached value is nil, %s authorization must be correctly packed any values", name)
}
a, ok := cached.(Authorization)
if !ok {
return nil, sdkerrors.ErrInvalidType.Wrapf("can't proto unmarshal %T", a)
}
return a, nil
}
func SetAuthorization(a Authorization) (*codectypes.Any, error) {
msg, ok := a.(proto.Message)
if !ok {
return nil, sdkerrors.ErrInvalidType.Wrapf("can't proto marshal %T", msg)
}
any, err := codectypes.NewAnyWithValue(a)
if err != nil {
return nil, err
}
return any, nil
}
func (p Pool) ValidateBasic() error {
if err := p.Treasury.Validate(); err != nil {
return err
}
return nil
}
// Members defines a repeated slice of Member objects.
type Members struct {
Members []Member
}
// ValidateBasic performs stateless validation on an array of members. On top
// of validating each member individually, it also makes sure there are no
// duplicate addresses.
func (ms Members) ValidateBasic() error {
index := make(map[string]struct{}, len(ms.Members))
for i := range ms.Members {
member := ms.Members[i]
if err := member.ValidateBasic(); err != nil {
return err
}
addr := member.Address
if _, exists := index[addr]; exists {
return sdkerrors.ErrInvalidRequest.Wrapf("duplicated address: %s", member.Address)
}
index[addr] = struct{}{}
}
return nil
}
// MemberRequests defines a repeated slice of MemberRequest objects.
type MemberRequests struct {
Members []MemberRequest
}
// ValidateBasic performs stateless validation on an array of members. On top
// of validating each member individually, it also makes sure there are no
// duplicate addresses.
func (ms MemberRequests) ValidateBasic() error {
index := make(map[string]struct{}, len(ms.Members))
for i := range ms.Members {
member := ms.Members[i]
if err := member.ValidateBasic(); err != nil {
return err
}
addr := member.Address
if _, exists := index[addr]; exists {
return sdkerrors.ErrInvalidRequest.Wrapf("duplicated address: %s", member.Address)
}
index[addr] = struct{}{}
}
return nil
}