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
+16-5Lines changed: 16 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -16,9 +16,11 @@ import "./RNG.sol";
16
16
contractBeaconRNGisRNG {
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
+
uintpublic constant ERROR =20; // 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.
19
20
20
21
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
+
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
24
23
25
/** @dev Constructor.
24
26
* @param _blockhashRNG The blockhash RNG deployed contract address.
@@ -48,6 +50,9 @@ contract BeaconRNG is RNG {
48
50
if (block.difficulty<=2**64) {
49
51
blockhashRNG.contribute(_block);
50
52
} else {
53
+
if (startingBlock[_block] ==0) {
54
+
startingBlock[_block] = _block; // Starting block is equal to requested by default.
55
+
}
51
56
contribute(_block);
52
57
}
53
58
}
@@ -62,11 +67,17 @@ contract BeaconRNG is RNG {
62
67
// fallback to blockhash RNG
63
68
if (block.difficulty<=2**64) {
64
69
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.
68
-
return0;
69
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