-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathsetup_steps.go
506 lines (463 loc) · 16.6 KB
/
setup_steps.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 vrfv2
import (
"context"
"fmt"
"math/big"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
"github.com/google/uuid"
"github.com/smartcontractkit/chainlink/integration-tests/actions"
"github.com/smartcontractkit/chainlink/integration-tests/docker/test_env"
testconfig "github.com/smartcontractkit/chainlink/integration-tests/testconfig/vrfv2"
"github.com/smartcontractkit/chainlink/integration-tests/types/config/node"
vrfcommon "github.com/smartcontractkit/chainlink/integration-tests/actions/vrf/common"
"github.com/smartcontractkit/chainlink/integration-tests/client"
"github.com/smartcontractkit/chainlink/integration-tests/contracts"
tc "github.com/smartcontractkit/chainlink/integration-tests/testconfig"
"github.com/smartcontractkit/chainlink/integration-tests/types"
)
func CreateVRFV2Job(
chainlinkNode *client.ChainlinkClient,
vrfJobSpecConfig vrfcommon.VRFJobSpecConfig,
) (*client.Job, error) {
jobUUID := uuid.New()
os := &client.VRFV2TxPipelineSpec{
Address: vrfJobSpecConfig.CoordinatorAddress,
EstimateGasMultiplier: vrfJobSpecConfig.EstimateGasMultiplier,
FromAddress: vrfJobSpecConfig.FromAddresses[0],
SimulationBlock: vrfJobSpecConfig.SimulationBlock,
}
ost, err := os.String()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrParseJob, err)
}
spec := &client.VRFV2JobSpec{
Name: fmt.Sprintf("vrf-v2-%s", jobUUID),
ForwardingAllowed: vrfJobSpecConfig.ForwardingAllowed,
CoordinatorAddress: vrfJobSpecConfig.CoordinatorAddress,
FromAddresses: vrfJobSpecConfig.FromAddresses,
EVMChainID: vrfJobSpecConfig.EVMChainID,
MinIncomingConfirmations: vrfJobSpecConfig.MinIncomingConfirmations,
PublicKey: vrfJobSpecConfig.PublicKey,
ExternalJobID: jobUUID.String(),
ObservationSource: ost,
BatchFulfillmentEnabled: vrfJobSpecConfig.BatchFulfillmentEnabled,
BatchFulfillmentGasMultiplier: vrfJobSpecConfig.BatchFulfillmentGasMultiplier,
PollPeriod: vrfJobSpecConfig.PollPeriod,
RequestTimeout: vrfJobSpecConfig.RequestTimeout,
}
if vrfJobSpecConfig.VRFOwnerConfig.UseVRFOwner {
spec.VRFOwner = vrfJobSpecConfig.VRFOwnerConfig.OwnerAddress
spec.UseVRFOwner = true
}
if vrfJobSpecConfig.BatchFulfillmentEnabled {
spec.BatchCoordinatorAddress = vrfJobSpecConfig.BatchCoordinatorAddress
}
job, err := chainlinkNode.MustCreateJob(spec)
if err != nil {
return nil, fmt.Errorf("%s, err %w", ErrCreatingVRFv2Job, err)
}
return job, nil
}
// SetupVRFV2Environment will create specified number of subscriptions and add the same conumer/s to each of them
func SetupVRFV2Environment(
ctx context.Context,
env *test_env.CLClusterTestEnv,
chainID int64,
nodesToCreate []vrfcommon.VRFNodeType,
vrfv2TestConfig types.VRFv2TestConfig,
useVRFOwner bool,
useTestCoordinator bool,
linkToken contracts.LinkToken,
mockNativeLINKFeed contracts.VRFMockETHLINKFeed,
registerProvingKeyAgainstAddress string,
numberOfTxKeysToCreate int,
l zerolog.Logger,
) (*vrfcommon.VRFContracts, *vrfcommon.VRFKeyData, map[vrfcommon.VRFNodeType]*vrfcommon.VRFNode, error) {
l.Info().Msg("Starting VRFV2 environment setup")
configGeneral := vrfv2TestConfig.GetVRFv2Config().General
vrfContracts, err := SetupVRFV2Contracts(
env,
chainID,
linkToken,
mockNativeLINKFeed,
useVRFOwner,
useTestCoordinator,
configGeneral,
l,
)
if err != nil {
return nil, nil, nil, err
}
nodeTypeToNodeMap, err := vrfcommon.CreateNodeTypeToNodeMap(env.ClCluster, nodesToCreate)
if err != nil {
return nil, nil, nil, err
}
vrfKey, pubKeyCompressed, err := vrfcommon.CreateVRFKeyOnVRFNode(nodeTypeToNodeMap[vrfcommon.VRF], l)
if err != nil {
return nil, nil, nil, err
}
l.Info().Str("Coordinator", vrfContracts.CoordinatorV2.Address()).Msg("Registering Proving Key")
provingKey, err := VRFV2RegisterProvingKey(vrfKey, registerProvingKeyAgainstAddress, vrfContracts.CoordinatorV2)
if err != nil {
return nil, nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrRegisteringProvingKey, err)
}
keyHash, err := vrfContracts.CoordinatorV2.HashOfKey(ctx, provingKey)
if err != nil {
return nil, nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrCreatingProvingKeyHash, err)
}
evmClient, err := env.GetEVMClient(chainID)
if err != nil {
return nil, nil, nil, err
}
vrfTXKeyAddressStrings, vrfTXKeyAddresses, err := vrfcommon.CreateFundAndGetSendingKeys(
evmClient,
nodeTypeToNodeMap[vrfcommon.VRF],
*vrfv2TestConfig.GetCommonConfig().ChainlinkNodeFunding,
numberOfTxKeysToCreate,
big.NewInt(chainID),
)
if err != nil {
return nil, nil, nil, err
}
err = evmClient.WaitForEvents()
if err != nil {
return nil, nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
}
nodeTypeToNodeMap[vrfcommon.VRF].TXKeyAddressStrings = vrfTXKeyAddressStrings
vrfOwnerConfig, err := SetupVRFOwnerContractIfNeeded(useVRFOwner, env, chainID, vrfContracts, vrfTXKeyAddressStrings, vrfTXKeyAddresses, l)
if err != nil {
return nil, nil, nil, err
}
g := errgroup.Group{}
if vrfNode, exists := nodeTypeToNodeMap[vrfcommon.VRF]; exists {
g.Go(func() error {
err := setupVRFNode(vrfContracts, big.NewInt(chainID), configGeneral, pubKeyCompressed, vrfOwnerConfig, l, vrfNode)
if err != nil {
return err
}
return nil
})
}
if bhsNode, exists := nodeTypeToNodeMap[vrfcommon.BHS]; exists {
g.Go(func() error {
err := vrfcommon.SetupBHSNode(
env,
configGeneral.General,
numberOfTxKeysToCreate,
big.NewInt(chainID),
vrfContracts.CoordinatorV2.Address(),
vrfContracts.BHS.Address(),
*vrfv2TestConfig.GetCommonConfig().ChainlinkNodeFunding,
l,
bhsNode,
)
if err != nil {
return err
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, nil, nil, fmt.Errorf("VRF node setup ended up with an error: %w", err)
}
vrfKeyData := vrfcommon.VRFKeyData{
VRFKey: vrfKey,
EncodedProvingKey: provingKey,
KeyHash: keyHash,
PubKeyCompressed: pubKeyCompressed,
}
l.Info().Msg("VRFV2 environment setup is finished")
return vrfContracts, &vrfKeyData, nodeTypeToNodeMap, nil
}
func setupVRFNode(contracts *vrfcommon.VRFContracts, chainID *big.Int, vrfv2Config *testconfig.General, pubKeyCompressed string, vrfOwnerConfig *vrfcommon.VRFOwnerConfig, l zerolog.Logger, vrfNode *vrfcommon.VRFNode) error {
vrfJobSpecConfig := vrfcommon.VRFJobSpecConfig{
ForwardingAllowed: *vrfv2Config.VRFJobForwardingAllowed,
CoordinatorAddress: contracts.CoordinatorV2.Address(),
BatchCoordinatorAddress: contracts.BatchCoordinatorV2.Address(),
FromAddresses: vrfNode.TXKeyAddressStrings,
EVMChainID: chainID.String(),
MinIncomingConfirmations: int(*vrfv2Config.MinimumConfirmations),
PublicKey: pubKeyCompressed,
EstimateGasMultiplier: *vrfv2Config.VRFJobEstimateGasMultiplier,
BatchFulfillmentEnabled: *vrfv2Config.VRFJobBatchFulfillmentEnabled,
BatchFulfillmentGasMultiplier: *vrfv2Config.VRFJobBatchFulfillmentGasMultiplier,
PollPeriod: vrfv2Config.VRFJobPollPeriod.Duration,
RequestTimeout: vrfv2Config.VRFJobRequestTimeout.Duration,
SimulationBlock: vrfv2Config.VRFJobSimulationBlock,
VRFOwnerConfig: vrfOwnerConfig,
}
l.Info().Msg("Creating VRFV2 Job")
vrfV2job, err := CreateVRFV2Job(
vrfNode.CLNode.API,
vrfJobSpecConfig,
)
if err != nil {
return fmt.Errorf("%s, err %w", ErrCreateVRFV2Jobs, err)
}
vrfNode.Job = vrfV2job
// this part is here because VRFv2 can work with only a specific key
// [[EVM.KeySpecific]]
// Key = '...'
nodeConfig := node.NewConfig(vrfNode.CLNode.NodeConfig,
node.WithLogPollInterval(1*time.Second),
node.WithVRFv2EVMEstimator(vrfNode.TXKeyAddressStrings, *vrfv2Config.CLNodeMaxGasPriceGWei),
)
l.Info().Msg("Restarting Node with new sending key PriceMax configuration")
err = vrfNode.CLNode.Restart(nodeConfig)
if err != nil {
return fmt.Errorf("%s, err %w", vrfcommon.ErrRestartCLNode, err)
}
return nil
}
func SetupVRFV2WrapperEnvironment(
ctx context.Context,
env *test_env.CLClusterTestEnv,
chainID int64,
vrfv2TestConfig tc.VRFv2TestConfig,
linkToken contracts.LinkToken,
mockNativeLINKFeed contracts.VRFMockETHLINKFeed,
coordinator contracts.VRFCoordinatorV2,
keyHash [32]byte,
wrapperConsumerContractsAmount int,
) (*VRFV2WrapperContracts, *uint64, error) {
evmClient, err := env.GetEVMClient(chainID)
if err != nil {
return nil, nil, err
}
// Deploy VRF v2 direct funding contracts
wrapperContracts, err := DeployVRFV2DirectFundingContracts(
env.ContractDeployer,
evmClient,
linkToken.Address(),
mockNativeLINKFeed.Address(),
coordinator,
wrapperConsumerContractsAmount,
)
if err != nil {
return nil, nil, err
}
err = evmClient.WaitForEvents()
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
}
vrfv2Config := vrfv2TestConfig.GetVRFv2Config()
// Configure VRF v2 wrapper contract
err = wrapperContracts.VRFV2Wrapper.SetConfig(
*vrfv2Config.General.WrapperGasOverhead,
*vrfv2Config.General.CoordinatorGasOverhead,
*vrfv2Config.General.WrapperPremiumPercentage,
keyHash,
*vrfv2Config.General.WrapperMaxNumberOfWords,
)
if err != nil {
return nil, nil, err
}
err = evmClient.WaitForEvents()
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
}
// Fetch wrapper subscription ID
wrapperSubID, err := wrapperContracts.VRFV2Wrapper.GetSubID(ctx)
if err != nil {
return nil, nil, err
}
err = evmClient.WaitForEvents()
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
}
// Fund wrapper subscription
err = FundSubscriptions(env, chainID, big.NewFloat(*vrfv2Config.General.SubscriptionFundingAmountLink), linkToken, coordinator, []uint64{wrapperSubID})
if err != nil {
return nil, nil, err
}
// Fund consumer with LINK
err = linkToken.Transfer(
wrapperContracts.LoadTestConsumers[0].Address(),
big.NewInt(0).Mul(big.NewInt(1e18), big.NewInt(*vrfv2Config.General.WrapperConsumerFundingAmountLink)),
)
if err != nil {
return nil, nil, err
}
err = evmClient.WaitForEvents()
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
}
return wrapperContracts, &wrapperSubID, nil
}
func SetupVRFV2Universe(ctx context.Context, t *testing.T, testConfig tc.TestConfig, chainID int64, cleanupFn func(), newEnvConfig vrfcommon.NewEnvConfig, l zerolog.Logger) (*test_env.CLClusterTestEnv, *vrfcommon.VRFContracts, *vrfcommon.VRFKeyData, map[vrfcommon.VRFNodeType]*vrfcommon.VRFNode, error) {
var (
env *test_env.CLClusterTestEnv
vrfContracts *vrfcommon.VRFContracts
vrfKey *vrfcommon.VRFKeyData
nodeTypeToNodeMap map[vrfcommon.VRFNodeType]*vrfcommon.VRFNode
err error
)
if *testConfig.VRFv2.General.UseExistingEnv {
vrfContracts, vrfKey, env, err = SetupVRFV2ForExistingEnv(ctx, t, testConfig, chainID, cleanupFn, l)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "Error setting up VRF V2 for Existing env", err)
}
} else {
vrfContracts, vrfKey, env, nodeTypeToNodeMap, err = SetupVRFV2ForNewEnv(ctx, t, testConfig, chainID, cleanupFn, newEnvConfig, l)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "Error setting up VRF V2 for New env", err)
}
}
return env, vrfContracts, vrfKey, nodeTypeToNodeMap, nil
}
func SetupVRFV2ForNewEnv(
ctx context.Context,
t *testing.T,
testConfig tc.TestConfig,
chainID int64,
cleanupFn func(),
newEnvConfig vrfcommon.NewEnvConfig,
l zerolog.Logger,
) (*vrfcommon.VRFContracts, *vrfcommon.VRFKeyData, *test_env.CLClusterTestEnv, map[vrfcommon.VRFNodeType]*vrfcommon.VRFNode, error) {
network, err := actions.EthereumNetworkConfigFromConfig(l, &testConfig)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "Error building ethereum network config", err)
}
env, err := test_env.NewCLTestEnvBuilder().
WithTestInstance(t).
WithTestConfig(&testConfig).
WithPrivateEthereumNetwork(network.EthereumNetworkConfig).
WithCLNodes(len(newEnvConfig.NodesToCreate)).
WithFunding(big.NewFloat(*testConfig.Common.ChainlinkNodeFunding)).
WithCustomCleanup(cleanupFn).
Build()
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error creating test env", err)
}
env.ParallelTransactions(true)
mockETHLinkFeed, err := env.ContractDeployer.DeployVRFMockETHLINKFeed(big.NewInt(*testConfig.VRFv2.General.LinkNativeFeedResponse))
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error deploying mock ETH/LINK feed", err)
}
linkToken, err := actions.DeployLINKToken(env.ContractDeployer)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error deploying LINK contract", err)
}
evmClient, err := env.GetEVMClient(chainID)
if err != nil {
return nil, nil, nil, nil, err
}
vrfContracts, vrfKey, nodeTypeToNode, err := SetupVRFV2Environment(
ctx,
env,
chainID,
newEnvConfig.NodesToCreate,
&testConfig,
newEnvConfig.UseVRFOwner,
newEnvConfig.UseTestCoordinator,
linkToken,
mockETHLinkFeed,
//register proving key against EOA address in order to return funds to this address
evmClient.GetDefaultWallet().Address(),
newEnvConfig.NumberOfTxKeysToCreate,
l,
)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error setting up VRF v2 env", err)
}
return vrfContracts, vrfKey, env, nodeTypeToNode, nil
}
func SetupVRFV2ForExistingEnv(ctx context.Context, t *testing.T, testConfig tc.TestConfig, chainID int64, cleanupFn func(), l zerolog.Logger) (*vrfcommon.VRFContracts, *vrfcommon.VRFKeyData, *test_env.CLClusterTestEnv, error) {
commonExistingEnvConfig := testConfig.VRFv2.ExistingEnvConfig.ExistingEnvConfig
env, err := test_env.NewCLTestEnvBuilder().
WithTestInstance(t).
WithTestConfig(&testConfig).
WithCustomCleanup(cleanupFn).
Build()
if err != nil {
return nil, nil, nil, fmt.Errorf("%s, err: %w", "error creating test env", err)
}
coordinator, err := env.ContractLoader.LoadVRFCoordinatorV2(*commonExistingEnvConfig.CoordinatorAddress)
if err != nil {
return nil, nil, nil, fmt.Errorf("%s, err: %w", "error loading VRFCoordinator2", err)
}
linkToken, err := env.ContractLoader.LoadLINKToken(*commonExistingEnvConfig.LinkAddress)
if err != nil {
return nil, nil, nil, fmt.Errorf("%s, err: %w", "error loading LinkToken", err)
}
evmClient, err := env.GetEVMClient(chainID)
if err != nil {
return nil, nil, nil, err
}
err = vrfcommon.FundNodesIfNeeded(ctx, commonExistingEnvConfig, evmClient, l)
if err != nil {
return nil, nil, nil, fmt.Errorf("err: %w", err)
}
vrfContracts := &vrfcommon.VRFContracts{
CoordinatorV2: coordinator,
VRFV2Consumers: nil,
LinkToken: linkToken,
BHS: nil,
}
vrfKey := &vrfcommon.VRFKeyData{
VRFKey: nil,
EncodedProvingKey: [2]*big.Int{},
KeyHash: common.HexToHash(*commonExistingEnvConfig.KeyHash),
}
return vrfContracts, vrfKey, env, nil
}
func SetupSubsAndConsumersForExistingEnv(
env *test_env.CLClusterTestEnv,
chainID int64,
coordinator contracts.VRFCoordinatorV2,
linkToken contracts.LinkToken,
numberOfConsumerContractsToDeployAndAddToSub int,
numberOfSubToCreate int,
testConfig tc.TestConfig,
l zerolog.Logger,
) ([]uint64, []contracts.VRFv2LoadTestConsumer, error) {
var (
subIDs []uint64
consumers []contracts.VRFv2LoadTestConsumer
err error
)
if *testConfig.VRFv2.General.UseExistingEnv {
commonExistingEnvConfig := testConfig.VRFv2.ExistingEnvConfig.ExistingEnvConfig
if *commonExistingEnvConfig.CreateFundSubsAndAddConsumers {
consumers, subIDs, err = SetupNewConsumersAndSubs(
env,
chainID,
coordinator,
testConfig,
linkToken,
numberOfConsumerContractsToDeployAndAddToSub,
numberOfSubToCreate,
l,
)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
}
} else {
consumer, err := env.ContractLoader.LoadVRFv2LoadTestConsumer(*commonExistingEnvConfig.ConsumerAddress)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
}
consumers = append(consumers, consumer)
subIDs = append(subIDs, *testConfig.VRFv2.ExistingEnvConfig.SubID)
}
} else {
consumers, subIDs, err = SetupNewConsumersAndSubs(
env,
chainID,
coordinator,
testConfig,
linkToken,
numberOfConsumerContractsToDeployAndAddToSub,
numberOfSubToCreate,
l,
)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
}
}
return subIDs, consumers, nil
}