Skip to content

Commit 36ef15c

Browse files
Merge pull request #364 from unknownunknown1/fix/rng
fix(RNG): store the RN value and combine BeaconRNG
2 parents b31a9fa + d980bbb commit 36ef15c

File tree

2 files changed

+40
-94
lines changed

2 files changed

+40
-94
lines changed
Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @authors: [@shalzz]
3-
* @reviewers: [@jaybuidl]
2+
* @authors: [@shalzz, @unknownunknown1]
3+
* @reviewers: [@jaybuidl*, @geaxed*]
44
* @auditors: []
55
* @bounties: []
66
* @deployments: []
@@ -13,26 +13,48 @@ import "./RNG.sol";
1313
/**
1414
* @title Random Number Generator using beacon chain random opcode
1515
*/
16-
contract BeaconRNG is RNG {
16+
contract BeaconRNG {
17+
18+
uint public constant LOOKAHEAD = 132; // Number of blocks that has to pass before obtaining the random number. 4 epochs + 4 slots, according to EIP-4399.
19+
uint public constant ERROR = 32; // Number of blocks after which the lookahead gets reset, so eligible blocks after lookahead don't go long distance, to avoid a possiblity for manipulation.
1720

18-
/**
19-
* @dev Since we don't really need to incentivise requesting the beacon chain randomness,
20-
* this is a stub implementation required for backwards compatibility with the
21-
* RNG interface.
22-
* @notice All the ETH sent here will be lost forever.
23-
* @param _block Block the random number is linked to.
21+
RNG public blockhashRNG; // Address of blockhashRNG to fall back on.
22+
23+
/** @dev Constructor.
24+
* @param _blockhashRNG The blockhash RNG deployed contract address.
2425
*/
25-
function contribute(uint _block) public payable {}
26+
constructor(RNG _blockhashRNG) public {
27+
blockhashRNG = _blockhashRNG;
28+
}
2629

30+
/**
31+
* @dev Request a random number. It is not used by this contract and only exists for backward compatibility.
32+
*/
33+
function requestRN(uint /*_block*/) public pure {}
2734

28-
/** @dev Return the random number from the PoS randomness beacon.
29-
* @param _block Block the random number is linked to.
30-
* @return RN Random Number. If the PoS upgrade defined by EIP-3675
31-
* has not yet executed 0 instead.
35+
/**
36+
* @dev Get an uncorrelated random number.
37+
* @param _block Block the random number is linked to.
38+
* @return RN Random Number. If the number is not ready or has not been required 0 instead.
3239
*/
33-
function getRN(uint _block) public returns (uint RN) {
34-
if (block.difficulty <= 2**64)
35-
return 0;
36-
return block.difficulty;
40+
function getUncorrelatedRN(uint _block) public returns (uint) {
41+
// Pre-Merge.
42+
if (block.difficulty <= 2**64) {
43+
uint baseRN = blockhashRNG.getRN(_block);
44+
if (baseRN == 0) {
45+
return 0;
46+
} else {
47+
return uint(keccak256(abi.encodePacked(msg.sender, baseRN)));
48+
}
49+
// Post-Merge.
50+
} else {
51+
if (block.number > _block && (block.number - _block) % (LOOKAHEAD + ERROR) > LOOKAHEAD) {
52+
// Eligible block number should exceed LOOKAHEAD but shouldn't be higher than LOOKAHEAD + ERROR.
53+
// In case of the latter LOOKAHEAD gets reset.
54+
return uint(keccak256(abi.encodePacked(msg.sender, block.difficulty)));
55+
} else {
56+
return 0;
57+
}
58+
}
3759
}
3860
}

contracts/standard/rng/BeaconRNGFallback.sol

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)