|
| 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