Skip to content

Commit

Permalink
VRF-1138: Make CTF tests to reuse existing VRF Wrapper (#13854)
Browse files Browse the repository at this point in the history
* VRF-1138: Make CTF tests to reuse existing VRF Wrapper

* VRF-1138: remove old code

* VRF-1138: remove comments

* VRF-1138: refactoring

* VRF-1138: pr comments

* VRF-1138: pr comments

* VRF-1138: fixing lint issues

* VRF-1138: PR comments
  • Loading branch information
iljapavlovs authored Aug 5, 2024
1 parent 05ef7fd commit 1ac2902
Show file tree
Hide file tree
Showing 19 changed files with 599 additions and 174 deletions.
6 changes: 5 additions & 1 deletion integration-tests/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ func RandBool() bool {
return rand.Intn(2) == 1
}

func ContinuouslyGenerateTXsOnChain(sethClient *seth.Client, stopChannel chan bool, l zerolog.Logger) (bool, error) {
func ContinuouslyGenerateTXsOnChain(sethClient *seth.Client, stopChannel chan bool, wg *sync.WaitGroup, l zerolog.Logger) (bool, error) {
counterContract, err := contracts.DeployCounterContract(sethClient)
if err != nil {
return false, err
Expand All @@ -1230,6 +1230,10 @@ func ContinuouslyGenerateTXsOnChain(sethClient *seth.Client, stopChannel chan bo
select {
case <-stopChannel:
l.Info().Str("Number of generated transactions on chain", count.String()).Msg("Stopping generating txs on chain. Desired block number reached.")
sleepDuration := time.Second * 10
l.Info().Str("Waiting for", sleepDuration.String()).Msg("Waiting for transactions to be mined and avoid nonce issues")
time.Sleep(sleepDuration)
wg.Done()
return true, nil
default:
err = counterContract.Increment()
Expand Down
30 changes: 30 additions & 0 deletions integration-tests/actions/vrf/common/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
ctf_test_env "github.com/smartcontractkit/chainlink-testing-framework/docker/test_env"
"github.com/smartcontractkit/chainlink-testing-framework/utils/conversions"
seth_utils "github.com/smartcontractkit/chainlink-testing-framework/utils/seth"
"github.com/smartcontractkit/chainlink-testing-framework/utils/testcontext"

"github.com/smartcontractkit/chainlink/integration-tests/actions"
"github.com/smartcontractkit/chainlink/integration-tests/client"
Expand Down Expand Up @@ -384,6 +385,35 @@ func BuildNewCLEnvForVRF(l zerolog.Logger, t *testing.T, envConfig VRFEnvConfig,
return env, sethClient, nil
}

func LoadExistingCLEnvForVRF(
t *testing.T,
envConfig VRFEnvConfig,
commonExistingEnvConfig *vrf_common_config.ExistingEnvConfig,
l zerolog.Logger,
) (*test_env.CLClusterTestEnv, *seth.Client, error) {
env, err := test_env.NewCLTestEnvBuilder().
WithTestInstance(t).
WithTestConfig(&envConfig.TestConfig).
WithCustomCleanup(envConfig.CleanupFn).
Build()
if err != nil {
return nil, nil, fmt.Errorf("%s, err: %w", "error creating test env", err)
}
evmNetwork, err := env.GetFirstEvmNetwork()
if err != nil {
return nil, nil, err
}
sethClient, err := seth_utils.GetChainClient(envConfig.TestConfig, *evmNetwork)
if err != nil {
return nil, nil, err
}
err = FundNodesIfNeeded(testcontext.Get(t), commonExistingEnvConfig, sethClient, l)
if err != nil {
return nil, nil, err
}
return env, sethClient, nil
}

func GetRPCUrl(env *test_env.CLClusterTestEnv, chainID int64) (string, error) {
provider, err := env.GetRpcProvider(chainID)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions integration-tests/actions/vrf/common/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type VRFContracts struct {
VRFV2PlusConsumer []contracts.VRFv2PlusLoadTestConsumer
LinkToken contracts.LinkToken
MockETHLINKFeed contracts.VRFMockETHLINKFeed
LinkNativeFeedAddress string
}

type VRFOwnerConfig struct {
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/actions/vrf/vrfv2/contract_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func SetupNewConsumersAndSubs(
) ([]contracts.VRFv2LoadTestConsumer, []uint64, error) {
consumers, err := DeployVRFV2Consumers(sethClient, coordinator.Address(), numberOfConsumerContractsToDeployAndAddToSub)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
return nil, nil, err
}
l.Info().
Str("Coordinator", *testConfig.VRFv2.ExistingEnvConfig.ExistingEnvConfig.CoordinatorAddress).
Expand All @@ -649,7 +649,7 @@ func SetupNewConsumersAndSubs(
numberOfSubToCreate,
)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
return nil, nil, err
}
return consumers, subIDs, nil
}
Expand Down
40 changes: 15 additions & 25 deletions integration-tests/actions/vrf/vrfv2/setup_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

"github.com/smartcontractkit/seth"

seth_utils "github.com/smartcontractkit/chainlink-testing-framework/utils/seth"

"github.com/ethereum/go-ethereum/common"
"github.com/rs/zerolog"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -371,38 +369,30 @@ func SetupVRFV2ForNewEnv(

func SetupVRFV2ForExistingEnv(t *testing.T, envConfig vrfcommon.VRFEnvConfig, l zerolog.Logger) (*vrfcommon.VRFContracts, *vrfcommon.VRFKeyData, *test_env.CLClusterTestEnv, *seth.Client, error) {
commonExistingEnvConfig := envConfig.TestConfig.VRFv2.ExistingEnvConfig.ExistingEnvConfig
env, err := test_env.NewCLTestEnvBuilder().
WithTestInstance(t).
WithTestConfig(&envConfig.TestConfig).
WithCustomCleanup(envConfig.CleanupFn).
Build()
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error creating test env", err)
}
evmNetwork, err := env.GetFirstEvmNetwork()
if err != nil {
return nil, nil, nil, nil, err
}
sethClient, err := seth_utils.GetChainClient(envConfig.TestConfig, *evmNetwork)
env, sethClient, err := vrfcommon.LoadExistingCLEnvForVRF(
t,
envConfig,
commonExistingEnvConfig,
l,
)
if err != nil {
return nil, nil, nil, nil, err
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error loading existing CL env", err)
}
coordinator, err := contracts.LoadVRFCoordinatorV2(sethClient, *commonExistingEnvConfig.ConsumerAddress)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error loading VRFCoordinator2", err)
}
linkAddr := common.HexToAddress(*commonExistingEnvConfig.LinkAddress)
linkToken, err := contracts.LoadLinkTokenContract(l, sethClient, linkAddr)
linkAddress, err := coordinator.GetLinkAddress(testcontext.Get(t))
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error loading LinkToken", err)
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error getting Link address from Coordinator", err)
}
err = vrfcommon.FundNodesIfNeeded(testcontext.Get(t), commonExistingEnvConfig, sethClient, l)
linkToken, err := contracts.LoadLinkTokenContract(l, sethClient, common.HexToAddress(linkAddress.String()))
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("err: %w", err)
return nil, nil, nil, nil, fmt.Errorf("%s, err: %w", "error loading LinkToken", err)
}
blockHashStoreAddress, err := coordinator.GetBlockHashStoreAddress(testcontext.Get(t))
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("err: %w", err)
return nil, nil, nil, nil, err
}
blockHashStore, err := contracts.LoadBlockHashStore(sethClient, blockHashStoreAddress.String())
if err != nil {
Expand Down Expand Up @@ -449,13 +439,13 @@ func SetupSubsAndConsumersForExistingEnv(
l,
)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
return nil, nil, err
}
} else {
addr := common.HexToAddress(*commonExistingEnvConfig.ConsumerAddress)
consumer, err := contracts.LoadVRFv2LoadTestConsumer(sethClient, addr)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
return nil, nil, err
}
consumers = append(consumers, consumer)
subIDs = append(subIDs, *testConfig.VRFv2.ExistingEnvConfig.SubID)
Expand All @@ -471,7 +461,7 @@ func SetupSubsAndConsumersForExistingEnv(
l,
)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
return nil, nil, err
}
}
return subIDs, consumers, nil
Expand Down
68 changes: 61 additions & 7 deletions integration-tests/actions/vrf/vrfv2plus/contract_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func DeployVRFV2_5Contracts(
}
batchCoordinator, err := contracts.DeployBatchVRFCoordinatorV2Plus(chainClient, coordinator.Address())
if err != nil {
return nil, fmt.Errorf("%s, err %w", ErrDeployBatchCoordinatorV2Plus, err)
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrDeployBatchCoordinatorV2Plus, err)
}
return &vrfcommon.VRFContracts{
CoordinatorV2Plus: coordinator,
Expand Down Expand Up @@ -407,7 +407,7 @@ func DeployVRFV2PlusDirectFundingContracts(
linkTokenAddress string,
linkEthFeedAddress string,
coordinator contracts.VRFCoordinatorV2_5,
consumerContractsAmount int,
numberOfConsumerContracts int,
wrapperSubId *big.Int,
configGeneral *vrfv2plusconfig.General,
) (*VRFV2PlusWrapperContracts, error) {
Expand All @@ -432,7 +432,7 @@ func DeployVRFV2PlusDirectFundingContracts(
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrDeployWrapper, err)
}
}
consumers, err := DeployVRFV2PlusWrapperConsumers(sethClient, vrfv2PlusWrapper, consumerContractsAmount)
consumers, err := DeployVRFV2PlusWrapperConsumers(sethClient, vrfv2PlusWrapper, numberOfConsumerContracts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -545,9 +545,9 @@ func WaitRandomWordsFulfilledEvent(
return randomWordsFulfilledEvent, err
}

func DeployVRFV2PlusWrapperConsumers(client *seth.Client, vrfV2PlusWrapper contracts.VRFV2PlusWrapper, consumerContractsAmount int) ([]contracts.VRFv2PlusWrapperLoadTestConsumer, error) {
func DeployVRFV2PlusWrapperConsumers(client *seth.Client, vrfV2PlusWrapper contracts.VRFV2PlusWrapper, numberOfConsumerContracts int) ([]contracts.VRFv2PlusWrapperLoadTestConsumer, error) {
var consumers []contracts.VRFv2PlusWrapperLoadTestConsumer
for i := 1; i <= consumerContractsAmount; i++ {
for i := 1; i <= numberOfConsumerContracts; i++ {
loadTestConsumer, err := contracts.DeployVRFV2PlusWrapperLoadTestConsumer(client, vrfV2PlusWrapper.Address())
if err != nil {
return nil, fmt.Errorf(vrfcommon.ErrGenericFormat, ErrAdvancedConsumer, err)
Expand Down Expand Up @@ -609,7 +609,7 @@ func SetupNewConsumersAndSubs(
) ([]contracts.VRFv2PlusLoadTestConsumer, []*big.Int, error) {
consumers, err := DeployVRFV2PlusConsumers(sethClient, coordinator, consumerContractsAmount)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
return nil, nil, err
}
l.Info().
Str("Coordinator", *testConfig.VRFv2Plus.ExistingEnvConfig.ExistingEnvConfig.CoordinatorAddress).
Expand All @@ -627,7 +627,7 @@ func SetupNewConsumersAndSubs(
*testConfig.VRFv2Plus.General.SubscriptionBillingType,
)
if err != nil {
return nil, nil, fmt.Errorf("err: %w", err)
return nil, nil, err
}
return consumers, subIDs, nil
}
Expand All @@ -652,3 +652,57 @@ func CancelSubsAndReturnFunds(ctx context.Context, vrfContracts *vrfcommon.VRFCo
}
}
}

func FundWrapperConsumer(
sethClient *seth.Client,
subFundingType string,
linkToken contracts.LinkToken,
wrapperConsumer contracts.VRFv2PlusWrapperLoadTestConsumer,
vrfv2PlusConfig *vrfv2plusconfig.General,
l zerolog.Logger,
) error {
fundConsumerWithLink := func() error {
//fund consumer with Link
linkAmount := big.NewInt(0).Mul(big.NewInt(1e18), big.NewInt(*vrfv2PlusConfig.WrapperConsumerFundingAmountLink))
l.Info().
Str("Link Amount", linkAmount.String()).
Str("WrapperConsumerAddress", wrapperConsumer.Address()).Msg("Funding WrapperConsumer with Link")
return linkToken.Transfer(
wrapperConsumer.Address(),
linkAmount,
)
}
fundConsumerWithNative := func() error {
//fund consumer with Eth (native token)
_, err := actions.SendFunds(l, sethClient, actions.FundsToSendPayload{
ToAddress: common.HexToAddress(wrapperConsumer.Address()),
Amount: conversions.EtherToWei(big.NewFloat(*vrfv2PlusConfig.WrapperConsumerFundingAmountNativeToken)),
PrivateKey: sethClient.PrivateKeys[0],
})
return err
}
switch vrfv2plusconfig.BillingType(subFundingType) {
case vrfv2plusconfig.BillingType_Link:
err := fundConsumerWithLink()
if err != nil {
return err
}
case vrfv2plusconfig.BillingType_Native:
err := fundConsumerWithNative()
if err != nil {
return err
}
case vrfv2plusconfig.BillingType_Link_and_Native:
err := fundConsumerWithLink()
if err != nil {
return err
}
err = fundConsumerWithNative()
if err != nil {
return err
}
default:
return fmt.Errorf("invalid billing type: %s", subFundingType)
}
return nil
}
4 changes: 2 additions & 2 deletions integration-tests/actions/vrf/vrfv2plus/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import (
)

type VRFV2PlusWrapperContracts struct {
VRFV2PlusWrapper contracts.VRFV2PlusWrapper
LoadTestConsumers []contracts.VRFv2PlusWrapperLoadTestConsumer
VRFV2PlusWrapper contracts.VRFV2PlusWrapper
WrapperConsumers []contracts.VRFv2PlusWrapperLoadTestConsumer
}
Loading

0 comments on commit 1ac2902

Please sign in to comment.