-
Notifications
You must be signed in to change notification settings - Fork 0
/
proposal.go
293 lines (238 loc) · 7.16 KB
/
proposal.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
package types
import (
"encoding/json"
"fmt"
sdk "github.com/ayher/anatha/types"
"github.com/ayher/anatha/x/params"
"strings"
"time"
gov "github.com/ayher/anatha/x/gov"
)
const DefaultStartingProposalID uint64 = 1
func init() {
RegisterProposalType(params.ProposalTypeChange)
}
type Proposal struct {
gov.Content `json:"content" yaml:"content"` // Proposal content interface
ProposalID uint64 `json:"id" yaml:"id"`
Status ProposalStatus `json:"proposal_status" yaml:"proposal_status"`
TallyResult TallyResult `json:"tally_result" yaml:"tally_result"`
VotingStartTime time.Time `json:"voting_start_time" yaml:"voting_start_time"`
VotingEndTime time.Time `json:"voting_end_time" yaml:"voting_end_time"`
ExecutionTime time.Time `json:"proposal_execution_time" yaml:"proposal_execution_time"`
Expedited bool `json:"expedited" yaml:"expedited"`
}
func NewProposal(content gov.Content, id uint64) Proposal {
return Proposal{
Content: content,
ProposalID: id,
TallyResult: EmptyTallyResult(),
ExecutionTime: time.Time{},
Expedited: false,
}
}
func (p Proposal) String() string {
return fmt.Sprintf(`Proposal %d:
Title: %s
Type: %s
Status: %s
Voting Start Time: %s
Voting End Time: %s
Execution Time: %s
Expedited: %s
Description: %s`,
p.ProposalID, p.GetTitle(), p.ProposalType(),
p.Status, p.VotingStartTime, p.VotingEndTime, p.ExecutionTime, p.Expedited , p.GetDescription(),
)
}
type Proposals []Proposal
func (p Proposals) String() string {
out := "ID - (Status) [Type] Title\n"
for _, prop := range p {
out += fmt.Sprintf("%d - (%s) [%s] %s\n",
prop.ProposalID, prop.Status,
prop.ProposalType(), prop.GetTitle())
}
return strings.TrimSpace(out)
}
type (
ProposalStatus byte
)
const (
StatusNil ProposalStatus = 0x00
StatusVotingPeriod ProposalStatus = 0x01
StatusExpediting ProposalStatus = 0x05
StatusPassed ProposalStatus = 0x02
StatusRejected ProposalStatus = 0x03
StatusFailed ProposalStatus = 0x04
)
func ProposalStatusFromString(str string) (ProposalStatus, error) {
switch str {
case "VotingPeriod":
return StatusVotingPeriod, nil
case "Passed":
return StatusPassed, nil
case "Rejected":
return StatusRejected, nil
case "Failed":
return StatusFailed, nil
case "":
return StatusNil, nil
default:
return ProposalStatus(0xff), fmt.Errorf("'%s' is not a valid proposal status", str)
}
}
func ValidProposalStatus(status ProposalStatus) bool {
if status == StatusVotingPeriod ||
status == StatusPassed ||
status == StatusRejected ||
status == StatusFailed {
return true
}
return false
}
func (status ProposalStatus) Marshal() ([]byte, error) {
return []byte{byte(status)}, nil
}
func (status *ProposalStatus) Unmarshal(data []byte) error {
*status = ProposalStatus(data[0])
return nil
}
func (status ProposalStatus) MarshalJSON() ([]byte, error) {
return json.Marshal(status.String())
}
func (status *ProposalStatus) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
bz2, err := ProposalStatusFromString(s)
if err != nil {
return err
}
*status = bz2
return nil
}
func (status ProposalStatus) String() string {
switch status {
case StatusVotingPeriod:
return "VotingPeriod"
case StatusPassed:
return "Passed"
case StatusRejected:
return "Rejected"
case StatusFailed:
return "Failed"
default:
return ""
}
}
func (status ProposalStatus) Format(s fmt.State, verb rune) {
switch verb {
case 's':
s.Write([]byte(status.String()))
default:
s.Write([]byte(fmt.Sprintf("%v", byte(status))))
}
}
var validProposalTypes = map[string]struct{}{
ProposalTypeText: {},
ProposalTypeAddGovernor: {},
ProposalTypeRemoveGovernor: {},
}
func RegisterProposalType(ty string) {
if _, ok := validProposalTypes[ty]; ok {
panic(fmt.Sprintf("already registered proposal type: %s", ty))
}
validProposalTypes[ty] = struct{}{}
}
func ContentFromProposalType(title, desc, ty string) gov.Content {
switch ty {
case ProposalTypeText:
return NewTextProposal(title, desc)
default:
return nil
}
}
func IsValidProposalType(ty string) bool {
_, ok := validProposalTypes[ty]
return ok
}
const (
ProposalTypeText string = "Text"
ProposalTypeAddGovernor = "AddGovernor"
ProposalTypeRemoveGovernor = "RemoveGovernor"
)
type TextProposal struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
}
func NewTextProposal(title, description string) gov.Content {
return TextProposal{
title,
description,
}
}
var _ gov.Content = TextProposal{}
func (tp TextProposal) GetTitle() string { return tp.Title }
func (tp TextProposal) GetDescription() string { return tp.Description }
func (tp TextProposal) ProposalRoute() string { return RouterKey }
func (tp TextProposal) ProposalType() string { return ProposalTypeText }
func (tp TextProposal) ValidateBasic() error { return ValidateAbstract(tp) }
func (tp TextProposal) String() string {
return fmt.Sprintf(`Text Proposal:
Title: %s
Description: %s
`, tp.Title, tp.Description)
}
type AddGovernorProposal struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Governor sdk.AccAddress `json:"governor" yaml:"governor"`
}
func NewAddGovernorProposal(title string, description string, governor sdk.AccAddress) gov.Content {
return AddGovernorProposal{
Title: title,
Description: description,
Governor: governor,
}
}
var _ gov.Content = AddGovernorProposal{}
func (p AddGovernorProposal) GetTitle() string { return p.Title }
func (p AddGovernorProposal) GetDescription() string { return p.Description }
func (p AddGovernorProposal) ProposalRoute() string { return RouterKey }
func (p AddGovernorProposal) ProposalType() string { return ProposalTypeAddGovernor }
func (p AddGovernorProposal) ValidateBasic() error { return ValidateAbstract(p) }
func (p AddGovernorProposal) String() string {
return fmt.Sprintf(`Add Governor Proposal:
Title: %s
Description: %s
Governor: %s
`, p.Title, p.Description, p.Governor)
}
type RemoveGovernorProposal struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Governor sdk.AccAddress `json:"governor" yaml:"governor"`
}
func NewRemoveGovernorProposal(title string, description string, governor sdk.AccAddress) gov.Content {
return RemoveGovernorProposal{
Title: title,
Description: description,
Governor: governor,
}
}
var _ gov.Content = RemoveGovernorProposal{}
func (p RemoveGovernorProposal) GetTitle() string { return p.Title }
func (p RemoveGovernorProposal) GetDescription() string { return p.Description }
func (p RemoveGovernorProposal) ProposalRoute() string { return RouterKey }
func (p RemoveGovernorProposal) ProposalType() string { return ProposalTypeRemoveGovernor }
func (p RemoveGovernorProposal) ValidateBasic() error { return ValidateAbstract(p) }
func (p RemoveGovernorProposal) String() string {
return fmt.Sprintf(`Remove Governor Proposal:
Title: %s
Description: %s
Governor: %s
`, p.Title, p.Description, p.Governor)
}