Created
February 9, 2026 14:55
-
-
Save Turupawn/b7351a009c14093097cbf1849542ce27 to your computer and use it in GitHub Desktop.
vrf benchmark rise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.20; | |
| interface IVRFCoordinator { | |
| function requestRandomNumbers(uint32 numNumbers, uint256 seed) external returns (uint256); | |
| } | |
| contract VRFBenchmark { | |
| IVRFCoordinator public constant coordinator = IVRFCoordinator(0x9d57aB4517ba97349551C876a01a7580B1338909); | |
| struct Request { | |
| uint256 requestedAt; | |
| uint256 fulfilledAt; | |
| uint256 latencyMs; | |
| uint256 randomNumber; | |
| bool fulfilled; | |
| } | |
| mapping(uint256 => Request) public requests; | |
| uint256[] public requestIds; | |
| uint256 public seedCounter; | |
| uint256 public totalRequests; | |
| uint256 public totalFulfilled; | |
| // Send a single VRF request | |
| function sendRequest() external returns (uint256 requestId) { | |
| uint256 seed = seedCounter++; | |
| requestId = coordinator.requestRandomNumbers(1, seed); | |
| requests[requestId] = Request({ | |
| requestedAt: block.timestamp, | |
| fulfilledAt: 0, | |
| latencyMs: 0, | |
| randomNumber: 0, | |
| fulfilled: false | |
| }); | |
| requestIds.push(requestId); | |
| totalRequests++; | |
| } | |
| // Send N requests in a batch | |
| function sendBatch(uint256 count) external { | |
| for (uint256 i = 0; i < count; i++) { | |
| uint256 seed = seedCounter++; | |
| uint256 requestId = coordinator.requestRandomNumbers(1, seed); | |
| requests[requestId] = Request({ | |
| requestedAt: block.timestamp, | |
| fulfilledAt: 0, | |
| latencyMs: 0, | |
| randomNumber: 0, | |
| fulfilled: false | |
| }); | |
| requestIds.push(requestId); | |
| totalRequests++; | |
| } | |
| } | |
| // VRF callback | |
| function rawFulfillRandomNumbers( | |
| uint256 requestId, | |
| uint256[] memory randomNumbers | |
| ) external { | |
| require(msg.sender == address(coordinator), "Only coordinator"); | |
| Request storage req = requests[requestId]; | |
| require(req.requestedAt > 0, "Unknown request"); | |
| require(!req.fulfilled, "Already fulfilled"); | |
| req.fulfilledAt = block.timestamp; | |
| req.latencyMs = (block.timestamp - req.requestedAt) * 1000; | |
| req.randomNumber = randomNumbers[0]; | |
| req.fulfilled = true; | |
| totalFulfilled++; | |
| } | |
| // Get summary: total, fulfilled, pending, avg latency | |
| function getSummary() external view returns ( | |
| uint256 total, | |
| uint256 fulfilled, | |
| uint256 pending, | |
| uint256 avgLatencySeconds | |
| ) { | |
| total = requestIds.length; | |
| fulfilled = totalFulfilled; | |
| pending = total - fulfilled; | |
| if (fulfilled > 0) { | |
| uint256 sumLatency = 0; | |
| for (uint256 i = 0; i < requestIds.length; i++) { | |
| if (requests[requestIds[i]].fulfilled) { | |
| sumLatency += requests[requestIds[i]].fulfilledAt - requests[requestIds[i]].requestedAt; | |
| } | |
| } | |
| avgLatencySeconds = sumLatency / fulfilled; | |
| } | |
| } | |
| // Get all results | |
| function getAllResults() external view returns ( | |
| uint256[] memory ids, | |
| uint256[] memory requestedAts, | |
| uint256[] memory fulfilledAts, | |
| uint256[] memory latencies, | |
| bool[] memory fulfilledFlags | |
| ) { | |
| uint256 len = requestIds.length; | |
| ids = new uint256[](len); | |
| requestedAts = new uint256[](len); | |
| fulfilledAts = new uint256[](len); | |
| latencies = new uint256[](len); | |
| fulfilledFlags = new bool[](len); | |
| for (uint256 i = 0; i < len; i++) { | |
| Request storage req = requests[requestIds[i]]; | |
| ids[i] = requestIds[i]; | |
| requestedAts[i] = req.requestedAt; | |
| fulfilledAts[i] = req.fulfilledAt; | |
| latencies[i] = req.fulfilledAt > 0 ? req.fulfilledAt - req.requestedAt : 0; | |
| fulfilledFlags[i] = req.fulfilled; | |
| } | |
| } | |
| // Check which requests are still pending | |
| function getPending() external view returns (uint256[] memory pendingIds, uint256[] memory waitingSeconds) { | |
| uint256 count = 0; | |
| for (uint256 i = 0; i < requestIds.length; i++) { | |
| if (!requests[requestIds[i]].fulfilled) count++; | |
| } | |
| pendingIds = new uint256[](count); | |
| waitingSeconds = new uint256[](count); | |
| uint256 idx = 0; | |
| for (uint256 i = 0; i < requestIds.length; i++) { | |
| if (!requests[requestIds[i]].fulfilled) { | |
| pendingIds[idx] = requestIds[i]; | |
| waitingSeconds[idx] = block.timestamp - requests[requestIds[i]].requestedAt; | |
| idx++; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment