-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RNG: Chainlink VRF + Fallback mechanism #966
Conversation
✅ Deploy Preview for kleros-v2-contracts ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for kleros-v2 ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
b1217ab
to
2149884
Compare
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
2149884
to
acfcd1b
Compare
Code Climate has analyzed commit 0428693 and detected 63 issues on this pull request. Here's the issue category breakdown:
View more on Code Climate. |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
RNG lookahead is removed since it's not needed with this fallback setup. It won't be used by ChainlinkRNG itself and is irrelevant for BlockhashRNG since |
✅ Deploy Preview for kleros-v2-university ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
This PR implements the Chainlink VRF Mechanism using the Subscription Method.
Rationale Subscription Method vs Direct Funding
Cost Efficiency
The Subscription Method reduces the gas cost of using VRF by pre-funding the subscription account with LINK when the direct funding method needs to calculate the cost of the request and fund it before actually making the request.
It is particularly efficient when many requests are made, which is the case as a random number is generated whenever jurors are drawn.
Simplified Management
Using a subscription centralizes the management:
Scalability
One subscription can support up to 100 consuming contracts. It allows for potential future expansion, which may require random number generation.
Contracts
VRFConsumerV2.sol
VRF Consumer tailored to the SortitionModule.
It inherits from VRFConsumerBaseV2.sol, provided by Chainlink
Compared to the RandomizerRNG, only the SortitionModule can make a request, and the callback function calls
sortitionModule.passPhase()
once the random number is ready.Implementation doc: Chainlink - Get a random number
VRFSubscriptionManagerV2.sol
A contract allowing programmatic management of the VRF subscription.
Without this contract, the subscription must be managed through the Subscription Manager UI.
A subscription is created in the constructor.
Implementation doc: Chainlink - Programmatic Subscription
Mock Contracts
To locally test the contract, we need to emulate the Chainlink VRF Coordinator.
VRFCoordinatorV2Mock.sol
This coordinator is provided by Chainlink: VRFCoordinationV2Mock.sol
VRFSubscriptionManagerV2Mock
The mock coordinator doesn't use LINK tokens, and the interface is different from the coordinator on public networks. Therefore a Subscription Manager mock had to be done.
Implementation doc: Chainlink - Local testing using a Mock contract
Deployment
The deployment scripts which deployed the Randomizer are 00-rng.ts and 00-home-chain-arbitration.ts.
I updated them to also deploy the VRFConsumerV2 and the VRFSubscriptionManagerV2Mock on the different networks.
The deployment scripts don't change the SortitionModule rng source (
SortitionModule.changeRandomNumberGenerator(address rngAddress, uint256 rngLookAhead)
), which is deployed with Randomizer as rng source.Parameters
Most parameters are network-dependent (e.g. address of the vrf coordinator) but some have to be defined:
KeyHash
I've set it to 30 gwei as it is the medium lane.
callbackGasLimit
The Randomizer sets a callbackGasLimit to 50 000, as the VRFConsumerV2 callback function makes an external call to
sortitionModule.passPhase()
, I chose to set it at 100 000.requestConfirmations
The range on Arbitrum: 1 - 200.
I've arbitrarily set it to 3.
numWords
I've set it to 1, as only one random number is needed to draw jurors.
Public Networks
Funds need to be added to the subscription.
Localhost
Hardhat Task - Credit LINK
To fund the subscription with LINK on public networks (Arbitrum One & Arbitrum Goerli), I wrote a hardhat task that transfers
amountInWei
LINK tokens from the deployer to the VRFSubscriptionManagerV2, which then funds the subscription by callingVRFSubscriptionManagerV2.topUpSubscription(amountInWei)
.The
amount
passed as a parameter is "normal values" (e.g. 100 LINK), and not in wei to avoid errors. TheamountInWei
is computed from it.Tests
Local tests making use of the Randomizer are arbitration/draw.ts, arbitration/unstake.ts, and integrations/index.ts.
I duplicated the tests, the first running with the default rng source (Randomizer from deployment) and the copy changing the rng source to VRFConsumerV2 and accordingly using it.
(Mock contracts: VRFCoordinator triggered manually in the test).
PR-Codex overview
This PR introduces a fallback mechanism for receiving randomness and updates Chainlink VRF interactions.
Detailed summary
receiveRandomnessFallback
function to RNG contracts.deploy/upgrade-sortition-module.ts
.scripts/creditLink.ts
.SortitionModule.sol
withrngFallbackTimeout
parameter.