-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
delegate.go
194 lines (174 loc) · 6.26 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
package ocrbootstrap
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
ocr "github.com/smartcontractkit/libocr/offchainreporting2plus"
"github.com/smartcontractkit/chainlink-common/pkg/loop"
"github.com/smartcontractkit/chainlink-common/pkg/sqlutil"
"github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/validate"
"github.com/smartcontractkit/chainlink/v2/core/services/ocrcommon"
)
type RelayGetter interface {
Get(types.RelayID) (loop.Relayer, error)
GetIDToRelayerMap() (map[types.RelayID]loop.Relayer, error)
}
// Delegate creates Bootstrap jobs
type Delegate struct {
ds sqlutil.DataSource
jobORM job.ORM
peerWrapper *ocrcommon.SingletonPeerWrapper
ocr2Cfg validate.OCR2Config
insecureCfg validate.InsecureConfig
lggr logger.SugaredLogger
RelayGetter
isNewlyCreatedJob bool
}
type relayConfig struct {
// providerType used for determining which type of contract to track config on
ProviderType string `json:"providerType"`
// Extra fields to enable router proxy contract support. Must match field names of functions' PluginConfig.
DONID string `json:"donID"`
ContractVersion uint32 `json:"contractVersion"`
ContractUpdateCheckFrequencySec uint32 `json:"contractUpdateCheckFrequencySec"`
}
// NewDelegateBootstrap creates a new Delegate
func NewDelegateBootstrap(
ds sqlutil.DataSource,
jobORM job.ORM,
peerWrapper *ocrcommon.SingletonPeerWrapper,
lggr logger.Logger,
ocr2Cfg validate.OCR2Config,
insecureCfg validate.InsecureConfig,
relayers RelayGetter,
) *Delegate {
return &Delegate{
ds: ds,
jobORM: jobORM,
peerWrapper: peerWrapper,
lggr: logger.Sugared(lggr),
ocr2Cfg: ocr2Cfg,
insecureCfg: insecureCfg,
RelayGetter: relayers,
}
}
// JobType satisfies the job.Delegate interface.
func (d *Delegate) JobType() job.Type {
return job.Bootstrap
}
func (d *Delegate) BeforeJobCreated(spec job.Job) {
d.isNewlyCreatedJob = true
}
// ServicesForSpec satisfies the job.Delegate interface.
func (d *Delegate) ServicesForSpec(ctx context.Context, jb job.Job) (services []job.ServiceCtx, err error) {
spec := jb.BootstrapSpec
if spec == nil {
return nil, errors.Errorf("Bootstrap.Delegate expects an *job.BootstrapSpec to be present, got %v", jb)
}
if d.peerWrapper == nil {
return nil, errors.New("cannot setup OCR2 job service, libp2p peer was missing")
} else if !d.peerWrapper.IsStarted() {
return nil, errors.New("peerWrapper is not started. OCR2 jobs require a started and running p2p v2 peer")
}
s := spec.AsOCR2Spec()
rid, err := s.RelayID()
if err != nil {
return nil, fmt.Errorf("ServicesForSpec: could not get relayer: %w", err)
}
relayer, err := d.RelayGetter.Get(rid)
if err != nil {
return nil, fmt.Errorf("ServiceForSpec: failed to get relay %s is it enabled?: %w", rid.Name(), err)
}
if spec.FeedID != nil {
spec.RelayConfig["feedID"] = *spec.FeedID
}
spec.RelayConfig.ApplyDefaultsOCR2(d.ocr2Cfg)
ctxVals := loop.ContextValues{
JobID: jb.ID,
JobName: jb.Name.ValueOrZero(),
ContractID: spec.ContractID,
FeedID: spec.FeedID,
}
ctx = ctxVals.ContextWithValues(ctx)
var relayCfg relayConfig
if err = json.Unmarshal(spec.RelayConfig.Bytes(), &relayCfg); err != nil {
return nil, err
}
var configProvider types.ConfigProvider
if relayCfg.DONID != "" {
if relayCfg.ContractVersion != 1 || relayCfg.ContractUpdateCheckFrequencySec == 0 {
return nil, errors.New("invalid router contract config")
}
configProvider, err = relayer.NewPluginProvider(
ctx,
types.RelayArgs{
ExternalJobID: jb.ExternalJobID,
JobID: jb.ID,
ContractID: spec.ContractID,
RelayConfig: spec.RelayConfig.Bytes(),
New: d.isNewlyCreatedJob,
ProviderType: string(types.Functions),
},
types.PluginArgs{
PluginConfig: spec.RelayConfig.Bytes(), // contains all necessary fields for config provider
},
)
} else {
configProvider, err = relayer.NewConfigProvider(ctx, types.RelayArgs{
ExternalJobID: jb.ExternalJobID,
JobID: jb.ID,
ContractID: spec.ContractID,
New: d.isNewlyCreatedJob,
RelayConfig: spec.RelayConfig.Bytes(),
ProviderType: relayCfg.ProviderType,
})
}
if err != nil {
return nil, errors.Wrap(err, "error calling 'relayer.NewConfigWatcher'")
}
lc, err := validate.ToLocalConfig(d.ocr2Cfg, d.insecureCfg, spec.AsOCR2Spec())
if err != nil {
return nil, err
}
if err = ocr.SanityCheckLocalConfig(lc); err != nil {
return nil, err
}
lggr := d.lggr.With(ctxVals.Args()...)
lggr.Infow("OCR2 job using local config",
"BlockchainTimeout", lc.BlockchainTimeout,
"ContractConfigConfirmations", lc.ContractConfigConfirmations,
"ContractConfigTrackerPollInterval", lc.ContractConfigTrackerPollInterval,
"ContractTransmitterTransmitTimeout", lc.ContractTransmitterTransmitTimeout,
"DatabaseTimeout", lc.DatabaseTimeout,
)
ocrLogger := ocrcommon.NewOCRWrapper(lggr.Named("OCRBootstrap"), d.ocr2Cfg.TraceLogging(), func(ctx context.Context, msg string) {
logger.Sugared(lggr).ErrorIf(d.jobORM.RecordError(ctx, jb.ID, msg), "unable to record error")
})
bootstrapNodeArgs := ocr.BootstrapperArgs{
BootstrapperFactory: d.peerWrapper.Peer2,
ContractConfigTracker: configProvider.ContractConfigTracker(),
Database: NewDB(d.ds, spec.ID, lggr),
LocalConfig: lc,
Logger: ocrLogger,
OffchainConfigDigester: configProvider.OffchainConfigDigester(),
}
lggr.Debugw("Launching new bootstrap node", "args", bootstrapNodeArgs)
bootstrapper, err := ocr.NewBootstrapper(bootstrapNodeArgs)
if err != nil {
return nil, errors.Wrap(err, "error calling NewBootstrapNode")
}
return []job.ServiceCtx{configProvider, ocrLogger, job.NewServiceAdapter(bootstrapper)}, nil
}
// AfterJobCreated satisfies the job.Delegate interface.
func (d *Delegate) AfterJobCreated(spec job.Job) {
}
// BeforeJobDeleted satisfies the job.Delegate interface.
func (d *Delegate) BeforeJobDeleted(spec job.Job) {}
// OnDeleteJob satisfies the job.Delegate interface.
func (d *Delegate) OnDeleteJob(context.Context, job.Job) error {
return nil
}