-
Notifications
You must be signed in to change notification settings - Fork 19
/
spec_definition.go
294 lines (268 loc) · 7.66 KB
/
spec_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
package vega
import (
datapb "code.vegaprotocol.io/vega/protos/vega/data/v1"
"google.golang.org/protobuf/types/known/structpb"
)
type DataSourceContentType int32
const (
DataSurceContentTypeInvalid DataSourceContentType = iota
DataSourceContentTypeOracle
DataSourceContentTypeEthOracle
DataSourceContentTypeInternalTimeTermination
DataSourceContentTypeInternalTimeTriggerTermination
)
// GetSigners tries to get the Signers from the DataSourceDefinition object.
func (d DataSourceDefinition) GetSigners() []*datapb.Signer {
signers := []*datapb.Signer{}
cnt := d.Content()
if cnt != nil {
switch tp := cnt.(type) {
case *DataSourceSpecConfiguration:
signers = tp.Signers
}
}
return signers
}
func (d DataSourceDefinition) GetFilters() []*datapb.Filter {
filters := []*datapb.Filter{}
cnt := d.Content()
if cnt != nil {
switch tp := cnt.(type) {
case *DataSourceSpecConfiguration:
if tp != nil {
return tp.Filters
}
case *EthCallSpec:
if tp != nil {
if tp.Filters != nil {
return tp.Filters
}
}
case *DataSourceSpecConfigurationTime:
if tp != nil { // wtf?
if len(tp.Conditions) > 0 {
filters = append(filters,
&datapb.Filter{
Key: &datapb.PropertyKey{
Name: "vegaprotocol.builtin.timestamp",
Type: datapb.PropertyKey_TYPE_TIMESTAMP,
},
Conditions: []*datapb.Condition{
tp.Conditions[0],
},
},
)
}
}
case *DataSourceSpecConfigurationTimeTrigger:
if tp != nil {
if len(tp.Conditions) > 0 {
filters = append(filters,
&datapb.Filter{
Key: &datapb.PropertyKey{
Name: "vegaprotocol.builtin.timetrigger",
Type: datapb.PropertyKey_TYPE_TIMESTAMP,
},
Conditions: tp.Conditions,
},
)
}
}
}
}
return filters
}
func NewDataSourceDefinitionWith(dst isDataSourceDefinition_SourceType) *DataSourceDefinition {
return &DataSourceDefinition{SourceType: dst}
}
func NewDataSourceDefinition(tp DataSourceContentType) *DataSourceDefinition {
ds := &DataSourceDefinition{}
switch tp {
case DataSourceContentTypeOracle:
ds.SourceType = &DataSourceDefinition_External{
External: &DataSourceDefinitionExternal{
SourceType: &DataSourceDefinitionExternal_Oracle{
Oracle: &DataSourceSpecConfiguration{
Signers: []*datapb.Signer{},
Filters: []*datapb.Filter{},
},
},
},
}
case DataSourceContentTypeEthOracle:
ds.SourceType = &DataSourceDefinition_External{
External: &DataSourceDefinitionExternal{
SourceType: &DataSourceDefinitionExternal_EthOracle{
EthOracle: &EthCallSpec{
Abi: "",
Args: []*structpb.Value{},
Trigger: &EthCallTrigger{},
Normalisers: []*Normaliser{},
Filters: []*datapb.Filter{},
},
},
},
}
case DataSourceContentTypeInternalTimeTermination:
ds.SourceType = &DataSourceDefinition_Internal{
Internal: &DataSourceDefinitionInternal{
SourceType: &DataSourceDefinitionInternal_Time{
Time: &DataSourceSpecConfigurationTime{
Conditions: []*datapb.Condition{},
},
},
},
}
case DataSourceContentTypeInternalTimeTriggerTermination:
ds.SourceType = &DataSourceDefinition_Internal{
Internal: &DataSourceDefinitionInternal{
SourceType: &DataSourceDefinitionInternal_TimeTrigger{
TimeTrigger: &DataSourceSpecConfigurationTimeTrigger{
Conditions: []*datapb.Condition{},
Triggers: []*datapb.InternalTimeTrigger{},
},
},
},
}
}
return ds
}
// SetOracleConfig sets a given oracle config in the receiver.
// If the receiver is not external oracle type of data source - it is not changed.
// This method does not care about object previous contents - use with caution (currenty needed only for testing purposes).
func (s *DataSourceDefinition) SetOracleConfig(oc isDataSourceDefinitionExternal_SourceType) *DataSourceDefinition {
if oc != nil {
cnt := NewDataSourceDefinitionWith(
&DataSourceDefinition_External{
External: &DataSourceDefinitionExternal{
SourceType: oc,
},
}).Content()
if s.SourceType != nil {
switch te := s.SourceType.(type) {
case *DataSourceDefinition_External:
if te.External != nil {
if te.External.SourceType != nil {
switch te.External.SourceType.(type) {
case *DataSourceDefinitionExternal_Oracle:
switch tp := cnt.(type) {
case *DataSourceSpecConfiguration:
ds := &DataSourceDefinition{
SourceType: &DataSourceDefinition_External{
External: &DataSourceDefinitionExternal{
SourceType: &DataSourceDefinitionExternal_Oracle{
Oracle: tp,
},
},
},
}
*s = *ds
}
case *DataSourceDefinitionExternal_EthOracle:
switch tp := cnt.(type) {
case *EthCallSpec:
ds := &DataSourceDefinition{
SourceType: &DataSourceDefinition_External{
External: &DataSourceDefinitionExternal{
SourceType: &DataSourceDefinitionExternal_EthOracle{
EthOracle: tp,
},
},
},
}
*s = *ds
}
}
}
}
}
}
}
return s
}
// SetTimeTriggerConditionConfig sets a condition to the time triggered 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 - use with caution (currenty needed only for testing purposes).
func (s *DataSourceDefinition) SetTimeTriggerConditionConfig(c []*datapb.Condition) *DataSourceDefinition {
if c != nil {
cnt := s.Content()
if cnt != nil {
switch cnt.(type) {
// For the case the vegapb.DataSourceDefinitionInternal is not nill
// and its embedded object is of type vegapb.DataSourceDefinitionInternal_Time
case *DataSourceSpecConfigurationTime:
// Set the new condition only in this case
cond := []*datapb.Condition{}
if len(c) > 0 {
cond = append(cond, c[0])
}
ds := &DataSourceDefinition{
SourceType: &DataSourceDefinition_Internal{
Internal: &DataSourceDefinitionInternal{
SourceType: &DataSourceDefinitionInternal_Time{
Time: &DataSourceSpecConfigurationTime{
Conditions: cond,
},
},
},
},
}
*s = *ds
case *DataSourceSpecConfigurationTimeTrigger:
// Set the new condition only in this case
ds := &DataSourceDefinition{
SourceType: &DataSourceDefinition_Internal{
Internal: &DataSourceDefinitionInternal{
SourceType: &DataSourceDefinitionInternal_TimeTrigger{
TimeTrigger: &DataSourceSpecConfigurationTimeTrigger{
Conditions: c,
},
},
},
},
}
*s = *ds
}
}
}
return s
}
func (s *DataSourceDefinition) Content() interface{} {
if s != nil {
if s.SourceType != nil {
switch tp := s.SourceType.(type) {
case *DataSourceDefinition_External:
if tp.External != nil {
if tp.External.SourceType != nil {
switch extTp := tp.External.SourceType.(type) {
case *DataSourceDefinitionExternal_Oracle:
if extTp.Oracle != nil {
return extTp.Oracle
}
case *DataSourceDefinitionExternal_EthOracle:
if extTp.EthOracle != nil {
return extTp.EthOracle
}
}
}
}
case *DataSourceDefinition_Internal:
if tp.Internal != nil {
if tp.Internal.SourceType != nil {
switch intTp := tp.Internal.SourceType.(type) {
case *DataSourceDefinitionInternal_Time:
if intTp.Time != nil {
return intTp.Time
}
case *DataSourceDefinitionInternal_TimeTrigger:
if intTp.TimeTrigger != nil {
return intTp.TimeTrigger
}
}
}
}
}
}
}
return nil
}