- 
                Notifications
    
You must be signed in to change notification settings  - Fork 50
 
RNG Fallback #1782
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
          
     Closed
      
      
    
  
     Closed
                    RNG Fallback #1782
Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      1ff6097
              
                feat: composite rng with fallback logic, removal of the block param f…
              
              
                jaybuidl 586006d
              
                refactor: renamed RNG into IRNG
              
              
                jaybuidl ec3cd79
              
                chore: sortition module lookahead state var removal
              
              
                jaybuidl f0746bd
              
                fix: various fixes for BlockhashRNG and RNGWillFallback, use block ti…
              
              
                jaybuidl 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 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
              | 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; | ||
                
       | 
||
| } | ||
| } | ||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              | 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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.