Skip to content

Commit

Permalink
add integration tests for pending simulation block and zero confirmat…
Browse files Browse the repository at this point in the history
…ion delay (only v2 plus) and add simulation block option to superscript
  • Loading branch information
jinhoonbang committed Feb 13, 2024
1 parent 28917f7 commit 2a6ae45
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 8 deletions.
12 changes: 8 additions & 4 deletions core/scripts/common/vrf/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ vrf [type=vrfv2
estimate_gas [type=estimategaslimit
to="%s"
multiplier="%f"
data="$(vrf.output)"]
data="$(vrf.output)"
block=%s]
simulate [type=ethcall
from="%s"
to="%s"
gas="$(estimate_gas)"
gasPrice="$(jobSpec.maxGasPrice)"
extractRevertReason=true
contract="%s"
data="$(vrf.output)"]
data="$(vrf.output)"
block=%s]
decode_log->vrf->estimate_gas->simulate
"""`

Expand Down Expand Up @@ -66,15 +68,17 @@ generate_proof [type=vrfv2plus
estimate_gas [type=estimategaslimit
to="%s"
multiplier="%f"
data="$(generate_proof.output)"]
data="$(generate_proof.output)"
block=%s]
simulate_fulfillment [type=ethcall
from="%s"
to="%s"
gas="$(estimate_gas)"
gasPrice="$(jobSpec.maxGasPrice)"
extractRevertReason=true
contract="%s"
data="$(generate_proof.output)"]
data="$(generate_proof.output)"
block=%s]
decode_log->generate_proof->estimate_gas->simulate_fulfillment
"""
`
Expand Down
7 changes: 7 additions & 0 deletions core/scripts/common/vrf/setup-envs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func main() {
"from this address you can perform `coordinator.oracleWithdraw` to withdraw earned funds from rand request fulfilments")
deployVRFOwner := flag.Bool("deploy-vrfv2-owner", true, "whether to deploy VRF owner contracts")
useTestCoordinator := flag.Bool("use-test-coordinator", true, "whether to use test coordinator contract or use the normal one")
simulationBlock := flag.String("simulation-block", "pending", "simulation block can be 'pending' or 'latest'")

e := helpers.SetupEnv(false)
flag.Parse()
Expand All @@ -104,6 +105,10 @@ func main() {
}
fmt.Println("Using VRF Version:", *vrfVersion)

if *simulationBlock != "pending" && *simulationBlock != "latest" {
helpers.PanicErr(fmt.Errorf("simulation block must be 'pending' or 'latest'"))
}

fundingAmount := decimal.RequireFromString(*nodeSendingKeyFundingAmount).BigInt()
subscriptionBalanceJuels := decimal.RequireFromString(*subscriptionBalanceJuelsString).BigInt()
subscriptionBalanceNativeWei := decimal.RequireFromString(*subscriptionBalanceNativeWeiString).BigInt()
Expand Down Expand Up @@ -229,6 +234,7 @@ func main() {
*deployVRFOwner,
coordinatorJobSpecConfig,
*useTestCoordinator,
*simulationBlock,
)
case "v2plus":
coordinatorConfigV2Plus := v2plusscripts.CoordinatorConfigV2Plus{
Expand Down Expand Up @@ -263,6 +269,7 @@ func main() {
nodesMap,
uint64(*maxGasPriceGwei),
coordinatorJobSpecConfig,
*simulationBlock,
)
}

Expand Down
11 changes: 11 additions & 0 deletions core/scripts/vrfv2/testnet/v2scripts/super_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func DeployUniverseViaCLI(e helpers.Environment) {

deployVRFOwner := deployCmd.Bool("deploy-vrf-owner", true, "whether to deploy VRF owner contracts")
useTestCoordinator := deployCmd.Bool("use-test-coordinator", true, "whether to use test coordinator")
simulationBlock := deployCmd.String("simulation-block", "pending", "simulation block can be 'pending' or 'latest'")

// optional flags
fallbackWeiPerUnitLinkString := deployCmd.String("fallback-wei-per-unit-link", constants.FallbackWeiPerUnitLink.String(), "fallback wei/link ratio")
Expand All @@ -83,6 +84,10 @@ func DeployUniverseViaCLI(e helpers.Environment) {
reqsForTier4 := deployCmd.Int64("reqs-for-tier-4", constants.ReqsForTier4, "requests for tier 4")
reqsForTier5 := deployCmd.Int64("reqs-for-tier-5", constants.ReqsForTier5, "requests for tier 5")

if *simulationBlock != "pending" && *simulationBlock != "latest" {
helpers.PanicErr(fmt.Errorf("simulation block must be 'pending' or 'latest'"))
}

helpers.ParseArgs(
deployCmd, os.Args[2:],
)
Expand Down Expand Up @@ -162,6 +167,7 @@ func DeployUniverseViaCLI(e helpers.Environment) {
*deployVRFOwner,
coordinatorJobSpecConfig,
*useTestCoordinator,
*simulationBlock,
)

vrfPrimaryNode := nodesMap[model.VRFPrimaryNodeName]
Expand All @@ -181,6 +187,7 @@ func VRFV2DeployUniverse(
deployVRFOwner bool,
coordinatorJobSpecConfig model.CoordinatorJobSpecConfig,
useTestCoordinator bool,
simulationBlock string,
) model.JobSpecs {
var compressedPkHex string
var keyHash common.Hash
Expand Down Expand Up @@ -347,6 +354,7 @@ func VRFV2DeployUniverse(
coordinatorJobSpecConfig.RequestTimeout, //requestTimeout
contractAddresses.CoordinatorAddress,
coordinatorJobSpecConfig.EstimateGasMultiplier, //estimateGasMultiplier
simulationBlock,
func() string {
if keys := nodesMap[model.VRFPrimaryNodeName].SendingKeys; len(keys) > 0 {
return keys[0].Address
Expand All @@ -355,6 +363,7 @@ func VRFV2DeployUniverse(
}(),
contractAddresses.CoordinatorAddress,
contractAddresses.CoordinatorAddress,
simulationBlock,
)
if deployVRFOwner {
formattedVrfPrimaryJobSpec = strings.Replace(formattedVrfPrimaryJobSpec,
Expand All @@ -378,6 +387,7 @@ func VRFV2DeployUniverse(
coordinatorJobSpecConfig.RequestTimeout, //requestTimeout
contractAddresses.CoordinatorAddress,
coordinatorJobSpecConfig.EstimateGasMultiplier, //estimateGasMultiplier
simulationBlock,
func() string {
if keys := nodesMap[model.VRFPrimaryNodeName].SendingKeys; len(keys) > 0 {
return keys[0].Address
Expand All @@ -386,6 +396,7 @@ func VRFV2DeployUniverse(
}(),
contractAddresses.CoordinatorAddress,
contractAddresses.CoordinatorAddress,
simulationBlock,
)
if deployVRFOwner {
formattedVrfBackupJobSpec = strings.Replace(formattedVrfBackupJobSpec,
Expand Down
11 changes: 11 additions & 0 deletions core/scripts/vrfv2plus/testnet/v2plusscripts/super_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ func DeployUniverseViaCLI(e helpers.Environment) {
estimateGasMultiplier := deployCmd.Float64("estimate-gas-multiplier", 1.1, "")
pollPeriod := deployCmd.String("poll-period", "300ms", "")
requestTimeout := deployCmd.String("request-timeout", "30m0s", "")
simulationBlock := deployCmd.String("simulation-block", "pending", "simulation block can be 'pending' or 'latest'")

// optional flags
fallbackWeiPerUnitLinkString := deployCmd.String("fallback-wei-per-unit-link", "6e16", "fallback wei/link ratio")
Expand Down Expand Up @@ -517,6 +518,10 @@ func DeployUniverseViaCLI(e helpers.Environment) {
}
}

if *simulationBlock != "pending" && *simulationBlock != "latest" {
helpers.PanicErr(fmt.Errorf("simulation block must be 'pending' or 'latest'"))
}

fallbackWeiPerUnitLink := decimal.RequireFromString(*fallbackWeiPerUnitLinkString).BigInt()
subscriptionBalanceJuels := decimal.RequireFromString(*subscriptionBalanceJuelsString).BigInt()
subscriptionBalanceNativeWei := decimal.RequireFromString(*subscriptionBalanceNativeWeiString).BigInt()
Expand Down Expand Up @@ -584,6 +589,7 @@ func DeployUniverseViaCLI(e helpers.Environment) {
nodesMap,
uint64(*gasLaneMaxGas),
coordinatorJobSpecConfig,
*simulationBlock,
)

vrfPrimaryNode := nodesMap[model.VRFPrimaryNodeName]
Expand All @@ -604,6 +610,7 @@ func VRFV2PlusDeployUniverse(e helpers.Environment,
nodesMap map[string]model.Node,
gasLaneMaxGas uint64,
coordinatorJobSpecConfig model.CoordinatorJobSpecConfig,
simulationBlock string,
) model.JobSpecs {
var compressedPkHex string
var keyHash common.Hash
Expand Down Expand Up @@ -741,6 +748,7 @@ func VRFV2PlusDeployUniverse(e helpers.Environment,
coordinatorJobSpecConfig.RequestTimeout, //requestTimeout
contractAddresses.CoordinatorAddress,
coordinatorJobSpecConfig.EstimateGasMultiplier, //estimateGasMultiplier
simulationBlock,
func() string {
if keys := nodesMap[model.VRFPrimaryNodeName].SendingKeys; len(keys) > 0 {
return keys[0].Address
Expand All @@ -749,6 +757,7 @@ func VRFV2PlusDeployUniverse(e helpers.Environment,
}(),
contractAddresses.CoordinatorAddress,
contractAddresses.CoordinatorAddress,
simulationBlock,
)

formattedVrfV2PlusBackupJobSpec := fmt.Sprintf(
Expand All @@ -765,6 +774,7 @@ func VRFV2PlusDeployUniverse(e helpers.Environment,
coordinatorJobSpecConfig.RequestTimeout, //requestTimeout
contractAddresses.CoordinatorAddress,
coordinatorJobSpecConfig.EstimateGasMultiplier, //estimateGasMultiplier
simulationBlock,
func() string {
if keys := nodesMap[model.VRFPrimaryNodeName].SendingKeys; len(keys) > 0 {
return keys[0].Address
Expand All @@ -773,6 +783,7 @@ func VRFV2PlusDeployUniverse(e helpers.Environment,
}(),
contractAddresses.CoordinatorAddress,
contractAddresses.CoordinatorAddress,
simulationBlock,
)

formattedBHSJobSpec := fmt.Sprintf(
Expand Down
4 changes: 0 additions & 4 deletions core/services/pipeline/task.eth_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func TestETHCallTask(t *testing.T) {
nil, // inputs
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) { // setupClientMocks
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
//[]byte("baz quux")
ethClient.
On("CallContext",
mock.Anything,
Expand Down Expand Up @@ -108,7 +107,6 @@ func TestETHCallTask(t *testing.T) {
nil,
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) { // setupClientMocks
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
//[]byte("baz quux")
ethClient.
On("CallContext",
mock.Anything,
Expand Down Expand Up @@ -147,7 +145,6 @@ func TestETHCallTask(t *testing.T) {
nil,
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) { // setupClientMocks
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
//[]byte("baz quux")
ethClient.
On("CallContext",
mock.Anything,
Expand Down Expand Up @@ -187,7 +184,6 @@ func TestETHCallTask(t *testing.T) {
func(ethClient *evmclimocks.Client, config *pipelinemocks.Config) { // setupClientMocks
contractAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
fromAddr := common.HexToAddress("0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF")
//[]byte("baz quux")
ethClient.
On("CallContext",
mock.Anything,
Expand Down
1 change: 1 addition & 0 deletions integration-tests/actions/vrf/vrfv2/vrfv2_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func setupVRFNode(contracts *vrfcommon.VRFContracts, chainID *big.Int, vrfv2Conf
BatchFulfillmentGasMultiplier: *vrfv2Config.VRFJobBatchFulfillmentGasMultiplier,
PollPeriod: vrfv2Config.VRFJobPollPeriod.Duration,
RequestTimeout: vrfv2Config.VRFJobRequestTimeout.Duration,
SimulationBlock: vrfv2Config.VRFJobSimulationBlock,
VRFOwnerConfig: vrfOwnerConfig,
}

Expand Down
1 change: 1 addition & 0 deletions integration-tests/actions/vrf/vrfv2plus/vrfv2plus_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ func setupVRFNode(contracts *vrfcommon.VRFContracts, chainID *big.Int, vrfv2Conf
BatchFulfillmentGasMultiplier: *vrfv2Config.VRFJobBatchFulfillmentGasMultiplier,
PollPeriod: vrfv2Config.VRFJobPollPeriod.Duration,
RequestTimeout: vrfv2Config.VRFJobRequestTimeout.Duration,
SimulationBlock: vrfv2Config.VRFJobSimulationBlock,
VRFOwnerConfig: nil,
}

Expand Down
86 changes: 86 additions & 0 deletions integration-tests/smoke/vrfv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,3 +866,89 @@ func TestVRFV2WithBHS(t *testing.T) {
require.Equal(t, 0, randomWordsRequestedEvent.Raw.BlockHash.Cmp(randRequestBlockHash))
})
}

func TestVRFV2PendingBlockSimulation(t *testing.T) {
t.Parallel()
l := logging.GetTestLogger(t)

config, err := tc.GetConfig("Smoke", tc.VRFv2)
require.NoError(t, err, "Error getting config")

config.VRFv2.General.VRFJobSimulationBlock = ptr.Ptr[string]("pending")
useVRFOwner := false
useTestCoordinator := false
network, err := actions.EthereumNetworkConfigFromConfig(l, &config)
require.NoError(t, err, "Error building ethereum network config")

env, err := test_env.NewCLTestEnvBuilder().
WithTestInstance(t).
WithTestConfig(&config).
WithPrivateEthereumNetwork(network).
WithCLNodes(1).
WithFunding(big.NewFloat(*config.Common.ChainlinkNodeFunding)).
WithStandardCleanup().
Build()
require.NoError(t, err, "error creating test env")

env.ParallelTransactions(true)

mockETHLinkFeed, err := actions.DeployMockETHLinkFeed(env.ContractDeployer, big.NewInt(*config.VRFv2.General.LinkNativeFeedResponse))
require.NoError(t, err)
linkToken, err := actions.DeployLINKToken(env.ContractDeployer)
require.NoError(t, err)

// register proving key against oracle address (sending key) in order to test oracleWithdraw
defaultWalletAddress := env.EVMClient.GetDefaultWallet().Address()

numberOfTxKeysToCreate := 1
vrfv2Contracts, subIDs, vrfv2KeyData, nodesMap, err := vrfv2.SetupVRFV2Environment(
env,
[]vrfcommon.VRFNodeType{vrfcommon.VRF},
&config,
useVRFOwner,
useTestCoordinator,
linkToken,
mockETHLinkFeed,
defaultWalletAddress,
numberOfTxKeysToCreate,
1,
1,
l,
)
require.NoError(t, err, "error setting up VRF v2 env")

subID := subIDs[0]

subscription, err := vrfv2Contracts.CoordinatorV2.GetSubscription(testcontext.Get(t), subID)
require.NoError(t, err, "error getting subscription information")

vrfv2.LogSubDetails(l, subscription, subID, vrfv2Contracts.CoordinatorV2)

jobRunsBeforeTest, err := nodesMap[vrfcommon.VRF].CLNode.API.MustReadRunsByJob(nodesMap[vrfcommon.VRF].Job.Data.ID)
require.NoError(t, err, "error reading job runs")

// test and assert
randomWordsFulfilledEvent, err := vrfv2.RequestRandomnessAndWaitForFulfillment(
l,
vrfv2Contracts.VRFV2Consumer[0],
vrfv2Contracts.CoordinatorV2,
subID,
vrfv2KeyData,
*config.VRFv2.General.MinimumConfirmations,
*config.VRFv2.General.CallbackGasLimit,
*config.VRFv2.General.NumberOfWords,
*config.VRFv2.General.RandomnessRequestCountPerRequest,
*config.VRFv2.General.RandomnessRequestCountPerRequestDeviation,
config.VRFv2.General.RandomWordsFulfilledEventTimeout.Duration,
)
require.NoError(t, err, "error requesting randomness and waiting for fulfilment")

jobRuns, err := nodesMap[vrfcommon.VRF].CLNode.API.MustReadRunsByJob(nodesMap[vrfcommon.VRF].Job.Data.ID)
require.NoError(t, err, "error reading job runs")
require.Equal(t, len(jobRunsBeforeTest.Data)+1, len(jobRuns.Data))

status, err := vrfv2Contracts.VRFV2Consumer[0].GetRequestStatus(testcontext.Get(t), randomWordsFulfilledEvent.RequestId)
require.NoError(t, err, "error getting rand request status")
require.True(t, status.Fulfilled)
l.Debug().Bool("Fulfilment Status", status.Fulfilled).Msg("Random Words Request Fulfilment Status")
}
Loading

0 comments on commit 2a6ae45

Please sign in to comment.