Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions contracts/standard/rng/BlockhashRNG.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
/**
* @authors: [@clesaege]
* @reviewers: [@remedcu]
* @auditors: []
* @bounties: []
* @deployments: []
*/

pragma solidity ^0.4.15;

import "./RNG.sol";

/**
* @title Random Number Generator usign blockhash
* @author Clément Lesaege - <clement@lesaege.com>
*
* This contract implements the RNG standard and gives parties incentives to save the blockhash to avoid it to become unreachable after 256 blocks.
*
*/
pragma solidity ^0.4.15;

import "./RNG.sol";

/** Simple Random Number Generator returning the blockhash.
* Simple Random Number Generator returning the blockhash.
* Allows saving the random number for use in the future.
* It allows the contract to still access the blockhash even after 256 blocks.
* The first party to call the save function gets the reward.
Expand All @@ -19,8 +26,6 @@ contract BlockHashRNG is RNG {
mapping (uint => uint) public randomNumber; // randomNumber[block] is the random number for this block, 0 otherwise.
mapping (uint => uint) public reward; // reward[block] is the amount to be paid to the party w.



/** @dev Contribute to the reward of a random number.
* @param _block Block the random number is linked to.
*/
Expand All @@ -47,8 +52,6 @@ contract BlockHashRNG is RNG {
function saveRN(uint _block) public {
if (blockhash(_block) != 0x0)
randomNumber[_block] = uint(blockhash(_block));
else
randomNumber[_block] = getFallbackRN(_block);

if (randomNumber[_block] != 0) { // If the number is set.
uint rewardToSend = reward[_block];
Expand All @@ -57,10 +60,4 @@ contract BlockHashRNG is RNG {
}
}

/** @dev Fallback strategy. This class has no fallback. Subclass provides fallback strategy by overriding this method.
* @param _block Block the random number is linked to.
*/
function getFallbackRN(uint _block) internal view returns (uint) {
return 0x0;
}
}
47 changes: 32 additions & 15 deletions contracts/standard/rng/BlockhashRNGFallback.sol
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
/**
* @authors: [@clesaege]
* @reviewers: [@remedcu]
* @auditors: []
* @bounties: []
* @deployments: [ https://etherscan.io/address/0x1738b62e403090666687243e758b1c29edffc90e ]
*/

pragma solidity ^0.4.15;

import "./BlockhashRNG.sol";

/**
* @title Random Number Generator using blockhash with fallback.
* @author Clément Lesaege - <clement@lesaege.com>
*
*
* This contract implements the RNG standard and gives parties incentives to save the blockhash to avoid it to become unreachable after 256 blocks.
* 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.
* Note that if someone calls it within the timeframe, this contracts acts exactly as BlockHashRNG.
*/
pragma solidity ^0.4.15;

import "./BlockhashRNG.sol";

/** Random Number Generator returning the blockhash with a backup behaviour.
* Allows saving the random number for use in the future.
*
* Random Number Generator returning the blockhash with a backup behaviour.
* Allows saving the random number for use in the future.
* It allows the contract to still access the blockhash even after 256 blocks.
* The first party to call the save function gets the reward.
* If no one calls the contract within 256 blocks, the contract fallback in returning the blockhash of a block in range.
* If no one calls the contract within 256 blocks, the contract fallback in returning the blockhash of the previous block.
*/
contract BlockHashRNGFallback is BlockHashRNG {
/** @dev Fallback by returning a blockhash in range.

/** @dev Save the random number for this blockhash and give the reward to the caller.
* @param _block Block the random number is linked to.
*/
function getFallbackRN(uint _block) internal view returns (uint) {
if (_block >= block.number) {
return 0x0;
function saveRN(uint _block) public {
if (_block<block.number && randomNumber[_block]==0) {// If the random number is not already set and can be.
if (blockhash(_block) != 0x0) // Normal case.
randomNumber[_block] = uint(blockhash(_block));
else // The contract was not called in time. Fallback to returning previous blockhash.
randomNumber[_block] = uint(blockhash(block.number-1));
}

if (randomNumber[_block] != 0) { // If the random number is set.
uint rewardToSend = reward[_block];
reward[_block] = 0;
msg.sender.send(rewardToSend); // Note that the use of send is on purpose as we don't want to block in case the msg.sender has a fallback issue.
}
return uint(blockhash((block.number - 1) - (block.number - 1 - _block)%256));
}

}
15 changes: 11 additions & 4 deletions contracts/standard/rng/RNG.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/**
* @authors: [@clesaege]
* @reviewers: [@remedcu]
* @auditors: []
* @bounties: []
* @deployments: []
*/

pragma solidity ^0.4.15;

/**
* @title Random Number Generator Standard
* @author Clément Lesaege - <clement@lesaege.com>
*
* @dev This is an abstract contract
*/

pragma solidity ^0.4.15;

contract RNG{

/** @dev Contribute to the reward of a random number.
Expand Down