|
| 1 | + /** |
| 2 | + * @authors: [@clesaege] |
| 3 | + * @reviewers: [@remedcu] |
| 4 | + * @auditors: [] |
| 5 | + * @bounties: [] |
| 6 | + * @deployments: [ https://etherscan.io/address/0x1738b62e403090666687243e758b1c29edffc90e ] |
| 7 | + */ |
| 8 | + |
| 9 | +pragma solidity ^0.4.15; |
| 10 | + |
| 11 | +import "./BlockhashRNG.sol"; |
| 12 | + |
1 | 13 | /** |
2 | 14 | * @title Random Number Generator using blockhash with fallback. |
3 | 15 | * @author Clément Lesaege - <clement@lesaege.com> |
4 | | - * |
| 16 | + * |
5 | 17 | * This contract implements the RNG standard and gives parties incentives to save the blockhash to avoid it to become unreachable after 256 blocks. |
6 | 18 | * In case no one called it within the 256 blocks, it returns the previous blockhash. |
7 | 19 | * This contract must be used when returning 0 is a worse failure mode than returning another blockhash. |
8 | 20 | * Note that if someone calls it within the timeframe, this contracts acts exactly as BlockHashRNG. |
9 | | - */ |
10 | | -pragma solidity ^0.4.15; |
11 | | - |
12 | | -import "./BlockhashRNG.sol"; |
13 | | - |
14 | | -/** Random Number Generator returning the blockhash with a backup behaviour. |
15 | | - * Allows saving the random number for use in the future. |
| 21 | + * |
| 22 | + * Random Number Generator returning the blockhash with a backup behaviour. |
| 23 | + * Allows saving the random number for use in the future. |
16 | 24 | * It allows the contract to still access the blockhash even after 256 blocks. |
17 | 25 | * The first party to call the save function gets the reward. |
18 | | - * If no one calls the contract within 256 blocks, the contract fallback in returning the blockhash of a block in range. |
| 26 | + * If no one calls the contract within 256 blocks, the contract fallback in returning the blockhash of the previous block. |
19 | 27 | */ |
20 | 28 | contract BlockHashRNGFallback is BlockHashRNG { |
21 | | - |
22 | | - /** @dev Fallback by returning a blockhash in range. |
| 29 | + |
| 30 | + /** @dev Save the random number for this blockhash and give the reward to the caller. |
23 | 31 | * @param _block Block the random number is linked to. |
24 | 32 | */ |
25 | | - function getFallbackRN(uint _block) internal view returns (uint) { |
26 | | - if (_block >= block.number) { |
27 | | - return 0x0; |
| 33 | + function saveRN(uint _block) public { |
| 34 | + if (_block<block.number && randomNumber[_block]==0) {// If the random number is not already set and can be. |
| 35 | + if (blockhash(_block) != 0x0) // Normal case. |
| 36 | + randomNumber[_block] = uint(blockhash(_block)); |
| 37 | + else // The contract was not called in time. Fallback to returning previous blockhash. |
| 38 | + randomNumber[_block] = uint(blockhash(block.number-1)); |
| 39 | + } |
| 40 | + |
| 41 | + if (randomNumber[_block] != 0) { // If the random number is set. |
| 42 | + uint rewardToSend = reward[_block]; |
| 43 | + reward[_block] = 0; |
| 44 | + 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. |
28 | 45 | } |
29 | | - return uint(blockhash((block.number - 1) - (block.number - 1 - _block)%256)); |
30 | 46 | } |
| 47 | + |
31 | 48 | } |
0 commit comments