feat: implement drand randomness precompile#2
Open
fine135 wants to merge 1 commit into
Open
Conversation
Introduces a new EVM precompile for smart contracts to access the Substrate Drand randomness beacon state. - Added DrandPrecompile in drand.rs to expose pallet-drand storage to EVM - Added PrecompileHandleExtStorage trait to automate gas cost calculation on database reads based on SCALE-encoded size - Created Solidity interface and ABI in drand.sol and drand.abi, mapped to address 0x0811 - Hooked the new precompile into the EVM environment in lib.rs - Added comprehensive TypeScript test suite in drand.precompile.test.ts covering edge cases, decoding, and gas metering - Updated related dependencies in Cargo.toml and yarn.lock
ppolewicz
reviewed
Mar 12, 2026
Comment on lines
+42
to
+46
| function getPulse(uint64 round) external view returns (DrandPulse memory); | ||
|
|
||
| /// @dev Returns the randomness from the latest stored round as bytes32. | ||
| /// Returns zero bytes32 if no pulse is stored. | ||
| function getCurrentRandomness() external view returns (bytes32); |
There was a problem hiding this comment.
We want the contracts to get the randomness for the current block regardless of whether it's drand-based or new-future-method-based, so I don't think it should use round numbers but block numbers which should then be, in subtensor, translated to the drand number or another-future-source randomness id.
Future proof it.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces a new EVM precompile for smart contracts to access the Substrate Drand randomness beacon state.
Description
This PR implements a new precompile mapped to address
0x0811that bridges the Substratepallet-drandstate into the EVM environment. This allows Solidity smart contracts to perform read-only queries for verifiable randomness pulses, beacon configurations, and round information using native Ethereum types (likebytes32). It introduces theDrandPrecompileRust implementation alongside an efficientPrecompileHandleExtStoragetrait to cleanly calculate gas costs based on the SCALE-encoded size of DB reads. Additional exhaustive TypeScript tests ensure correct behavior, accurate ABI decoding, and precise gas metering.Note on
getPalletVersion():As requested in the initial specification (opentensor/subtensor#2455), this precompile includes a
getPalletVersion()function. Since thepallet-dranditself does not maintain its own explicit versioning in storage, this function is currently implemented to return a hardcoded placeholder version based on the requirements described in that issue.Type of Change
Checklist
Additional Notes
The implementation uses a 1-byte minimum gas charge rule for empty database reads to prevent spamming the RPC / node with arbitrary read attempts.