-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
delegate.go
127 lines (105 loc) · 4.29 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
package llo
import (
"context"
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
ocrcommontypes "github.com/smartcontractkit/libocr/commontypes"
ocr2plus "github.com/smartcontractkit/libocr/offchainreporting2plus"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
ocr2types "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"gopkg.in/guregu/null.v4"
"github.com/smartcontractkit/chainlink-common/pkg/services"
llotypes "github.com/smartcontractkit/chainlink-common/pkg/types/llo"
"github.com/smartcontractkit/chainlink-data-streams/llo"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/llo/evm"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
"github.com/smartcontractkit/chainlink/v2/core/services/streams"
)
var _ job.ServiceCtx = &delegate{}
type Closer interface {
Close() error
}
type delegate struct {
services.StateMachine
cfg DelegateConfig
codecs map[llotypes.ReportFormat]llo.ReportCodec
prrc llo.PredecessorRetirementReportCache
src llo.ShouldRetireCache
ds llo.DataSource
oracle Closer
}
type DelegateConfig struct {
Logger logger.Logger
Queryer pg.Queryer
Runner streams.Runner
Registry Registry
JobName null.String
// LLO
ChannelDefinitionCache llotypes.ChannelDefinitionCache
// OCR3
BinaryNetworkEndpointFactory ocr2types.BinaryNetworkEndpointFactory
V2Bootstrappers []ocrcommontypes.BootstrapperLocator
ContractConfigTracker ocr2types.ContractConfigTracker
ContractTransmitter ocr3types.ContractTransmitter[llotypes.ReportInfo]
Database ocr3types.Database
OCRLogger ocrcommontypes.Logger
MonitoringEndpoint ocrcommontypes.MonitoringEndpoint
OffchainConfigDigester ocr2types.OffchainConfigDigester
OffchainKeyring ocr2types.OffchainKeyring
OnchainKeyring ocr3types.OnchainKeyring[llotypes.ReportInfo]
LocalConfig ocr2types.LocalConfig
}
func NewDelegate(cfg DelegateConfig) (job.ServiceCtx, error) {
if cfg.Queryer == nil {
return nil, errors.New("Queryer must not be nil")
}
if cfg.Runner == nil {
return nil, errors.New("Runner must not be nil")
}
if cfg.Registry == nil {
return nil, errors.New("Registry must not be nil")
}
codecs := make(map[llotypes.ReportFormat]llo.ReportCodec)
// NOTE: All codecs must be specified here
codecs[llotypes.ReportFormatJSON] = llo.JSONReportCodec{}
codecs[llotypes.ReportFormatEVM] = evm.ReportCodec{}
// TODO: Do these services need starting?
// https://smartcontract-it.atlassian.net/browse/MERC-3386
prrc := llo.NewPredecessorRetirementReportCache()
src := llo.NewShouldRetireCache()
ds := newDataSource(cfg.Logger.Named("DataSource"), cfg.Registry)
return &delegate{services.StateMachine{}, cfg, codecs, prrc, src, ds, nil}, nil
}
func (d *delegate) Start(ctx context.Context) error {
return d.StartOnce("LLODelegate", func() error {
// create the oracle from config values
oracle, err := ocr2plus.NewOracle(ocr2plus.OCR3OracleArgs[llotypes.ReportInfo]{
BinaryNetworkEndpointFactory: d.cfg.BinaryNetworkEndpointFactory,
V2Bootstrappers: d.cfg.V2Bootstrappers,
ContractConfigTracker: d.cfg.ContractConfigTracker,
ContractTransmitter: d.cfg.ContractTransmitter,
Database: d.cfg.Database,
LocalConfig: d.cfg.LocalConfig,
Logger: d.cfg.OCRLogger,
MonitoringEndpoint: d.cfg.MonitoringEndpoint,
OffchainConfigDigester: d.cfg.OffchainConfigDigester,
OffchainKeyring: d.cfg.OffchainKeyring,
OnchainKeyring: d.cfg.OnchainKeyring,
ReportingPluginFactory: llo.NewPluginFactory(
d.prrc, d.src, d.cfg.ChannelDefinitionCache, d.ds, d.cfg.Logger.Named("LLOReportingPlugin"), d.codecs,
),
MetricsRegisterer: prometheus.WrapRegistererWith(map[string]string{"job_name": d.cfg.JobName.ValueOrZero()}, prometheus.DefaultRegisterer),
})
if err != nil {
return fmt.Errorf("%w: failed to create new OCR oracle", err)
}
d.oracle = oracle
return oracle.Start()
})
}
func (d *delegate) Close() error {
return d.StopOnce("LLODelegate", d.oracle.Close)
}