Skip to content

Commit eaa70dc

Browse files
fix(RNG): store the RN value and combine BeaconRNG
1 parent b31a9fa commit eaa70dc

File tree

2 files changed

+48
-86
lines changed

2 files changed

+48
-86
lines changed
Lines changed: 48 additions & 10 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: []
@@ -14,25 +14,63 @@ import "./RNG.sol";
1414
* @title Random Number Generator using beacon chain random opcode
1515
*/
1616
contract BeaconRNG is RNG {
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+
20+
RNG public blockhashRNG; // Address of blockhashRNG to fall back on.
21+
mapping (uint => uint) public randomNumber; // randomNumber[block] is the random number for this requested block, 0 otherwise.
22+
23+
/** @dev Constructor.
24+
* @param _blockhashRNG The blockhash RNG deployed contract address.
25+
*/
26+
constructor(RNG _blockhashRNG) public {
27+
blockhashRNG = _blockhashRNG;
28+
}
1729

1830
/**
19-
* @dev Since we don't really need to incentivise requesting the beacon chain randomness,
31+
* @dev Since we don't really need to incentivize requesting the beacon chain randomness,
2032
* this is a stub implementation required for backwards compatibility with the
2133
* RNG interface.
2234
* @notice All the ETH sent here will be lost forever.
2335
* @param _block Block the random number is linked to.
2436
*/
2537
function contribute(uint _block) public payable {}
2638

39+
/**
40+
* @dev Request a random number.
41+
* @dev Since the beacon chain randomness is not related to a block
42+
* we can call ahead its getRN function to check if the PoS merge has happened or not.
43+
*
44+
* @param _block Block linked to the request.
45+
*/
46+
function requestRN(uint _block) public payable {
47+
// Use the old RNG pre-Merge.
48+
if (block.difficulty <= 2**64) {
49+
blockhashRNG.contribute(_block);
50+
} else {
51+
contribute(_block);
52+
}
53+
}
2754

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.
55+
/**
56+
* @dev Get the random number.
57+
* @param _block Block the random number is linked to.
58+
* @return RN Random Number. If the number is not ready or has not been required 0 instead.
3259
*/
33-
function getRN(uint _block) public returns (uint RN) {
34-
if (block.difficulty <= 2**64)
60+
function getRN(uint _block) public returns (uint) {
61+
// if beacon chain randomness is zero
62+
// fallback to blockhash RNG
63+
if (block.difficulty <= 2**64) {
64+
return blockhashRNG.getRN(_block);
65+
} else if (block.number < _block + LOOKAHEAD) {
66+
// Beacon chain returns the random number, but sufficient number of blocks hasn't been mined yet.
67+
// In this case signal to the court that RN isn't ready.
3568
return 0;
36-
return block.difficulty;
69+
} else {
70+
if (randomNumber[_block] == 0) {
71+
randomNumber[_block] = block.difficulty;
72+
}
73+
return randomNumber[_block];
74+
}
3775
}
3876
}

contracts/standard/rng/BeaconRNGFallback.sol

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

0 commit comments

Comments
 (0)