/
ocr2_helpers_local.go
363 lines (334 loc) · 13.4 KB
/
ocr2_helpers_local.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
package actions
import (
"crypto/ed25519"
"encoding/hex"
"fmt"
"net/http"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/rs/zerolog/log"
"github.com/smartcontractkit/libocr/offchainreporting2/reportingplugin/median"
"github.com/smartcontractkit/libocr/offchainreporting2plus/confighelper"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"golang.org/x/sync/errgroup"
"gopkg.in/guregu/null.v4"
"github.com/smartcontractkit/chainlink-common/pkg/codec"
"github.com/smartcontractkit/chainlink-testing-framework/docker/test_env"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types"
"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"
)
func CreateOCRv2JobsLocal(
ocrInstances []contracts.OffchainAggregatorV2,
bootstrapNode *client.ChainlinkClient,
workerChainlinkNodes []*client.ChainlinkClient,
mockAdapter *test_env.Killgrave,
mockAdapterPath string, // Path on the mock server for the Chainlink nodes to query
mockAdapterValue int, // Value to get from the mock server when querying the path
chainId uint64, // EVM chain ID
forwardingAllowed bool,
enableChainReaderAndCodec 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)
// Set the value for the jobs to report on
err = mockAdapter.SetAdapterBasedIntValuePath(mockAdapterPath, []string{http.MethodGet, http.MethodPost}, mockAdapterValue)
if err != nil {
return err
}
// Set the juelsPerFeeCoinSource config value
err = mockAdapter.SetAdapterBasedIntValuePath(fmt.Sprintf("%s/juelsPerFeeCoinSource", mockAdapterPath), []string{http.MethodGet, http.MethodPost}, mockAdapterValue)
if err != nil {
return err
}
for _, ocrInstance := range ocrInstances {
bootstrapSpec := &client.OCR2TaskJobSpec{
Name: fmt.Sprintf("ocr2_bootstrap-%s", uuid.NewString()),
JobType: "bootstrap",
OCR2OracleSpec: job.OCR2OracleSpec{
ContractID: ocrInstance.Address(),
Relay: "evm",
RelayConfig: map[string]interface{}{
"chainID": chainId,
},
MonitoringEndpoint: null.StringFrom(fmt.Sprintf("%s/%s", mockAdapter.InternalEndpoint, mockAdapterPath)),
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
bta := &client.BridgeTypeAttributes{
Name: fmt.Sprintf("%s-%s", mockAdapterPath, uuid.NewString()),
URL: fmt.Sprintf("%s/%s", mockAdapter.InternalEndpoint, mockAdapterPath),
}
juelsBridge := &client.BridgeTypeAttributes{
Name: fmt.Sprintf("juels-%s", uuid.NewString()),
URL: fmt.Sprintf("%s/%s/juelsPerFeeCoinSource", mockAdapter.InternalEndpoint, mockAdapterPath),
}
err = chainlinkNode.MustCreateBridge(bta)
if err != nil {
return fmt.Errorf("creating bridge on CL node failed: %w", err)
}
err = chainlinkNode.MustCreateBridge(juelsBridge)
if err != nil {
return fmt.Errorf("creating bridge on CL node failed: %w", 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
},
}
if enableChainReaderAndCodec {
ocrSpec.OCR2OracleSpec.RelayConfig["chainReader"] = evmtypes.ChainReaderConfig{
Contracts: map[string]evmtypes.ChainContractReader{
"median": {
ContractABI: `[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"requester","type":"address"},{"indexed":false,"internalType":"bytes32","name":"configDigest","type":"bytes32"},{"indexed":false,"internalType":"uint32","name":"epoch","type":"uint32"},{"indexed":false,"internalType":"uint8","name":"round","type":"uint8"}],"name":"RoundRequested","type":"event"},{"inputs":[],"name":"latestTransmissionDetails","outputs":[{"internalType":"bytes32","name":"configDigest","type":"bytes32"},{"internalType":"uint32","name":"epoch","type":"uint32"},{"internalType":"uint8","name":"round","type":"uint8"},{"internalType":"int192","name":"latestAnswer_","type":"int192"},{"internalType":"uint64","name":"latestTimestamp_","type":"uint64"}],"stateMutability":"view","type":"function"}]`,
Configs: map[string]*evmtypes.ChainReaderDefinition{
"LatestTransmissionDetails": {
ChainSpecificName: "latestTransmissionDetails",
OutputModifications: codec.ModifiersConfig{
&codec.EpochToTimeModifierConfig{
Fields: []string{"LatestTimestamp_"},
},
&codec.RenameModifierConfig{
Fields: map[string]string{
"LatestAnswer_": "LatestAnswer",
"LatestTimestamp_": "LatestTimestamp",
},
},
},
},
"LatestRoundRequested": {
ChainSpecificName: "RoundRequested",
ReadType: evmtypes.Event,
},
},
},
},
}
ocrSpec.OCR2OracleSpec.RelayConfig["codec"] = evmtypes.CodecConfig{
Configs: map[string]evmtypes.ChainCodecConfig{
"MedianReport": {
TypeABI: `[{"Name": "Timestamp","Type": "uint32"},{"Name": "Observers","Type": "bytes32"},{"Name": "Observations","Type": "int192[]"},{"Name": "JuelsPerFeeCoin","Type": "int192"}]`,
},
},
}
}
_, err = chainlinkNode.MustCreateJob(ocrSpec)
if err != nil {
return fmt.Errorf("creating OCR task job on OCR node have failed: %w", err)
}
}
}
return nil
}
func BuildMedianOCR2ConfigLocal(workerNodes []*client.ChainlinkClient, ocrOffchainOptions contracts.OffchainOptions) (*contracts.OCRv2Config, error) {
S, oracleIdentities, err := GetOracleIdentitiesWithKeyIndexLocal(workerNodes, 0)
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
}
func GetOracleIdentitiesWithKeyIndexLocal(
chainlinkNodes []*client.ChainlinkClient,
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()
}
// DeleteJobs will delete ALL jobs from the nodes
func DeleteJobs(nodes []*client.ChainlinkClient) error {
for _, node := range nodes {
if node == nil {
return fmt.Errorf("found a nil chainlink node in the list of chainlink nodes while tearing down: %v", nodes)
}
jobs, _, err := node.ReadJobs()
if err != nil {
return fmt.Errorf("error reading jobs from chainlink node: %w", err)
}
for _, maps := range jobs.Data {
if _, ok := maps["id"]; !ok {
return fmt.Errorf("error reading job id from chainlink node's jobs %+v", jobs.Data)
}
id := maps["id"].(string)
_, err2 := node.DeleteJob(id)
if err2 != nil {
return fmt.Errorf("error deleting job from chainlink node: %w", err)
}
}
}
return nil
}
// DeleteBridges will delete ALL bridges from the nodes
func DeleteBridges(nodes []*client.ChainlinkClient) error {
for _, node := range nodes {
if node == nil {
return fmt.Errorf("found a nil chainlink node in the list of chainlink nodes while tearing down: %v", nodes)
}
bridges, _, err := node.ReadBridges()
if err != nil {
return err
}
for _, b := range bridges.Data {
_, err = node.DeleteBridge(b.Attributes.Name)
if err != nil {
return err
}
}
}
return nil
}