-
Notifications
You must be signed in to change notification settings - Fork 46
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 Fallback #1782
Draft
jaybuidl
wants to merge
3
commits into
dev
Choose a base branch
from
feat/rng-fallback
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
RNG Fallback #1782
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,43 +2,47 @@ | |
|
||
pragma solidity 0.8.24; | ||
|
||
import "./RNG.sol"; | ||
import "./IRNG.sol"; | ||
|
||
/// @title Random Number Generator using blockhash with fallback. | ||
/// @author Clément Lesaege - <[email protected]> | ||
/// @dev | ||
/// Random Number Generator returning the blockhash with a fallback behaviour. | ||
/// In case no one called it within the 256 blocks, it returns the previous blockhash. | ||
/// This contract must be used when returning 0 is a worse failure mode than returning another blockhash. | ||
/// Allows saving the random number for use in the future. It allows the contract to still access the blockhash even after 256 blocks. | ||
contract BlockHashRNG is RNG { | ||
contract BlockHashRNG is IRNG { | ||
uint256 public immutable lookahead; // Minimal block distance between requesting and obtaining a random number. | ||
uint256 public requestBlock; // Block number of the current request | ||
mapping(uint256 block => uint256 number) public randomNumbers; // randomNumbers[block] is the random number for this block, 0 otherwise. | ||
|
||
constructor(uint256 _lookahead) { | ||
lookahead = _lookahead + lookahead; | ||
} | ||
|
||
/// @dev Request a random number. | ||
/// @param _block Block the random number is linked to. | ||
function requestRandomness(uint256 _block) external override { | ||
// nop | ||
function requestRandomness() external override { | ||
requestBlock = block.number; | ||
} | ||
|
||
/// @dev Return the random number. If it has not been saved and is still computable compute it. | ||
/// @param _block Block the random number is linked to. | ||
/// @return randomNumber The random number or 0 if it is not ready or has not been requested. | ||
function receiveRandomness(uint256 _block) external override returns (uint256 randomNumber) { | ||
randomNumber = randomNumbers[_block]; | ||
function receiveRandomness() external override returns (uint256 randomNumber) { | ||
uint256 expectedBlock = requestBlock; | ||
randomNumber = randomNumbers[expectedBlock]; | ||
if (randomNumber != 0) { | ||
return randomNumber; | ||
} | ||
|
||
if (_block < block.number) { | ||
if (expectedBlock < block.number) { | ||
// The random number is not already set and can be. | ||
if (blockhash(_block) != 0x0) { | ||
if (blockhash(expectedBlock) != 0x0) { | ||
// Normal case. | ||
randomNumber = uint256(blockhash(_block)); | ||
randomNumber = uint256(blockhash(expectedBlock)); | ||
} else { | ||
// The contract was not called in time. Fallback to returning previous blockhash. | ||
randomNumber = uint256(blockhash(block.number - 1)); | ||
} | ||
} | ||
randomNumbers[_block] = randomNumber; | ||
randomNumbers[expectedBlock] = randomNumber; | ||
Comment on lines
+29
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review fallback mechanism security implications The fallback mechanism using the previous blockhash when the original block is no longer available has several security implications:
Consider:
if (blockhash(expectedBlock) != 0x0) {
// Normal case.
randomNumber = uint256(blockhash(expectedBlock));
} else {
// The contract was not called in time. Fallback to returning previous blockhash.
+ emit FallbackUsed(expectedBlock, block.number - 1);
randomNumber = uint256(blockhash(block.number - 1));
}
|
||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity 0.8.24; | ||
|
||
/// @title Random Number Generator interface | ||
interface IRNG { | ||
/// @dev Request a random number. | ||
function requestRandomness() external; | ||
|
||
/// @dev Receive the random number. | ||
/// @return randomNumber Random Number. If the number is not ready or has not been required 0 instead. | ||
function receiveRandomness() external returns (uint256 randomNumber); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix potential initialization bug in constructor
There's a bug in the constructor where
lookahead
is being added to itself, which would double the intended value.📝 Committable suggestion