Skip to content

Commit e7020ee

Browse files
authored
Merge pull request #313 from remedcu/master
Created BlockhashRNGFallbackRange.sol
2 parents c82a06f + b4711ba commit e7020ee

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @authors: [@remedcu]
3+
* @reviewers: [@clesaege]
4+
* @auditors: []
5+
* @bounties: []
6+
* @deployments: []
7+
*/
8+
9+
pragma solidity ^0.4.15;
10+
11+
import "./BlockhashRNG.sol";
12+
13+
/**
14+
* @title Random Number Generator using blockhash with fallback from the last 256 blockhash mined.
15+
* @author Shebin John - <admin@remedcu.com>
16+
*
17+
* Random Number Generator returning the blockhash with a backup behaviour.
18+
* This contract implements the RNG standard and gives parties incentives to save the blockhash to avoid it to become unreachable after 256 blocks.
19+
* In case no one called it within the 256 blocks, it returns the blockhash from any one of the last mined 256 blocks.
20+
* Thus allowing the contract to still return a value even after 256 blocks.
21+
* Allows saving the random number for use in the future.
22+
* The first party to call the save function gets the reward.
23+
* This contract must be used when returning 0 is a worse failure mode than returning another blockhash.
24+
* Note that if someone calls it within the timeframe, this contracts acts exactly as BlockHashRNG.
25+
*/
26+
contract BlockHashRNGFallback is BlockHashRNG {
27+
28+
/** @dev Save the random number for this blockhash and give the reward to the caller.
29+
* @param _block Block the random number is linked to.
30+
*/
31+
function saveRN(uint _block) public {
32+
if (_block < block.number && randomNumber[_block] == 0) { // If the random number is not already set and can be.
33+
// Returns the blockhash of _block if accessible (one of the last 256 blocks).
34+
// If the blockhash hasn't been saved in time, return a blockhash of a block in range as a fallback.
35+
randomNumber[_block] = uint(blockhash((block.number-1) - (block.number-1-_block)%256));
36+
}
37+
38+
if (randomNumber[_block] != 0) { // If the random number is set.
39+
uint rewardToSend = reward[_block];
40+
reward[_block] = 0;
41+
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.
42+
}
43+
}
44+
45+
}

0 commit comments

Comments
 (0)