forked from CosmWasm/wasmd
/
proposal.go
365 lines (321 loc) · 11.9 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
package types
import (
"encoding/base64"
"encoding/json"
"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"
ProposalTypeStoreInstantiateContract 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,
ProposalTypeStoreInstantiateContract,
ProposalTypeMigrateContract,
ProposalTypeUpdateAdmin,
ProposalTypeClearAdmin,
}
func init() { // register new content types with the sdk
govtypes.RegisterProposalType(string(ProposalTypeStoreCode))
govtypes.RegisterProposalType(string(ProposalTypeStoreInstantiateContract))
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")
}
// WasmProposal contains common proposal data.
type WasmProposal struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
}
// GetTitle returns the title of a parameter change proposal.
func (p WasmProposal) GetTitle() string { return p.Title }
// GetDescription returns the description of a parameter change proposal.
func (p WasmProposal) GetDescription() string { return p.Description }
// ProposalRoute returns the routing key of a parameter change proposal.
func (p WasmProposal) ProposalRoute() string { return RouterKey }
// ValidateBasic validates the proposal
func (p WasmProposal) ValidateBasic() error {
if strings.TrimSpace(p.Title) != p.Title {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal title must not start/end with white spaces")
}
if len(p.Title) == 0 {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal title cannot be blank")
}
if len(p.Title) > govtypes.MaxTitleLength {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalContent, "proposal title is longer than max length of %d", govtypes.MaxTitleLength)
}
if strings.TrimSpace(p.Description) != p.Description {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal description must not start/end with white spaces")
}
if len(p.Description) == 0 {
return sdkerrors.Wrap(govtypes.ErrInvalidProposalContent, "proposal description cannot be blank")
}
if len(p.Description) > govtypes.MaxDescriptionLength {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalContent, "proposal description is longer than max length of %d", govtypes.MaxDescriptionLength)
}
return nil
}
// StoreCodeProposal gov proposal content type to store wasm code.
type StoreCodeProposal struct {
WasmProposal
// RunAs is the address that "owns" the code object
RunAs sdk.AccAddress `json:"run_as"`
// WASMByteCode can be raw or gzip compressed
WASMByteCode []byte `json:"wasm_byte_code"`
// Source is a valid absolute HTTPS URI to the contract's source code, optional
Source string `json:"source"`
// Builder is a valid docker image name with tag, optional
Builder string `json:"builder"`
// InstantiatePermission to apply on contract creation, optional
InstantiatePermission *AccessConfig `json:"instantiate_permission"`
}
// ProposalType returns the type
func (p StoreCodeProposal) ProposalType() string { return string(ProposalTypeStoreCode) }
// ValidateBasic validates the proposal
func (p StoreCodeProposal) ValidateBasic() error {
if err := p.WasmProposal.ValidateBasic(); err != nil {
return err
}
if err := sdk.VerifyAddressFormat(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)
}
func (p StoreCodeProposal) MarshalYAML() (interface{}, error) {
return struct {
WasmProposal `yaml:",inline"`
RunAs sdk.AccAddress `yaml:"run_as"`
WASMByteCode string `yaml:"wasm_byte_code"`
Source string `yaml:"source"`
Builder string `yaml:"builder"`
InstantiatePermission *AccessConfig `yaml:"instantiate_permission"`
}{
WasmProposal: p.WasmProposal,
RunAs: p.RunAs,
WASMByteCode: base64.StdEncoding.EncodeToString(p.WASMByteCode),
Source: p.Source,
Builder: p.Builder,
InstantiatePermission: p.InstantiatePermission,
}, nil
}
// InstantiateContractProposal gov proposal content type to instantiate a contract.
type InstantiateContractProposal struct {
WasmProposal
// RunAs is the address that pays the init funds
RunAs sdk.AccAddress `json:"run_as"`
// Admin is an optional address that can execute migrations
Admin sdk.AccAddress `json:"admin,omitempty"`
CodeID uint64 `json:"code_id"`
Label string `json:"label"`
InitMsg json.RawMessage `json:"init_msg"`
InitFunds sdk.Coins `json:"init_funds"`
}
// ProposalType returns the type
func (p InstantiateContractProposal) ProposalType() string {
return string(ProposalTypeStoreInstantiateContract)
}
// ValidateBasic validates the proposal
func (p InstantiateContractProposal) ValidateBasic() error {
if err := p.WasmProposal.ValidateBasic(); err != nil {
return err
}
if err := sdk.VerifyAddressFormat(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.VerifyAddressFormat(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)
}
func (p InstantiateContractProposal) MarshalYAML() (interface{}, error) {
return struct {
WasmProposal `yaml:",inline"`
RunAs sdk.AccAddress `yaml:"run_as"`
Admin sdk.AccAddress `yaml:"admin"`
CodeID uint64 `yaml:"code_id"`
Label string `yaml:"label"`
InitMsg string `yaml:"init_msg"`
InitFunds sdk.Coins `yaml:"init_funds"`
}{
WasmProposal: p.WasmProposal,
RunAs: p.RunAs,
Admin: p.Admin,
CodeID: p.CodeID,
Label: p.Label,
InitMsg: string(p.InitMsg),
InitFunds: p.InitFunds,
}, nil
}
// MigrateContractProposal gov proposal content type to migrate a contract.
type MigrateContractProposal struct {
WasmProposal `yaml:",inline"`
Contract sdk.AccAddress `json:"contract"`
CodeID uint64 `json:"code_id"`
MigrateMsg json.RawMessage `json:"msg"`
// RunAs is the address that is passed to the contract's environment as sender
RunAs sdk.AccAddress `json:"run_as"`
}
// ProposalType returns the type
func (p MigrateContractProposal) ProposalType() string { return string(ProposalTypeMigrateContract) }
// ValidateBasic validates the proposal
func (p MigrateContractProposal) ValidateBasic() error {
if err := p.WasmProposal.ValidateBasic(); err != nil {
return err
}
if p.CodeID == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "code_id is required")
}
if err := sdk.VerifyAddressFormat(p.Contract); err != nil {
return sdkerrors.Wrap(err, "contract")
}
if err := sdk.VerifyAddressFormat(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)
}
func (p MigrateContractProposal) MarshalYAML() (interface{}, error) {
return struct {
WasmProposal `yaml:",inline"`
Contract sdk.AccAddress `yaml:"contract"`
CodeID uint64 `yaml:"code_id"`
MigrateMsg string `yaml:"msg"`
RunAs sdk.AccAddress `yaml:"run_as"`
}{
WasmProposal: p.WasmProposal,
Contract: p.Contract,
CodeID: p.CodeID,
MigrateMsg: string(p.MigrateMsg),
RunAs: p.RunAs,
}, nil
}
// UpdateAdminProposal gov proposal content type to set an admin for a contract.
type UpdateAdminProposal struct {
WasmProposal `yaml:",inline"`
NewAdmin sdk.AccAddress `json:"new_admin" yaml:"new_admin"`
Contract sdk.AccAddress `json:"contract" yaml:"contract"`
}
// ProposalType returns the type
func (p UpdateAdminProposal) ProposalType() string { return string(ProposalTypeUpdateAdmin) }
// ValidateBasic validates the proposal
func (p UpdateAdminProposal) ValidateBasic() error {
if err := p.WasmProposal.ValidateBasic(); err != nil {
return err
}
if err := sdk.VerifyAddressFormat(p.Contract); err != nil {
return sdkerrors.Wrap(err, "contract")
}
if err := sdk.VerifyAddressFormat(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)
}
// ClearAdminProposal gov proposal content type to clear the admin of a contract.
type ClearAdminProposal struct {
WasmProposal `yaml:",inline"`
Contract sdk.AccAddress `json:"contract" yaml:"contract"`
}
// ProposalType returns the type
func (p ClearAdminProposal) ProposalType() string { return string(ProposalTypeClearAdmin) }
// ValidateBasic validates the proposal
func (p ClearAdminProposal) ValidateBasic() error {
if err := p.WasmProposal.ValidateBasic(); err != nil {
return err
}
if err := sdk.VerifyAddressFormat(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)
}