-
Notifications
You must be signed in to change notification settings - Fork 42
/
msgs.go
493 lines (447 loc) · 16.5 KB
/
msgs.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package types
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// oracle message types
const (
TypeMsgRequestData = "request"
TypeMsgReportData = "report"
TypeMsgCreateDataSource = "create_data_source"
TypeMsgEditDataSource = "edit_data_source"
TypeMsgCreateOracleScript = "create_oracle_script"
TypeMsgEditOracleScript = "edit_oracle_script"
TypeMsgActivate = "activate"
TypeMsgAddReporter = "add_reporter"
TypeMsgRemoveReporter = "remove_reporter"
)
var (
_ sdk.Msg = &MsgRequestData{}
_ sdk.Msg = &MsgReportData{}
_ sdk.Msg = &MsgCreateDataSource{}
_ sdk.Msg = &MsgEditDataSource{}
_ sdk.Msg = &MsgCreateOracleScript{}
_ sdk.Msg = &MsgEditOracleScript{}
_ sdk.Msg = &MsgActivate{}
)
// NewMsgRequestData creates a new MsgRequestData instance.
func NewMsgRequestData(
oracleScriptID OracleScriptID,
calldata []byte,
askCount, minCount uint64,
clientID string,
feeLimit sdk.Coins,
prepareGas, executeGas uint64,
sender sdk.AccAddress,
) *MsgRequestData {
return &MsgRequestData{
OracleScriptID: oracleScriptID,
Calldata: calldata,
AskCount: askCount,
MinCount: minCount,
ClientID: clientID,
FeeLimit: feeLimit,
Sender: sender.String(),
PrepareGas: prepareGas,
ExecuteGas: executeGas,
}
}
// Route returns the route of MsgRequestData - "oracle" (sdk.Msg interface).
func (msg MsgRequestData) Route() string { return RouterKey }
// Type returns the message type of MsgRequestData (sdk.Msg interface).
func (msg MsgRequestData) Type() string { return TypeMsgRequestData }
// ValidateBasic checks whether the given MsgRequestData instance (sdk.Msg interface).
func (msg MsgRequestData) ValidateBasic() error {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return err
}
if err := sdk.VerifyAddressFormat(sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "sender: %s", msg.Sender)
}
if msg.MinCount <= 0 {
return sdkerrors.Wrapf(ErrInvalidMinCount, "got: %d", msg.MinCount)
}
if msg.AskCount < msg.MinCount {
return sdkerrors.Wrapf(ErrInvalidAskCount, "got: %d, min count: %d", msg.AskCount, msg.MinCount)
}
if len(msg.ClientID) > MaxClientIDLength {
return WrapMaxError(ErrTooLongClientID, len(msg.ClientID), MaxClientIDLength)
}
if msg.PrepareGas <= 0 {
return sdkerrors.Wrapf(ErrInvalidOwasmGas, "invalid prepare gas: %d", msg.PrepareGas)
}
if msg.ExecuteGas <= 0 {
return sdkerrors.Wrapf(ErrInvalidOwasmGas, "invalid execute gas: %d", msg.ExecuteGas)
}
if msg.PrepareGas+msg.ExecuteGas > MaximumOwasmGas {
return sdkerrors.Wrapf(
ErrInvalidOwasmGas,
"sum of prepare gas and execute gas (%d) exceed %d",
msg.PrepareGas+msg.ExecuteGas,
MaximumOwasmGas,
)
}
if !msg.FeeLimit.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.FeeLimit.String())
}
return nil
}
// GetSigners returns the required signers for the given MsgRequestData (sdk.Msg interface).
func (msg MsgRequestData) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Sender)
return []sdk.AccAddress{sender}
}
// GetSignBytes returns raw JSON bytes to be signed by the signers (sdk.Msg interface).
func (msg MsgRequestData) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// NewMsgReportData creates a new MsgReportData instance
func NewMsgReportData(requestID RequestID, rawReports []RawReport, validator sdk.ValAddress) *MsgReportData {
return &MsgReportData{
RequestID: requestID,
RawReports: rawReports,
Validator: validator.String(),
}
}
// Route returns the route of MsgReportData - "oracle" (sdk.Msg interface).
func (msg MsgReportData) Route() string { return RouterKey }
// Type returns the message type of MsgReportData (sdk.Msg interface).
func (msg MsgReportData) Type() string { return TypeMsgReportData }
// ValidateBasic checks whether the given MsgReportData instance (sdk.Msg interface).
func (msg MsgReportData) ValidateBasic() error {
valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return err
}
if err := sdk.VerifyAddressFormat(valAddr); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "validator: %s", msg.Validator)
}
if len(msg.RawReports) == 0 {
return ErrEmptyReport
}
uniqueMap := make(map[ExternalID]bool)
for _, r := range msg.RawReports {
if _, found := uniqueMap[r.ExternalID]; found {
return sdkerrors.Wrapf(ErrDuplicateExternalID, "external id: %d", r.ExternalID)
}
uniqueMap[r.ExternalID] = true
}
return nil
}
// GetSigners returns the required signers for the given MsgReportData (sdk.Msg interface).
func (msg MsgReportData) GetSigners() []sdk.AccAddress {
validator, _ := sdk.ValAddressFromBech32(msg.Validator)
return []sdk.AccAddress{sdk.AccAddress(validator)}
}
// GetSignBytes returns raw JSON bytes to be signed by the signers (sdk.Msg interface).
func (msg MsgReportData) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// NewMsgCreateDataSource creates a new MsgCreateDataSource instance
func NewMsgCreateDataSource(
name, description string, executable []byte, fee sdk.Coins, treasury, owner, sender sdk.AccAddress,
) *MsgCreateDataSource {
return &MsgCreateDataSource{
Name: name,
Description: description,
Executable: executable,
Fee: fee,
Treasury: treasury.String(),
Owner: owner.String(),
Sender: sender.String(),
}
}
// Route returns the route of MsgCreateDataSource - "oracle" (sdk.Msg interface).
func (msg MsgCreateDataSource) Route() string { return RouterKey }
// Type returns the message type of MsgCreateDataSource (sdk.Msg interface).
func (msg MsgCreateDataSource) Type() string { return TypeMsgCreateDataSource }
// ValidateBasic checks whether the given MsgCreateDataSource instance (sdk.Msg interface).
func (msg MsgCreateDataSource) ValidateBasic() error {
treasury, err := sdk.AccAddressFromBech32(msg.Treasury)
if err != nil {
return err
}
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return err
}
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return err
}
if err := sdk.VerifyAddressFormat(treasury); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "treasury: %s", msg.Treasury)
}
if err := sdk.VerifyAddressFormat(owner); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "owner: %s", msg.Owner)
}
if err := sdk.VerifyAddressFormat(sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "sender: %s", msg.Sender)
}
if len(msg.Name) > MaxNameLength {
return WrapMaxError(ErrTooLongName, len(msg.Name), MaxNameLength)
}
if len(msg.Description) > MaxDescriptionLength {
return WrapMaxError(ErrTooLongDescription, len(msg.Description), MaxDescriptionLength)
}
if !msg.Fee.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Fee.String())
}
if len(msg.Executable) == 0 {
return ErrEmptyExecutable
}
if len(msg.Executable) > MaxExecutableSize {
return WrapMaxError(ErrTooLargeExecutable, len(msg.Executable), MaxExecutableSize)
}
if bytes.Equal(msg.Executable, DoNotModifyBytes) {
return ErrCreateWithDoNotModify
}
return nil
}
// GetSigners returns the required signers for the given MsgCreateDataSource (sdk.Msg interface).
func (msg MsgCreateDataSource) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Sender)
return []sdk.AccAddress{sender}
}
// GetSignBytes returns raw JSON bytes to be signed by the signers (sdk.Msg interface).
func (msg MsgCreateDataSource) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// NewMsgEditDataSource creates a new MsgEditDataSource instance
func NewMsgEditDataSource(
dataSourceID DataSourceID,
name string,
description string,
executable []byte,
fee sdk.Coins,
treasury, owner, sender sdk.AccAddress,
) *MsgEditDataSource {
return &MsgEditDataSource{
DataSourceID: dataSourceID,
Name: name,
Description: description,
Executable: executable,
Fee: fee,
Treasury: treasury.String(),
Owner: owner.String(),
Sender: sender.String(),
}
}
// Route returns the route of MsgEditDataSource - "oracle" (sdk.Msg interface).
func (msg MsgEditDataSource) Route() string { return RouterKey }
// Type returns the message type of MsgEditDataSource (sdk.Msg interface).
func (msg MsgEditDataSource) Type() string { return TypeMsgEditDataSource }
// ValidateBasic checks whether the given MsgEditDataSource instance (sdk.Msg interface).
func (msg MsgEditDataSource) ValidateBasic() error {
treasury, err := sdk.AccAddressFromBech32(msg.Treasury)
if err != nil {
return err
}
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return err
}
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return err
}
if err := sdk.VerifyAddressFormat(treasury); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "treasury: %s", msg.Treasury)
}
if err := sdk.VerifyAddressFormat(owner); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "owner: %s", msg.Owner)
}
if err := sdk.VerifyAddressFormat(sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "sender: %s", msg.Sender)
}
if len(msg.Name) > MaxNameLength {
return WrapMaxError(ErrTooLongName, len(msg.Name), MaxNameLength)
}
if len(msg.Description) > MaxDescriptionLength {
return WrapMaxError(ErrTooLongDescription, len(msg.Description), MaxDescriptionLength)
}
if !msg.Fee.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Fee.String())
}
if len(msg.Executable) == 0 {
return ErrEmptyExecutable
}
if len(msg.Executable) > MaxExecutableSize {
return WrapMaxError(ErrTooLargeExecutable, len(msg.Executable), MaxExecutableSize)
}
return nil
}
// GetSigners returns the required signers for the given MsgEditDataSource (sdk.Msg interface).
func (msg MsgEditDataSource) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Sender)
return []sdk.AccAddress{sender}
}
// GetSignBytes returns raw JSON bytes to be signed by the signers (sdk.Msg interface).
func (msg MsgEditDataSource) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// NewMsgCreateOracleScript creates a new MsgCreateOracleScript instance
func NewMsgCreateOracleScript(
name, description, schema, sourceCodeURL string, code []byte, owner, sender sdk.AccAddress,
) *MsgCreateOracleScript {
return &MsgCreateOracleScript{
Name: name,
Description: description,
Schema: schema,
SourceCodeURL: sourceCodeURL,
Code: code,
Owner: owner.String(),
Sender: sender.String(),
}
}
// Route returns the route of MsgCreateOracleScript - "oracle" (sdk.Msg interface).
func (msg MsgCreateOracleScript) Route() string { return RouterKey }
// Type returns the message type of MsgCreateOracleScript (sdk.Msg interface).
func (msg MsgCreateOracleScript) Type() string { return TypeMsgCreateOracleScript }
// ValidateBasic checks whether the given MsgCreateOracleScript instance (sdk.Msg interface).
func (msg MsgCreateOracleScript) ValidateBasic() error {
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return err
}
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return err
}
if err := sdk.VerifyAddressFormat(owner); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "owner: %s", msg.Owner)
}
if err := sdk.VerifyAddressFormat(sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "sender: %s", msg.Sender)
}
if len(msg.Name) > MaxNameLength {
return WrapMaxError(ErrTooLongName, len(msg.Name), MaxNameLength)
}
if len(msg.Description) > MaxDescriptionLength {
return WrapMaxError(ErrTooLongDescription, len(msg.Description), MaxDescriptionLength)
}
if len(msg.Schema) > MaxSchemaLength {
return WrapMaxError(ErrTooLongSchema, len(msg.Schema), MaxSchemaLength)
}
if len(msg.SourceCodeURL) > MaxURLLength {
return WrapMaxError(ErrTooLongURL, len(msg.SourceCodeURL), MaxURLLength)
}
if len(msg.Code) == 0 {
return ErrEmptyWasmCode
}
if len(msg.Code) > MaxWasmCodeSize {
return WrapMaxError(ErrTooLargeWasmCode, len(msg.Code), MaxWasmCodeSize)
}
if bytes.Equal(msg.Code, DoNotModifyBytes) {
return ErrCreateWithDoNotModify
}
return nil
}
// GetSigners returns the required signers for the given MsgCreateOracleScript (sdk.Msg interface).
func (msg MsgCreateOracleScript) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Sender)
return []sdk.AccAddress{sender}
}
// GetSignBytes returns raw JSON bytes to be signed by the signers (sdk.Msg interface).
func (msg MsgCreateOracleScript) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// NewMsgEditOracleScript creates a new MsgEditOracleScript instance
func NewMsgEditOracleScript(
oracleScriptID OracleScriptID,
name, description, schema, sourceCodeURL string,
code []byte,
owner, sender sdk.AccAddress,
) *MsgEditOracleScript {
return &MsgEditOracleScript{
OracleScriptID: oracleScriptID,
Name: name,
Description: description,
Schema: schema,
SourceCodeURL: sourceCodeURL,
Code: code,
Owner: owner.String(),
Sender: sender.String(),
}
}
// Route returns the route of MsgEditOracleScript - "oracle" (sdk.Msg interface).
func (msg MsgEditOracleScript) Route() string { return RouterKey }
// Type returns the message type of MsgEditOracleScript (sdk.Msg interface).
func (msg MsgEditOracleScript) Type() string { return TypeMsgEditOracleScript }
// ValidateBasic checks whether the given MsgEditOracleScript instance (sdk.Msg interface).
func (msg MsgEditOracleScript) ValidateBasic() error {
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return err
}
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return err
}
if err := sdk.VerifyAddressFormat(owner); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "owner: %s", msg.Owner)
}
if err := sdk.VerifyAddressFormat(sender); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "sender: %s", msg.Sender)
}
if len(msg.Name) > MaxNameLength {
return WrapMaxError(ErrTooLongName, len(msg.Name), MaxNameLength)
}
if len(msg.Description) > MaxDescriptionLength {
return WrapMaxError(ErrTooLongDescription, len(msg.Description), MaxDescriptionLength)
}
if len(msg.Schema) > MaxSchemaLength {
return WrapMaxError(ErrTooLongSchema, len(msg.Schema), MaxSchemaLength)
}
if len(msg.SourceCodeURL) > MaxURLLength {
return WrapMaxError(ErrTooLongURL, len(msg.SourceCodeURL), MaxURLLength)
}
if len(msg.Code) == 0 {
return ErrEmptyWasmCode
}
if len(msg.Code) > MaxWasmCodeSize {
return WrapMaxError(ErrTooLargeWasmCode, len(msg.Code), MaxWasmCodeSize)
}
return nil
}
// GetSigners returns the required signers for the given MsgEditOracleScript (sdk.Msg interface).
func (msg MsgEditOracleScript) GetSigners() []sdk.AccAddress {
sender, _ := sdk.AccAddressFromBech32(msg.Sender)
return []sdk.AccAddress{sender}
}
// GetSignBytes returns raw JSON bytes to be signed by the signers (sdk.Msg interface).
func (msg MsgEditOracleScript) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// NewMsgActivate creates a new MsgActivate instance
func NewMsgActivate(validator sdk.ValAddress) *MsgActivate {
return &MsgActivate{
Validator: validator.String(),
}
}
// Route returns the route of MsgActivate - "oracle" (sdk.Msg interface).
func (msg MsgActivate) Route() string { return RouterKey }
// Type returns the message type of MsgActivate (sdk.Msg interface).
func (msg MsgActivate) Type() string { return TypeMsgActivate }
// ValidateBasic checks whether the given MsgActivate instance (sdk.Msg interface).
func (msg MsgActivate) ValidateBasic() error {
val, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return err
}
if err := sdk.VerifyAddressFormat(val); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "validator: %s", msg.Validator)
}
return nil
}
// GetSigners returns the required signers for the given MsgActivate (sdk.Msg interface).
func (msg MsgActivate) GetSigners() []sdk.AccAddress {
val, _ := sdk.ValAddressFromBech32(msg.Validator)
return []sdk.AccAddress{sdk.AccAddress(val)}
}
// GetSignBytes returns raw JSON bytes to be signed by the signers (sdk.Msg interface).
func (msg MsgActivate) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}