-
Notifications
You must be signed in to change notification settings - Fork 19
/
definition.go
461 lines (402 loc) · 12.5 KB
/
definition.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//lint:file-ignore ST1003 Ignore underscores in names, this is straigh copied from the proto package to ease introducing the domain types
package definition
import (
"errors"
"fmt"
"time"
"code.vegaprotocol.io/vega/core/datasource/common"
dserrors "code.vegaprotocol.io/vega/core/datasource/errors"
ethcallcommon "code.vegaprotocol.io/vega/core/datasource/external/ethcall/common"
"code.vegaprotocol.io/vega/core/datasource/external/signedoracle"
"code.vegaprotocol.io/vega/core/datasource/internal/timetrigger"
"code.vegaprotocol.io/vega/core/datasource/internal/vegatime"
vegapb "code.vegaprotocol.io/vega/protos/vega"
datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
)
type ContentType int32
const (
ContentTypeInvalid ContentType = iota
ContentTypeOracle
ContentTypeEthOracle
ContentTypeInternalTimeTermination
ContentTypeInternalTimeTriggerTermination
)
type Definition struct {
common.DataSourceType
}
func NewWith(dst common.DataSourceType) *Definition {
if dst == nil {
return &Definition{}
}
return &Definition{
DataSourceType: dst.DeepClone(),
}
}
// New creates a new EMPTY Definition object.
// TODO: eth oracle type too.
func New(tp ContentType) *Definition {
ds := &Definition{}
switch tp {
case ContentTypeOracle:
return NewWith(
signedoracle.SpecConfiguration{
Signers: []*common.Signer{},
Filters: []*common.SpecFilter{},
})
case ContentTypeEthOracle:
return NewWith(
ethcallcommon.Spec{
AbiJson: []byte{},
ArgsJson: []string{},
Trigger: ðcallcommon.TimeTrigger{},
Normalisers: map[string]string{},
Filters: common.SpecFilters{},
})
case ContentTypeInternalTimeTermination:
return NewWith(
vegatime.SpecConfiguration{
Conditions: []*common.SpecCondition{},
})
case ContentTypeInternalTimeTriggerTermination:
return NewWith(
timetrigger.SpecConfiguration{
Triggers: common.InternalTimeTriggers{},
Conditions: []*common.SpecCondition{},
})
}
return ds
}
// IntoProto returns the proto object from Definition
// that is - vegapb.DataSourceDefinition that may have external or internal SourceType.
// Returns the whole proto object.
func (s *Definition) IntoProto() *vegapb.DataSourceDefinition {
if s.DataSourceType == nil {
return &vegapb.DataSourceDefinition{}
}
proto, err := s.ToDefinitionProto()
if err != nil {
// TODO: bubble error
return &vegapb.DataSourceDefinition{}
}
return proto
}
// DeepClone returns a clone of the Definition object.
func (s Definition) DeepClone() common.DataSourceType {
if s.DataSourceType != nil {
return &Definition{
DataSourceType: s.DataSourceType.DeepClone(),
}
}
return nil
}
func (s Definition) String() string {
if s.DataSourceType != nil {
return s.DataSourceType.String()
}
return ""
}
func (s *Definition) Content() interface{} {
return s.DataSourceType
}
// FromProto tries to build the Definiition object
// from the given proto object.
func FromProto(protoConfig *vegapb.DataSourceDefinition, tm *time.Time) (common.DataSourceType, error) {
if protoConfig != nil {
data := protoConfig.Content()
switch dtp := data.(type) {
case *vegapb.DataSourceSpecConfiguration:
return signedoracle.SpecConfigurationFromProto(dtp), nil
case *vegapb.EthCallSpec:
return ethcallcommon.SpecFromProto(dtp)
case *vegapb.DataSourceSpecConfigurationTime:
return vegatime.SpecConfigurationFromProto(dtp), nil
case *vegapb.DataSourceSpecConfigurationTimeTrigger:
return timetrigger.SpecConfigurationFromProto(dtp, tm)
}
}
return &Definition{}, nil
}
// GetSigners tries to get the signers from the Definition if they exist.
func (s *Definition) GetSigners() []*common.Signer {
signers := []*common.Signer{}
data := s.Content()
if data != nil {
switch tp := data.(type) {
case signedoracle.SpecConfiguration:
signers = tp.Signers
}
}
return signers
}
// GetFilters tries to get the filters from the Definition if they exist.
func (s *Definition) GetFilters() []*common.SpecFilter {
filters := []*common.SpecFilter{}
data := s.Content()
if data != nil {
switch tp := data.(type) {
case signedoracle.SpecConfiguration:
filters = tp.Filters
case ethcallcommon.Spec:
filters = tp.Filters
case vegatime.SpecConfiguration:
// TODO: Fix this to use the same method as in the vegatime package (example: as below)
// For the case the internal data source is time based
// (as of OT https://github.com/vegaprotocol/specs/blob/master/protocol/0048-DSRI-data_source_internal.md#13-vega-time-changed)
// We add the filter key values manually to match a time based data source
// Ensure only a single filter has been created, that holds the first condition
if len(tp.Conditions) > 0 {
filters = append(
filters,
&common.SpecFilter{
Key: &common.SpecPropertyKey{
Name: vegatime.VegaTimeKey,
Type: datapb.PropertyKey_TYPE_TIMESTAMP,
},
Conditions: []*common.SpecCondition{
tp.Conditions[0],
},
},
)
}
case timetrigger.SpecConfiguration:
sc := s.GetInternalTimeTriggerSpecConfiguration()
filters = sc.GetFilters()
}
}
return filters
}
// GetSignedOracleSpecConfiguration returns the base object - vega oracle SpecConfiguration
// from the Definition.
func (s *Definition) GetSignedOracleSpecConfiguration() signedoracle.SpecConfiguration {
data := s.Content()
if data != nil {
switch tp := data.(type) {
case signedoracle.SpecConfiguration:
return tp
}
}
return signedoracle.SpecConfiguration{}
}
// GetEthCallSpec returns the base object - EthCallSpec
// from the Definition.
func (s *Definition) GetEthCallSpec() ethcallcommon.Spec {
data := s.Content()
if data != nil {
switch tp := data.(type) {
case ethcallcommon.Spec:
return tp
}
}
return ethcallcommon.Spec{}
}
func (s *Definition) IsEthCallSpec() bool {
data := s.Content()
if data != nil {
switch data.(type) {
case ethcallcommon.Spec:
return true
}
}
return false
}
// Definition is also a `Timer`.
func (s *Definition) GetTimeTriggers() common.InternalTimeTriggers {
data := s.Content()
if data != nil {
switch tp := data.(type) {
case timetrigger.SpecConfiguration:
return tp.GetTimeTriggers()
}
}
return common.InternalTimeTriggers{}
}
func (s *Definition) IsTriggered(tm time.Time) bool {
data := s.Content()
if data != nil {
switch tp := data.(type) {
case timetrigger.SpecConfiguration:
return tp.IsTriggered(tm)
}
}
return false
}
// UpdateFilters updates the Definition Filters.
func (s *Definition) UpdateFilters(filters []*common.SpecFilter) error {
fTypeCheck := map[*common.SpecFilter]struct{}{}
fNameCheck := map[string]struct{}{}
for _, f := range filters {
if _, ok := fTypeCheck[f]; ok {
return dserrors.ErrDataSourceSpecHasMultipleSameKeyNamesInFilterList
}
if f.Key != nil {
if _, ok := fNameCheck[f.Key.Name]; ok {
return dserrors.ErrDataSourceSpecHasMultipleSameKeyNamesInFilterList
}
fNameCheck[f.Key.Name] = struct{}{}
}
fTypeCheck[f] = struct{}{}
}
// maybe todo - enforce that it's never nil
if s.DataSourceType == nil {
return nil
}
switch content := s.DataSourceType.DeepClone().(type) {
case signedoracle.SpecConfiguration:
content.Filters = filters
s.DataSourceType = content
case ethcallcommon.Spec:
content.Filters = filters
s.DataSourceType = content
case vegatime.SpecConfiguration:
// The data source definition is an internal time based source
// For this case we take only the first item from the list of filters
// https://github.com/vegaprotocol/specs/blob/master/protocol/0048-DSRI-data_source_internal.md#13-vega-time-changed
c := []*common.SpecCondition{}
if len(filters) > 0 {
if len(filters[0].Conditions) > 0 {
c = append(c, filters[0].Conditions[0])
}
}
content.Conditions = c
s.DataSourceType = content
case timetrigger.SpecConfiguration:
c := []*common.SpecCondition{}
if len(filters) > 0 {
for _, f := range filters {
if len(f.Conditions) > 0 {
c = append(c, f.Conditions...)
}
}
}
content.Conditions = c
s.DataSourceType = content
default:
return fmt.Errorf("unable to set filters on data source type: %T", content)
}
return nil
}
func (s *Definition) SetFilterDecimals(d uint64) *Definition {
switch content := s.DataSourceType.DeepClone().(type) {
case signedoracle.SpecConfiguration:
for i := range content.Filters {
content.Filters[i].Key.NumberDecimalPlaces = &d
}
s.DataSourceType = content
case ethcallcommon.Spec:
for i := range content.Filters {
content.Filters[i].Key.NumberDecimalPlaces = &d
}
s.DataSourceType = content
default:
// we should really be returning an error here but this method is only used in the integration tests
panic(fmt.Sprintf("unable to set filter decimals on data source type: %T", content))
}
return s
}
// SetOracleConfig sets a given oracle config in the receiver.
// If the receiver is not external oracle type data source - it is not changed.
// This method does not care about object previous contents.
func (s *Definition) SetOracleConfig(oc common.DataSourceType) *Definition {
if _, ok := s.DataSourceType.(signedoracle.SpecConfiguration); ok {
s.DataSourceType = oc.DeepClone()
}
if _, ok := s.DataSourceType.(ethcallcommon.Spec); ok {
s.DataSourceType = oc.DeepClone()
}
return s
}
// SetTimeTriggerConditionConfig sets a given conditions config in the receiver.
// If the receiver is not a time triggered data source - it does not set anything to it.
// This method does not care about object previous contents.
func (s *Definition) SetTimeTriggerConditionConfig(c []*common.SpecCondition) *Definition {
if _, ok := s.DataSourceType.(vegatime.SpecConfiguration); ok {
s.DataSourceType = vegatime.SpecConfiguration{
Conditions: c,
}
}
if sc, ok := s.DataSourceType.(timetrigger.SpecConfiguration); ok {
s.DataSourceType = timetrigger.SpecConfiguration{
Triggers: sc.Triggers,
Conditions: c,
}
}
return s
}
func (s *Definition) SetTimeTriggerTriggersConfig(tr common.InternalTimeTriggers) *Definition {
if sc, ok := s.DataSourceType.(timetrigger.SpecConfiguration); ok {
s.DataSourceType = timetrigger.SpecConfiguration{
Triggers: tr,
Conditions: sc.Conditions,
}
}
return s
}
func (s *Definition) GetVegaTimeSpecConfiguration() vegatime.SpecConfiguration {
data := s.Content()
switch tp := data.(type) {
case vegatime.SpecConfiguration:
return tp
}
return vegatime.SpecConfiguration{}
}
func (s *Definition) GetInternalTimeTriggerSpecConfiguration() timetrigger.SpecConfiguration {
data := s.Content()
switch tp := data.(type) {
case timetrigger.SpecConfiguration:
return tp
}
return timetrigger.SpecConfiguration{}
}
func (s *Definition) IsExternal() (bool, error) {
switch s.DataSourceType.(type) {
case signedoracle.SpecConfiguration:
return true, nil
case ethcallcommon.Spec:
return true, nil
case vegatime.SpecConfiguration:
return false, nil
case timetrigger.SpecConfiguration:
return false, nil
}
return false, errors.New("unknown type of data source provided")
}
func (s *Definition) Type() (ContentType, bool) {
switch s.DataSourceType.(type) {
case signedoracle.SpecConfiguration:
return ContentTypeOracle, true
case ethcallcommon.Spec:
return ContentTypeEthOracle, true
case vegatime.SpecConfiguration:
return ContentTypeInternalTimeTermination, false
case timetrigger.SpecConfiguration:
return ContentTypeInternalTimeTriggerTermination, false
}
return ContentTypeInvalid, false
}
func (s *Definition) GetSpecConfiguration() common.DataSourceType {
switch s.DataSourceType.(type) {
case signedoracle.SpecConfiguration:
return s.GetSignedOracleSpecConfiguration()
case ethcallcommon.Spec:
return s.GetEthCallSpec()
case vegatime.SpecConfiguration:
return s.GetVegaTimeSpecConfiguration()
case timetrigger.SpecConfiguration:
return s.GetInternalTimeTriggerSpecConfiguration()
}
return nil
}