-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
common.go
327 lines (289 loc) · 8.71 KB
/
common.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
package pipeline
import (
"context"
"database/sql/driver"
"encoding/json"
"net/url"
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/utils"
"gopkg.in/guregu/null.v4"
)
//go:generate mockery --name Config --output ./mocks/ --case=underscore
type (
Task interface {
Type() TaskType
ID() int
DotID() string
Run(ctx context.Context, vars Vars, inputs []Result) Result
Base() *BaseTask
Outputs() []Task
Inputs() []Task
OutputIndex() int32
TaskTimeout() (time.Duration, bool)
}
Config interface {
BridgeResponseURL() *url.URL
DatabaseMaximumTxDuration() time.Duration
DatabaseURL() url.URL
DefaultHTTPLimit() int64
DefaultHTTPTimeout() models.Duration
DefaultMaxHTTPAttempts() uint
DefaultHTTPAllowUnrestrictedNetworkAccess() bool
EthGasLimitDefault() uint64
EthMaxQueuedTransactions() uint64
TriggerFallbackDBPollInterval() time.Duration
JobPipelineMaxRunDuration() time.Duration
JobPipelineReaperInterval() time.Duration
JobPipelineReaperThreshold() time.Duration
}
)
var (
ErrWrongInputCardinality = errors.New("wrong number of task inputs")
ErrBadInput = errors.New("bad input for task")
ErrParameterEmpty = errors.New("parameter is empty")
ErrTooManyErrors = errors.New("too many errors")
ErrTimeout = errors.New("timeout")
ErrTaskRunFailed = errors.New("task run failed")
)
const (
InputTaskKey = "input"
)
// Result is the result of a TaskRun
type Result struct {
Value interface{}
Error error
}
// OutputDB dumps a single result output for a pipeline_run or pipeline_task_run
func (result Result) OutputDB() JSONSerializable {
return JSONSerializable{Val: result.Value, Null: result.Value == nil}
}
// ErrorDB dumps a single result error for a pipeline_task_run
func (result Result) ErrorDB() null.String {
var errString null.String
if result.Error != nil {
errString = null.StringFrom(result.Error.Error())
}
return errString
}
// FinalResult is the result of a Run
type FinalResult struct {
Values []interface{}
Errors []error
}
// HasErrors returns true if the final result has any errors
func (result FinalResult) HasErrors() bool {
for _, err := range result.Errors {
if err != nil {
return true
}
}
return false
}
// SingularResult returns a single result if the FinalResult only has one set of outputs/errors
func (result FinalResult) SingularResult() (Result, error) {
if len(result.Errors) != 1 || len(result.Values) != 1 {
return Result{}, errors.Errorf("cannot cast FinalResult to singular result; it does not have exactly 1 error and exactly 1 output: %#v", result)
}
return Result{Error: result.Errors[0], Value: result.Values[0]}, nil
}
// TaskRunResult describes the result of a task run, suitable for database
// update or insert.
// ID might be zero if the TaskRun has not been inserted yet
// TaskSpecID will always be non-zero
type TaskRunResult struct {
ID uuid.UUID
Task Task
TaskRun TaskRun
Result Result
CreatedAt time.Time
FinishedAt null.Time
}
func (result *TaskRunResult) IsPending() bool {
return !result.FinishedAt.Valid && result.Result == Result{}
}
func (result *TaskRunResult) IsTerminal() bool {
return len(result.Task.Outputs()) == 0
}
// TaskRunResults represents a collection of results for all task runs for one pipeline run
type TaskRunResults []TaskRunResult
// FinalResult pulls the FinalResult for the pipeline_run from the task runs
// It needs to respect the output index of each task
func (trrs TaskRunResults) FinalResult() FinalResult {
var found bool
var fr FinalResult
sort.Slice(trrs, func(i, j int) bool {
return trrs[i].Task.OutputIndex() < trrs[j].Task.OutputIndex()
})
for _, trr := range trrs {
if trr.IsTerminal() {
fr.Values = append(fr.Values, trr.Result.Value)
fr.Errors = append(fr.Errors, trr.Result.Error)
found = true
}
}
if !found {
logger.Errorw("expected at least one task to be final", "tasks", trrs)
panic("expected at least one task to be final")
}
return fr
}
type RunWithResults struct {
Run Run
TaskRunResults TaskRunResults
}
type JSONSerializable struct {
Val interface{}
Null bool
}
func (js *JSONSerializable) UnmarshalJSON(bs []byte) error {
if js == nil {
*js = JSONSerializable{}
}
return json.Unmarshal(bs, &js.Val)
}
func (js JSONSerializable) MarshalJSON() ([]byte, error) {
switch x := js.Val.(type) {
case []byte:
return json.Marshal(string(x))
default:
return json.Marshal(js.Val)
}
}
func (js *JSONSerializable) Scan(value interface{}) error {
if value == nil {
*js = JSONSerializable{Null: true}
return nil
}
bytes, ok := value.([]byte)
if !ok {
return errors.Errorf("JSONSerializable#Scan received a value of type %T", value)
}
if js == nil {
*js = JSONSerializable{}
}
return js.UnmarshalJSON(bytes)
}
func (js JSONSerializable) Value() (driver.Value, error) {
if js.Null {
return nil, nil
}
return js.MarshalJSON()
}
func (js *JSONSerializable) Empty() bool {
return js == nil || js.Null
}
type TaskType string
func (t TaskType) String() string {
return string(t)
}
const (
TaskTypeHTTP TaskType = "http"
TaskTypeBridge TaskType = "bridge"
TaskTypeMean TaskType = "mean"
TaskTypeMedian TaskType = "median"
TaskTypeMode TaskType = "mode"
TaskTypeSum TaskType = "sum"
TaskTypeMultiply TaskType = "multiply"
TaskTypeDivide TaskType = "divide"
TaskTypeJSONParse TaskType = "jsonparse"
TaskTypeCBORParse TaskType = "cborparse"
TaskTypeAny TaskType = "any"
TaskTypeVRF TaskType = "vrf"
TaskTypeETHCall TaskType = "ethcall"
TaskTypeETHTx TaskType = "ethtx"
TaskTypeETHABIEncode TaskType = "ethabiencode"
TaskTypeETHABIDecode TaskType = "ethabidecode"
TaskTypeETHABIDecodeLog TaskType = "ethabidecodelog"
// Testing only.
TaskTypePanic TaskType = "panic"
)
var (
stringType = reflect.TypeOf("")
int32Type = reflect.TypeOf(int32(0))
)
func UnmarshalTaskFromMap(taskType TaskType, taskMap interface{}, ID int, dotID string) (_ Task, err error) {
defer utils.WrapIfError(&err, "UnmarshalTaskFromMap")
switch taskMap.(type) {
default:
return nil, errors.Errorf("UnmarshalTaskFromMap only accepts a map[string]interface{} or a map[string]string. Got %v (%#v) of type %T", taskMap, taskMap, taskMap)
case map[string]interface{}, map[string]string:
}
taskType = TaskType(strings.ToLower(string(taskType)))
var task Task
switch taskType {
case TaskTypePanic:
task = &PanicTask{BaseTask: BaseTask{id: ID, dotID: dotID}}
case TaskTypeHTTP:
task = &HTTPTask{BaseTask: BaseTask{id: ID, dotID: dotID}}
case TaskTypeBridge:
task = &BridgeTask{BaseTask: BaseTask{id: ID, dotID: dotID}}
case TaskTypeMedian:
task = &MedianTask{BaseTask: BaseTask{id: ID, dotID: dotID}}
case TaskTypeAny:
task = &AnyTask{BaseTask: BaseTask{id: ID, dotID: dotID}}
case TaskTypeJSONParse:
task = &JSONParseTask{BaseTask: BaseTask{id: ID, dotID: dotID}}
case TaskTypeMultiply:
task = &MultiplyTask{BaseTask: BaseTask{id: ID, dotID: dotID}}
case TaskTypeVRF:
task = &VRFTask{BaseTask: BaseTask{id: ID, dotID: dotID}}
case TaskTypeETHCall:
task = ÐCallTask{BaseTask: BaseTask{dotID: dotID}}
default:
return nil, errors.Errorf(`unknown task type: "%v"`, taskType)
}
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: task,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc(),
func(from reflect.Type, to reflect.Type, data interface{}) (interface{}, error) {
switch from {
case stringType:
switch to {
case int32Type:
i, err2 := strconv.ParseInt(data.(string), 10, 32)
return int32(i), err2
}
}
return data, nil
},
),
})
if err != nil {
return nil, err
}
err = decoder.Decode(taskMap)
if err != nil {
return nil, err
}
return task, nil
}
func CheckInputs(inputs []Result, minLen, maxLen, maxErrors int) ([]interface{}, error) {
if minLen >= 0 && len(inputs) < minLen {
return nil, errors.Wrapf(ErrWrongInputCardinality, "min: %v max: %v (got %v)", minLen, maxLen, len(inputs))
} else if maxLen >= 0 && len(inputs) > maxLen {
return nil, errors.Wrapf(ErrWrongInputCardinality, "min: %v max: %v (got %v)", minLen, maxLen, len(inputs))
}
var vals []interface{}
var errs int
for _, input := range inputs {
if input.Error != nil {
errs++
continue
}
vals = append(vals, input.Value)
}
if maxErrors >= 0 && errs > maxErrors {
return nil, ErrTooManyErrors
}
return vals, nil
}