Repository navigation
Expand file tree
/
Copy pathmanaged_mercury_oracle.go
More file actions
228 lines (207 loc) · 7.53 KB
/
Copy pathmanaged_mercury_oracle.go
File metadata and controls
228 lines (207 loc) · 7.53 KB
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
package managed
import (
"context"
"fmt"
"github.com/smartcontractkit/libocr/commontypes"
"github.com/smartcontractkit/libocr/internal/loghelper"
"github.com/smartcontractkit/libocr/offchainreporting2plus/internal/config/ocr3config"
"github.com/smartcontractkit/libocr/offchainreporting2plus/internal/managed/limits"
"github.com/smartcontractkit/libocr/offchainreporting2plus/internal/mercuryshim"
"github.com/smartcontractkit/libocr/offchainreporting2plus/internal/ocr3/protocol"
"github.com/smartcontractkit/libocr/offchainreporting2plus/internal/ocr3/serialization"
"github.com/smartcontractkit/libocr/offchainreporting2plus/internal/shim"
"github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3types"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/libocr/subprocesses"
"go.uber.org/multierr"
)
// RunManagedMercuryOracle runs a "managed" version of protocol.RunOracle. It handles
// setting up telemetry, garbage collection, configuration updates, translating
// from commontypes.BinaryNetworkEndpoint to protocol.NetworkEndpoint, and
// creation/teardown of reporting plugins.
func RunManagedMercuryOracle(
ctx context.Context,
v2bootstrappers []commontypes.BootstrapperLocator,
configTracker types.ContractConfigTracker,
contractTransmitter types.ContractTransmitter,
database ocr3types.Database,
localConfig types.LocalConfig,
logger loghelper.LoggerWithContext,
monitoringEndpoint commontypes.MonitoringEndpoint,
netEndpointFactory types.BinaryNetworkEndpointFactory,
offchainConfigDigester types.OffchainConfigDigester,
offchainKeyring types.OffchainKeyring,
onchainKeyring types.OnchainKeyring,
mercuryPluginFactory ocr3types.MercuryPluginFactory,
) {
subs := subprocesses.Subprocesses{}
defer subs.Wait()
var chTelemetrySend chan<- *serialization.TelemetryWrapper
{
chTelemetry := make(chan *serialization.TelemetryWrapper, 100)
chTelemetrySend = chTelemetry
subs.Go(func() {
forwardTelemetry(ctx, logger, monitoringEndpoint, chTelemetry)
})
}
runWithContractConfig(
ctx,
configTracker,
database,
func(ctx context.Context, contractConfig types.ContractConfig, logger loghelper.LoggerWithContext) {
skipResourceExhaustionChecks := localConfig.DevelopmentMode == types.EnableDangerousDevelopmentMode
fromAccount, err := contractTransmitter.FromAccount()
if err != nil {
logger.Error("ManagedMercuryOracle: error getting FromAccount", commontypes.LogFields{
"error": err,
})
return
}
ocr3OnchainKeyring := mercuryshim.NewMercuryOCR3OnchainKeyring(onchainKeyring)
sharedConfig, oid, err := ocr3config.SharedConfigFromContractConfig[mercuryshim.MercuryReportInfo](
skipResourceExhaustionChecks,
contractConfig,
offchainKeyring,
ocr3OnchainKeyring,
netEndpointFactory.PeerID(),
fromAccount,
)
if err != nil {
logger.Error("ManagedMercuryOracle: error while updating config", commontypes.LogFields{
"error": err,
})
return
}
// Run with new config
peerIDs := []string{}
for _, identity := range sharedConfig.OracleIdentities {
peerIDs = append(peerIDs, identity.PeerID)
}
childLogger := logger.MakeChild(commontypes.LogFields{
"oid": oid,
})
mercuryPlugin, mercuryPluginInfo, err := mercuryPluginFactory.NewMercuryPlugin(ocr3types.MercuryPluginConfig{
sharedConfig.ConfigDigest,
oid,
sharedConfig.N(),
sharedConfig.F,
sharedConfig.OnchainConfig,
sharedConfig.ReportingPluginConfig,
sharedConfig.MinRoundInterval(),
sharedConfig.MaxDurationObservation,
})
if err != nil {
logger.Error("ManagedMercuryOracle: error during NewReportingPlugin()", commontypes.LogFields{
"error": err,
})
return
}
defer loghelper.CloseLogError(
mercuryPlugin,
logger,
"ManagedMercuryOracle: error during reportingPlugin.Close()",
)
if err := validateMercuryPluginLimits(mercuryPluginInfo.Limits); err != nil {
logger.Error("ManagedMercuryOracle: invalid MercuryPluginInfo", commontypes.LogFields{
"error": err,
"mercuryPluginInfo": mercuryPluginInfo,
})
return
}
reportingPluginLimits := mercuryshim.ReportingPluginLimits(mercuryPluginInfo.Limits)
lims, err := limits.OCR3Limits(sharedConfig.PublicConfig, reportingPluginLimits, ocr3OnchainKeyring.MaxSignatureLength())
if err != nil {
logger.Error("ManagedMercuryOracle: error during limits", commontypes.LogFields{
"error": err,
"publicConfig": sharedConfig.PublicConfig,
"reportingPluginLimits": reportingPluginLimits,
"maxSigLen": ocr3OnchainKeyring.MaxSignatureLength(),
})
return
}
binNetEndpoint, err := netEndpointFactory.NewEndpoint(
sharedConfig.ConfigDigest,
peerIDs,
v2bootstrappers,
sharedConfig.F,
lims,
)
if err != nil {
logger.Error("ManagedMercuryOracle: error during NewEndpoint", commontypes.LogFields{
"error": err,
"peerIDs": peerIDs,
"v2bootstrappers": v2bootstrappers,
})
return
}
// No need to binNetEndpoint.Start/Close since netEndpoint will handle that for us
netEndpoint := shim.NewOCR3SerializingEndpoint[mercuryshim.MercuryReportInfo](
chTelemetrySend,
sharedConfig.ConfigDigest,
binNetEndpoint,
ocr3OnchainKeyring.MaxSignatureLength(),
childLogger,
reportingPluginLimits,
sharedConfig.N(),
sharedConfig.F,
)
if err := netEndpoint.Start(); err != nil {
logger.Error("ManagedMercuryOracle: error during netEndpoint.Start()", commontypes.LogFields{
"error": err,
"configDigest": sharedConfig.ConfigDigest,
})
return
}
defer loghelper.CloseLogError(
netEndpoint,
logger,
"ManagedMercuryOracle: error during netEndpoint.Close()",
)
reportingPluginConfig := ocr3types.ReportingPluginConfig{
sharedConfig.ConfigDigest,
oid,
sharedConfig.N(),
sharedConfig.F,
sharedConfig.OnchainConfig,
sharedConfig.ReportingPluginConfig,
sharedConfig.DeltaRound,
sharedConfig.MaxDurationQuery,
sharedConfig.MaxDurationObservation,
sharedConfig.MaxDurationShouldAcceptAttestedReport,
sharedConfig.MaxDurationShouldTransmitAcceptedReport,
}
reportingPlugin := &mercuryshim.MercuryReportingPlugin{
reportingPluginConfig,
mercuryPlugin,
mercuryPluginInfo.Limits,
}
protocol.RunOracle[mercuryshim.MercuryReportInfo](
ctx,
sharedConfig,
mercuryshim.NewMercuryOCR3ContractTransmitter(contractTransmitter),
&shim.SerializingOCR3Database{database},
oid,
localConfig,
childLogger,
netEndpoint,
offchainKeyring,
ocr3OnchainKeyring,
shim.LimitCheckOCR3ReportingPlugin[mercuryshim.MercuryReportInfo]{reportingPlugin, reportingPluginLimits},
shim.MakeOCR3TelemetrySender(chTelemetrySend, childLogger),
)
},
localConfig,
logger,
offchainConfigDigester,
)
}
func validateMercuryPluginLimits(limits ocr3types.MercuryPluginLimits) error {
var err error
if !(0 <= limits.MaxObservationLength && limits.MaxObservationLength <= ocr3types.MaxMaxMercuryObservationLength) {
err = multierr.Append(err, fmt.Errorf("MaxObservationLength (%v) out of range. Should be between 0 and %v", limits.MaxObservationLength, ocr3types.MaxMaxMercuryObservationLength))
}
if !(0 <= limits.MaxReportLength && limits.MaxReportLength <= ocr3types.MaxMaxMercuryReportLength) {
err = multierr.Append(err, fmt.Errorf("MaxReportLength (%v) out of range. Should be between 0 and %v", limits.MaxReportLength, ocr3types.MaxMaxMercuryReportLength))
}
return err
}