/
ocr2_helpers.go
506 lines (468 loc) · 18 KB
/
ocr2_helpers.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
package actions
import (
"crypto/ed25519"
"encoding/hex"
"fmt"
"math/big"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
"gopkg.in/guregu/null.v4"
"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median"
"github.com/smartcontractkit/libocr/offchainreporting2plus/confighelper"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"github.com/smartcontractkit/chainlink-testing-framework/blockchain"
ctfClient "github.com/smartcontractkit/chainlink-testing-framework/client"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/chaintype"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/testhelpers"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/integration-tests/client"
"github.com/smartcontractkit/chainlink/integration-tests/contracts"
)
// DeployOCRv2Contracts deploys a number of OCRv2 contracts and configures them with defaults
// Deprecated: we are moving away from blockchain.EVMClient, use actions_seth.DeployOCRv2Contracts
func DeployOCRv2Contracts(
numberOfContracts int,
linkTokenContract contracts.LinkToken,
contractDeployer contracts.ContractDeployer,
transmitters []string,
client blockchain.EVMClient,
ocrOptions contracts.OffchainOptions,
) ([]contracts.OffchainAggregatorV2, error) {
var ocrInstances []contracts.OffchainAggregatorV2
for contractCount := 0; contractCount < numberOfContracts; contractCount++ {
ocrInstance, err := contractDeployer.DeployOffchainAggregatorV2(
linkTokenContract.Address(),
ocrOptions,
)
if err != nil {
return nil, fmt.Errorf("OCRv2 instance deployment have failed: %w", err)
}
ocrInstances = append(ocrInstances, ocrInstance)
if (contractCount+1)%ContractDeploymentInterval == 0 { // For large amounts of contract deployments, space things out some
err = client.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("failed to wait for OCRv2 contract deployments: %w", err)
}
}
}
err := client.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("error waiting for OCRv2 contract deployments: %w", err)
}
// Gather address payees
var payees []string
for range transmitters {
payees = append(payees, client.GetDefaultWallet().Address())
}
// Set Payees
for contractCount, ocrInstance := range ocrInstances {
err = ocrInstance.SetPayees(transmitters, payees)
if err != nil {
return nil, fmt.Errorf("error settings OCR payees: %w", err)
}
if (contractCount+1)%ContractDeploymentInterval == 0 { // For large amounts of contract deployments, space things out some
err = client.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("failed to wait for setting OCR payees: %w", err)
}
}
}
return ocrInstances, client.WaitForEvents()
}
// Deprecated: we are moving away from blockchain.EVMClient, use actions_seth.ConfigureOCRv2AggregatorContracts
func ConfigureOCRv2AggregatorContracts(
client blockchain.EVMClient,
contractConfig *contracts.OCRv2Config,
ocrv2Contracts []contracts.OffchainAggregatorV2,
) error {
for contractCount, ocrInstance := range ocrv2Contracts {
// Exclude the first node, which will be used as a bootstrapper
err := ocrInstance.SetConfig(contractConfig)
if err != nil {
return fmt.Errorf("error setting OCR config for contract '%s': %w", ocrInstance.Address(), err)
}
if (contractCount+1)%ContractDeploymentInterval == 0 { // For large amounts of contract deployments, space things out some
err = client.WaitForEvents()
if err != nil {
return fmt.Errorf("failed to wait for setting OCR config: %w", err)
}
}
}
return client.WaitForEvents()
}
// BuildMedianOCR2Config builds a default OCRv2 config for the given chainlink nodes for a standard median aggregation job
func BuildMedianOCR2Config(
workerNodes []*client.ChainlinkK8sClient,
ocrOffchainOptions contracts.OffchainOptions,
) (*contracts.OCRv2Config, error) {
S, oracleIdentities, err := GetOracleIdentities(workerNodes)
if err != nil {
return nil, err
}
signerKeys, transmitterAccounts, f_, _, offchainConfigVersion, offchainConfig, err := confighelper.ContractSetConfigArgsForTests(
30*time.Second, // deltaProgress time.Duration,
30*time.Second, // deltaResend time.Duration,
10*time.Second, // deltaRound time.Duration,
20*time.Second, // deltaGrace time.Duration,
20*time.Second, // deltaStage time.Duration,
3, // rMax uint8,
S, // s []int,
oracleIdentities, // oracles []OracleIdentityExtra,
median.OffchainConfig{
AlphaReportInfinite: false,
AlphaReportPPB: 1,
AlphaAcceptInfinite: false,
AlphaAcceptPPB: 1,
DeltaC: time.Minute * 30,
}.Encode(), // reportingPluginConfig []byte,
5*time.Second, // maxDurationQuery time.Duration,
5*time.Second, // maxDurationObservation time.Duration,
5*time.Second, // maxDurationReport time.Duration,
5*time.Second, // maxDurationShouldAcceptFinalizedReport time.Duration,
5*time.Second, // maxDurationShouldTransmitAcceptedReport time.Duration,
1, // f int,
nil, // The median reporting plugin has an empty onchain config
)
if err != nil {
return nil, err
}
// Convert signers to addresses
var signerAddresses []common.Address
for _, signer := range signerKeys {
signerAddresses = append(signerAddresses, common.BytesToAddress(signer))
}
// Convert transmitters to addresses
var transmitterAddresses []common.Address
for _, account := range transmitterAccounts {
transmitterAddresses = append(transmitterAddresses, common.HexToAddress(string(account)))
}
onchainConfig, err := testhelpers.GenerateDefaultOCR2OnchainConfig(ocrOffchainOptions.MinimumAnswer, ocrOffchainOptions.MaximumAnswer)
return &contracts.OCRv2Config{
Signers: signerAddresses,
Transmitters: transmitterAddresses,
F: f_,
OnchainConfig: onchainConfig,
OffchainConfigVersion: offchainConfigVersion,
OffchainConfig: []byte(fmt.Sprintf("0x%s", offchainConfig)),
}, err
}
// GetOracleIdentities retrieves all chainlink nodes' OCR2 config identities with defaul key index
func GetOracleIdentities(chainlinkNodes []*client.ChainlinkK8sClient) ([]int, []confighelper.OracleIdentityExtra, error) {
return GetOracleIdentitiesWithKeyIndex(chainlinkNodes, 0)
}
// GetOracleIdentitiesWithKeyIndex retrieves all chainlink nodes' OCR2 config identities by key index
func GetOracleIdentitiesWithKeyIndex(
chainlinkNodes []*client.ChainlinkK8sClient,
keyIndex int,
) ([]int, []confighelper.OracleIdentityExtra, error) {
S := make([]int, len(chainlinkNodes))
oracleIdentities := make([]confighelper.OracleIdentityExtra, len(chainlinkNodes))
sharedSecretEncryptionPublicKeys := make([]types.ConfigEncryptionPublicKey, len(chainlinkNodes))
eg := &errgroup.Group{}
for i, cl := range chainlinkNodes {
index, chainlinkNode := i, cl
eg.Go(func() error {
addresses, err := chainlinkNode.EthAddresses()
if err != nil {
return err
}
ocr2Keys, err := chainlinkNode.MustReadOCR2Keys()
if err != nil {
return err
}
var ocr2Config client.OCR2KeyAttributes
for _, key := range ocr2Keys.Data {
if key.Attributes.ChainType == string(chaintype.EVM) {
ocr2Config = key.Attributes
break
}
}
keys, err := chainlinkNode.MustReadP2PKeys()
if err != nil {
return err
}
p2pKeyID := keys.Data[0].Attributes.PeerID
offchainPkBytes, err := hex.DecodeString(strings.TrimPrefix(ocr2Config.OffChainPublicKey, "ocr2off_evm_"))
if err != nil {
return err
}
offchainPkBytesFixed := [ed25519.PublicKeySize]byte{}
n := copy(offchainPkBytesFixed[:], offchainPkBytes)
if n != ed25519.PublicKeySize {
return fmt.Errorf("wrong number of elements copied")
}
configPkBytes, err := hex.DecodeString(strings.TrimPrefix(ocr2Config.ConfigPublicKey, "ocr2cfg_evm_"))
if err != nil {
return err
}
configPkBytesFixed := [ed25519.PublicKeySize]byte{}
n = copy(configPkBytesFixed[:], configPkBytes)
if n != ed25519.PublicKeySize {
return fmt.Errorf("wrong number of elements copied")
}
onchainPkBytes, err := hex.DecodeString(strings.TrimPrefix(ocr2Config.OnChainPublicKey, "ocr2on_evm_"))
if err != nil {
return err
}
sharedSecretEncryptionPublicKeys[index] = configPkBytesFixed
oracleIdentities[index] = confighelper.OracleIdentityExtra{
OracleIdentity: confighelper.OracleIdentity{
OnchainPublicKey: onchainPkBytes,
OffchainPublicKey: offchainPkBytesFixed,
PeerID: p2pKeyID,
TransmitAccount: types.Account(addresses[keyIndex]),
},
ConfigEncryptionPublicKey: configPkBytesFixed,
}
S[index] = 1
log.Debug().
Interface("OnChainPK", onchainPkBytes).
Interface("OffChainPK", offchainPkBytesFixed).
Interface("ConfigPK", configPkBytesFixed).
Str("PeerID", p2pKeyID).
Str("Address", addresses[keyIndex]).
Msg("Oracle identity")
return nil
})
}
return S, oracleIdentities, eg.Wait()
}
// CreateOCRv2Jobs bootstraps the first node and to the other nodes sends ocr jobs that
// read from different adapters, to be used in combination with SetAdapterResponses
func CreateOCRv2Jobs(
ocrInstances []contracts.OffchainAggregatorV2,
bootstrapNode *client.ChainlinkK8sClient,
workerChainlinkNodes []*client.ChainlinkK8sClient,
mockserver *ctfClient.MockserverClient,
mockServerValue int, // Value to get from the mock server when querying the path
chainId int64, // EVM chain ID
forwardingAllowed bool,
) error {
// Collect P2P ID
bootstrapP2PIds, err := bootstrapNode.MustReadP2PKeys()
if err != nil {
return err
}
p2pV2Bootstrapper := fmt.Sprintf("%s@%s:%d", bootstrapP2PIds.Data[0].Attributes.PeerID, bootstrapNode.InternalIP(), 6690)
mockJuelsPath := "ocr2/juelsPerFeeCoinSource"
// Set the juelsPerFeeCoinSource config value
err = mockserver.SetValuePath(mockJuelsPath, mockServerValue)
if err != nil {
return err
}
// Create the juels bridge for each node only once
juelsBridge := &client.BridgeTypeAttributes{
Name: "juels",
URL: fmt.Sprintf("%s/%s", mockserver.Config.ClusterURL, mockJuelsPath),
}
for _, chainlinkNode := range workerChainlinkNodes {
err = chainlinkNode.MustCreateBridge(juelsBridge)
if err != nil {
return fmt.Errorf("failed creating bridge %s on CL node : %w", juelsBridge.Name, err)
}
}
for _, ocrInstance := range ocrInstances {
bootstrapSpec := &client.OCR2TaskJobSpec{
Name: fmt.Sprintf("ocr2-bootstrap-%s", ocrInstance.Address()),
JobType: "bootstrap",
OCR2OracleSpec: job.OCR2OracleSpec{
ContractID: ocrInstance.Address(),
Relay: "evm",
RelayConfig: map[string]interface{}{
"chainID": chainId,
},
MonitoringEndpoint: null.StringFrom(fmt.Sprintf("%s/%s", mockserver.Config.ClusterURL, "ocr2")),
ContractConfigTrackerPollInterval: *models.NewInterval(15 * time.Second),
},
}
_, err := bootstrapNode.MustCreateJob(bootstrapSpec)
if err != nil {
return fmt.Errorf("creating bootstrap job have failed: %w", err)
}
for _, chainlinkNode := range workerChainlinkNodes {
nodeTransmitterAddress, err := chainlinkNode.PrimaryEthAddress()
if err != nil {
return fmt.Errorf("getting primary ETH address from OCR node have failed: %w", err)
}
nodeOCRKeys, err := chainlinkNode.MustReadOCR2Keys()
if err != nil {
return fmt.Errorf("getting OCR keys from OCR node have failed: %w", err)
}
nodeOCRKeyId := nodeOCRKeys.Data[0].ID
nodeContractPairID, err := BuildOCR2NodeContractPairID(chainlinkNode, ocrInstance)
if err != nil {
return err
}
bta := &client.BridgeTypeAttributes{
Name: nodeContractPairID,
URL: fmt.Sprintf("%s/%s", mockserver.Config.ClusterURL, strings.TrimPrefix(nodeContractPairID, "/")),
}
err = chainlinkNode.MustCreateBridge(bta)
if err != nil {
return fmt.Errorf("failed creating bridge %s on CL node: %w", bta.Name, err)
}
ocrSpec := &client.OCR2TaskJobSpec{
Name: fmt.Sprintf("ocr2-%s", uuid.NewString()),
JobType: "offchainreporting2",
MaxTaskDuration: "1m",
ObservationSource: client.ObservationSourceSpecBridge(bta),
ForwardingAllowed: forwardingAllowed,
OCR2OracleSpec: job.OCR2OracleSpec{
PluginType: "median",
Relay: "evm",
RelayConfig: map[string]interface{}{
"chainID": chainId,
},
PluginConfig: map[string]any{
"juelsPerFeeCoinSource": fmt.Sprintf("\"\"\"%s\"\"\"", client.ObservationSourceSpecBridge(juelsBridge)),
},
ContractConfigTrackerPollInterval: *models.NewInterval(15 * time.Second),
ContractID: ocrInstance.Address(), // registryAddr
OCRKeyBundleID: null.StringFrom(nodeOCRKeyId), // get node ocr2config.ID
TransmitterID: null.StringFrom(nodeTransmitterAddress), // node addr
P2PV2Bootstrappers: pq.StringArray{p2pV2Bootstrapper}, // bootstrap node key and address <p2p-key>@bootstrap:6690
},
}
_, err = chainlinkNode.MustCreateJob(ocrSpec)
if err != nil {
return fmt.Errorf("creating OCR task job on OCR node have failed: %w", err)
}
}
}
return nil
}
// StartNewOCR2Round requests a new round from the ocr2 contracts and waits for confirmation
func StartNewOCR2Round(
roundNumber int64,
ocrInstances []contracts.OffchainAggregatorV2,
client blockchain.EVMClient,
timeout time.Duration,
logger zerolog.Logger,
) error {
time.Sleep(5 * time.Second)
for i := 0; i < len(ocrInstances); i++ {
err := ocrInstances[i].RequestNewRound()
if err != nil {
return fmt.Errorf("requesting new OCR round %d have failed: %w", i+1, err)
}
ocrRound := contracts.NewOffchainAggregatorV2RoundConfirmer(ocrInstances[i], big.NewInt(roundNumber), timeout, logger)
client.AddHeaderEventSubscription(ocrInstances[i].Address(), ocrRound)
err = ocrRound.Wait() // wait for OCR Round to complete
if err != nil {
return fmt.Errorf("failed to wait for OCR Round %d to complete instance %d", roundNumber, i)
}
if !ocrRound.Complete() {
return fmt.Errorf("failed to complete OCR Round %d for ocr instance %d", roundNumber, i)
}
}
return nil
}
// WatchNewOCR2Round is the same as StartNewOCR2Round but does NOT explicitly request a new round
// as that can cause odd behavior in tandem with changing adapter values in OCR2
func WatchNewOCR2Round(
roundNumber int64,
ocrInstances []contracts.OffchainAggregatorV2,
client blockchain.EVMClient,
timeout time.Duration,
logger zerolog.Logger,
) error {
for i := 0; i < len(ocrInstances); i++ {
ocrRound := contracts.NewOffchainAggregatorV2RoundConfirmer(ocrInstances[i], big.NewInt(roundNumber), timeout, logger)
client.AddHeaderEventSubscription(ocrInstances[i].Address(), ocrRound)
err := client.WaitForEvents()
if err != nil {
return fmt.Errorf("failed to wait for event subscriptions of OCR instance %d: %w", i+1, err)
}
}
return nil
}
// SetOCR2AdapterResponse sets a single adapter response that correlates with an ocr contract and a chainlink node
// used for OCR2 tests
func SetOCR2AdapterResponse(
response int,
ocrInstance contracts.OffchainAggregatorV2,
chainlinkNode *client.ChainlinkK8sClient,
mockserver *ctfClient.MockserverClient,
) error {
nodeContractPairID, err := BuildOCR2NodeContractPairID(chainlinkNode, ocrInstance)
if err != nil {
return err
}
path := fmt.Sprintf("/%s", nodeContractPairID)
err = mockserver.SetValuePath(path, response)
if err != nil {
return fmt.Errorf("setting mockserver value path failed: %w", err)
}
return nil
}
// SetOCR2AllAdapterResponsesToTheSameValue sets the mock responses in mockserver that are read by chainlink nodes
// to simulate different adapters. This sets all adapter responses for each node and contract to the same response
// used for OCR2 tests
func SetOCR2AllAdapterResponsesToTheSameValue(
response int,
ocrInstances []contracts.OffchainAggregatorV2,
chainlinkNodes []*client.ChainlinkK8sClient,
mockserver *ctfClient.MockserverClient,
) error {
eg := &errgroup.Group{}
for _, o := range ocrInstances {
ocrInstance := o
for _, n := range chainlinkNodes {
node := n
eg.Go(func() error {
return SetOCR2AdapterResponse(response, ocrInstance, node, mockserver)
})
}
}
return eg.Wait()
}
// SetOCR2AllAdapterResponsesToDifferentValues sets the mock responses in mockserver that are read by chainlink nodes
// to simulate different adapters. This sets all adapter responses for each node and contract to different responses
// used for OCR2 tests
func SetOCR2AllAdapterResponsesToDifferentValues(
responses []int,
ocrInstances []contracts.OffchainAggregatorV2,
chainlinkNodes []*client.ChainlinkK8sClient,
mockserver *ctfClient.MockserverClient,
) error {
if len(responses) != len(ocrInstances)*len(chainlinkNodes) {
return fmt.Errorf(
"amount of responses %d should be equal to the amount of OCR instances %d times the amount of Chainlink nodes %d",
len(responses), len(ocrInstances), len(chainlinkNodes),
)
}
eg := &errgroup.Group{}
for _, o := range ocrInstances {
ocrInstance := o
for ni := 1; ni < len(chainlinkNodes); ni++ {
nodeIndex := ni
eg.Go(func() error {
return SetOCR2AdapterResponse(responses[nodeIndex-1], ocrInstance, chainlinkNodes[nodeIndex], mockserver)
})
}
}
return eg.Wait()
}
// BuildOCR2NodeContractPairID builds a UUID based on a related pair of a Chainlink node and OCRv2 contract
func BuildOCR2NodeContractPairID(node *client.ChainlinkK8sClient, ocrInstance contracts.OffchainAggregatorV2) (string, error) {
if node == nil {
return "", fmt.Errorf("chainlink node is nil")
}
if ocrInstance == nil {
return "", fmt.Errorf("OCR Instance is nil")
}
nodeAddress, err := node.PrimaryEthAddress()
if err != nil {
return "", fmt.Errorf("getting chainlink node's primary ETH address failed: %w", err)
}
shortNodeAddr := nodeAddress[2:12]
shortOCRAddr := ocrInstance.Address()[2:12]
return strings.ToLower(fmt.Sprintf("node_%s_contract_%s", shortNodeAddr, shortOCRAddr)), nil
}