-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
delegate.go
397 lines (367 loc) · 14.3 KB
/
delegate.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
package ocr2
import (
"encoding/json"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
libocr2 "github.com/smartcontractkit/libocr/offchainreporting2"
"github.com/smartcontractkit/ocr2vrf/altbn_128"
dkgpkg "github.com/smartcontractkit/ocr2vrf/dkg"
"github.com/smartcontractkit/ocr2vrf/ocr2vrf"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink-relay/pkg/types"
"github.com/smartcontractkit/chainlink/core/chains/evm"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/job"
"github.com/smartcontractkit/chainlink/core/services/keystore"
"github.com/smartcontractkit/chainlink/core/services/ocr2/plugins"
"github.com/smartcontractkit/chainlink/core/services/ocr2/plugins/dkg"
"github.com/smartcontractkit/chainlink/core/services/ocr2/plugins/median"
"github.com/smartcontractkit/chainlink/core/services/ocr2/plugins/ocr2vrf/blockhashes"
ocr2vrfconfig "github.com/smartcontractkit/chainlink/core/services/ocr2/plugins/ocr2vrf/config"
ocr2coordinator "github.com/smartcontractkit/chainlink/core/services/ocr2/plugins/ocr2vrf/coordinator"
"github.com/smartcontractkit/chainlink/core/services/ocr2/plugins/ocr2vrf/juelsfeecoin"
"github.com/smartcontractkit/chainlink/core/services/ocr2/plugins/ocr2vrf/reportserializer"
"github.com/smartcontractkit/chainlink/core/services/ocr2/validate"
"github.com/smartcontractkit/chainlink/core/services/ocrcommon"
"github.com/smartcontractkit/chainlink/core/services/pipeline"
"github.com/smartcontractkit/chainlink/core/services/relay"
evmrelay "github.com/smartcontractkit/chainlink/core/services/relay/evm"
"github.com/smartcontractkit/chainlink/core/services/telemetry"
)
type Delegate struct {
db *sqlx.DB
jobORM job.ORM
pipelineRunner pipeline.Runner
peerWrapper *ocrcommon.SingletonPeerWrapper
monitoringEndpointGen telemetry.MonitoringEndpointGenerator
chainSet evm.ChainSet
cfg validate.Config
lggr logger.Logger
ks keystore.OCR2
dkgSignKs keystore.DKGSign
dkgEncryptKs keystore.DKGEncrypt
relayers map[relay.Network]types.Relayer
}
var _ job.Delegate = (*Delegate)(nil)
func NewDelegate(
db *sqlx.DB,
jobORM job.ORM,
pipelineRunner pipeline.Runner,
peerWrapper *ocrcommon.SingletonPeerWrapper,
monitoringEndpointGen telemetry.MonitoringEndpointGenerator,
chainSet evm.ChainSet,
lggr logger.Logger,
cfg validate.Config,
ks keystore.OCR2,
dkgSignKs keystore.DKGSign,
dkgEncryptKs keystore.DKGEncrypt,
relayers map[relay.Network]types.Relayer,
) *Delegate {
return &Delegate{
db,
jobORM,
pipelineRunner,
peerWrapper,
monitoringEndpointGen,
chainSet,
cfg,
lggr,
ks,
dkgSignKs,
dkgEncryptKs,
relayers,
}
}
func (d Delegate) JobType() job.Type {
return job.OffchainReporting2
}
func (Delegate) OnJobCreated(spec job.Job) {}
func (Delegate) OnJobDeleted(spec job.Job) {}
func (Delegate) AfterJobCreated(spec job.Job) {}
func (Delegate) BeforeJobDeleted(spec job.Job) {}
// ServicesForSpec returns the OCR2 services that need to run for this job
func (d Delegate) ServicesForSpec(jobSpec job.Job) ([]job.ServiceCtx, error) {
spec := jobSpec.OCR2OracleSpec
if spec == nil {
return nil, errors.Errorf("offchainreporting2.Delegate expects an *job.Offchainreporting2OracleSpec to be present, got %v", jobSpec)
}
if !spec.TransmitterID.Valid {
return nil, errors.Errorf("expected a transmitterID to be specified")
}
relayer, exists := d.relayers[spec.Relay]
if !exists {
return nil, errors.Errorf("%s relay does not exist is it enabled?", spec.Relay)
}
ocrDB := NewDB(d.db, spec.ID, d.lggr, d.cfg)
peerWrapper := d.peerWrapper
if peerWrapper == nil {
return nil, errors.New("cannot setup OCR2 job service, libp2p peer was missing")
} else if !peerWrapper.IsStarted() {
return nil, errors.New("peerWrapper is not started. OCR2 jobs require a started and running peer. Did you forget to specify P2P_LISTEN_PORT?")
}
lggr := d.lggr.Named("OCR").With(
"contractID", spec.ContractID,
"jobName", jobSpec.Name.ValueOrZero(),
"jobID", jobSpec.ID,
)
ocrLogger := logger.NewOCRWrapper(lggr, true, func(msg string) {
d.lggr.ErrorIf(d.jobORM.RecordError(jobSpec.ID, msg), "unable to record error")
})
lc := validate.ToLocalConfig(d.cfg, *spec)
if err := libocr2.SanityCheckLocalConfig(lc); err != nil {
return nil, err
}
d.lggr.Infow("OCR2 job using local config",
"BlockchainTimeout", lc.BlockchainTimeout,
"ContractConfigConfirmations", lc.ContractConfigConfirmations,
"ContractConfigTrackerPollInterval", lc.ContractConfigTrackerPollInterval,
"ContractTransmitterTransmitTimeout", lc.ContractTransmitterTransmitTimeout,
"DatabaseTimeout", lc.DatabaseTimeout,
)
bootstrapPeers, err := ocrcommon.GetValidatedBootstrapPeers(spec.P2PV2Bootstrappers, peerWrapper.Config().P2PV2Bootstrappers())
if err != nil {
return nil, err
}
d.lggr.Debugw("Using bootstrap peers", "peers", bootstrapPeers)
// Fetch the specified OCR2 key bundle
var kbID string
if spec.OCRKeyBundleID.Valid {
kbID = spec.OCRKeyBundleID.String
} else if kbID, err = d.cfg.OCR2KeyBundleID(); err != nil {
return nil, err
}
kb, err := d.ks.Get(kbID)
if err != nil {
return nil, err
}
runResults := make(chan pipeline.Run, d.cfg.JobPipelineResultWriteQueueDepth())
var pluginOracle plugins.OraclePlugin
var ocr2Provider types.Plugin
switch spec.PluginType {
case job.Median:
medianProvider, err2 := relayer.NewMedianProvider(
types.RelayArgs{
ExternalJobID: jobSpec.ExternalJobID,
JobID: spec.ID,
ContractID: spec.ContractID,
RelayConfig: spec.RelayConfig.Bytes(),
}, types.PluginArgs{
TransmitterID: spec.TransmitterID.String,
PluginConfig: spec.PluginConfig.Bytes(),
})
if err2 != nil {
return nil, err2
}
ocr2Provider = medianProvider
pluginOracle, err = median.NewMedian(jobSpec, medianProvider, d.pipelineRunner, runResults, lggr, ocrLogger)
case job.DKG:
chainIDInterface, ok := jobSpec.OCR2OracleSpec.RelayConfig["chainID"]
if !ok {
return nil, errors.New("chainID must be provided in relay config")
}
chainID := int64(chainIDInterface.(float64))
chain, err2 := d.chainSet.Get(big.NewInt(chainID))
if err2 != nil {
return nil, errors.Wrap(err2, "get chainset")
}
ocr2vrfRelayer := evmrelay.NewOCR2VRFRelayer(d.db, chain, lggr.Named("OCR2VRFRelayer"))
dkgProvider, err2 := ocr2vrfRelayer.NewDKGProvider(
types.RelayArgs{
ExternalJobID: jobSpec.ExternalJobID,
JobID: spec.ID,
ContractID: spec.ContractID,
RelayConfig: spec.RelayConfig.Bytes(),
}, types.PluginArgs{
TransmitterID: spec.TransmitterID.String,
PluginConfig: spec.PluginConfig.Bytes(),
})
if err2 != nil {
return nil, err2
}
ocr2Provider = dkgProvider
pluginOracle, err = dkg.NewDKG(
jobSpec,
dkgProvider,
lggr.Named("DKG"),
ocrLogger,
d.dkgSignKs,
d.dkgEncryptKs,
chain.Client())
if err != nil {
return nil, errors.Wrap(err, "error while instantiating DKG")
}
case job.OCR2VRF:
chainIDInterface, ok := jobSpec.OCR2OracleSpec.RelayConfig["chainID"]
if !ok {
return nil, errors.New("chainID must be provided in relay config")
}
chainID := int64(chainIDInterface.(float64))
chain, err2 := d.chainSet.Get(big.NewInt(chainID))
if err2 != nil {
return nil, errors.Wrap(err2, "get chainset")
}
var cfg ocr2vrfconfig.PluginConfig
err2 = json.Unmarshal(spec.PluginConfig.Bytes(), &cfg)
if err2 != nil {
return nil, errors.Wrap(err2, "unmarshal ocr2vrf plugin config")
}
err2 = ocr2vrfconfig.ValidatePluginConfig(cfg, d.dkgSignKs, d.dkgEncryptKs)
if err2 != nil {
return nil, errors.Wrap(err2, "validate ocr2vrf plugin config")
}
ocr2vrfRelayer := evmrelay.NewOCR2VRFRelayer(d.db, chain, lggr.Named("OCR2VRFRelayer"))
vrfProvider, err2 := ocr2vrfRelayer.NewOCR2VRFProvider(
types.RelayArgs{
ExternalJobID: jobSpec.ExternalJobID,
JobID: spec.ID,
ContractID: spec.ContractID,
RelayConfig: spec.RelayConfig.Bytes(),
}, types.PluginArgs{
TransmitterID: spec.TransmitterID.String,
PluginConfig: spec.PluginConfig.Bytes(),
})
if err2 != nil {
return nil, errors.Wrap(err2, "new vrf provider")
}
dkgProvider, err2 := ocr2vrfRelayer.NewDKGProvider(
types.RelayArgs{
ExternalJobID: jobSpec.ExternalJobID,
JobID: spec.ID,
ContractID: cfg.DKGContractAddress,
RelayConfig: spec.RelayConfig.Bytes(),
}, types.PluginArgs{
TransmitterID: spec.TransmitterID.String,
PluginConfig: spec.PluginConfig.Bytes(),
})
if err2 != nil {
return nil, errors.Wrap(err2, "new dkg provider")
}
dkgContract, err2 := dkg.NewOnchainDKGClient(cfg.DKGContractAddress, chain.Client())
if err2 != nil {
return nil, errors.Wrap(err2, "new onchain dkg client")
}
juelsPerFeeCoin, err2 := juelsfeecoin.NewLinkEthPriceProvider(
common.HexToAddress(cfg.LinkEthFeedAddress), chain.Client(), 1*time.Second)
if err2 != nil {
return nil, errors.Wrap(err2, "new link eth price provider")
}
// No need to error check here, we check these keys exist when validating
// the configuration.
encryptionSecretKey, _ := d.dkgEncryptKs.Get(cfg.DKGEncryptionPublicKey)
signingSecretKey, _ := d.dkgSignKs.Get(cfg.DKGSigningPublicKey)
keyID, err2 := dkg.DecodeKeyID(cfg.DKGKeyID)
if err2 != nil {
return nil, errors.Wrap(err2, "decode DKG key ID")
}
coordinator, err2 := ocr2coordinator.New(
lggr.Named("OCR2VRFCoordinator"),
common.HexToAddress(spec.ContractID),
common.HexToAddress(cfg.DKGContractAddress),
chain.Client(),
cfg.LookbackBlocks,
chain.LogPoller(),
)
if err2 != nil {
return nil, errors.Wrap(err2, "create ocr2vrf coordinator")
}
l := d.lggr.Named("OCR2VRF").With(
"jobName", jobSpec.Name.ValueOrZero(),
"jobID", jobSpec.ID,
)
vrfLogger := logger.NewOCRWrapper(l.With(
"vrfContractID", spec.ContractID), true, func(msg string) {
d.lggr.ErrorIf(d.jobORM.RecordError(jobSpec.ID, msg), "unable to record error")
})
dkgLogger := logger.NewOCRWrapper(l.With(
"dkgContractID", cfg.DKGContractAddress), true, func(msg string) {
d.lggr.ErrorIf(d.jobORM.RecordError(jobSpec.ID, msg), "unable to record error")
})
oracles, err2 := ocr2vrf.NewOCR2VRF(ocr2vrf.DKGVRFArgs{
VRFLogger: vrfLogger,
DKGLogger: dkgLogger,
BinaryNetworkEndpointFactory: peerWrapper.Peer2,
V2Bootstrappers: bootstrapPeers,
OffchainKeyring: kb,
OnchainKeyring: kb,
VRFOffchainConfigDigester: vrfProvider.OffchainConfigDigester(),
VRFContractConfigTracker: vrfProvider.ContractConfigTracker(),
VRFContractTransmitter: vrfProvider.ContractTransmitter(),
VRFDatabase: ocrDB,
VRFLocalConfig: lc,
VRFMonitoringEndpoint: d.monitoringEndpointGen.GenMonitoringEndpoint(spec.ContractID),
DKGContractConfigTracker: dkgProvider.ContractConfigTracker(),
DKGOffchainConfigDigester: dkgProvider.OffchainConfigDigester(),
DKGContract: dkgpkg.NewOnchainContract(dkgContract, &altbn_128.G2{}),
DKGContractTransmitter: dkgProvider.ContractTransmitter(),
DKGDatabase: ocrDB,
DKGLocalConfig: lc,
DKGMonitoringEndpoint: d.monitoringEndpointGen.GenMonitoringEndpoint(cfg.DKGContractAddress),
Blockhashes: blockhashes.NewFixedBlockhashProvider(chain.Client(), 256, 256),
Serializer: reportserializer.NewReportSerializer(&altbn_128.G1{}),
JulesPerFeeCoin: juelsPerFeeCoin,
Coordinator: coordinator,
ConfirmationDelays: cfg.ConfirmationDelays,
Esk: encryptionSecretKey.KyberScalar(),
Ssk: signingSecretKey.KyberScalar(),
KeyID: keyID,
})
if err2 != nil {
return nil, errors.Wrap(err2, "new ocr2vrf")
}
// RunResultSaver needs to be started first, so it's available
// to read odb writes. It is stopped last after the OraclePlugin is shut down
// so no further runs are enqueued, and we can drain the queue.
runResultSaver := ocrcommon.NewResultRunSaver(
runResults,
d.pipelineRunner,
make(chan struct{}),
lggr)
// NOTE: we return from here with the services because the OCR2VRF oracles are defined
// and exported from the ocr2vrf library. It takes care of running the DKG and OCR2VRF
// oracles under the hood together.
oracleCtx := job.NewServiceAdapter(oracles)
return []job.ServiceCtx{runResultSaver, vrfProvider, oracleCtx}, nil
default:
return nil, errors.Errorf("plugin type %s not supported", spec.PluginType)
}
if err != nil {
return nil, errors.Wrap(err, "failed to initialise plugin")
}
pluginFactory, err := pluginOracle.GetPluginFactory()
if err != nil {
return nil, errors.Wrap(err, "failed to get plugin factory")
}
pluginServices, err := pluginOracle.GetServices()
if err != nil {
return nil, errors.Wrap(err, "failed to get plugin services")
}
oracle, err := libocr2.NewOracle(libocr2.OracleArgs{
BinaryNetworkEndpointFactory: peerWrapper.Peer2,
V2Bootstrappers: bootstrapPeers,
ContractTransmitter: ocr2Provider.ContractTransmitter(),
ContractConfigTracker: ocr2Provider.ContractConfigTracker(),
Database: ocrDB,
LocalConfig: lc,
Logger: ocrLogger,
MonitoringEndpoint: d.monitoringEndpointGen.GenMonitoringEndpoint(spec.ContractID),
OffchainConfigDigester: ocr2Provider.OffchainConfigDigester(),
OffchainKeyring: kb,
OnchainKeyring: kb,
ReportingPluginFactory: pluginFactory,
})
if err != nil {
return nil, errors.Wrap(err, "error calling NewOracle")
}
// RunResultSaver needs to be started first, so it's available
// to read odb writes. It is stopped last after the OraclePlugin is shut down
// so no further runs are enqueued, and we can drain the queue.
runResultSaver := ocrcommon.NewResultRunSaver(
runResults,
d.pipelineRunner,
make(chan struct{}),
lggr)
oracleCtx := job.NewServiceAdapter(oracle)
return append([]job.ServiceCtx{runResultSaver, ocr2Provider, oracleCtx}, pluginServices...), nil
}