Skip to content

Commit

Permalink
VRF-1027: minor improvements in order to run VRF e2e test against exi… (
Browse files Browse the repository at this point in the history
#12898)

* VRF-1027: minor improvements in order to run VRF e2e test against existing envs

* VRF-1027: PR comments and tech debt fixes from Sonar

* VRF-1027: fixing lint

* VRF-1027: fixing tests
  • Loading branch information
iljapavlovs authored Apr 23, 2024
1 parent 1c35398 commit c48b61a
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 140 deletions.
1 change: 1 addition & 0 deletions integration-tests/actions/vrf/common/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

const (
ErrGenericFormat = "%s, err %w"
ErrNodePrimaryKey = "error getting node's primary ETH key"
ErrNodeNewTxKey = "error creating node's EVM transaction key"
ErrCreatingProvingKeyHash = "error creating a keyHash from the proving key"
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/actions/vrf/vrfv2/contract_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"
"github.com/shopspring/decimal"

"github.com/smartcontractkit/chainlink-testing-framework/blockchain"
"github.com/smartcontractkit/chainlink-testing-framework/utils/conversions"
Expand Down Expand Up @@ -212,7 +213,7 @@ func SetupVRFV2Contracts(
*vrfv2Config.MaxGasLimitCoordinatorConfig,
*vrfv2Config.StalenessSeconds,
*vrfv2Config.GasAfterPaymentCalculation,
big.NewInt(*vrfv2Config.FallbackWeiPerUnitLink),
decimal.RequireFromString(*vrfv2Config.FallbackWeiPerUnitLink).BigInt(),
vrfCoordinatorV2FeeConfig,
)
if err != nil {
Expand Down
111 changes: 39 additions & 72 deletions integration-tests/actions/vrf/vrfv2plus/contract_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"
"github.com/shopspring/decimal"

"github.com/smartcontractkit/chainlink-testing-framework/blockchain"
"github.com/smartcontractkit/chainlink-testing-framework/utils/conversions"
Expand All @@ -27,27 +28,27 @@ func DeployVRFV2_5Contracts(
) (*vrfcommon.VRFContracts, error) {
bhs, err := contractDeployer.DeployBlockhashStore()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrDeployBlockHashStore, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrDeployBlockHashStore, err)
}
err = chainClient.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}
batchBHS, err := contractDeployer.DeployBatchBlockhashStore(bhs.Address())
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrDeployBatchBlockHashStore, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrDeployBatchBlockHashStore, err)
}
err = chainClient.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("%s, batchBHS err %w", vrfcommon.ErrWaitTXsComplete, err)
}
coordinator, err := contractDeployer.DeployVRFCoordinatorV2_5(bhs.Address())
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrDeployCoordinator, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrDeployCoordinator, err)
}
err = chainClient.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}
return &vrfcommon.VRFContracts{
CoordinatorV2Plus: coordinator,
Expand All @@ -62,7 +63,7 @@ func DeployVRFV2PlusConsumers(contractDeployer contracts.ContractDeployer, coord
for i := 1; i <= consumerContractsAmount; i++ {
loadTestConsumer, err := contractDeployer.DeployVRFv2PlusLoadTestConsumer(coordinator.Address())
if err != nil {
return nil, fmt.Errorf("%s, err %w", ErrAdvancedConsumer, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrAdvancedConsumer, err)
}
consumers = append(consumers, loadTestConsumer)
}
Expand All @@ -76,14 +77,14 @@ func VRFV2_5RegisterProvingKey(
) (vrfcommon.VRFEncodedProvingKey, error) {
provingKey, err := actions.EncodeOnChainVRFProvingKey(*vrfKey)
if err != nil {
return vrfcommon.VRFEncodedProvingKey{}, fmt.Errorf("%s, err %w", vrfcommon.ErrEncodingProvingKey, err)
return vrfcommon.VRFEncodedProvingKey{}, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrEncodingProvingKey, err)
}
err = coordinator.RegisterProvingKey(
provingKey,
gasLaneMaxGas,
)
if err != nil {
return vrfcommon.VRFEncodedProvingKey{}, fmt.Errorf("%s, err %w", vrfcommon.ErrRegisterProvingKey, err)
return vrfcommon.VRFEncodedProvingKey{}, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrRegisterProvingKey, err)
}
return provingKey, nil
}
Expand All @@ -95,14 +96,14 @@ func VRFV2PlusUpgradedVersionRegisterProvingKey(
) (vrfcommon.VRFEncodedProvingKey, error) {
provingKey, err := actions.EncodeOnChainVRFProvingKey(*vrfKey)
if err != nil {
return vrfcommon.VRFEncodedProvingKey{}, fmt.Errorf("%s, err %w", vrfcommon.ErrEncodingProvingKey, err)
return vrfcommon.VRFEncodedProvingKey{}, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrEncodingProvingKey, err)
}
err = coordinator.RegisterProvingKey(
provingKey,
gasLaneMaxGasPrice,
)
if err != nil {
return vrfcommon.VRFEncodedProvingKey{}, fmt.Errorf("%s, err %w", vrfcommon.ErrRegisterProvingKey, err)
return vrfcommon.VRFEncodedProvingKey{}, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrRegisterProvingKey, err)
}
return provingKey, nil
}
Expand All @@ -116,11 +117,11 @@ func FundVRFCoordinatorV2_5Subscription(
) error {
encodedSubId, err := chainlinkutils.ABIEncode(`[{"type":"uint256"}]`, subscriptionID)
if err != nil {
return fmt.Errorf("%s, err %w", vrfcommon.ErrABIEncodingFunding, err)
return fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrABIEncodingFunding, err)
}
_, err = linkToken.TransferAndCall(coordinator.Address(), linkFundingAmountJuels, encodedSubId)
if err != nil {
return fmt.Errorf("%s, err %w", vrfcommon.ErrSendingLinkToken, err)
return fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrSendingLinkToken, err)
}
return chainClient.WaitForEvents()
}
Expand Down Expand Up @@ -169,7 +170,7 @@ func CreateFundSubsAndAddConsumers(

err = evmClient.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}
return subIDs, nil
}
Expand All @@ -194,7 +195,7 @@ func CreateSubsAndFund(

err = evmClient.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}
err = FundSubscriptions(
env,
Expand Down Expand Up @@ -237,7 +238,7 @@ func AddConsumersToSubs(
for _, consumer := range consumers {
err := coordinator.AddConsumer(subID, consumer.Address())
if err != nil {
return fmt.Errorf("%s, err %w", ErrAddConsumerToSub, err)
return fmt.Errorf(vrfcommon.ErrGenericFormat, ErrAddConsumerToSub, err)
}
}
}
Expand All @@ -247,20 +248,20 @@ func AddConsumersToSubs(
func CreateSubAndFindSubID(env *test_env.CLClusterTestEnv, chainID int64, coordinator contracts.VRFCoordinatorV2_5) (*big.Int, error) {
tx, err := coordinator.CreateSubscription()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrCreateVRFSubscription, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrCreateVRFSubscription, err)
}
evmClient, err := env.GetEVMClient(chainID)
if err != nil {
return nil, err
}
err = evmClient.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}

receipt, err := evmClient.GetTxReceipt(tx.Hash())
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}

//SubscriptionsCreated Log should be emitted with the subscription ID
Expand Down Expand Up @@ -291,42 +292,42 @@ func FundSubscriptions(
amountWei,
)
if err != nil {
return fmt.Errorf("%s, err %w", ErrFundSubWithNativeToken, err)
return fmt.Errorf(vrfcommon.ErrGenericFormat, ErrFundSubWithNativeToken, err)
}
//Link Billing
amountJuels := conversions.EtherToWei(subscriptionFundingAmountLink)
err = FundVRFCoordinatorV2_5Subscription(linkAddress, coordinator, evmClient, subID, amountJuels)
if err != nil {
return fmt.Errorf("%s, err %w", vrfcommon.ErrFundSubWithLinkToken, err)
return fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrFundSubWithLinkToken, err)
}
}
err = evmClient.WaitForEvents()
if err != nil {
return fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}
return nil
}

func GetUpgradedCoordinatorTotalBalance(coordinator contracts.VRFCoordinatorV2PlusUpgradedVersion) (linkTotalBalance *big.Int, nativeTokenTotalBalance *big.Int, err error) {
linkTotalBalance, err = coordinator.GetLinkTotalBalance(context.Background())
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", ErrLinkTotalBalance, err)
return nil, nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrLinkTotalBalance, err)
}
nativeTokenTotalBalance, err = coordinator.GetNativeTokenTotalBalance(context.Background())
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", ErrNativeTokenBalance, err)
return nil, nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrNativeTokenBalance, err)
}
return
}

func GetCoordinatorTotalBalance(coordinator contracts.VRFCoordinatorV2_5) (linkTotalBalance *big.Int, nativeTokenTotalBalance *big.Int, err error) {
linkTotalBalance, err = coordinator.GetLinkTotalBalance(context.Background())
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", ErrLinkTotalBalance, err)
return nil, nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrLinkTotalBalance, err)
}
nativeTokenTotalBalance, err = coordinator.GetNativeTokenTotalBalance(context.Background())
if err != nil {
return nil, nil, fmt.Errorf("%s, err %w", ErrNativeTokenBalance, err)
return nil, nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrNativeTokenBalance, err)
}
return
}
Expand Down Expand Up @@ -360,7 +361,7 @@ func RequestRandomness(
*config.RandomnessRequestCountPerRequest,
)
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrRequestRandomness, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrRequestRandomness, err)
}
vrfcommon.LogRandomnessRequestedEvent(l, coordinator, randomWordsRequestedEvent, isNativeBilling)

Expand Down Expand Up @@ -404,40 +405,6 @@ func RequestRandomnessAndWaitForFulfillment(

}

//func RequestRandomnessAndWaitForFulfillmentUpgraded(
// consumer contracts.VRFv2PlusLoadTestConsumer,
// coordinator contracts.Coordinator,
// vrfKeyData *vrfcommon.VRFKeyData,
// subID *big.Int,
// isNativeBilling bool,
// config *vrfv2plus_config.General,
// l zerolog.Logger,
//) (*contracts.CoordinatorRandomWordsFulfilled, error) {
//
// randomWordsRequestedEvent, err := RequestRandomness(
// consumer,
// coordinator,
// vrfKeyData,
// subID,
// isNativeBilling,
// config,
// l,
// )
//
// if err != nil {
// return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrRequestRandomness, err)
// }
//
// return WaitForRandomWordsFulfilledEventUpgraded(
// coordinator,
// randomWordsRequestedEvent.RequestId,
// subID,
// isNativeBilling,
// config.RandomWordsFulfilledEventTimeout.Duration,
// l,
// )
//}

func DeployVRFV2PlusDirectFundingContracts(
contractDeployer contracts.ContractDeployer,
chainClient blockchain.EVMClient,
Expand All @@ -450,11 +417,11 @@ func DeployVRFV2PlusDirectFundingContracts(

vrfv2PlusWrapper, err := contractDeployer.DeployVRFV2PlusWrapper(linkTokenAddress, linkEthFeedAddress, coordinator.Address(), wrapperSubId)
if err != nil {
return nil, fmt.Errorf("%s, err %w", ErrDeployWrapper, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrDeployWrapper, err)
}
err = chainClient.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}

consumers, err := DeployVRFV2PlusWrapperConsumers(contractDeployer, vrfv2PlusWrapper, consumerContractsAmount)
Expand All @@ -463,7 +430,7 @@ func DeployVRFV2PlusDirectFundingContracts(
}
err = chainClient.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}
return &VRFV2PlusWrapperContracts{vrfv2PlusWrapper, consumers}, nil
}
Expand All @@ -489,7 +456,7 @@ func WrapperRequestRandomness(consumer contracts.VRFv2PlusWrapperLoadTestConsume
*config.RandomnessRequestCountPerRequest,
)
if err != nil {
return nil, "", fmt.Errorf("%s, err %w", ErrRequestRandomnessDirectFundingNativePayment, err)
return nil, "", fmt.Errorf(vrfcommon.ErrGenericFormat, ErrRequestRandomnessDirectFundingNativePayment, err)
}
} else {
randomWordsRequestedEvent, err = consumer.RequestRandomness(
Expand All @@ -500,7 +467,7 @@ func WrapperRequestRandomness(consumer contracts.VRFv2PlusWrapperLoadTestConsume
*config.RandomnessRequestCountPerRequest,
)
if err != nil {
return nil, "", fmt.Errorf("%s, err %w", ErrRequestRandomnessDirectFundingLinkPayment, err)
return nil, "", fmt.Errorf(vrfcommon.ErrGenericFormat, ErrRequestRandomnessDirectFundingLinkPayment, err)
}
}
vrfcommon.LogRandomnessRequestedEvent(l, coordinator, randomWordsRequestedEvent, isNativeBilling)
Expand Down Expand Up @@ -551,7 +518,7 @@ func WaitRandomWordsFulfilledEvent(
},
)
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitRandomWordsFulfilledEvent, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitRandomWordsFulfilledEvent, err)
}

vrfcommon.LogRandomWordsFulfilledEvent(l, coordinator, randomWordsFulfilledEvent, isNativeBilling)
Expand All @@ -563,7 +530,7 @@ func DeployVRFV2PlusWrapperConsumers(contractDeployer contracts.ContractDeployer
for i := 1; i <= consumerContractsAmount; i++ {
loadTestConsumer, err := contractDeployer.DeployVRFV2PlusWrapperLoadTestConsumer(vrfV2PlusWrapper.Address())
if err != nil {
return nil, fmt.Errorf("%s, err %w", ErrAdvancedConsumer, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrAdvancedConsumer, err)
}
consumers = append(consumers, loadTestConsumer)
}
Expand All @@ -585,7 +552,7 @@ func SetupVRFV2PlusContracts(
}
vrfContracts, err := DeployVRFV2_5Contracts(env.ContractDeployer, evmClient)
if err != nil {
return nil, fmt.Errorf("%s, err %w", ErrDeployVRFV2_5Contracts, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrDeployVRFV2_5Contracts, err)
}
vrfContracts.LinkToken = linkToken
vrfContracts.MockETHLINKFeed = mockNativeLINKFeed
Expand All @@ -596,24 +563,24 @@ func SetupVRFV2PlusContracts(
*configGeneral.MaxGasLimitCoordinatorConfig,
*configGeneral.StalenessSeconds,
*configGeneral.GasAfterPaymentCalculation,
big.NewInt(*configGeneral.FallbackWeiPerUnitLink),
decimal.RequireFromString(*configGeneral.FallbackWeiPerUnitLink).BigInt(),
*configGeneral.FulfillmentFlatFeeNativePPM,
*configGeneral.FulfillmentFlatFeeLinkDiscountPPM,
*configGeneral.NativePremiumPercentage,
*configGeneral.LinkPremiumPercentage,
)
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrSetVRFCoordinatorConfig, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, 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, fmt.Errorf("%s, err %w", ErrSetLinkNativeLinkFeed, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrSetLinkNativeLinkFeed, err)
}
err = evmClient.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitTXsComplete, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, vrfcommon.ErrWaitTXsComplete, err)
}

return vrfContracts, nil
Expand Down
Loading

0 comments on commit c48b61a

Please sign in to comment.