-
Notifications
You must be signed in to change notification settings - Fork 364
/
committee.go
466 lines (394 loc) · 13.4 KB
/
committee.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
package types
import (
fmt "fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
proto "github.com/gogo/protobuf/proto"
"sigs.k8s.io/yaml"
)
const MaxCommitteeDescriptionLength int = 512
const (
BaseCommitteeType = "kava/BaseCommittee"
MemberCommitteeType = "kava/MemberCommittee" // Committee is composed of member addresses that vote to enact proposals within their permissions
TokenCommitteeType = "kava/TokenCommittee" // Committee is composed of token holders with voting power determined by total token balance
BondDenom = "ukava"
)
// Marshal needed for protobuf compatibility.
func (t TallyOption) Marshal() ([]byte, error) {
return []byte{byte(t)}, nil
}
// Unmarshal needed for protobuf compatibility.
func (t *TallyOption) Unmarshal(data []byte) error {
*t = TallyOption(data[0])
return nil
}
// Committee is an interface for handling common actions on committees
type Committee interface {
codec.ProtoMarshaler
codectypes.UnpackInterfacesMessage
GetID() uint64
GetType() string
GetDescription() string
GetMembers() []sdk.AccAddress
SetMembers([]sdk.AccAddress)
HasMember(addr sdk.AccAddress) bool
GetPermissions() []Permission
SetPermissions([]Permission)
HasPermissionsFor(ctx sdk.Context, appCdc codec.Codec, pk ParamKeeper, proposal PubProposal) bool
GetProposalDuration() time.Duration
SetProposalDuration(time.Duration)
GetVoteThreshold() sdk.Dec
SetVoteThreshold(sdk.Dec)
GetTallyOption() TallyOption
Validate() error
String() string
}
var (
_ Committee = &BaseCommittee{}
_ codectypes.UnpackInterfacesMessage = &Committees{}
)
type Committees []Committee
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (c Committees) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
for _, committee := range c {
if err := committee.UnpackInterfaces(unpacker); err != nil {
return err
}
}
return nil
}
// GetType is a getter for committee type
func (c *BaseCommittee) GetType() string { return BaseCommitteeType }
// GetID is a getter for committee ID
func (c *BaseCommittee) GetID() uint64 { return c.ID }
// GetDescription is a getter for committee description
func (c *BaseCommittee) GetDescription() string { return c.Description }
// GetMembers is a getter for committee members
func (c BaseCommittee) GetMembers() []sdk.AccAddress { return c.Members }
// SetMembers is a setter for committee members
func (c *BaseCommittee) SetMembers(members []sdk.AccAddress) { c.Members = members }
// HasMember returns if a committee contains a given member address
func (c *BaseCommittee) HasMember(addr sdk.AccAddress) bool {
for _, m := range c.GetMembers() {
if m.Equals(addr) {
return true
}
}
return false
}
// GetPermissions is a getter for committee permissions
func (c *BaseCommittee) GetPermissions() []Permission {
permissions, err := UnpackPermissions(c.Permissions)
if err != nil {
panic(err)
}
return permissions
}
// SetPermissions is a setter for committee permissions
func (c *BaseCommittee) SetPermissions(permissions []Permission) {
if len(permissions) == 0 {
c.Permissions = nil
}
permissionsAny, err := PackPermissions(permissions)
if err != nil {
panic(err)
}
c.Permissions = permissionsAny
}
// HasPermissionsFor returns whether the committee is authorized to enact a proposal.
// As long as one permission allows the proposal then it goes through. Its the OR of all permissions.
func (c BaseCommittee) HasPermissionsFor(ctx sdk.Context, appCdc codec.Codec, pk ParamKeeper, proposal PubProposal) bool {
for _, p := range c.GetPermissions() {
if p.Allows(ctx, pk, proposal) {
return true
}
}
return false
}
// String implements fmt.Stringer
func (c BaseCommittee) String() string {
return fmt.Sprintf(`Committee %d:
Description: %s
Members: %s
Permissions: %s
VoteThreshold: %s
ProposalDuration: %s
TallyOption: %s`,
c.ID, c.Description, c.GetMembers(), c.Permissions,
c.VoteThreshold.String(), c.ProposalDuration.String(),
c.TallyOption.String(),
)
}
// GetVoteThreshold is a getter for committee VoteThreshold
func (c BaseCommittee) GetVoteThreshold() sdk.Dec { return c.VoteThreshold }
// SetVoteThreshold is a setter for committee VoteThreshold
func (c *BaseCommittee) SetVoteThreshold(voteThreshold sdk.Dec) {
c.VoteThreshold = voteThreshold
}
// GetProposalDuration is a getter for committee ProposalDuration
func (c BaseCommittee) GetProposalDuration() time.Duration { return c.ProposalDuration }
// SetProposalDuration is a setter for committee ProposalDuration
func (c *BaseCommittee) SetProposalDuration(proposalDuration time.Duration) {
c.ProposalDuration = proposalDuration
}
// GetTallyOption is a getter for committee TallyOption
func (c BaseCommittee) GetTallyOption() TallyOption { return c.TallyOption }
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (c BaseCommittee) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
for _, any := range c.Permissions {
var permission Permission
err := unpacker.UnpackAny(any, &permission)
if err != nil {
return err
}
}
return nil
}
// Validate validates BaseCommittee fields
func (c BaseCommittee) Validate() error {
if len(c.Description) > MaxCommitteeDescriptionLength {
return fmt.Errorf("description length %d longer than max allowed %d", len(c.Description), MaxCommitteeDescriptionLength)
}
if len(c.Members) <= 0 {
return fmt.Errorf("committee must have members")
}
addressMap := make(map[string]bool, len(c.Members))
for _, m := range c.Members {
// check there are no duplicate members
if _, ok := addressMap[m.String()]; ok {
return fmt.Errorf("committee cannot have duplicate members, %s", m)
}
// check for valid addresses
if m.Empty() {
return fmt.Errorf("committee cannot have empty member address")
}
addressMap[m.String()] = true
}
// validate permissions
permissions, err := UnpackPermissions(c.Permissions)
if err != nil {
return err
}
for _, p := range permissions {
if p == nil {
return fmt.Errorf("committee cannot have a nil permission")
}
}
if c.ProposalDuration < 0 {
return fmt.Errorf("invalid proposal duration: %s", c.ProposalDuration)
}
// threshold must be in the range [0, 1]
if c.VoteThreshold.IsNil() || c.VoteThreshold.LTE(sdk.ZeroDec()) || c.VoteThreshold.GT(sdk.NewDec(1)) {
return fmt.Errorf("invalid threshold: %s", c.VoteThreshold)
}
if c.TallyOption <= 0 || c.TallyOption > 2 {
return fmt.Errorf("invalid tally option: %d", c.TallyOption)
}
return nil
}
// NewMemberCommittee instantiates a new instance of MemberCommittee
func NewMemberCommittee(id uint64, description string, members []sdk.AccAddress, permissions []Permission,
threshold sdk.Dec, duration time.Duration, tallyOption TallyOption,
) (*MemberCommittee, error) {
permissionsAny, err := PackPermissions(permissions)
if err != nil {
return nil, err
}
return &MemberCommittee{
BaseCommittee: &BaseCommittee{
ID: id,
Description: description,
Members: members,
Permissions: permissionsAny,
VoteThreshold: threshold,
ProposalDuration: duration,
TallyOption: tallyOption,
},
}, nil
}
// MustNewMemberCommittee instantiates a new instance of MemberCommittee and panics on error
func MustNewMemberCommittee(id uint64, description string, members []sdk.AccAddress, permissions []Permission,
threshold sdk.Dec, duration time.Duration, tallyOption TallyOption,
) *MemberCommittee {
committee, err := NewMemberCommittee(id, description, members, permissions, threshold, duration, tallyOption)
if err != nil {
panic(err)
}
return committee
}
// GetType is a getter for committee type
func (c MemberCommittee) GetType() string { return MemberCommitteeType }
// NewTokenCommittee instantiates a new instance of TokenCommittee
func NewTokenCommittee(id uint64, description string, members []sdk.AccAddress, permissions []Permission,
threshold sdk.Dec, duration time.Duration, tallyOption TallyOption, quorum sdk.Dec, tallyDenom string,
) (*TokenCommittee, error) {
permissionsAny, err := PackPermissions(permissions)
if err != nil {
return nil, err
}
return &TokenCommittee{
BaseCommittee: &BaseCommittee{
ID: id,
Description: description,
Members: members,
Permissions: permissionsAny,
VoteThreshold: threshold,
ProposalDuration: duration,
TallyOption: tallyOption,
},
Quorum: quorum,
TallyDenom: tallyDenom,
}, nil
}
// MustNewTokenCommittee instantiates a new instance of TokenCommittee and panics on error
func MustNewTokenCommittee(id uint64, description string, members []sdk.AccAddress, permissions []Permission,
threshold sdk.Dec, duration time.Duration, tallyOption TallyOption, quorum sdk.Dec, tallyDenom string,
) *TokenCommittee {
committee, err := NewTokenCommittee(id, description, members, permissions, threshold, duration, tallyOption, quorum, tallyDenom)
if err != nil {
panic(err)
}
return committee
}
// GetType is a getter for committee type
func (c TokenCommittee) GetType() string { return TokenCommitteeType }
// GetQuorum returns the quorum of the committee
func (c TokenCommittee) GetQuorum() sdk.Dec { return c.Quorum }
// GetTallyDenom returns the tally denom of the committee
func (c TokenCommittee) GetTallyDenom() string { return c.TallyDenom }
// Validate validates the committee's fields
func (c TokenCommittee) Validate() error {
if c.TallyDenom == BondDenom {
return fmt.Errorf("invalid tally denom: %s", c.TallyDenom)
}
err := sdk.ValidateDenom(c.TallyDenom)
if err != nil {
return err
}
if c.Quorum.IsNil() || c.Quorum.IsNegative() || c.Quorum.GT(sdk.NewDec(1)) {
return fmt.Errorf("invalid quorum: %s", c.Quorum)
}
return c.BaseCommittee.Validate()
}
// ------------------------------------------
// Proposals
// ------------------------------------------
// PubProposal is the interface that all proposals must fulfill to be submitted to a committee.
// Proposal types can be created external to this module. For example a ParamChangeProposal, or CommunityPoolSpendProposal.
// It is pinned to the equivalent type in the gov module to create compatibility between proposal types.
type PubProposal govv1beta1.Content
var (
_ PubProposal = Proposal{}
_ codectypes.UnpackInterfacesMessage = Proposals{}
)
type Proposals []Proposal
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (p Proposals) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
for _, committee := range p {
if err := committee.UnpackInterfaces(unpacker); err != nil {
return err
}
}
return nil
}
// NewProposal instantiates a new instance of Proposal
func NewProposal(pubProposal PubProposal, id uint64, committeeID uint64, deadline time.Time) (Proposal, error) {
msg, ok := pubProposal.(proto.Message)
if !ok {
return Proposal{}, fmt.Errorf("%T does not implement proto.Message", pubProposal)
}
proposalAny, err := codectypes.NewAnyWithValue(msg)
if err != nil {
return Proposal{}, err
}
return Proposal{
Content: proposalAny,
ID: id,
CommitteeID: committeeID,
Deadline: deadline,
}, nil
}
// MustNewProposal instantiates a new instance of Proposal and panics if there is an error
func MustNewProposal(pubProposal PubProposal, id uint64, committeeID uint64, deadline time.Time) Proposal {
proposal, err := NewProposal(pubProposal, id, committeeID, deadline)
if err != nil {
panic(err)
}
return proposal
}
// GetPubProposal returns the PubProposal (govtypes.Content)
func (p Proposal) GetContent() PubProposal {
content, ok := p.Content.GetCachedValue().(PubProposal)
if !ok {
return nil
}
return content
}
// String implements the fmt.Stringer interface.
func (p Proposal) String() string {
bz, _ := yaml.Marshal(p)
return string(bz)
}
func (p Proposal) GetTitle() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.GetTitle()
}
func (p Proposal) GetDescription() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.GetDescription()
}
func (p Proposal) ProposalRoute() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.ProposalRoute()
}
func (p Proposal) ProposalType() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.ProposalType()
}
func (p Proposal) ValidateBasic() error {
content := p.GetContent()
if content == nil {
return nil
}
return content.ValidateBasic()
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (p Proposal) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var content PubProposal
return unpacker.UnpackAny(p.Content, &content)
}
// HasExpiredBy calculates if the proposal will have expired by a certain time.
// All votes must be cast before deadline, those cast at time == deadline are not valid
func (p Proposal) HasExpiredBy(time time.Time) bool {
return !time.Before(p.Deadline)
}
// NewVote instantiates a new instance of Vote
func NewVote(proposalID uint64, voter sdk.AccAddress, voteType VoteType) Vote {
return Vote{
ProposalID: proposalID,
Voter: voter,
VoteType: voteType,
}
}
// Validates Vote fields
func (v Vote) Validate() error {
if v.Voter.Empty() {
return fmt.Errorf("voter address cannot be empty")
}
return v.VoteType.Validate()
}