/
proposal.go
368 lines (317 loc) · 12.3 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
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
package types
import (
"encoding/base64"
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)
type ProposalType string
const (
ProposalTypeStoreCode ProposalType = "StoreCode"
ProposalTypeInstantiateContract ProposalType = "InstantiateContract"
ProposalTypeMigrateContract ProposalType = "MigrateContract"
ProposalTypeUpdateAdmin ProposalType = "UpdateAdmin"
ProposalTypeClearAdmin ProposalType = "ClearAdmin"
)
// DisableAllProposals contains no wasm gov types.
var DisableAllProposals []ProposalType
// EnableAllProposals contains all wasm gov types as keys.
var EnableAllProposals = []ProposalType{
ProposalTypeStoreCode,
ProposalTypeInstantiateContract,
ProposalTypeMigrateContract,
ProposalTypeUpdateAdmin,
ProposalTypeClearAdmin,
}
// ConvertToProposals maps each key to a ProposalType and returns a typed list.
// If any string is not a valid type (in this file), then return an error
func ConvertToProposals(keys []string) ([]ProposalType, error) {
valid := make(map[string]bool, len(EnableAllProposals))
for _, key := range EnableAllProposals {
valid[string(key)] = true
}
proposals := make([]ProposalType, len(keys))
for i, key := range keys {
if _, ok := valid[key]; !ok {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "'%s' is not a valid ProposalType", key)
}
proposals[i] = ProposalType(key)
}
return proposals, nil
}
func init() { // register new content types with the sdk
govtypes.RegisterProposalType(string(ProposalTypeStoreCode))
govtypes.RegisterProposalType(string(ProposalTypeInstantiateContract))
govtypes.RegisterProposalType(string(ProposalTypeMigrateContract))
govtypes.RegisterProposalType(string(ProposalTypeUpdateAdmin))
govtypes.RegisterProposalType(string(ProposalTypeClearAdmin))
govtypes.RegisterProposalTypeCodec(StoreCodeProposal{}, "wasm/StoreCodeProposal")
govtypes.RegisterProposalTypeCodec(InstantiateContractProposal{}, "wasm/InstantiateContractProposal")
govtypes.RegisterProposalTypeCodec(MigrateContractProposal{}, "wasm/MigrateContractProposal")
govtypes.RegisterProposalTypeCodec(UpdateAdminProposal{}, "wasm/UpdateAdminProposal")
govtypes.RegisterProposalTypeCodec(ClearAdminProposal{}, "wasm/ClearAdminProposal")
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p StoreCodeProposal) ProposalRoute() string { return RouterKey }
// GetTitle returns the title of the proposal
func (p *StoreCodeProposal) GetTitle() string { return p.Title }
// GetDescription returns the human readable description of the proposal
func (p StoreCodeProposal) GetDescription() string { return p.Description }
// ProposalType returns the type
func (p StoreCodeProposal) ProposalType() string { return string(ProposalTypeStoreCode) }
// ValidateBasic validates the proposal
func (p StoreCodeProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(p.RunAs); err != nil {
return sdkerrors.Wrap(err, "run as")
}
if err := validateWasmCode(p.WASMByteCode); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "code bytes %s", err.Error())
}
if err := validateSourceURL(p.Source); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "source %s", err.Error())
}
if err := validateBuilder(p.Builder); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "builder %s", err.Error())
}
if p.InstantiatePermission != nil {
if err := p.InstantiatePermission.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "instantiate permission")
}
}
return nil
}
// String implements the Stringer interface.
func (p StoreCodeProposal) String() string {
return fmt.Sprintf(`Store Code Proposal:
Title: %s
Description: %s
Run as: %s
WasmCode: %X
Source: %s
Builder: %s
`, p.Title, p.Description, p.RunAs, p.WASMByteCode, p.Source, p.Builder)
}
// MarshalYAML pretty prints the wasm byte code
func (p StoreCodeProposal) MarshalYAML() (interface{}, error) {
return struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
RunAs string `yaml:"run_as"`
WASMByteCode string `yaml:"wasm_byte_code"`
Source string `yaml:"source"`
Builder string `yaml:"builder"`
InstantiatePermission *AccessConfig `yaml:"instantiate_permission"`
}{
Title: p.Title,
Description: p.Description,
RunAs: p.RunAs,
WASMByteCode: base64.StdEncoding.EncodeToString(p.WASMByteCode),
Source: p.Source,
Builder: p.Builder,
InstantiatePermission: p.InstantiatePermission,
}, nil
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p InstantiateContractProposal) ProposalRoute() string { return RouterKey }
// GetTitle returns the title of the proposal
func (p *InstantiateContractProposal) GetTitle() string { return p.Title }
// GetDescription returns the human readable description of the proposal
func (p InstantiateContractProposal) GetDescription() string { return p.Description }
// ProposalType returns the type
func (p InstantiateContractProposal) ProposalType() string {
return string(ProposalTypeInstantiateContract)
}
// ValidateBasic validates the proposal
func (p InstantiateContractProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(p.RunAs); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "run as")
}
if p.CodeID == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "code id is required")
}
if err := validateLabel(p.Label); err != nil {
return err
}
if !p.InitFunds.IsValid() {
return sdkerrors.ErrInvalidCoins
}
if len(p.Admin) != 0 {
if _, err := sdk.AccAddressFromBech32(p.Admin); err != nil {
return err
}
}
return nil
}
// String implements the Stringer interface.
func (p InstantiateContractProposal) String() string {
return fmt.Sprintf(`Instantiate Code Proposal:
Title: %s
Description: %s
Run as: %s
Admin: %s
Code id: %d
Label: %s
InitMsg: %q
InitFunds: %s
`, p.Title, p.Description, p.RunAs, p.Admin, p.CodeID, p.Label, p.InitMsg, p.InitFunds)
}
// MarshalYAML pretty prints the init message
func (p InstantiateContractProposal) MarshalYAML() (interface{}, error) {
return struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
RunAs string `yaml:"run_as"`
Admin string `yaml:"admin"`
CodeID uint64 `yaml:"code_id"`
Label string `yaml:"label"`
InitMsg string `yaml:"init_msg"`
InitFunds sdk.Coins `yaml:"init_funds"`
}{
Title: p.Title,
Description: p.Description,
RunAs: p.RunAs,
Admin: p.Admin,
CodeID: p.CodeID,
Label: p.Label,
InitMsg: string(p.InitMsg),
InitFunds: p.InitFunds,
}, nil
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p MigrateContractProposal) ProposalRoute() string { return RouterKey }
// GetTitle returns the title of the proposal
func (p *MigrateContractProposal) GetTitle() string { return p.Title }
// GetDescription returns the human readable description of the proposal
func (p MigrateContractProposal) GetDescription() string { return p.Description }
// ProposalType returns the type
func (p MigrateContractProposal) ProposalType() string { return string(ProposalTypeMigrateContract) }
// ValidateBasic validates the proposal
func (p MigrateContractProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
if p.CodeID == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "code_id is required")
}
if _, err := sdk.AccAddressFromBech32(p.Contract); err != nil {
return sdkerrors.Wrap(err, "contract")
}
if _, err := sdk.AccAddressFromBech32(p.RunAs); err != nil {
return sdkerrors.Wrap(err, "run as")
}
return nil
}
// String implements the Stringer interface.
func (p MigrateContractProposal) String() string {
return fmt.Sprintf(`Migrate Contract Proposal:
Title: %s
Description: %s
Contract: %s
Code id: %d
Run as: %s
MigrateMsg %q
`, p.Title, p.Description, p.Contract, p.CodeID, p.RunAs, p.MigrateMsg)
}
// MarshalYAML pretty prints the migrate message
func (p MigrateContractProposal) MarshalYAML() (interface{}, error) {
return struct {
Title string `yaml:"title"`
Description string `yaml:"description"`
Contract string `yaml:"contract"`
CodeID uint64 `yaml:"code_id"`
MigrateMsg string `yaml:"msg"`
RunAs string `yaml:"run_as"`
}{
Title: p.Title,
Description: p.Description,
Contract: p.Contract,
CodeID: p.CodeID,
MigrateMsg: string(p.MigrateMsg),
RunAs: p.RunAs,
}, nil
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p UpdateAdminProposal) ProposalRoute() string { return RouterKey }
// GetTitle returns the title of the proposal
func (p *UpdateAdminProposal) GetTitle() string { return p.Title }
// GetDescription returns the human readable description of the proposal
func (p UpdateAdminProposal) GetDescription() string { return p.Description }
// ProposalType returns the type
func (p UpdateAdminProposal) ProposalType() string { return string(ProposalTypeUpdateAdmin) }
// ValidateBasic validates the proposal
func (p UpdateAdminProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(p.Contract); err != nil {
return sdkerrors.Wrap(err, "contract")
}
if _, err := sdk.AccAddressFromBech32(p.NewAdmin); err != nil {
return sdkerrors.Wrap(err, "new admin")
}
return nil
}
// String implements the Stringer interface.
func (p UpdateAdminProposal) String() string {
return fmt.Sprintf(`Update Contract Admin Proposal:
Title: %s
Description: %s
Contract: %s
New Admin: %s
`, p.Title, p.Description, p.Contract, p.NewAdmin)
}
// ProposalRoute returns the routing key of a parameter change proposal.
func (p ClearAdminProposal) ProposalRoute() string { return RouterKey }
// GetTitle returns the title of the proposal
func (p *ClearAdminProposal) GetTitle() string { return p.Title }
// GetDescription returns the human readable description of the proposal
func (p ClearAdminProposal) GetDescription() string { return p.Description }
// ProposalType returns the type
func (p ClearAdminProposal) ProposalType() string { return string(ProposalTypeClearAdmin) }
// ValidateBasic validates the proposal
func (p ClearAdminProposal) ValidateBasic() error {
if err := validateProposalCommons(p.Title, p.Description); err != nil {
return err
}
if _, err := sdk.AccAddressFromBech32(p.Contract); err != nil {
return sdkerrors.Wrap(err, "contract")
}
return nil
}
// String implements the Stringer interface.
func (p ClearAdminProposal) String() string {
return fmt.Sprintf(`Clear Contract Admin Proposal:
Title: %s
Description: %s
Contract: %s
`, p.Title, p.Description, p.Contract)
}
func validateProposalCommons(title, description string) error {
if strings.TrimSpace(title) != title {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal title must not start/end with white spaces")
}
if len(title) == 0 {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal title cannot be blank")
}
if len(title) > govtypes.MaxTitleLength {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalContent, "proposal title is longer than max length of %d", govtypes.MaxTitleLength)
}
if strings.TrimSpace(description) != description {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal description must not start/end with white spaces")
}
if len(description) == 0 {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal description cannot be blank")
}
if len(description) > govtypes.MaxDescriptionLength {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalContent, "proposal description is longer than max length of %d", govtypes.MaxDescriptionLength)
}
return nil
}