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
Copy file name to clipboardExpand all lines: contracts/standard/rng/BeaconRNG.sol
+22-45Lines changed: 22 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -13,14 +13,13 @@ import "./RNG.sol";
13
13
/**
14
14
* @title Random Number Generator using beacon chain random opcode
15
15
*/
16
-
contractBeaconRNGisRNG{
16
+
contractBeaconRNG {
17
17
18
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
19
uintpublic 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.
20
20
21
21
RNG public blockhashRNG; // Address of blockhashRNG to fall back on.
22
-
mapping (uint=>uint) public randomNumber; // randomNumber[_requestedBlock] is the random number for this requested block, 0 otherwise.
23
-
mapping (uint=>uint) public startingBlock; // The starting block number for lookahead countdown. startingBlock[_requestedBlock].
22
+
mapping (uint=>uint) public randomNumber; // randomNumber[_block] is the random number for this requested block, 0 otherwise.
24
23
25
24
/** @dev Constructor.
26
25
* @param _blockhashRNG The blockhash RNG deployed contract address.
@@ -30,58 +29,36 @@ contract BeaconRNG is RNG {
30
29
}
31
30
32
31
/**
33
-
* @dev Since we don't really need to incentivize requesting the beacon chain randomness,
34
-
* this is a stub implementation required for backwards compatibility with the
35
-
* RNG interface.
36
-
* @notice All the ETH sent here will be lost forever.
37
-
* @param _block Block the random number is linked to.
38
-
*/
39
-
function contribute(uint_block) publicpayable {}
40
-
41
-
/**
42
-
* @dev Request a random number.
43
-
* @dev Since the beacon chain randomness is not related to a block
44
-
* we can call ahead its getRN function to check if the PoS merge has happened or not.
45
-
*
46
-
* @param _block Block linked to the request.
32
+
* @dev Request a random number. It is not used by this contract and only exists for backward compatibility.
47
33
*/
48
-
function requestRN(uint_block) publicpayable {
49
-
// Use the old RNG pre-Merge.
50
-
if (block.difficulty<=2**64) {
51
-
blockhashRNG.contribute(_block);
52
-
} else {
53
-
if (startingBlock[_block] ==0) {
54
-
startingBlock[_block] = _block; // Starting block is equal to requested by default.
55
-
}
56
-
contribute(_block);
57
-
}
58
-
}
34
+
function requestRN(uint/*_block*/) publicpure {}
59
35
60
36
/**
61
-
* @dev Get the random number.
37
+
* @dev Get aт uncorrelated random number.
62
38
* @param _block Block the random number is linked to.
63
39
* @return RN Random Number. If the number is not ready or has not been required 0 instead.
64
40
*/
65
-
function getRN(uint_block) publicreturns (uint) {
66
-
// if beacon chain randomness is zero
67
-
// fallback to blockhash RNG
41
+
function getUncorrelatedRN(uint_block) publicreturns (uint) {
42
+
// Pre-Merge.
68
43
if (block.difficulty<=2**64) {
69
-
return blockhashRNG.getRN(_block);
70
-
} else {
71
-
// Reset the starting block if too many blocks passed since lookahead.
72
-
if (block.number> startingBlock[_block] + LOOKAHEAD + ERROR) {
73
-
startingBlock[_block] =block.number;
74
-
}
75
-
if (block.number< startingBlock[_block] + LOOKAHEAD) {
76
-
// Beacon chain returns the random number, but sufficient number of blocks hasn't been mined yet.
77
-
// In this case signal to the court that RN isn't ready.
0 commit comments