Skip to content

Commit

Permalink
VRF-881: fixing sonar and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
iljapavlovs committed Feb 24, 2024
1 parent 214acd0 commit 1fe0b96
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 87 deletions.
26 changes: 26 additions & 0 deletions integration-tests/actions/vrf/common/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,29 @@ func retrieveLoadTestMetrics(
}
metricsChannel <- metrics
}

func CreateNodeTypeToNodeMap(cluster *test_env.ClCluster, nodesToCreate []VRFNodeType) map[VRFNodeType]*VRFNode {
var nodesMap = make(map[VRFNodeType]*VRFNode)
for i, nodeType := range nodesToCreate {
nodesMap[nodeType] = &VRFNode{
CLNode: cluster.Nodes[i],
}
}
return nodesMap
}

func CreateVRFKeyOnVRFNode(vrfNode *VRFNode, l zerolog.Logger) (*client.VRFKey, string, error) {
l.Info().Str("Node URL", vrfNode.CLNode.API.URL()).Msg("Creating VRF Key on the Node")
vrfKey, err := vrfNode.CLNode.API.MustCreateVRFKey()
if err != nil {
return nil, "", fmt.Errorf("%s, err %w", ErrCreatingVRFKey, err)
}
pubKeyCompressed := vrfKey.Data.ID
l.Info().
Str("Node URL", vrfNode.CLNode.API.URL()).
Str("Keyhash", vrfKey.Data.Attributes.Hash).
Str("VRF Compressed Key", vrfKey.Data.Attributes.Compressed).
Str("VRF Uncompressed Key", vrfKey.Data.Attributes.Uncompressed).
Msg("VRF Key created on the Node")
return vrfKey, pubKeyCompressed, nil
}
1 change: 1 addition & 0 deletions integration-tests/actions/vrf/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
ErrWaitTXsComplete = "error waiting for TXs to complete"
ErrRequestRandomness = "error requesting randomness"
ErrLoadingCoordinator = "error loading coordinator contract"
ErrCreatingVRFKey = "error creating VRF key"

ErrWaitRandomWordsRequestedEvent = "error waiting for RandomWordsRequested event"
ErrWaitRandomWordsFulfilledEvent = "error waiting for RandomWordsFulfilled event"
Expand Down
1 change: 0 additions & 1 deletion integration-tests/actions/vrf/vrfv2/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package vrfv2

const (
ErrCreatingVRFv2Key = "error creating VRFv2 key"
ErrDeployVRFV2Wrapper = "error deploying VRFV2Wrapper"
ErrCreateVRFV2Jobs = "error creating VRF V2 Jobs"
ErrDeployVRFV2Contracts = "error deploying VRFV2 contracts"
Expand Down
39 changes: 15 additions & 24 deletions integration-tests/actions/vrf/vrfv2/vrfv2_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ func SetupVRFV2Environment(
) (*vrfcommon.VRFContracts, []uint64, *vrfcommon.VRFKeyData, map[vrfcommon.VRFNodeType]*vrfcommon.VRFNode, error) {
l.Info().Msg("Starting VRFV2 environment setup")
configGeneral := vrfv2TestConfig.GetVRFv2Config().General

vrfContracts, subIDs, err := SetupContracts(
vrfContracts, subIDs, err := SetupVRFV2Contracts(
env,
linkToken,
mockNativeLINKFeed,
Expand All @@ -277,24 +276,11 @@ func SetupVRFV2Environment(
return nil, nil, nil, nil, err
}

var nodesMap = make(map[vrfcommon.VRFNodeType]*vrfcommon.VRFNode)
for i, nodeType := range nodesToCreate {
nodesMap[nodeType] = &vrfcommon.VRFNode{
CLNode: env.ClCluster.Nodes[i],
}
}
l.Info().Str("Node URL", nodesMap[vrfcommon.VRF].CLNode.API.URL()).Msg("Creating VRF Key on the Node")
vrfKey, err := nodesMap[vrfcommon.VRF].CLNode.API.MustCreateVRFKey()
nodeTypeToNodeMap := vrfcommon.CreateNodeTypeToNodeMap(env.ClCluster, nodesToCreate)
vrfKey, pubKeyCompressed, err := vrfcommon.CreateVRFKeyOnVRFNode(nodeTypeToNodeMap[vrfcommon.VRF], l)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err %w", ErrCreatingVRFv2Key, err)
return nil, nil, nil, nil, err
}
pubKeyCompressed := vrfKey.Data.ID
l.Info().
Str("Node URL", nodesMap[vrfcommon.VRF].CLNode.API.URL()).
Str("Keyhash", vrfKey.Data.Attributes.Hash).
Str("VRF Compressed Key", vrfKey.Data.Attributes.Compressed).
Str("VRF Uncompressed Key", vrfKey.Data.Attributes.Uncompressed).
Msg("VRF Key created on the Node")

l.Info().Str("Coordinator", vrfContracts.CoordinatorV2.Address()).Msg("Registering Proving Key")
provingKey, err := VRFV2RegisterProvingKey(vrfKey, registerProvingKeyAgainstAddress, vrfContracts.CoordinatorV2)
Expand All @@ -309,15 +295,20 @@ func SetupVRFV2Environment(
chainID := env.EVMClient.GetChainID()
vrfTXKeyAddressStrings, vrfTXKeyAddresses, err := vrfcommon.CreateFundAndGetSendingKeys(
env.EVMClient,
nodesMap[vrfcommon.VRF],
nodeTypeToNodeMap[vrfcommon.VRF],
*vrfv2TestConfig.GetCommonConfig().ChainlinkNodeFunding,
numberOfTxKeysToCreate,
chainID,
)
if err != nil {
return nil, nil, nil, nil, err
}
nodesMap[vrfcommon.VRF].TXKeyAddressStrings = vrfTXKeyAddressStrings
err = env.EVMClient.WaitForEvents()
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
}

nodeTypeToNodeMap[vrfcommon.VRF].TXKeyAddressStrings = vrfTXKeyAddressStrings

var vrfOwnerConfig *vrfcommon.VRFOwnerConfig
if useVRFOwner {
Expand All @@ -337,7 +328,7 @@ func SetupVRFV2Environment(
}

g := errgroup.Group{}
if vrfNode, exists := nodesMap[vrfcommon.VRF]; exists {
if vrfNode, exists := nodeTypeToNodeMap[vrfcommon.VRF]; exists {
g.Go(func() error {
err := setupVRFNode(vrfContracts, chainID, configGeneral, pubKeyCompressed, vrfOwnerConfig, l, vrfNode)
if err != nil {
Expand All @@ -347,7 +338,7 @@ func SetupVRFV2Environment(
})
}

if bhsNode, exists := nodesMap[vrfcommon.BHS]; exists {
if bhsNode, exists := nodeTypeToNodeMap[vrfcommon.BHS]; exists {
g.Go(func() error {
err := vrfcommon.SetupBHSNode(
env,
Expand Down Expand Up @@ -378,7 +369,7 @@ func SetupVRFV2Environment(
}

l.Info().Msg("VRFV2 environment setup is finished")
return vrfContracts, subIDs, &vrfKeyData, nodesMap, nil
return vrfContracts, subIDs, &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 {
Expand Down Expand Up @@ -423,7 +414,7 @@ func setupVRFNode(contracts *vrfcommon.VRFContracts, chainID *big.Int, vrfv2Conf
return nil
}

func SetupContracts(
func SetupVRFV2Contracts(
env *test_env.CLClusterTestEnv,
linkToken contracts.LinkToken,
mockNativeLINKFeed contracts.MockETHLINKFeed,
Expand Down
130 changes: 69 additions & 61 deletions integration-tests/actions/vrf/vrfv2plus/vrfv2plus_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
commonassets "github.com/smartcontractkit/chainlink-common/pkg/assets"
"github.com/smartcontractkit/chainlink-testing-framework/utils/conversions"
vrfcommon "github.com/smartcontractkit/chainlink/integration-tests/actions/vrf/common"
testconfig "github.com/smartcontractkit/chainlink/integration-tests/testconfig/vrfv2plus"
"github.com/smartcontractkit/chainlink/integration-tests/types/config/node"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/vrfv2plus_wrapper_load_test_consumer"
Expand Down Expand Up @@ -112,7 +113,6 @@ func CreateVRFV2PlusJob(
if err != nil {
return nil, fmt.Errorf("%s, err %w", ErrCreatingVRFv2PlusJob, err)
}

return job, nil
}

Expand Down Expand Up @@ -183,72 +183,25 @@ func SetupVRFV2_5Environment(
l zerolog.Logger,
) (*vrfcommon.VRFContracts, []*big.Int, *vrfcommon.VRFKeyData, map[vrfcommon.VRFNodeType]*vrfcommon.VRFNode, error) {
l.Info().Msg("Starting VRFV2 Plus environment setup")
l.Info().Msg("Deploying VRFV2 Plus contracts")
vrfContracts, err := DeployVRFV2_5Contracts(env.ContractDeployer, env.EVMClient, numberOfConsumers)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err %w", ErrDeployVRFV2_5Contracts, err)
}

l.Info().Str("Coordinator", vrfContracts.CoordinatorV2Plus.Address()).Msg("Setting Coordinator Config")
configGeneral := vrfv2PlusTestConfig.GetVRFv2PlusConfig().General
err = vrfContracts.CoordinatorV2Plus.SetConfig(
*configGeneral.MinimumConfirmations,
*configGeneral.MaxGasLimitCoordinatorConfig,
*configGeneral.StalenessSeconds,
*configGeneral.GasAfterPaymentCalculation,
big.NewInt(*configGeneral.FallbackWeiPerUnitLink),
*configGeneral.FulfillmentFlatFeeNativePPM,
*configGeneral.FulfillmentFlatFeeLinkDiscountPPM,
*configGeneral.NativePremiumPercentage,
*configGeneral.LinkPremiumPercentage,
)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrSetVRFCoordinatorConfig, err)
}

l.Info().Str("Coordinator", vrfContracts.CoordinatorV2Plus.Address()).Msg("Setting Link and ETH/LINK feed")
err = vrfContracts.CoordinatorV2Plus.SetLINKAndLINKNativeFeed(linkToken.Address(), mockNativeLINKFeed.Address())
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err %w", ErrSetLinkNativeLinkFeed, err)
}
err = env.EVMClient.WaitForEvents()
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
}
l.Info().
Str("Coordinator", vrfContracts.CoordinatorV2Plus.Address()).
Int("Number of Subs to create", numberOfSubToCreate).
Msg("Creating and funding subscriptions, adding consumers")
subIDs, err := CreateFundSubsAndAddConsumers(
vrfContracts, subIDs, err := SetupVRFV2PlusContracts(
env,
big.NewFloat(*configGeneral.SubscriptionFundingAmountNative),
big.NewFloat(*configGeneral.SubscriptionFundingAmountLink),
linkToken,
vrfContracts.CoordinatorV2Plus, vrfContracts.VRFV2PlusConsumer,
mockNativeLINKFeed,
configGeneral,
numberOfSubToCreate,
numberOfConsumers,
l,
)
if err != nil {
return nil, nil, nil, nil, err
}

var nodesMap = make(map[vrfcommon.VRFNodeType]*vrfcommon.VRFNode)
for i, nodeType := range nodesToCreate {
nodesMap[nodeType] = &vrfcommon.VRFNode{
CLNode: env.ClCluster.Nodes[i],
}
}
l.Info().Str("Node URL", nodesMap[vrfcommon.VRF].CLNode.API.URL()).Msg("Creating VRF Key on the Node")
vrfKey, err := nodesMap[vrfcommon.VRF].CLNode.API.MustCreateVRFKey()
nodeTypeToNodeMap := vrfcommon.CreateNodeTypeToNodeMap(env.ClCluster, nodesToCreate)
vrfKey, pubKeyCompressed, err := vrfcommon.CreateVRFKeyOnVRFNode(nodeTypeToNodeMap[vrfcommon.VRF], l)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err %w", ErrCreatingVRFv2PlusKey, err)
return nil, nil, nil, nil, err
}
pubKeyCompressed := vrfKey.Data.ID
l.Info().
Str("Node URL", nodesMap[vrfcommon.VRF].CLNode.API.URL()).
Str("Keyhash", vrfKey.Data.Attributes.Hash).
Str("VRF Compressed Key", vrfKey.Data.Attributes.Compressed).
Str("VRF Uncompressed Key", vrfKey.Data.Attributes.Uncompressed).
Msg("VRF Key created on the Node")

l.Info().Str("Coordinator", vrfContracts.CoordinatorV2Plus.Address()).Msg("Registering Proving Key")
provingKey, err := VRFV2_5RegisterProvingKey(vrfKey, vrfContracts.CoordinatorV2Plus, uint64(assets.GWei(*configGeneral.CLNodeMaxGasPriceGWei).Int64()))
Expand All @@ -263,7 +216,7 @@ func SetupVRFV2_5Environment(
chainID := env.EVMClient.GetChainID()
vrfTXKeyAddressStrings, _, err := vrfcommon.CreateFundAndGetSendingKeys(
env.EVMClient,
nodesMap[vrfcommon.VRF],
nodeTypeToNodeMap[vrfcommon.VRF],
*vrfv2PlusTestConfig.GetCommonConfig().ChainlinkNodeFunding,
numberOfTxKeysToCreate,
chainID,
Expand All @@ -276,10 +229,10 @@ func SetupVRFV2_5Environment(
return nil, nil, nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
}

nodesMap[vrfcommon.VRF].TXKeyAddressStrings = vrfTXKeyAddressStrings
nodeTypeToNodeMap[vrfcommon.VRF].TXKeyAddressStrings = vrfTXKeyAddressStrings

g := errgroup.Group{}
if vrfNode, exists := nodesMap[vrfcommon.VRF]; exists {
if vrfNode, exists := nodeTypeToNodeMap[vrfcommon.VRF]; exists {
g.Go(func() error {
err := setupVRFNode(vrfContracts, chainID, configGeneral, pubKeyCompressed, l, vrfNode)
if err != nil {
Expand All @@ -289,7 +242,7 @@ func SetupVRFV2_5Environment(
})
}

if bhsNode, exists := nodesMap[vrfcommon.BHS]; exists {
if bhsNode, exists := nodeTypeToNodeMap[vrfcommon.BHS]; exists {
g.Go(func() error {
err := vrfcommon.SetupBHSNode(
env,
Expand Down Expand Up @@ -320,7 +273,62 @@ func SetupVRFV2_5Environment(
}

l.Info().Msg("VRFV2 Plus environment setup is finished")
return vrfContracts, subIDs, &vrfKeyData, nodesMap, nil
return vrfContracts, subIDs, &vrfKeyData, nodeTypeToNodeMap, nil
}

func SetupVRFV2PlusContracts(
env *test_env.CLClusterTestEnv,
linkToken contracts.LinkToken,
mockNativeLINKFeed contracts.MockETHLINKFeed,
configGeneral *testconfig.General,
numberOfSubToCreate int,
numberOfConsumers int,
l zerolog.Logger,
) (*vrfcommon.VRFContracts, []*big.Int, error) {
l.Info().Msg("Deploying VRFV2 Plus contracts")
vrfContracts, err := DeployVRFV2_5Contracts(env.ContractDeployer, env.EVMClient, numberOfConsumers)
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", ErrDeployVRFV2_5Contracts, err)
}

l.Info().Str("Coordinator", vrfContracts.CoordinatorV2Plus.Address()).Msg("Setting Coordinator Config")
err = vrfContracts.CoordinatorV2Plus.SetConfig(
*configGeneral.MinimumConfirmations,
*configGeneral.MaxGasLimitCoordinatorConfig,
*configGeneral.StalenessSeconds,
*configGeneral.GasAfterPaymentCalculation,
big.NewInt(*configGeneral.FallbackWeiPerUnitLink),
*configGeneral.FulfillmentFlatFeeNativePPM,
*configGeneral.FulfillmentFlatFeeLinkDiscountPPM,
*configGeneral.NativePremiumPercentage,
*configGeneral.LinkPremiumPercentage,
)
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrSetVRFCoordinatorConfig, err)
}

l.Info().Str("Coordinator", vrfContracts.CoordinatorV2Plus.Address()).Msg("Setting Link and ETH/LINK feed")
err = vrfContracts.CoordinatorV2Plus.SetLINKAndLINKNativeFeed(linkToken.Address(), mockNativeLINKFeed.Address())
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", ErrSetLinkNativeLinkFeed, err)
}
err = env.EVMClient.WaitForEvents()
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
}
l.Info().
Str("Coordinator", vrfContracts.CoordinatorV2Plus.Address()).
Int("Number of Subs to create", numberOfSubToCreate).
Msg("Creating and funding subscriptions, adding consumers")
subIDs, err := CreateFundSubsAndAddConsumers(

Check failure on line 323 in integration-tests/actions/vrf/vrfv2plus/vrfv2plus_steps.go

View workflow job for this annotation

GitHub Actions / Build and Lint integration-tests (integration-tests, ./integration-tests, e2e)

ineffectual assignment to err (ineffassign)
env,
big.NewFloat(*configGeneral.SubscriptionFundingAmountNative),
big.NewFloat(*configGeneral.SubscriptionFundingAmountLink),
linkToken,
vrfContracts.CoordinatorV2Plus, vrfContracts.VRFV2PlusConsumer,
numberOfSubToCreate,
)
return vrfContracts, subIDs, nil
}

func setupVRFNode(contracts *vrfcommon.VRFContracts, chainID *big.Int, config *vrfv2plus_config.General, pubKeyCompressed string, l zerolog.Logger, vrfNode *vrfcommon.VRFNode) error {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/testconfig/vrfv2plus/vrfv2plus.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ link_premium_percentage=1

# VRF Job config
vrf_job_forwarding_allowed = false
vrf_job_estimate_gas_multiplier = 1.0
vrf_job_estimate_gas_multiplier = 1.1
vrf_job_batch_fulfillment_enabled = false
vrf_job_batch_fulfillment_gas_multiplier = 1.15
vrf_job_poll_period = "1s"
Expand Down

0 comments on commit 1fe0b96

Please sign in to comment.