/
application.go
748 lines (659 loc) · 24.3 KB
/
application.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
package chainlink
import (
"bytes"
"context"
"fmt"
"math/big"
"reflect"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"go.uber.org/multierr"
"go.uber.org/zap/zapcore"
"github.com/smartcontractkit/chainlink-solana/pkg/solana"
pkgterra "github.com/smartcontractkit/chainlink-terra/pkg/terra"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink/core/bridges"
"github.com/smartcontractkit/chainlink/core/chains/evm"
"github.com/smartcontractkit/chainlink/core/chains/evm/txmgr"
evmtypes "github.com/smartcontractkit/chainlink/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/core/chains/terra"
"github.com/smartcontractkit/chainlink/core/config"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services"
"github.com/smartcontractkit/chainlink/core/services/blockhashstore"
"github.com/smartcontractkit/chainlink/core/services/cron"
"github.com/smartcontractkit/chainlink/core/services/directrequest"
"github.com/smartcontractkit/chainlink/core/services/feeds"
"github.com/smartcontractkit/chainlink/core/services/fluxmonitorv2"
"github.com/smartcontractkit/chainlink/core/services/job"
"github.com/smartcontractkit/chainlink/core/services/keeper"
"github.com/smartcontractkit/chainlink/core/services/keystore"
"github.com/smartcontractkit/chainlink/core/services/ocr"
"github.com/smartcontractkit/chainlink/core/services/ocr2"
"github.com/smartcontractkit/chainlink/core/services/ocrbootstrap"
"github.com/smartcontractkit/chainlink/core/services/ocrcommon"
"github.com/smartcontractkit/chainlink/core/services/periodicbackup"
"github.com/smartcontractkit/chainlink/core/services/pg"
"github.com/smartcontractkit/chainlink/core/services/pipeline"
"github.com/smartcontractkit/chainlink/core/services/promreporter"
"github.com/smartcontractkit/chainlink/core/services/relay"
evmrelay "github.com/smartcontractkit/chainlink/core/services/relay/evm"
relaytypes "github.com/smartcontractkit/chainlink/core/services/relay/types"
"github.com/smartcontractkit/chainlink/core/services/synchronization"
"github.com/smartcontractkit/chainlink/core/services/telemetry"
"github.com/smartcontractkit/chainlink/core/services/vrf"
"github.com/smartcontractkit/chainlink/core/services/webhook"
"github.com/smartcontractkit/chainlink/core/sessions"
"github.com/smartcontractkit/chainlink/core/utils"
)
//go:generate mockery --name Application --output ../../internal/mocks/ --case=underscore
// Application implements the common functions used in the core node.
type Application interface {
Start(ctx context.Context) error
Stop() error
GetLogger() logger.Logger
GetHealthChecker() services.Checker
GetSqlxDB() *sqlx.DB
GetConfig() config.GeneralConfig
SetLogLevel(lvl zapcore.Level) error
GetKeyStore() keystore.Master
GetEventBroadcaster() pg.EventBroadcaster
WakeSessionReaper()
GetWebAuthnConfiguration() sessions.WebAuthnConfiguration
GetExternalInitiatorManager() webhook.ExternalInitiatorManager
GetChains() Chains
// V2 Jobs (TOML specified)
JobSpawner() job.Spawner
JobORM() job.ORM
EVMORM() evmtypes.ORM
PipelineORM() pipeline.ORM
BridgeORM() bridges.ORM
SessionORM() sessions.ORM
TxmORM() txmgr.ORM
AddJobV2(ctx context.Context, job *job.Job) error
DeleteJob(ctx context.Context, jobID int32) error
RunWebhookJobV2(ctx context.Context, jobUUID uuid.UUID, requestBody string, meta pipeline.JSONSerializable) (int64, error)
ResumeJobV2(ctx context.Context, taskID uuid.UUID, result pipeline.Result) error
// Testing only
RunJobV2(ctx context.Context, jobID int32, meta map[string]interface{}) (int64, error)
SetServiceLogLevel(ctx context.Context, service string, level zapcore.Level) error
// Feeds
GetFeedsService() feeds.Service
// ReplayFromBlock replays logs from on or after the given block number. If forceBroadcast is
// set to true, consumers will reprocess data even if it has already been processed.
ReplayFromBlock(chainID *big.Int, number uint64, forceBroadcast bool) error
// ID is unique to this particular application instance
ID() uuid.UUID
}
// ChainlinkApplication contains fields for the JobSubscriber, Scheduler,
// and Store. The JobSubscriber and Scheduler are also available
// in the services package, but the Store has its own package.
type ChainlinkApplication struct {
Chains Chains
EventBroadcaster pg.EventBroadcaster
jobORM job.ORM
jobSpawner job.Spawner
pipelineORM pipeline.ORM
pipelineRunner pipeline.Runner
bridgeORM bridges.ORM
sessionORM sessions.ORM
txmORM txmgr.ORM
FeedsService feeds.Service
webhookJobRunner webhook.JobRunner
Config config.GeneralConfig
KeyStore keystore.Master
ExternalInitiatorManager webhook.ExternalInitiatorManager
SessionReaper utils.SleeperTask
shutdownOnce sync.Once
explorerClient synchronization.ExplorerClient
subservices []services.ServiceCtx
HealthChecker services.Checker
Nurse *services.Nurse
logger logger.Logger
closeLogger func() error
sqlxDB *sqlx.DB
started bool
startStopMu sync.Mutex
}
type ApplicationOpts struct {
Config config.GeneralConfig
EventBroadcaster pg.EventBroadcaster
SqlxDB *sqlx.DB
KeyStore keystore.Master
Chains Chains
Logger logger.Logger
CloseLogger func() error
ExternalInitiatorManager webhook.ExternalInitiatorManager
Version string
}
// Chains holds a ChainSet for each type of chain.
type Chains struct {
EVM evm.ChainSet
Terra terra.ChainSet // nil if disabled
}
func (c *Chains) services() (s []services.ServiceCtx) {
if c.EVM != nil {
s = append(s, c.EVM)
}
if c.Terra != nil {
s = append(s, c.Terra)
}
return
}
// NewApplication initializes a new store if one is not already
// present at the configured root directory (default: ~/.chainlink),
// the logger at the same directory and returns the Application to
// be used by the node.
// TODO: Inject more dependencies here to save booting up useless stuff in tests
func NewApplication(opts ApplicationOpts) (Application, error) {
var subservices []services.ServiceCtx
db := opts.SqlxDB
cfg := opts.Config
keyStore := opts.KeyStore
chains := opts.Chains
globalLogger := opts.Logger
eventBroadcaster := opts.EventBroadcaster
externalInitiatorManager := opts.ExternalInitiatorManager
var nurse *services.Nurse
if cfg.AutoPprofEnabled() {
globalLogger.Info("Nurse service (automatic pprof profiling) is enabled")
nurse = services.NewNurse(cfg, globalLogger)
err := nurse.Start()
if err != nil {
return nil, err
}
} else {
globalLogger.Info("Nurse service (automatic pprof profiling) is disabled")
}
healthChecker := services.NewChecker()
telemetryIngressClient := synchronization.TelemetryIngressClient(&synchronization.NoopTelemetryIngressClient{})
telemetryIngressBatchClient := synchronization.TelemetryIngressBatchClient(&synchronization.NoopTelemetryIngressBatchClient{})
explorerClient := synchronization.ExplorerClient(&synchronization.NoopExplorerClient{})
monitoringEndpointGen := telemetry.MonitoringEndpointGenerator(&telemetry.NoopAgent{})
if cfg.ExplorerURL() != nil && cfg.TelemetryIngressURL() != nil {
globalLogger.Warn("Both EXPLORER_URL and TELEMETRY_INGRESS_URL are set, defaulting to Explorer")
}
if cfg.ExplorerURL() != nil {
explorerClient = synchronization.NewExplorerClient(cfg.ExplorerURL(), cfg.ExplorerAccessKey(), cfg.ExplorerSecret(), globalLogger)
monitoringEndpointGen = telemetry.NewExplorerAgent(explorerClient)
}
// Use Explorer over TelemetryIngress if both URLs are set
if cfg.ExplorerURL() == nil && cfg.TelemetryIngressURL() != nil {
if cfg.TelemetryIngressUseBatchSend() {
telemetryIngressBatchClient = synchronization.NewTelemetryIngressBatchClient(cfg.TelemetryIngressURL(),
cfg.TelemetryIngressServerPubKey(), keyStore.CSA(), cfg.TelemetryIngressLogging(), globalLogger, cfg.TelemetryIngressBufferSize(), cfg.TelemetryIngressMaxBatchSize(), cfg.TelemetryIngressSendInterval(), cfg.TelemetryIngressSendTimeout(), cfg.TelemetryIngressUniConn())
monitoringEndpointGen = telemetry.NewIngressAgentBatchWrapper(telemetryIngressBatchClient)
} else {
telemetryIngressClient = synchronization.NewTelemetryIngressClient(cfg.TelemetryIngressURL(),
cfg.TelemetryIngressServerPubKey(), keyStore.CSA(), cfg.TelemetryIngressLogging(), globalLogger)
monitoringEndpointGen = telemetry.NewIngressAgentWrapper(telemetryIngressClient)
}
}
subservices = append(subservices, explorerClient, telemetryIngressClient, telemetryIngressBatchClient)
if cfg.DatabaseBackupMode() != config.DatabaseBackupModeNone && cfg.DatabaseBackupFrequency() > 0 {
globalLogger.Infow("DatabaseBackup: periodic database backups are enabled", "frequency", cfg.DatabaseBackupFrequency())
databaseBackup, err := periodicbackup.NewDatabaseBackup(cfg, globalLogger)
if err != nil {
return nil, errors.Wrap(err, "NewApplication: failed to initialize database backup")
}
subservices = append(subservices, databaseBackup)
} else {
globalLogger.Info("DatabaseBackup: periodic database backups are disabled. To enable automatic backups, set DATABASE_BACKUP_MODE=lite or DATABASE_BACKUP_MODE=full")
}
subservices = append(subservices, eventBroadcaster)
subservices = append(subservices, chains.services()...)
promReporter := promreporter.NewPromReporter(db.DB, globalLogger)
subservices = append(subservices, promReporter)
var (
pipelineORM = pipeline.NewORM(db, globalLogger, cfg)
bridgeORM = bridges.NewORM(db, globalLogger, cfg)
sessionORM = sessions.NewORM(db, cfg.SessionTimeout().Duration(), globalLogger)
pipelineRunner = pipeline.NewRunner(pipelineORM, cfg, chains.EVM, keyStore.Eth(), keyStore.VRF(), globalLogger)
jobORM = job.NewORM(db, chains.EVM, pipelineORM, keyStore, globalLogger, cfg)
txmORM = txmgr.NewORM(db, globalLogger, cfg)
)
for _, chain := range chains.EVM.Chains() {
chain.HeadBroadcaster().Subscribe(promReporter)
chain.TxManager().RegisterResumeCallback(pipelineRunner.ResumeRun)
}
var (
delegates = map[job.Type]job.Delegate{
job.DirectRequest: directrequest.NewDelegate(
globalLogger,
pipelineRunner,
pipelineORM,
chains.EVM),
job.Keeper: keeper.NewDelegate(
db,
jobORM,
pipelineRunner,
globalLogger,
chains.EVM),
job.VRF: vrf.NewDelegate(
db,
keyStore,
pipelineRunner,
pipelineORM,
chains.EVM,
globalLogger,
cfg),
job.Webhook: webhook.NewDelegate(
pipelineRunner,
externalInitiatorManager,
globalLogger),
job.Cron: cron.NewDelegate(
pipelineRunner,
globalLogger),
job.BlockhashStore: blockhashstore.NewDelegate(
globalLogger,
chains.EVM,
keyStore.Eth()),
}
webhookJobRunner = delegates[job.Webhook].(*webhook.Delegate).WebhookJobRunner()
)
// Flux monitor requires ethereum just to boot, silence errors with a null delegate
if !cfg.EVMRPCEnabled() {
delegates[job.FluxMonitor] = &job.NullDelegate{Type: job.FluxMonitor}
} else {
delegates[job.FluxMonitor] = fluxmonitorv2.NewDelegate(
keyStore.Eth(),
jobORM,
pipelineORM,
pipelineRunner,
db,
chains.EVM,
globalLogger,
)
}
var peerWrapper *ocrcommon.SingletonPeerWrapper
if cfg.P2PEnabled() {
if err := ocrcommon.ValidatePeerWrapperConfig(cfg); err != nil {
return nil, err
}
peerWrapper = ocrcommon.NewSingletonPeerWrapper(keyStore, cfg, db, globalLogger)
subservices = append(subservices, peerWrapper)
} else {
globalLogger.Debug("P2P stack disabled")
}
if cfg.FeatureOffchainReporting() {
delegates[job.OffchainReporting] = ocr.NewDelegate(
db,
jobORM,
keyStore,
pipelineRunner,
peerWrapper,
monitoringEndpointGen,
chains.EVM,
globalLogger,
)
} else {
globalLogger.Debug("Off-chain reporting disabled")
}
if cfg.FeatureOffchainReporting2() {
globalLogger.Debug("Off-chain reporting v2 enabled")
// master/delegate relay is started once, on app start, as root subservice
relay := relay.NewDelegate(keyStore)
if cfg.EVMEnabled() {
evmRelayer := evmrelay.NewRelayer(db, chains.EVM, globalLogger.Named("EVM"))
relay.AddRelayer(relaytypes.EVM, evmRelayer)
}
if cfg.SolanaEnabled() {
solanaRelayer := solana.NewRelayer(globalLogger.Named("Solana.Relayer"))
solanaRelayerCtx := solanaRelayer
relay.AddRelayer(relaytypes.Solana, solanaRelayerCtx)
}
if cfg.TerraEnabled() {
terraRelayer := pkgterra.NewRelayer(globalLogger.Named("Terra.Relayer"), chains.Terra)
terraRelayerCtx := terraRelayer
relay.AddRelayer(relaytypes.Terra, terraRelayerCtx)
}
subservices = append(subservices, relay)
delegates[job.OffchainReporting2] = ocr2.NewDelegate(
db,
jobORM,
pipelineRunner,
peerWrapper,
monitoringEndpointGen,
chains.EVM,
globalLogger,
cfg,
keyStore.OCR2(),
relay,
)
delegates[job.Bootstrap] = ocrbootstrap.NewDelegateBootstrap(
db,
jobORM,
peerWrapper,
globalLogger,
cfg,
relay,
)
} else {
globalLogger.Debug("Off-chain reporting v2 disabled")
}
var lbs []utils.DependentAwaiter
for _, c := range chains.EVM.Chains() {
lbs = append(lbs, c.LogBroadcaster())
}
jobSpawner := job.NewSpawner(jobORM, cfg, delegates, db, globalLogger, lbs)
subservices = append(subservices, jobSpawner, pipelineRunner)
// TODO: Make feeds manager compatible with multiple chains
// See: https://app.clubhouse.io/chainlinklabs/story/14615/add-ability-to-set-chain-id-in-all-pipeline-tasks-that-interact-with-evm
var feedsService feeds.Service
if cfg.FeatureFeedsManager() {
feedsORM := feeds.NewORM(db, opts.Logger, cfg)
chain, err := chains.EVM.Default()
if err != nil {
globalLogger.Warnw("Unable to load feeds service; no default chain available", "err", err)
feedsService = &feeds.NullService{}
} else {
feedsService = feeds.NewService(feedsORM, jobORM, db, jobSpawner, keyStore, chain.Config(), chains.EVM, globalLogger, opts.Version)
}
} else {
feedsService = &feeds.NullService{}
}
app := &ChainlinkApplication{
Chains: chains,
EventBroadcaster: eventBroadcaster,
jobORM: jobORM,
jobSpawner: jobSpawner,
pipelineRunner: pipelineRunner,
pipelineORM: pipelineORM,
bridgeORM: bridgeORM,
sessionORM: sessionORM,
txmORM: txmORM,
FeedsService: feedsService,
Config: cfg,
webhookJobRunner: webhookJobRunner,
KeyStore: keyStore,
SessionReaper: sessions.NewSessionReaper(db.DB, cfg, globalLogger),
ExternalInitiatorManager: externalInitiatorManager,
explorerClient: explorerClient,
HealthChecker: healthChecker,
Nurse: nurse,
logger: globalLogger,
closeLogger: opts.CloseLogger,
sqlxDB: opts.SqlxDB,
// NOTE: Can keep things clean by putting more things in subservices
// instead of manually start/closing
subservices: subservices,
}
for _, service := range app.subservices {
checkable := service.(services.Checkable)
if err := app.HealthChecker.Register(reflect.TypeOf(service).String(), checkable); err != nil {
return nil, err
}
}
return app, nil
}
func (app *ChainlinkApplication) SetLogLevel(lvl zapcore.Level) error {
if err := app.Config.SetLogLevel(lvl); err != nil {
return err
}
app.logger.SetLogLevel(lvl)
return nil
}
// SetServiceLogLevel sets the Logger level for a given service and stores the setting in the db.
func (app *ChainlinkApplication) SetServiceLogLevel(ctx context.Context, serviceName string, level zapcore.Level) error {
// TODO: Implement other service loggers
switch serviceName {
case logger.HeadTracker:
for _, c := range app.Chains.EVM.Chains() {
c.HeadTracker().SetLogLevel(level)
}
case logger.FluxMonitor:
// TODO: Set FMv2?
case logger.Keeper:
default:
return fmt.Errorf("no service found with name: %s", serviceName)
}
return logger.NewORM(app.GetSqlxDB(), app.GetLogger()).SetServiceLogLevel(ctx, serviceName, level.String())
}
// Start all necessary services. If successful, nil will be returned.
// Start sequence is aborted if the context gets cancelled.
func (app *ChainlinkApplication) Start(ctx context.Context) error {
app.startStopMu.Lock()
defer app.startStopMu.Unlock()
if app.started {
panic("application is already started")
}
if app.FeedsService != nil {
if err := app.FeedsService.Start(); err != nil {
app.logger.Infof("[Feeds Service] %v", err)
}
}
for _, subservice := range app.subservices {
if ctx.Err() != nil {
return errors.Wrap(ctx.Err(), "aborting start")
}
app.logger.Debugw("Starting service...", "serviceType", reflect.TypeOf(subservice))
if err := subservice.Start(ctx); err != nil {
return err
}
}
// Start HealthChecker last, so that the other services had the chance to
// start enough to immediately pass the readiness check.
if err := app.HealthChecker.Start(); err != nil {
return err
}
app.started = true
return nil
}
func (app *ChainlinkApplication) StopIfStarted() error {
app.startStopMu.Lock()
defer app.startStopMu.Unlock()
if app.started {
return app.stop()
}
return nil
}
// Stop allows the application to exit by halting schedules, closing
// logs, and closing the DB connection.
func (app *ChainlinkApplication) Stop() error {
app.startStopMu.Lock()
defer app.startStopMu.Unlock()
return app.stop()
}
func (app *ChainlinkApplication) stop() (err error) {
if !app.started {
panic("application is already stopped")
}
app.shutdownOnce.Do(func() {
defer func() {
if lerr := app.closeLogger(); lerr != nil {
err = multierr.Append(err, lerr)
}
}()
app.logger.Info("Gracefully exiting...")
// Stop services in the reverse order from which they were started
for i := len(app.subservices) - 1; i >= 0; i-- {
service := app.subservices[i]
app.logger.Debugw("Closing service...", "serviceType", reflect.TypeOf(service))
err = multierr.Append(err, service.Close())
}
app.logger.Debug("Stopping SessionReaper...")
err = multierr.Append(err, app.SessionReaper.Stop())
app.logger.Debug("Closing HealthChecker...")
err = multierr.Append(err, app.HealthChecker.Close())
if app.FeedsService != nil {
app.logger.Debug("Closing Feeds Service...")
err = multierr.Append(err, app.FeedsService.Close())
}
if app.Nurse != nil {
err = multierr.Append(err, app.Nurse.Close())
}
app.logger.Info("Exited all services")
app.started = false
})
return err
}
func (app *ChainlinkApplication) GetConfig() config.GeneralConfig {
return app.Config
}
func (app *ChainlinkApplication) GetKeyStore() keystore.Master {
return app.KeyStore
}
func (app *ChainlinkApplication) GetLogger() logger.Logger {
return app.logger
}
func (app *ChainlinkApplication) GetHealthChecker() services.Checker {
return app.HealthChecker
}
func (app *ChainlinkApplication) JobSpawner() job.Spawner {
return app.jobSpawner
}
func (app *ChainlinkApplication) JobORM() job.ORM {
return app.jobORM
}
func (app *ChainlinkApplication) BridgeORM() bridges.ORM {
return app.bridgeORM
}
func (app *ChainlinkApplication) SessionORM() sessions.ORM {
return app.sessionORM
}
func (app *ChainlinkApplication) EVMORM() evmtypes.ORM {
return app.Chains.EVM.ORM()
}
func (app *ChainlinkApplication) PipelineORM() pipeline.ORM {
return app.pipelineORM
}
func (app *ChainlinkApplication) TxmORM() txmgr.ORM {
return app.txmORM
}
func (app *ChainlinkApplication) GetExternalInitiatorManager() webhook.ExternalInitiatorManager {
return app.ExternalInitiatorManager
}
// WakeSessionReaper wakes up the reaper to do its reaping.
func (app *ChainlinkApplication) WakeSessionReaper() {
app.SessionReaper.WakeUp()
}
func (app *ChainlinkApplication) AddJobV2(ctx context.Context, j *job.Job) error {
return app.jobSpawner.CreateJob(j, pg.WithParentCtx(ctx))
}
func (app *ChainlinkApplication) DeleteJob(ctx context.Context, jobID int32) error {
// Do not allow the job to be deleted if it is managed by the Feeds Manager
isManaged, err := app.FeedsService.IsJobManaged(ctx, int64(jobID))
if err != nil {
return err
}
if isManaged {
return errors.New("job must be deleted in the feeds manager")
}
return app.jobSpawner.DeleteJob(jobID, pg.WithParentCtx(ctx))
}
func (app *ChainlinkApplication) RunWebhookJobV2(ctx context.Context, jobUUID uuid.UUID, requestBody string, meta pipeline.JSONSerializable) (int64, error) {
return app.webhookJobRunner.RunJob(ctx, jobUUID, requestBody, meta)
}
// Only used for local testing, not supported by the UI.
func (app *ChainlinkApplication) RunJobV2(
ctx context.Context,
jobID int32,
meta map[string]interface{},
) (int64, error) {
if !app.GetConfig().Dev() {
return 0, errors.New("manual job runs only supported in dev mode - export CHAINLINK_DEV=true to use")
}
jb, err := app.jobORM.FindJob(ctx, jobID)
if err != nil {
return 0, errors.Wrapf(err, "job ID %v", jobID)
}
var runID int64
// Some jobs are special in that they do not have a task graph.
isBootstrap := jb.Type == job.OffchainReporting && jb.OCROracleSpec != nil && jb.OCROracleSpec.IsBootstrapPeer
if jb.Type.RequiresPipelineSpec() || !isBootstrap {
var vars map[string]interface{}
var saveTasks bool
if jb.Type == job.VRF {
saveTasks = true
// Create a dummy log to trigger a run
testLog := types.Log{
Data: bytes.Join([][]byte{
jb.VRFSpec.PublicKey.MustHash().Bytes(), // key hash
common.BigToHash(big.NewInt(42)).Bytes(), // seed
utils.NewHash().Bytes(), // sender
utils.NewHash().Bytes(), // fee
utils.NewHash().Bytes()}, // requestID
[]byte{}),
Topics: []common.Hash{{}, jb.ExternalIDEncodeBytesToTopic()}, // jobID BYTES
TxHash: utils.NewHash(),
BlockNumber: 10,
BlockHash: utils.NewHash(),
}
vars = map[string]interface{}{
"jobSpec": map[string]interface{}{
"databaseID": jb.ID,
"externalJobID": jb.ExternalJobID,
"name": jb.Name.ValueOrZero(),
"publicKey": jb.VRFSpec.PublicKey[:],
},
"jobRun": map[string]interface{}{
"meta": meta,
"logBlockHash": testLog.BlockHash[:],
"logBlockNumber": testLog.BlockNumber,
"logTxHash": testLog.TxHash,
"logTopics": testLog.Topics,
"logData": testLog.Data,
},
}
} else {
vars = map[string]interface{}{
"jobRun": map[string]interface{}{
"meta": meta,
},
}
}
runID, _, err = app.pipelineRunner.ExecuteAndInsertFinishedRun(ctx, *jb.PipelineSpec, pipeline.NewVarsFrom(vars), app.logger, saveTasks)
}
return runID, err
}
func (app *ChainlinkApplication) ResumeJobV2(
ctx context.Context,
taskID uuid.UUID,
result pipeline.Result,
) error {
return app.pipelineRunner.ResumeRun(taskID, result.Value, result.Error)
}
func (app *ChainlinkApplication) GetFeedsService() feeds.Service {
return app.FeedsService
}
// ReplayFromBlock implements the Application interface.
func (app *ChainlinkApplication) ReplayFromBlock(chainID *big.Int, number uint64, forceBroadcast bool) error {
chain, err := app.Chains.EVM.Get(chainID)
if err != nil {
return err
}
chain.LogBroadcaster().ReplayFromBlock(int64(number), forceBroadcast)
return nil
}
// GetChains returns Chains.
func (app *ChainlinkApplication) GetChains() Chains {
return app.Chains
}
func (app *ChainlinkApplication) GetEventBroadcaster() pg.EventBroadcaster {
return app.EventBroadcaster
}
func (app *ChainlinkApplication) GetSqlxDB() *sqlx.DB {
return app.sqlxDB
}
// Returns the configuration to use for creating and authenticating
// new WebAuthn credentials
func (app *ChainlinkApplication) GetWebAuthnConfiguration() sessions.WebAuthnConfiguration {
rpid := app.Config.RPID()
rporigin := app.Config.RPOrigin()
if rpid == "" {
app.GetLogger().Errorf("RPID is not set, WebAuthn will likely not work as intended")
}
if rporigin == "" {
app.GetLogger().Errorf("RPOrigin is not set, WebAuthn will likely not work as intended")
}
return sessions.WebAuthnConfiguration{
RPID: rpid,
RPOrigin: rporigin,
}
}
func (app *ChainlinkApplication) ID() uuid.UUID {
return app.Config.AppID()
}