/
automation_ocr_helpers.go
381 lines (351 loc) · 15.5 KB
/
automation_ocr_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
package actions
//revive:disable:dot-imports
import (
"encoding/json"
"fmt"
"math"
"math/big"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v4"
ocr2 "github.com/smartcontractkit/libocr/offchainreporting2plus/confighelper"
ocr3 "github.com/smartcontractkit/libocr/offchainreporting2plus/ocr3confighelper"
"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
ocr2keepers20config "github.com/smartcontractkit/chainlink-automation/pkg/v2/config"
ocr2keepers30config "github.com/smartcontractkit/chainlink-automation/pkg/v3/config"
"github.com/smartcontractkit/chainlink-testing-framework/blockchain"
"github.com/smartcontractkit/chainlink-testing-framework/logging"
"github.com/smartcontractkit/chainlink/integration-tests/client"
"github.com/smartcontractkit/chainlink/integration-tests/contracts"
"github.com/smartcontractkit/chainlink/integration-tests/contracts/ethereum"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/chaintype"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
)
func BuildAutoOCR2ConfigVars(
t *testing.T,
chainlinkNodes []*client.ChainlinkK8sClient,
registryConfig contracts.KeeperRegistrySettings,
registrar string,
deltaStage time.Duration,
chainModuleAddress common.Address,
reorgProtectionEnabled bool,
) (contracts.OCRv2Config, error) {
return BuildAutoOCR2ConfigVarsWithKeyIndex(t, chainlinkNodes, registryConfig, registrar, deltaStage, 0, common.Address{}, chainModuleAddress, reorgProtectionEnabled)
}
func BuildAutoOCR2ConfigVarsWithKeyIndex(
t *testing.T,
chainlinkNodes []*client.ChainlinkK8sClient,
registryConfig contracts.KeeperRegistrySettings,
registrar string,
deltaStage time.Duration,
keyIndex int,
registryOwnerAddress common.Address,
chainModuleAddress common.Address,
reorgProtectionEnabled bool,
) (contracts.OCRv2Config, error) {
l := logging.GetTestLogger(t)
S, oracleIdentities, err := GetOracleIdentitiesWithKeyIndex(chainlinkNodes, keyIndex)
if err != nil {
return contracts.OCRv2Config{}, err
}
var offC []byte
var signerOnchainPublicKeys []types.OnchainPublicKey
var transmitterAccounts []types.Account
var f uint8
var offchainConfigVersion uint64
var offchainConfig []byte
if registryConfig.RegistryVersion == ethereum.RegistryVersion_2_1 || registryConfig.RegistryVersion == ethereum.RegistryVersion_2_2 {
offC, err = json.Marshal(ocr2keepers30config.OffchainConfig{
TargetProbability: "0.999",
TargetInRounds: 1,
PerformLockoutWindow: 3600000, // Intentionally set to be higher than in prod for testing purpose
GasLimitPerReport: 5_300_000,
GasOverheadPerUpkeep: 300_000,
MinConfirmations: 0,
MaxUpkeepBatchSize: 10,
})
if err != nil {
return contracts.OCRv2Config{}, err
}
signerOnchainPublicKeys, transmitterAccounts, f, _, offchainConfigVersion, offchainConfig, err = ocr3.ContractSetConfigArgsForTests(
10*time.Second, // deltaProgress time.Duration,
15*time.Second, // deltaResend time.Duration,
500*time.Millisecond, // deltaInitial time.Duration,
1000*time.Millisecond, // deltaRound time.Duration,
200*time.Millisecond, // deltaGrace time.Duration,
300*time.Millisecond, // deltaCertifiedCommitRequest time.Duration
deltaStage, // deltaStage time.Duration,
24, // rMax uint64,
S, // s []int,
oracleIdentities, // oracles []OracleIdentityExtra,
offC, // reportingPluginConfig []byte,
20*time.Millisecond, // maxDurationQuery time.Duration,
20*time.Millisecond, // maxDurationObservation time.Duration, // good to here
1200*time.Millisecond, // maxDurationShouldAcceptAttestedReport time.Duration,
20*time.Millisecond, // maxDurationShouldTransmitAcceptedReport time.Duration,
1, // f int,
nil, // onchainConfig []byte,
)
if err != nil {
return contracts.OCRv2Config{}, err
}
} else {
offC, err = json.Marshal(ocr2keepers20config.OffchainConfig{
TargetProbability: "0.999",
TargetInRounds: 1,
PerformLockoutWindow: 3600000, // Intentionally set to be higher than in prod for testing purpose
GasLimitPerReport: 5_300_000,
GasOverheadPerUpkeep: 300_000,
SamplingJobDuration: 3000,
MinConfirmations: 0,
MaxUpkeepBatchSize: 1,
})
if err != nil {
return contracts.OCRv2Config{}, err
}
signerOnchainPublicKeys, transmitterAccounts, f, _, offchainConfigVersion, offchainConfig, err = ocr2.ContractSetConfigArgsForTests(
10*time.Second, // deltaProgress time.Duration,
15*time.Second, // deltaResend time.Duration,
3000*time.Millisecond, // deltaRound time.Duration,
200*time.Millisecond, // deltaGrace time.Duration,
deltaStage, // deltaStage time.Duration,
24, // rMax uint8,
S, // s []int,
oracleIdentities, // oracles []OracleIdentityExtra,
offC, // reportingPluginConfig []byte,
20*time.Millisecond, // maxDurationQuery time.Duration,
20*time.Millisecond, // maxDurationObservation time.Duration,
1200*time.Millisecond, // maxDurationReport time.Duration,
20*time.Millisecond, // maxDurationShouldAcceptFinalizedReport time.Duration,
20*time.Millisecond, // maxDurationShouldTransmitAcceptedReport time.Duration,
1, // f int,
nil, // onchainConfig []byte,
)
if err != nil {
return contracts.OCRv2Config{}, err
}
}
var signers []common.Address
for _, signer := range signerOnchainPublicKeys {
require.Equal(t, 20, len(signer), "OnChainPublicKey '%v' has wrong length for address", signer)
signers = append(signers, common.BytesToAddress(signer))
}
var transmitters []common.Address
for _, transmitter := range transmitterAccounts {
require.True(t, common.IsHexAddress(string(transmitter)), "TransmitAccount '%s' is not a valid Ethereum address", string(transmitter))
transmitters = append(transmitters, common.HexToAddress(string(transmitter)))
}
ocrConfig := contracts.OCRv2Config{
Signers: signers,
Transmitters: transmitters,
F: f,
OffchainConfigVersion: offchainConfigVersion,
OffchainConfig: offchainConfig,
}
if registryConfig.RegistryVersion == ethereum.RegistryVersion_2_0 {
ocrConfig.OnchainConfig = registryConfig.Encode20OnchainConfig(registrar)
} else if registryConfig.RegistryVersion == ethereum.RegistryVersion_2_1 {
ocrConfig.TypedOnchainConfig21 = registryConfig.Create21OnchainConfig(registrar, registryOwnerAddress)
} else if registryConfig.RegistryVersion == ethereum.RegistryVersion_2_2 {
ocrConfig.TypedOnchainConfig22 = registryConfig.Create22OnchainConfig(registrar, registryOwnerAddress, chainModuleAddress, reorgProtectionEnabled)
}
l.Info().Msg("Done building OCR config")
return ocrConfig, nil
}
// CreateOCRKeeperJobs bootstraps the first node and to the other nodes sends ocr jobs
func CreateOCRKeeperJobs(
t *testing.T,
chainlinkNodes []*client.ChainlinkK8sClient,
registryAddr string,
chainID int64,
keyIndex int,
registryVersion ethereum.KeeperRegistryVersion,
) {
l := logging.GetTestLogger(t)
bootstrapNode := chainlinkNodes[0]
bootstrapP2PIds, err := bootstrapNode.MustReadP2PKeys()
require.NoError(t, err, "Shouldn't fail reading P2P keys from bootstrap node")
bootstrapP2PId := bootstrapP2PIds.Data[0].Attributes.PeerID
var contractVersion string
if registryVersion == ethereum.RegistryVersion_2_2 {
contractVersion = "v2.1+"
} else if registryVersion == ethereum.RegistryVersion_2_1 {
contractVersion = "v2.1"
} else {
require.FailNow(t, "v2.0, v2.1, and v2.2 are the only supported versions")
}
bootstrapSpec := &client.OCR2TaskJobSpec{
Name: "ocr2 bootstrap node " + registryAddr,
JobType: "bootstrap",
OCR2OracleSpec: job.OCR2OracleSpec{
ContractID: registryAddr,
Relay: "evm",
RelayConfig: map[string]interface{}{
"chainID": int(chainID),
},
ContractConfigTrackerPollInterval: *models.NewInterval(time.Second * 15),
},
}
_, err = bootstrapNode.MustCreateJob(bootstrapSpec)
require.NoError(t, err, "Shouldn't fail creating bootstrap job on bootstrap node")
// TODO: Use service name returned by chainlink-env once that is available
P2Pv2Bootstrapper := fmt.Sprintf("%s@%s-node-1:%d", bootstrapP2PId, bootstrapNode.Name(), 6690)
for nodeIndex := 1; nodeIndex < len(chainlinkNodes); nodeIndex++ {
nodeTransmitterAddress, err := chainlinkNodes[nodeIndex].EthAddresses()
require.NoError(t, err, "Shouldn't fail getting primary ETH address from OCR node %d", nodeIndex+1)
nodeOCRKeys, err := chainlinkNodes[nodeIndex].MustReadOCR2Keys()
require.NoError(t, err, "Shouldn't fail getting OCR keys from OCR node %d", nodeIndex+1)
var nodeOCRKeyId []string
for _, key := range nodeOCRKeys.Data {
if key.Attributes.ChainType == string(chaintype.EVM) {
nodeOCRKeyId = append(nodeOCRKeyId, key.ID)
break
}
}
autoOCR2JobSpec := client.OCR2TaskJobSpec{
Name: "ocr2 " + registryAddr,
JobType: "offchainreporting2",
OCR2OracleSpec: job.OCR2OracleSpec{
PluginType: "ocr2automation",
Relay: "evm",
RelayConfig: map[string]interface{}{
"chainID": int(chainID),
},
PluginConfig: map[string]interface{}{
"mercuryCredentialName": "\"cred1\"",
"contractVersion": "\"" + contractVersion + "\"",
},
ContractConfigTrackerPollInterval: *models.NewInterval(time.Second * 15),
ContractID: registryAddr, // registryAddr
OCRKeyBundleID: null.StringFrom(nodeOCRKeyId[0]), // get node ocr2config.ID
TransmitterID: null.StringFrom(nodeTransmitterAddress[keyIndex]), // node addr
P2PV2Bootstrappers: pq.StringArray{P2Pv2Bootstrapper}, // bootstrap node key and address <p2p-key>@bootstrap:8000
},
}
_, err = chainlinkNodes[nodeIndex].MustCreateJob(&autoOCR2JobSpec)
require.NoError(t, err, "Shouldn't fail creating OCR Task job on OCR node %d err: %+v", nodeIndex+1, err)
}
l.Info().Msg("Done creating OCR automation jobs")
}
// DeployAutoOCRRegistryAndRegistrar registry and registrar
func DeployAutoOCRRegistryAndRegistrar(
t *testing.T,
registryVersion ethereum.KeeperRegistryVersion,
registrySettings contracts.KeeperRegistrySettings,
linkToken contracts.LinkToken,
contractDeployer contracts.ContractDeployer,
client blockchain.EVMClient,
) (contracts.KeeperRegistry, contracts.KeeperRegistrar) {
registry := deployRegistry(t, registryVersion, registrySettings, contractDeployer, client, linkToken)
registrar := deployRegistrar(t, registryVersion, registry, linkToken, contractDeployer, client)
return registry, registrar
}
func DeployConsumers(t *testing.T, registry contracts.KeeperRegistry, registrar contracts.KeeperRegistrar, linkToken contracts.LinkToken, contractDeployer contracts.ContractDeployer, client blockchain.EVMClient, numberOfUpkeeps int, linkFundsForEachUpkeep *big.Int, upkeepGasLimit uint32, isLogTrigger bool, isMercury bool) ([]contracts.KeeperConsumer, []*big.Int) {
upkeeps := DeployKeeperConsumers(t, contractDeployer, client, numberOfUpkeeps, isLogTrigger, isMercury)
var upkeepsAddresses []string
for _, upkeep := range upkeeps {
upkeepsAddresses = append(upkeepsAddresses, upkeep.Address())
}
upkeepIds := RegisterUpkeepContracts(
t, linkToken, linkFundsForEachUpkeep, client, upkeepGasLimit, registry, registrar, numberOfUpkeeps, upkeepsAddresses, isLogTrigger, isMercury,
)
return upkeeps, upkeepIds
}
func DeployPerformanceConsumers(
t *testing.T,
registry contracts.KeeperRegistry,
registrar contracts.KeeperRegistrar,
linkToken contracts.LinkToken,
contractDeployer contracts.ContractDeployer,
client blockchain.EVMClient,
numberOfUpkeeps int,
linkFundsForEachUpkeep *big.Int,
upkeepGasLimit uint32,
blockRange, // How many blocks to run the test for
blockInterval, // Interval of blocks that upkeeps are expected to be performed
checkGasToBurn, // How much gas should be burned on checkUpkeep() calls
performGasToBurn int64, // How much gas should be burned on performUpkeep() calls
) ([]contracts.KeeperConsumerPerformance, []*big.Int) {
upkeeps := DeployKeeperConsumersPerformance(
t, contractDeployer, client, numberOfUpkeeps, blockRange, blockInterval, checkGasToBurn, performGasToBurn,
)
var upkeepsAddresses []string
for _, upkeep := range upkeeps {
upkeepsAddresses = append(upkeepsAddresses, upkeep.Address())
}
upkeepIds := RegisterUpkeepContracts(t, linkToken, linkFundsForEachUpkeep, client, upkeepGasLimit, registry, registrar, numberOfUpkeeps, upkeepsAddresses, false, false)
return upkeeps, upkeepIds
}
func DeployPerformDataCheckerConsumers(
t *testing.T,
registry contracts.KeeperRegistry,
registrar contracts.KeeperRegistrar,
linkToken contracts.LinkToken,
contractDeployer contracts.ContractDeployer,
client blockchain.EVMClient,
numberOfUpkeeps int,
linkFundsForEachUpkeep *big.Int,
upkeepGasLimit uint32,
expectedData []byte,
) ([]contracts.KeeperPerformDataChecker, []*big.Int) {
upkeeps := DeployPerformDataChecker(t, contractDeployer, client, numberOfUpkeeps, expectedData)
var upkeepsAddresses []string
for _, upkeep := range upkeeps {
upkeepsAddresses = append(upkeepsAddresses, upkeep.Address())
}
upkeepIds := RegisterUpkeepContracts(t, linkToken, linkFundsForEachUpkeep, client, upkeepGasLimit, registry, registrar, numberOfUpkeeps, upkeepsAddresses, false, false)
return upkeeps, upkeepIds
}
func deployRegistrar(
t *testing.T,
registryVersion ethereum.KeeperRegistryVersion,
registry contracts.KeeperRegistry,
linkToken contracts.LinkToken,
contractDeployer contracts.ContractDeployer,
client blockchain.EVMClient,
) contracts.KeeperRegistrar {
registrarSettings := contracts.KeeperRegistrarSettings{
AutoApproveConfigType: 2,
AutoApproveMaxAllowed: math.MaxUint16,
RegistryAddr: registry.Address(),
MinLinkJuels: big.NewInt(0),
}
registrar, err := contractDeployer.DeployKeeperRegistrar(registryVersion, linkToken.Address(), registrarSettings)
require.NoError(t, err, "Deploying KeeperRegistrar contract shouldn't fail")
err = client.WaitForEvents()
require.NoError(t, err, "Failed waiting for registrar to deploy")
return registrar
}
func deployRegistry(
t *testing.T,
registryVersion ethereum.KeeperRegistryVersion,
registrySettings contracts.KeeperRegistrySettings,
contractDeployer contracts.ContractDeployer,
client blockchain.EVMClient,
linkToken contracts.LinkToken,
) contracts.KeeperRegistry {
ef, err := contractDeployer.DeployMockETHLINKFeed(big.NewInt(2e18))
require.NoError(t, err, "Deploying mock ETH-Link feed shouldn't fail")
gf, err := contractDeployer.DeployMockGasFeed(big.NewInt(2e11))
require.NoError(t, err, "Deploying mock gas feed shouldn't fail")
err = client.WaitForEvents()
require.NoError(t, err, "Failed waiting for mock feeds to deploy")
// Deploy the transcoder here, and then set it to the registry
transcoder := DeployUpkeepTranscoder(t, contractDeployer, client)
registry := DeployKeeperRegistry(t, contractDeployer, client,
&contracts.KeeperRegistryOpts{
RegistryVersion: registryVersion,
LinkAddr: linkToken.Address(),
ETHFeedAddr: ef.Address(),
GasFeedAddr: gf.Address(),
TranscoderAddr: transcoder.Address(),
RegistrarAddr: ZeroAddress.Hex(),
Settings: registrySettings,
},
)
return registry
}