-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
proposal.go
263 lines (218 loc) · 6.43 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
package types
import (
"fmt"
"strings"
"time"
"github.com/gogo/protobuf/proto"
yaml "gopkg.in/yaml.v2"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// DefaultStartingProposalID is 1
const DefaultStartingProposalID uint64 = 1
// NewProposal creates a new Proposal instance
func NewProposal(content Content, id uint64, submitTime, depositEndTime time.Time) (Proposal, error) {
p := Proposal{
ProposalId: id,
Status: StatusDepositPeriod,
FinalTallyResult: EmptyTallyResult(),
TotalDeposit: sdk.NewCoins(),
SubmitTime: submitTime,
DepositEndTime: depositEndTime,
}
msg, ok := content.(proto.Message)
if !ok {
return Proposal{}, fmt.Errorf("%T does not implement proto.Message", content)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
return Proposal{}, err
}
p.Content = any
return p, nil
}
// String implements stringer interface
func (p Proposal) String() string {
out, _ := yaml.Marshal(p)
return string(out)
}
// GetContent returns the proposal Content
func (p Proposal) GetContent() Content {
content, ok := p.Content.GetCachedValue().(Content)
if !ok {
return nil
}
return content
}
func (p Proposal) ProposalType() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.ProposalType()
}
func (p Proposal) ProposalRoute() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.ProposalRoute()
}
func (p Proposal) GetTitle() string {
content := p.GetContent()
if content == nil {
return ""
}
return content.GetTitle()
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (p Proposal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var content Content
return unpacker.UnpackAny(p.Content, &content)
}
// Proposals is an array of proposal
type Proposals []Proposal
var _ types.UnpackInterfacesMessage = Proposals{}
// Equal returns true if two slices (order-dependant) of proposals are equal.
func (p Proposals) Equal(other Proposals) bool {
if len(p) != len(other) {
return false
}
for i, proposal := range p {
if !proposal.Equal(other[i]) {
return false
}
}
return true
}
// String implements stringer interface
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)
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (p Proposals) UnpackInterfaces(unpacker types.AnyUnpacker) error {
for _, x := range p {
err := x.UnpackInterfaces(unpacker)
if err != nil {
return err
}
}
return nil
}
type (
// ProposalQueue defines a queue for proposal ids
ProposalQueue []uint64
)
// ProposalStatusFromString turns a string into a ProposalStatus
func ProposalStatusFromString(str string) (ProposalStatus, error) {
num, ok := ProposalStatus_value[str]
if !ok {
return StatusNil, fmt.Errorf("'%s' is not a valid proposal status", str)
}
return ProposalStatus(num), nil
}
// ValidProposalStatus returns true if the proposal status is valid and false
// otherwise.
func ValidProposalStatus(status ProposalStatus) bool {
if status == StatusDepositPeriod ||
status == StatusVotingPeriod ||
status == StatusPassed ||
status == StatusRejected ||
status == StatusFailed {
return true
}
return false
}
// Marshal needed for protobuf compatibility
func (status ProposalStatus) Marshal() ([]byte, error) {
return []byte{byte(status)}, nil
}
// Unmarshal needed for protobuf compatibility
func (status *ProposalStatus) Unmarshal(data []byte) error {
*status = ProposalStatus(data[0])
return nil
}
// Format implements the fmt.Formatter interface.
// nolint: errcheck
func (status ProposalStatus) Format(s fmt.State, verb rune) {
switch verb {
case 's':
s.Write([]byte(status.String()))
default:
// TODO: Do this conversion more directly
s.Write([]byte(fmt.Sprintf("%v", byte(status))))
}
}
// Proposal types
const (
ProposalTypeText string = "Text"
)
// Implements Content Interface
var _ Content = &TextProposal{}
// NewTextProposal creates a text proposal Content
func NewTextProposal(title, description string) Content {
return &TextProposal{title, description}
}
// GetTitle returns the proposal title
func (tp *TextProposal) GetTitle() string { return tp.Title }
// GetDescription returns the proposal description
func (tp *TextProposal) GetDescription() string { return tp.Description }
// ProposalRoute returns the proposal router key
func (tp *TextProposal) ProposalRoute() string { return RouterKey }
// ProposalType is "Text"
func (tp *TextProposal) ProposalType() string { return ProposalTypeText }
// ValidateBasic validates the content's title and description of the proposal
func (tp *TextProposal) ValidateBasic() error { return ValidateAbstract(tp) }
// String implements Stringer interface
func (tp TextProposal) String() string {
out, _ := yaml.Marshal(tp)
return string(out)
}
var validProposalTypes = map[string]struct{}{
ProposalTypeText: {},
}
// RegisterProposalType registers a proposal type. It will panic if the type is
// already registered.
func RegisterProposalType(ty string) {
if _, ok := validProposalTypes[ty]; ok {
panic(fmt.Sprintf("already registered proposal type: %s", ty))
}
validProposalTypes[ty] = struct{}{}
}
// ContentFromProposalType returns a Content object based on the proposal type.
func ContentFromProposalType(title, desc, ty string) Content {
switch ty {
case ProposalTypeText:
return NewTextProposal(title, desc)
default:
return nil
}
}
// IsValidProposalType returns a boolean determining if the proposal type is
// valid.
//
// NOTE: Modules with their own proposal types must register them.
func IsValidProposalType(ty string) bool {
_, ok := validProposalTypes[ty]
return ok
}
// ProposalHandler implements the Handler interface for governance module-based
// proposals (ie. TextProposal ). Since these are
// merely signaling mechanisms at the moment and do not affect state, it
// performs a no-op.
func ProposalHandler(_ sdk.Context, c Content) error {
switch c.ProposalType() {
case ProposalTypeText:
// both proposal types do not change state so this performs a no-op
return nil
default:
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unrecognized gov proposal type: %s", c.ProposalType())
}
}