Skip to content

Commit c82a06f

Browse files
authored
Merge pull request #312 from remedcu/master
Changes to RNG Folder
2 parents a292052 + cf43218 commit c82a06f

File tree

3 files changed

+56
-35
lines changed

3 files changed

+56
-35
lines changed

contracts/standard/rng/BlockhashRNG.sol

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
/**
2+
* @authors: [@clesaege]
3+
* @reviewers: [@remedcu]
4+
* @auditors: []
5+
* @bounties: []
6+
* @deployments: []
7+
*/
8+
9+
pragma solidity ^0.4.15;
10+
11+
import "./RNG.sol";
12+
113
/**
214
* @title Random Number Generator usign blockhash
315
* @author Clément Lesaege - <clement@lesaege.com>
416
*
517
* This contract implements the RNG standard and gives parties incentives to save the blockhash to avoid it to become unreachable after 256 blocks.
618
*
7-
*/
8-
pragma solidity ^0.4.15;
9-
10-
import "./RNG.sol";
11-
12-
/** Simple Random Number Generator returning the blockhash.
19+
* Simple Random Number Generator returning the blockhash.
1320
* Allows saving the random number for use in the future.
1421
* It allows the contract to still access the blockhash even after 256 blocks.
1522
* The first party to call the save function gets the reward.
@@ -19,8 +26,6 @@ contract BlockHashRNG is RNG {
1926
mapping (uint => uint) public randomNumber; // randomNumber[block] is the random number for this block, 0 otherwise.
2027
mapping (uint => uint) public reward; // reward[block] is the amount to be paid to the party w.
2128

22-
23-
2429
/** @dev Contribute to the reward of a random number.
2530
* @param _block Block the random number is linked to.
2631
*/
@@ -47,8 +52,6 @@ contract BlockHashRNG is RNG {
4752
function saveRN(uint _block) public {
4853
if (blockhash(_block) != 0x0)
4954
randomNumber[_block] = uint(blockhash(_block));
50-
else
51-
randomNumber[_block] = getFallbackRN(_block);
5255

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

60-
/** @dev Fallback strategy. This class has no fallback. Subclass provides fallback strategy by overriding this method.
61-
* @param _block Block the random number is linked to.
62-
*/
63-
function getFallbackRN(uint _block) internal view returns (uint) {
64-
return 0x0;
65-
}
6663
}
Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
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+
113
/**
214
* @title Random Number Generator using blockhash with fallback.
315
* @author Clément Lesaege - <clement@lesaege.com>
4-
*
16+
*
517
* This contract implements the RNG standard and gives parties incentives to save the blockhash to avoid it to become unreachable after 256 blocks.
618
* In case no one called it within the 256 blocks, it returns the previous blockhash.
719
* This contract must be used when returning 0 is a worse failure mode than returning another blockhash.
820
* 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.
1624
* It allows the contract to still access the blockhash even after 256 blocks.
1725
* 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.
1927
*/
2028
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.
2331
* @param _block Block the random number is linked to.
2432
*/
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.
2845
}
29-
return uint(blockhash((block.number - 1) - (block.number - 1 - _block)%256));
3046
}
47+
3148
}

contracts/standard/rng/RNG.sol

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
/**
2+
* @authors: [@clesaege]
3+
* @reviewers: [@remedcu]
4+
* @auditors: []
5+
* @bounties: []
6+
* @deployments: []
7+
*/
8+
9+
pragma solidity ^0.4.15;
10+
111
/**
212
* @title Random Number Generator Standard
313
* @author Clément Lesaege - <clement@lesaege.com>
4-
*
14+
* @dev This is an abstract contract
515
*/
6-
7-
pragma solidity ^0.4.15;
8-
916
contract RNG{
1017

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

0 commit comments

Comments
 (0)