Search code examples
blockchainethereumsoliditychainlink

Chain Link VRF takes a long time to get random numbers


Description

Getting a random number takes a really long time. After executing the getRandomNumber function, a few minutes go by before I can interact with my random number.

Basically I click getRandomNumber and have to wait 2-3 minutes until the random number shows up in the randomResult variable.

Steps to Reproduce

  1. Head over to the documentation here : https://docs.chain.link/docs/get-a-random-number/
  2. Scroll down and click on "Deploy this contract using Remix" (blue outline btn)
  3. Click on one of the folders that looks like 536123b61468ad4442cfc4278e8de577 then RandomNumberConsumer.sol
  4. Replace the LINK Token, VRF Coordinator, and Key Hash to be unique to rinkeby https://docs.chain.link/docs/vrf-contracts/
  5. Navigation to the Solidity Compiler Tab and click on Compile RandomNumber.sol.
  6. Deploy the contract on Rinkeby
  7. Copy to contract address and send LINK token to fund the contract.
  8. Click on the orange getRandomNumber btn in remix
  9. Click on randomResult and observe how long it takes for the value in randomResult to change. (Keep clicking until it finally changes)

Additional Information

I am not sure if this behavior is intentional or if I need to change up the code. Ideally I would like to have the value of randomResult once the getRandomNumber function finishes executing. Right now I don't know when the value of randomResult will show up.


Solution

  • Chainlink VRF follows the request and receive cycle of getting a number. This means, it has to look outside the blockchain to get a response.

    So, 2 transactions actually take place:

    1. Your requesting transaction
    2. The response transaction

    This is necessary to look outside the blockchain like this, otherwise you'll get a deterministic number as opposed to random. The speed at which the random number is returned, is dictated by the block time of the blockchain you're using.

    So for Ethereum, the fastest time possible for a Random Number to be returned is 2 * block time, or about 30ish seconds.

    On faster chains like polygon, this is drastically lower.