Skip to content

Commit

Permalink
VRF-961: refactor VRF V2 e2e tests - do not wait for RandomWordsReque… (
Browse files Browse the repository at this point in the history
#12610)

* VRF-961: refactor VRF V2 e2e tests - do not wait for RandomWordsRequested event, but get event from the TX receipt

* VRF-961: fixing Sonar

* VRF-961: removing comments
  • Loading branch information
iljapavlovs authored Mar 27, 2024
1 parent 5cd9689 commit c070345
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 146 deletions.
153 changes: 38 additions & 115 deletions integration-tests/actions/vrf/vrfv2/contract_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ func FundVRFCoordinatorV2Subscription(
}

func DirectFundingRequestRandomnessAndWaitForFulfillment(
ctx context.Context,
l zerolog.Logger,
consumer contracts.VRFv2WrapperLoadTestConsumer,
coordinator contracts.VRFCoordinatorV2,
Expand All @@ -475,7 +474,8 @@ func DirectFundingRequestRandomnessAndWaitForFulfillment(
randomnessRequestCountPerRequestDeviation,
vrfv2KeyData.KeyHash,
)
_, err := consumer.RequestRandomness(
randomWordsRequestedEvent, err := consumer.RequestRandomness(
coordinator,
minimumConfirmations,
callbackGasLimit,
numberOfWords,
Expand All @@ -484,15 +484,9 @@ func DirectFundingRequestRandomnessAndWaitForFulfillment(
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrRequestRandomness, err)
}
wrapperAddress, err := consumer.GetWrapper(ctx)
if err != nil {
return nil, fmt.Errorf("error getting wrapper address, err: %w", err)
}
fulfillmentEvents, err := WaitForRequestAndFulfillmentEvents(
wrapperAddress.String(),
fulfillmentEvents, err := WaitRandomWordsFulfilledEvent(
coordinator,
vrfv2KeyData,
subID,
randomWordsRequestedEvent.RequestId,
randomWordsFulfilledEventTimeout,
l,
)
Expand All @@ -512,62 +506,34 @@ func RequestRandomnessAndWaitForFulfillment(
randomnessRequestCountPerRequestDeviation uint16,
randomWordsFulfilledEventTimeout time.Duration,
) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsFulfilled, error) {
logRandRequest(
randomWordsRequestedEvent, err := RequestRandomness(
l,
consumer.Address(),
coordinator.Address(),
consumer,
coordinator,
subID,
vrfKeyData,
minimumConfirmations,
callbackGasLimit,
numberOfWords,
randomnessRequestCountPerRequest,
randomnessRequestCountPerRequestDeviation,
vrfKeyData.KeyHash,
)
ch := make(chan *vrf_coordinator_v2.VRFCoordinatorV2RandomWordsFulfilled)
errorChannel := make(chan error)
go func() {
_, err := consumer.RequestRandomness(
vrfKeyData.KeyHash,
subID,
minimumConfirmations,
callbackGasLimit,
numberOfWords,
randomnessRequestCountPerRequest,
)
if err != nil {
l.Error().Err(err).Msg(err.Error())
errorChannel <- err
}
}()
go func() {
fulfillmentEvents, err := WaitForRequestAndFulfillmentEvents(
consumer.Address(),
coordinator,
vrfKeyData,
subID,
randomWordsFulfilledEventTimeout,
l,
)
if err != nil {
l.Error().Err(err).Msg("error waiting for RandomnessRequested and RandomWordsFulfilled events")
errorChannel <- err
}
ch <- fulfillmentEvents
}()
for {
select {
case err := <-errorChannel:
return nil, err
case fulfillmentEvent := <-ch:
return fulfillmentEvent, nil
case <-time.After(randomWordsFulfilledEventTimeout):
return nil, fmt.Errorf("timeout waiting for RandomnessRequested and RandomWordsFulfilled events")
}
if err != nil {
return nil, err
}
fulfillmentEvents, err := WaitRandomWordsFulfilledEvent(
coordinator,
randomWordsRequestedEvent.RequestId,
randomWordsFulfilledEventTimeout,
l,
)
if err != nil {
return nil, err
}
return fulfillmentEvents, nil
}

func RequestRandomnessAndWaitForRequestedEvent(
func RequestRandomness(
l zerolog.Logger,
consumer contracts.VRFv2LoadTestConsumer,
coordinator contracts.VRFCoordinatorV2,
Expand All @@ -578,7 +544,6 @@ func RequestRandomnessAndWaitForRequestedEvent(
numberOfWords uint32,
randomnessRequestCountPerRequest uint16,
randomnessRequestCountPerRequestDeviation uint16,
randomWordsRequestedEventTimeout time.Duration,
) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested, error) {
logRandRequest(
l,
Expand All @@ -592,50 +557,21 @@ func RequestRandomnessAndWaitForRequestedEvent(
randomnessRequestCountPerRequestDeviation,
vrfKeyData.KeyHash,
)
ch := make(chan *vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested)
errorChannel := make(chan error)
go func() {
_, err := consumer.RequestRandomness(
vrfKeyData.KeyHash,
subID,
minimumConfirmations,
callbackGasLimit,
numberOfWords,
randomnessRequestCountPerRequest,
)
if err != nil {
l.Error().Err(err).Msg(err.Error())
errorChannel <- err
}
}()
go func() {
randomWordsRequestedEvent, err := coordinator.WaitForRandomWordsRequestedEvent(
[][32]byte{vrfKeyData.KeyHash},
[]uint64{subID},
[]common.Address{common.HexToAddress(consumer.Address())},
time.Minute*1,
)
if err != nil {
l.Error().Err(err).Msg(err.Error())
errorChannel <- err
}
LogRandomnessRequestedEvent(l, coordinator, randomWordsRequestedEvent)
if err != nil {
l.Error().Err(err).Msg("error waiting for RandomnessRequested events")
errorChannel <- err
}
ch <- randomWordsRequestedEvent
}()
for {
select {
case err := <-errorChannel:
return nil, err
case randRequestedEvent := <-ch:
return randRequestedEvent, nil
case <-time.After(randomWordsRequestedEventTimeout):
return nil, fmt.Errorf("timeout waiting for RandomnessRequested events")
}
randomWordsRequestedEvent, err := consumer.RequestRandomness(
coordinator,
vrfKeyData.KeyHash,
subID,
minimumConfirmations,
callbackGasLimit,
numberOfWords,
randomnessRequestCountPerRequest,
)
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrRequestRandomness, err)
}
LogRandomnessRequestedEvent(l, coordinator, randomWordsRequestedEvent)

return randomWordsRequestedEvent, err
}

func RequestRandomnessWithForceFulfillAndWaitForFulfillment(
Expand Down Expand Up @@ -739,27 +675,14 @@ func RequestRandomnessWithForceFulfillAndWaitForFulfillment(
return configSetEvent, randomWordsFulfilledEvent, randomWordsForcedEvent, err
}

func WaitForRequestAndFulfillmentEvents(
consumerAddress string,
func WaitRandomWordsFulfilledEvent(
coordinator contracts.VRFCoordinatorV2,
vrfv2KeyData *vrfcommon.VRFKeyData,
subID uint64,
requestId *big.Int,
randomWordsFulfilledEventTimeout time.Duration,
l zerolog.Logger,
) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsFulfilled, error) {
randomWordsRequestedEvent, err := coordinator.WaitForRandomWordsRequestedEvent(
[][32]byte{vrfv2KeyData.KeyHash},
[]uint64{subID},
[]common.Address{common.HexToAddress(consumerAddress)},
time.Minute*1,
)
if err != nil {
return nil, fmt.Errorf("%s, err %w", vrfcommon.ErrWaitRandomWordsRequestedEvent, err)
}
LogRandomnessRequestedEvent(l, coordinator, randomWordsRequestedEvent)

randomWordsFulfilledEvent, err := coordinator.WaitForRandomWordsFulfilledEvent(
[]*big.Int{randomWordsRequestedEvent.RequestId},
[]*big.Int{requestId},
randomWordsFulfilledEventTimeout,
)
if err != nil {
Expand Down
13 changes: 11 additions & 2 deletions integration-tests/contracts/contract_vrf_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type VRFCoordinatorV2 interface {
PendingRequestsExist(ctx context.Context, subID uint64) (bool, error)
OwnerCancelSubscription(subID uint64) (*types.Transaction, error)
ParseSubscriptionCanceled(log types.Log) (*vrf_coordinator_v2.VRFCoordinatorV2SubscriptionCanceled, error)
ParseRandomWordsRequested(log types.Log) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested, error)
ParseLog(log types.Log) (generated.AbigenLog, error)
CancelSubscription(subID uint64, to common.Address) (*types.Transaction, error)
FindSubscriptionID(subID uint64) (uint64, error)
Expand Down Expand Up @@ -209,7 +210,15 @@ type VRFv2Consumer interface {

type VRFv2LoadTestConsumer interface {
Address() string
RequestRandomness(hash [32]byte, subID uint64, confs uint16, gasLimit uint32, numWords uint32, requestCount uint16) (*types.Transaction, error)
RequestRandomness(
coordinator VRFCoordinatorV2,
keyHash [32]byte,
subID uint64,
requestConfirmations uint16,
callbackGasLimit uint32,
numWords uint32,
requestCount uint16,
) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested, error)
RequestRandomWordsWithForceFulfill(
keyHash [32]byte,
requestConfirmations uint16,
Expand All @@ -228,7 +237,7 @@ type VRFv2LoadTestConsumer interface {
type VRFv2WrapperLoadTestConsumer interface {
Address() string
Fund(ethAmount *big.Float) error
RequestRandomness(requestConfirmations uint16, callbackGasLimit uint32, numWords uint32, requestCount uint16) (*types.Transaction, error)
RequestRandomness(coordinator VRFCoordinatorV2, requestConfirmations uint16, callbackGasLimit uint32, numWords uint32, requestCount uint16) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested, error)
GetRequestStatus(ctx context.Context, requestID *big.Int) (vrfv2_wrapper_load_test_consumer.GetRequestStatus, error)
GetLastRequestId(ctx context.Context) (*big.Int, error)
GetWrapper(ctx context.Context) (common.Address, error)
Expand Down
61 changes: 55 additions & 6 deletions integration-tests/contracts/ethereum_vrfv2_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ func (v *EthereumVRFCoordinatorV2) ParseSubscriptionCanceled(log types.Log) (*vr
return v.coordinator.ParseSubscriptionCanceled(log)
}

func (v *EthereumVRFCoordinatorV2) ParseRandomWordsRequested(log types.Log) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested, error) {
return v.coordinator.ParseRandomWordsRequested(log)
}

func (v *EthereumVRFCoordinatorV2) ParseLog(log types.Log) (generated.AbigenLog, error) {
return v.coordinator.ParseLog(log)
}
Expand Down Expand Up @@ -790,23 +794,36 @@ func (v *EthereumVRFv2LoadTestConsumer) Address() string {
}

func (v *EthereumVRFv2LoadTestConsumer) RequestRandomness(
coordinator VRFCoordinatorV2,
keyHash [32]byte,
subID uint64,
requestConfirmations uint16,
callbackGasLimit uint32,
numWords uint32,
requestCount uint16,
) (*types.Transaction, error) {
) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested, error) {
opts, err := v.client.TransactionOpts(v.client.GetDefaultWallet())
if err != nil {
return nil, err
}

tx, err := v.consumer.RequestRandomWords(opts, subID, requestConfirmations, keyHash, callbackGasLimit, numWords, requestCount)
if err != nil {
return nil, err
return nil, fmt.Errorf("RequestRandomWords failed, err: %w", err)
}
return tx, v.client.ProcessTransaction(tx)
err = v.client.ProcessTransaction(tx)
if err != nil {
return nil, fmt.Errorf("ProcessTransaction failed, err: %w", err)
}
err = v.client.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("WaitForEvents failed, err: %w", err)
}
receipt, err := v.client.GetTxReceipt(tx.Hash())
if err != nil {
return nil, fmt.Errorf("GetTxReceipt failed, err: %w", err)
}
randomWordsRequestedEvent, err := parseRequestRandomnessLogs(coordinator, receipt.Logs)
return randomWordsRequestedEvent, err
}

func (v *EthereumVRFv2LoadTestConsumer) RequestRandomWordsWithForceFulfill(
Expand Down Expand Up @@ -971,7 +988,7 @@ func (v *EthereumVRFV2WrapperLoadTestConsumer) Fund(ethAmount *big.Float) error
return v.client.Fund(v.address.Hex(), ethAmount, gasEstimates)
}

func (v *EthereumVRFV2WrapperLoadTestConsumer) RequestRandomness(requestConfirmations uint16, callbackGasLimit uint32, numWords uint32, requestCount uint16) (*types.Transaction, error) {
func (v *EthereumVRFV2WrapperLoadTestConsumer) RequestRandomness(coordinator VRFCoordinatorV2, requestConfirmations uint16, callbackGasLimit uint32, numWords uint32, requestCount uint16) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested, error) {
opts, err := v.client.TransactionOpts(v.client.GetDefaultWallet())
if err != nil {
return nil, err
Expand All @@ -980,7 +997,23 @@ func (v *EthereumVRFV2WrapperLoadTestConsumer) RequestRandomness(requestConfirma
if err != nil {
return nil, err
}
return tx, v.client.ProcessTransaction(tx)
err = v.client.ProcessTransaction(tx)
if err != nil {
return nil, err
}
err = v.client.WaitForEvents()
if err != nil {
return nil, fmt.Errorf("WaitForEvents failed, err: %w", err)
}
receipt, err := v.client.GetTxReceipt(tx.Hash())
if err != nil {
return nil, fmt.Errorf("GetTxReceipt failed, err: %w", err)
}
randomWordsRequestedEvent, err := parseRequestRandomnessLogs(coordinator, receipt.Logs)
if err != nil {
return nil, err
}
return randomWordsRequestedEvent, err
}

func (v *EthereumVRFV2WrapperLoadTestConsumer) GetRequestStatus(ctx context.Context, requestID *big.Int) (vrfv2_wrapper_load_test_consumer.GetRequestStatus, error) {
Expand Down Expand Up @@ -1162,3 +1195,19 @@ func (v *EthereumVRFMockETHLINKFeed) SetBlockTimestampDeduction(blockTimestampDe
}
return v.client.ProcessTransaction(tx)
}

func parseRequestRandomnessLogs(coordinator VRFCoordinatorV2, logs []*types.Log) (*vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested, error) {
var randomWordsRequestedEvent *vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested
var err error
for _, eventLog := range logs {
for _, topic := range eventLog.Topics {
if topic.Cmp(vrf_coordinator_v2.VRFCoordinatorV2RandomWordsRequested{}.Topic()) == 0 {
randomWordsRequestedEvent, err = coordinator.ParseRandomWordsRequested(*eventLog)
if err != nil {
return nil, fmt.Errorf("parse RandomWordsRequested log failed, err: %w", err)
}
}
}
}
return randomWordsRequestedEvent, nil
}
3 changes: 1 addition & 2 deletions integration-tests/load/vrfv2/gun.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewBHSTestGun(

// Call implements example gun call, assertions on response bodies should be done here
func (m *BHSTestGun) Call(_ *wasp.Generator) *wasp.Response {
_, err := vrfv2.RequestRandomnessAndWaitForRequestedEvent(
_, err := vrfv2.RequestRandomness(
m.logger,
m.contracts.VRFV2Consumers[0],
m.contracts.CoordinatorV2,
Expand All @@ -48,7 +48,6 @@ func (m *BHSTestGun) Call(_ *wasp.Generator) *wasp.Response {
*m.testConfig.General.NumberOfWords,
*m.testConfig.General.RandomnessRequestCountPerRequest,
*m.testConfig.General.RandomnessRequestCountPerRequestDeviation,
m.testConfig.General.RandomWordsFulfilledEventTimeout.Duration,
)
//todo - might need to store randRequestBlockNumber and blockhash to verify that it was stored in BHS contract at the end of the test
if err != nil {
Expand Down
Loading

0 comments on commit c070345

Please sign in to comment.