Skip to content

Commit 2c88a46

Browse files
committed
feat: add BeaconRNG With blockhash fallback
1 parent ab8b223 commit 2c88a46

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

contracts/standard/rng/BeaconRNG.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @authors: [@shalzz
2+
* @authors: [@shalzz]
33
* @reviewers: []
44
* @auditors: []
55
* @bounties: []
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @authors: [@shalzz]
3+
* @reviewers: []
4+
* @auditors: []
5+
* @bounties: []
6+
* @deployments: []
7+
*/
8+
9+
pragma solidity ^0.4.15;
10+
11+
import "./RNG.sol";
12+
13+
/**
14+
* @title Random Number Generator using beacon chain random opcode
15+
*/
16+
contract BeaconRNGFallBack is RNG {
17+
18+
RNG public beaconRNG;
19+
RNG public blockhashRNG;
20+
21+
/** @dev Constructor.
22+
* @param _beaconRNG The beacon chain RNG deployed contract address
23+
* @param _blockhashRNG The blockhash RNG deployed contract address
24+
*/
25+
constructor(RNG _beaconRNG, RNG _blockhashRNG) public {
26+
beaconRNG = _beaconRNG;
27+
blockhashRNG = _blockhashRNG;
28+
}
29+
30+
/**
31+
* @dev Request a random number.
32+
* @dev Since the beacon chain randomness is not related to a block
33+
* we can call ahead its getRN function to check if the PoS merge has happened or not.
34+
*
35+
* @param _block Block linked to the request.
36+
*/
37+
function requestRN(uint _block) public payable {
38+
uint RN = beaconRNG.getRN(_block);
39+
40+
if (RN == 0) {
41+
blockhashRNG.contribute(_block);
42+
} else {
43+
beaconRNG.contribute(_block);
44+
}
45+
}
46+
47+
/**
48+
* @dev Get the random number.
49+
* @param _block Block the random number is linked to.
50+
* @return RN Random Number. If the number is not ready or has not been required 0 instead.
51+
*/
52+
function getRN(uint _block) public returns (uint RN) {
53+
RN = beaconRNG.getRN(_block);
54+
55+
// if beacon chain randomness is zero
56+
// fallback to blockhash RNG
57+
if (RN == 0) {
58+
RN = blockhashRNG.getRN(_block);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)