-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
data_source.go
276 lines (239 loc) · 8.87 KB
/
data_source.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
package v3
import (
"context"
"errors"
"fmt"
"math/big"
"sync"
pkgerrors "github.com/pkg/errors"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/chainlink-common/pkg/types/mercury"
v3types "github.com/smartcontractkit/chainlink-common/pkg/types/mercury/v3"
v3 "github.com/smartcontractkit/chainlink-data-streams/mercury/v3"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/ocrcommon"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/types"
mercurytypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/types"
mercuryutils "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/utils"
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/mercury/v3/reportcodec"
"github.com/smartcontractkit/chainlink/v2/core/utils"
)
type Runner interface {
ExecuteRun(ctx context.Context, spec pipeline.Spec, vars pipeline.Vars, l logger.Logger) (run *pipeline.Run, trrs pipeline.TaskRunResults, err error)
}
type LatestReportFetcher interface {
LatestPrice(ctx context.Context, feedID [32]byte) (*big.Int, error)
LatestTimestamp(context.Context) (int64, error)
}
type datasource struct {
pipelineRunner Runner
jb job.Job
spec pipeline.Spec
feedID mercuryutils.FeedID
lggr logger.Logger
saver ocrcommon.Saver
orm types.DataSourceORM
codec reportcodec.ReportCodec
fetcher LatestReportFetcher
linkFeedID mercuryutils.FeedID
nativeFeedID mercuryutils.FeedID
mu sync.RWMutex
chEnhancedTelem chan<- ocrcommon.EnhancedTelemetryMercuryData
}
var _ v3.DataSource = &datasource{}
func NewDataSource(orm types.DataSourceORM, pr pipeline.Runner, jb job.Job, spec pipeline.Spec, feedID mercuryutils.FeedID, lggr logger.Logger, s ocrcommon.Saver, enhancedTelemChan chan ocrcommon.EnhancedTelemetryMercuryData, fetcher LatestReportFetcher, linkFeedID, nativeFeedID mercuryutils.FeedID) *datasource {
return &datasource{pr, jb, spec, feedID, lggr, s, orm, reportcodec.ReportCodec{}, fetcher, linkFeedID, nativeFeedID, sync.RWMutex{}, enhancedTelemChan}
}
func (ds *datasource) Observe(ctx context.Context, repts ocrtypes.ReportTimestamp, fetchMaxFinalizedTimestamp bool) (obs v3types.Observation, pipelineExecutionErr error) {
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(ctx)
if fetchMaxFinalizedTimestamp {
wg.Add(1)
go func() {
defer wg.Done()
latest, dbErr := ds.orm.LatestReport(ctx, ds.feedID)
if dbErr != nil {
obs.MaxFinalizedTimestamp.Err = dbErr
return
}
if latest != nil {
maxFinalizedBlockNumber, decodeErr := ds.codec.ObservationTimestampFromReport(latest)
obs.MaxFinalizedTimestamp.Val, obs.MaxFinalizedTimestamp.Err = int64(maxFinalizedBlockNumber), decodeErr
return
}
obs.MaxFinalizedTimestamp.Val, obs.MaxFinalizedTimestamp.Err = ds.fetcher.LatestTimestamp(ctx)
}()
}
var trrs pipeline.TaskRunResults
wg.Add(1)
go func() {
defer wg.Done()
var run *pipeline.Run
run, trrs, pipelineExecutionErr = ds.executeRun(ctx)
if pipelineExecutionErr != nil {
cancel()
pipelineExecutionErr = fmt.Errorf("Observe failed while executing run: %w", pipelineExecutionErr)
return
}
ds.saver.Save(run)
var parsed parseOutput
parsed, pipelineExecutionErr = ds.parse(trrs)
if pipelineExecutionErr != nil {
cancel()
// This is not expected under normal circumstances
ds.lggr.Errorw("Observe failed while parsing run results", "err", pipelineExecutionErr)
pipelineExecutionErr = fmt.Errorf("Observe failed while parsing run results: %w", pipelineExecutionErr)
return
}
obs.BenchmarkPrice = parsed.benchmarkPrice
obs.Bid = parsed.bid
obs.Ask = parsed.ask
}()
var isLink, isNative bool
if ds.feedID == ds.linkFeedID {
isLink = true
} else {
wg.Add(1)
go func() {
defer wg.Done()
obs.LinkPrice.Val, obs.LinkPrice.Err = ds.fetcher.LatestPrice(ctx, ds.linkFeedID)
if obs.LinkPrice.Val == nil && obs.LinkPrice.Err == nil {
mercurytypes.PriceFeedMissingCount.WithLabelValues(ds.linkFeedID.String()).Inc()
ds.lggr.Warnw(fmt.Sprintf("Mercury server was missing LINK feed, using sentinel value of %s", v3.MissingPrice), "linkFeedID", ds.linkFeedID)
obs.LinkPrice.Val = v3.MissingPrice
} else if obs.LinkPrice.Err != nil {
mercurytypes.PriceFeedErrorCount.WithLabelValues(ds.linkFeedID.String()).Inc()
ds.lggr.Errorw("Mercury server returned error querying LINK price feed", "err", obs.LinkPrice.Err, "linkFeedID", ds.linkFeedID)
}
}()
}
if ds.feedID == ds.nativeFeedID {
isNative = true
} else {
wg.Add(1)
go func() {
defer wg.Done()
obs.NativePrice.Val, obs.NativePrice.Err = ds.fetcher.LatestPrice(ctx, ds.nativeFeedID)
if obs.NativePrice.Val == nil && obs.NativePrice.Err == nil {
mercurytypes.PriceFeedMissingCount.WithLabelValues(ds.nativeFeedID.String()).Inc()
ds.lggr.Warnw(fmt.Sprintf("Mercury server was missing native feed, using sentinel value of %s", v3.MissingPrice), "nativeFeedID", ds.nativeFeedID)
obs.NativePrice.Val = v3.MissingPrice
} else if obs.NativePrice.Err != nil {
mercurytypes.PriceFeedErrorCount.WithLabelValues(ds.nativeFeedID.String()).Inc()
ds.lggr.Errorw("Mercury server returned error querying native price feed", "err", obs.NativePrice.Err, "nativeFeedID", ds.nativeFeedID)
}
}()
}
wg.Wait()
cancel()
if pipelineExecutionErr != nil {
return
}
if isLink || isNative {
// run has now completed so it is safe to use benchmark price
if isLink {
// This IS the LINK feed, use our observed price
obs.LinkPrice.Val, obs.LinkPrice.Err = obs.BenchmarkPrice.Val, obs.BenchmarkPrice.Err
}
if isNative {
// This IS the native feed, use our observed price
obs.NativePrice.Val, obs.NativePrice.Err = obs.BenchmarkPrice.Val, obs.BenchmarkPrice.Err
}
}
ocrcommon.MaybeEnqueueEnhancedTelem(ds.jb, ds.chEnhancedTelem, ocrcommon.EnhancedTelemetryMercuryData{
V3Observation: &obs,
TaskRunResults: trrs,
RepTimestamp: repts,
FeedVersion: mercuryutils.REPORT_V3,
FetchMaxFinalizedTimestamp: fetchMaxFinalizedTimestamp,
IsLinkFeed: isLink,
IsNativeFeed: isNative,
})
return obs, nil
}
func toBigInt(val interface{}) (*big.Int, error) {
dec, err := utils.ToDecimal(val)
if err != nil {
return nil, err
}
return dec.BigInt(), nil
}
type parseOutput struct {
benchmarkPrice mercury.ObsResult[*big.Int]
bid mercury.ObsResult[*big.Int]
ask mercury.ObsResult[*big.Int]
}
func (ds *datasource) parse(trrs pipeline.TaskRunResults) (o parseOutput, merr error) {
var finaltrrs []pipeline.TaskRunResult
for _, trr := range trrs {
// only return terminal trrs from executeRun
if trr.IsTerminal() {
finaltrrs = append(finaltrrs, trr)
}
}
// pipeline.TaskRunResults comes ordered asc by index, this is guaranteed
// by the pipeline executor
if len(finaltrrs) != 3 {
return o, fmt.Errorf("invalid number of results, expected: 3, got: %d", len(finaltrrs))
}
merr = errors.Join(
setBenchmarkPrice(&o, finaltrrs[0].Result),
setBid(&o, finaltrrs[1].Result),
setAsk(&o, finaltrrs[2].Result),
)
return o, merr
}
func setBenchmarkPrice(o *parseOutput, res pipeline.Result) error {
if res.Error != nil {
o.benchmarkPrice.Err = res.Error
return res.Error
}
val, err := toBigInt(res.Value)
if err != nil {
return fmt.Errorf("failed to parse BenchmarkPrice: %w", err)
}
o.benchmarkPrice.Val = val
return nil
}
func setBid(o *parseOutput, res pipeline.Result) error {
if res.Error != nil {
o.bid.Err = res.Error
return res.Error
}
val, err := toBigInt(res.Value)
if err != nil {
return fmt.Errorf("failed to parse Bid: %w", err)
}
o.bid.Val = val
return nil
}
func setAsk(o *parseOutput, res pipeline.Result) error {
if res.Error != nil {
o.ask.Err = res.Error
return res.Error
}
val, err := toBigInt(res.Value)
if err != nil {
return fmt.Errorf("failed to parse Ask: %w", err)
}
o.ask.Val = val
return nil
}
// The context passed in here has a timeout of (ObservationTimeout + ObservationGracePeriod).
// Upon context cancellation, its expected that we return any usable values within ObservationGracePeriod.
func (ds *datasource) executeRun(ctx context.Context) (*pipeline.Run, pipeline.TaskRunResults, error) {
vars := pipeline.NewVarsFrom(map[string]interface{}{
"jb": map[string]interface{}{
"databaseID": ds.jb.ID,
"externalJobID": ds.jb.ExternalJobID,
"name": ds.jb.Name.ValueOrZero(),
},
})
run, trrs, err := ds.pipelineRunner.ExecuteRun(ctx, ds.spec, vars, ds.lggr)
if err != nil {
return nil, nil, pkgerrors.Wrapf(err, "error executing run for spec ID %v", ds.spec.ID)
}
return run, trrs, err
}