You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* @title Random Number Generator using beacon chain random opcode
15
15
*/
16
16
contractBeaconRNGisRNG {
17
+
18
+
uintpublic 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
+
}
17
29
18
30
/**
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,
20
32
* this is a stub implementation required for backwards compatibility with the
21
33
* RNG interface.
22
34
* @notice All the ETH sent here will be lost forever.
23
35
* @param _block Block the random number is linked to.
24
36
*/
25
37
function contribute(uint_block) publicpayable {}
26
38
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) publicpayable {
47
+
// Use the old RNG pre-Merge.
48
+
if (block.difficulty<=2**64) {
49
+
blockhashRNG.contribute(_block);
50
+
} else {
51
+
contribute(_block);
52
+
}
53
+
}
27
54
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.
32
59
*/
33
-
function getRN(uint_block) publicreturns (uintRN) {
34
-
if (block.difficulty<=2**64)
60
+
function getRN(uint_block) publicreturns (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
+
} elseif (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.
0 commit comments