Skip to content

Commit 00a4cc5

Browse files
committed
feat: rng fallback
1 parent f4ed9a0 commit 00a4cc5

26 files changed

+505
-269
lines changed

contracts/deploy/00-chainlink-rng.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const deployRng: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
7070
],
7171
log: true,
7272
});
73+
74+
console.log("Register this Chainlink consumer here: http://vrf.chain.link/");
7375
};
7476

7577
deployRng.tags = ["ChainlinkRNG"];

contracts/deploy/00-home-chain-arbitration-neo.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import { ChainlinkRNG, DisputeKitClassic, KlerosCoreNeo, RandomizerRNG } from ".
1010

1111
const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
1212
const { ethers, deployments, getNamedAccounts, getChainId } = hre;
13-
const { deploy, execute } = deployments;
13+
const { deploy } = deployments;
1414
const { ZeroAddress } = hre.ethers;
15-
const RNG_LOOKAHEAD = 20;
1615

1716
// fallback to hardhat node signers on local network
1817
const deployer = (await getNamedAccounts()).deployer ?? (await hre.ethers.getSigners())[0].address;
@@ -50,16 +49,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
5049
const maxTotalStaked = PNK(2_000_000);
5150
const sortitionModule = await deployUpgradable(deployments, "SortitionModuleNeo", {
5251
from: deployer,
53-
args: [
54-
deployer,
55-
klerosCoreAddress,
56-
minStakingTime,
57-
maxFreezingTime,
58-
rng.target,
59-
RNG_LOOKAHEAD,
60-
maxStakePerJuror,
61-
maxTotalStaked,
62-
],
52+
args: [deployer, klerosCoreAddress, minStakingTime, maxFreezingTime, rng.target, maxStakePerJuror, maxTotalStaked],
6353
log: true,
6454
}); // nonce (implementation), nonce+1 (proxy)
6555

@@ -93,11 +83,11 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
9383
await disputeKitContract.changeCore(klerosCore.address);
9484
}
9585

96-
// rng.changeSortitionModule() only if necessary
97-
const rngSortitionModule = await rng.sortitionModule();
98-
if (rngSortitionModule !== sortitionModule.address) {
99-
console.log(`rng.changeSortitionModule(${sortitionModule.address})`);
100-
await rng.changeSortitionModule(sortitionModule.address);
86+
// rng.changeConsumer() only if necessary
87+
const rngConsumer = await rng.consumer();
88+
if (rngConsumer !== sortitionModule.address) {
89+
console.log(`rng.changeConsumer(${sortitionModule.address})`);
90+
await rng.changeConsumer(sortitionModule.address);
10191
}
10292

10393
const core = (await hre.ethers.getContract("KlerosCoreNeo")) as KlerosCoreNeo;

contracts/deploy/00-home-chain-arbitration.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
1212
const { ethers, deployments, getNamedAccounts, getChainId } = hre;
1313
const { deploy } = deployments;
1414
const { ZeroAddress } = hre.ethers;
15-
const RNG_LOOKAHEAD = 20;
1615

1716
// fallback to hardhat node signers on local network
1817
const deployer = (await getNamedAccounts()).deployer ?? (await hre.ethers.getSigners())[0].address;
@@ -53,7 +52,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
5352
const rng = (await ethers.getContract("ChainlinkRNG")) as ChainlinkRNG;
5453
const sortitionModule = await deployUpgradable(deployments, "SortitionModule", {
5554
from: deployer,
56-
args: [deployer, klerosCoreAddress, minStakingTime, maxFreezingTime, rng.target, RNG_LOOKAHEAD],
55+
args: [deployer, klerosCoreAddress, minStakingTime, maxFreezingTime, rng.target],
5756
log: true,
5857
}); // nonce (implementation), nonce+1 (proxy)
5958

@@ -86,11 +85,11 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
8685
await disputeKitContract.changeCore(klerosCore.address);
8786
}
8887

89-
// rng.changeSortitionModule() only if necessary
90-
const rngSortitionModule = await rng.sortitionModule();
91-
if (rngSortitionModule !== sortitionModule.address) {
92-
console.log(`rng.changeSortitionModule(${sortitionModule.address})`);
93-
await rng.changeSortitionModule(sortitionModule.address);
88+
// rng.changeConsumer() only if necessary
89+
const rngConsumer = await rng.consumer();
90+
if (rngConsumer !== sortitionModule.address) {
91+
console.log(`rng.changeConsumer(${sortitionModule.address})`);
92+
await rng.changeConsumer(sortitionModule.address);
9493
}
9594

9695
const core = (await hre.ethers.getContract("KlerosCore")) as KlerosCore;

contracts/deploy/00-home-chain-resolver.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { HardhatRuntimeEnvironment } from "hardhat/types";
22
import { DeployFunction } from "hardhat-deploy/types";
33
import { HomeChains, isSkipped } from "./utils";
4-
import { deployUpgradable } from "./utils/deployUpgradable";
54

65
const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
76
const { deployments, getNamedAccounts, getChainId } = hre;

contracts/deploy/00-rng.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
22
import { DeployFunction } from "hardhat-deploy/types";
33
import { SortitionModule } from "../typechain-types";
44
import { HomeChains, isMainnet, isSkipped } from "./utils";
5-
import { deployUpgradable } from "./utils/deployUpgradable";
65
import { getContractOrDeploy } from "./utils/getContractOrDeploy";
76

87
const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
98
const { deployments, getNamedAccounts, getChainId, ethers } = hre;
109
const { deploy } = deployments;
11-
const RNG_LOOKAHEAD = 20;
10+
const RNG_LOOKAHEAD_TIME = 30 * 60; // 30 minutes in seconds
1211

1312
// fallback to hardhat node signers on local network
1413
const deployer = (await getNamedAccounts()).deployer ?? (await hre.ethers.getSigners())[0].address;
@@ -32,11 +31,15 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
3231

3332
const rng2 = await deploy("BlockHashRNG", {
3433
from: deployer,
35-
args: [],
34+
args: [
35+
deployer, // governor
36+
sortitionModule.target, // consumer
37+
RNG_LOOKAHEAD_TIME,
38+
],
3639
log: true,
3740
});
3841

39-
await sortitionModule.changeRandomNumberGenerator(rng2.address, RNG_LOOKAHEAD);
42+
await sortitionModule.changeRandomNumberGenerator(rng2.address);
4043
};
4144

4245
deployArbitration.tags = ["RNG"];

contracts/deploy/change-sortition-module-rng.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ const task: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
2323
sortitionModule = await ethers.getContract<SortitionModule>("SortitionModule");
2424
}
2525

26-
console.log(`chainlinkRng.changeSortitionModule(${sortitionModule.target})`);
27-
await chainlinkRng.changeSortitionModule(sortitionModule.target);
26+
console.log(`chainlinkRng.changeConsumer(${sortitionModule.target})`);
27+
await chainlinkRng.changeConsumer(sortitionModule.target);
2828

29-
console.log(`sortitionModule.changeRandomNumberGenerator(${chainlinkRng.target}, 0)`);
30-
await sortitionModule.changeRandomNumberGenerator(chainlinkRng.target, 0);
29+
console.log(`sortitionModule.changeRandomNumberGenerator(${chainlinkRng.target})`);
30+
await sortitionModule.changeRandomNumberGenerator(chainlinkRng.target);
3131
};
3232

3333
task.tags = ["ChangeSortitionModuleRNG"];

contracts/src/arbitration/SortitionModule.sol

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
pragma solidity ^0.8.24;
44

5-
import {SortitionModuleBase, KlerosCore, RNG} from "./SortitionModuleBase.sol";
5+
import {SortitionModuleBase, KlerosCore, IRNG} from "./SortitionModuleBase.sol";
66

77
/// @title SortitionModule
88
/// @dev A factory of trees that keeps track of staked values for sortition.
@@ -24,16 +24,14 @@ contract SortitionModule is SortitionModuleBase {
2424
/// @param _minStakingTime Minimal time to stake
2525
/// @param _maxDrawingTime Time after which the drawing phase can be switched
2626
/// @param _rng The random number generator.
27-
/// @param _rngLookahead Lookahead value for rng.
2827
function initialize(
2928
address _governor,
3029
KlerosCore _core,
3130
uint256 _minStakingTime,
3231
uint256 _maxDrawingTime,
33-
RNG _rng,
34-
uint256 _rngLookahead
32+
IRNG _rng
3533
) external reinitializer(1) {
36-
__SortitionModuleBase_initialize(_governor, _core, _minStakingTime, _maxDrawingTime, _rng, _rngLookahead);
34+
__SortitionModuleBase_initialize(_governor, _core, _minStakingTime, _maxDrawingTime, _rng);
3735
}
3836

3937
function initialize4() external reinitializer(4) {

contracts/src/arbitration/SortitionModuleBase.sol

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {ISortitionModule} from "./interfaces/ISortitionModule.sol";
77
import {IDisputeKit} from "./interfaces/IDisputeKit.sol";
88
import {Initializable} from "../proxy/Initializable.sol";
99
import {UUPSProxiable} from "../proxy/UUPSProxiable.sol";
10-
import {RNG} from "../rng/RNG.sol";
10+
import {IRNG} from "../rng/IRNG.sol";
1111
import "../libraries/Constants.sol";
1212

1313
/// @title SortitionModuleBase
@@ -50,11 +50,9 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
5050
uint256 public minStakingTime; // The time after which the phase can be switched to Drawing if there are open disputes.
5151
uint256 public maxDrawingTime; // The time after which the phase can be switched back to Staking.
5252
uint256 public lastPhaseChange; // The last time the phase was changed.
53-
uint256 public randomNumberRequestBlock; // Number of the block when RNG request was made.
5453
uint256 public disputesWithoutJurors; // The number of disputes that have not finished drawing jurors.
55-
RNG public rng; // The random number generator.
54+
IRNG public rng; // The random number generator.
5655
uint256 public randomNumber; // Random number returned by RNG.
57-
uint256 public rngLookahead; // Minimal block distance between requesting and obtaining a random number.
5856
uint256 public delayedStakeWriteIndex; // The index of the last `delayedStake` item that was written to the array. 0 index is skipped.
5957
uint256 public delayedStakeReadIndex; // The index of the next `delayedStake` item that should be processed. Starts at 1 because 0 index is skipped.
6058
mapping(bytes32 treeHash => SortitionSumTree) sortitionSumTrees; // The mapping trees by keys.
@@ -104,16 +102,14 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
104102
KlerosCore _core,
105103
uint256 _minStakingTime,
106104
uint256 _maxDrawingTime,
107-
RNG _rng,
108-
uint256 _rngLookahead
105+
IRNG _rng
109106
) internal onlyInitializing {
110107
governor = _governor;
111108
core = _core;
112109
minStakingTime = _minStakingTime;
113110
maxDrawingTime = _maxDrawingTime;
114111
lastPhaseChange = block.timestamp;
115112
rng = _rng;
116-
rngLookahead = _rngLookahead;
117113
delayedStakeReadIndex = 1;
118114
}
119115

@@ -153,15 +149,12 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
153149
maxDrawingTime = _maxDrawingTime;
154150
}
155151

156-
/// @dev Changes the `_rng` and `_rngLookahead` storage variables.
157-
/// @param _rng The new value for the `RNGenerator` storage variable.
158-
/// @param _rngLookahead The new value for the `rngLookahead` storage variable.
159-
function changeRandomNumberGenerator(RNG _rng, uint256 _rngLookahead) external onlyByGovernor {
152+
/// @dev Changes the `rng` storage variable.
153+
/// @param _rng The new random number generator.
154+
function changeRandomNumberGenerator(IRNG _rng) external onlyByGovernor {
160155
rng = _rng;
161-
rngLookahead = _rngLookahead;
162156
if (phase == Phase.generating) {
163-
rng.requestRandomness(block.number + rngLookahead);
164-
randomNumberRequestBlock = block.number;
157+
rng.requestRandomness();
165158
}
166159
}
167160

@@ -176,11 +169,10 @@ abstract contract SortitionModuleBase is ISortitionModule, Initializable, UUPSPr
176169
"The minimum staking time has not passed yet."
177170
);
178171
require(disputesWithoutJurors > 0, "There are no disputes that need jurors.");
179-
rng.requestRandomness(block.number + rngLookahead);
180-
randomNumberRequestBlock = block.number;
172+
rng.requestRandomness();
181173
phase = Phase.generating;
182174
} else if (phase == Phase.generating) {
183-
randomNumber = rng.receiveRandomness(randomNumberRequestBlock + rngLookahead);
175+
randomNumber = rng.receiveRandomness();
184176
require(randomNumber != 0, "Random number is not ready yet");
185177
phase = Phase.drawing;
186178
} else if (phase == Phase.drawing) {

contracts/src/arbitration/SortitionModuleNeo.sol

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
pragma solidity ^0.8.24;
44

5-
import {SortitionModuleBase, KlerosCore, RNG, StakingResult} from "./SortitionModuleBase.sol";
5+
import {SortitionModuleBase, KlerosCore, IRNG, StakingResult} from "./SortitionModuleBase.sol";
66

77
/// @title SortitionModuleNeo
88
/// @dev A factory of trees that keeps track of staked values for sortition.
@@ -32,20 +32,18 @@ contract SortitionModuleNeo is SortitionModuleBase {
3232
/// @param _minStakingTime Minimal time to stake
3333
/// @param _maxDrawingTime Time after which the drawing phase can be switched
3434
/// @param _rng The random number generator.
35-
/// @param _rngLookahead Lookahead value for rng.
3635
/// @param _maxStakePerJuror The maximum amount of PNK a juror can stake in a court.
3736
/// @param _maxTotalStaked The maximum amount of PNK that can be staked in all courts.
3837
function initialize(
3938
address _governor,
4039
KlerosCore _core,
4140
uint256 _minStakingTime,
4241
uint256 _maxDrawingTime,
43-
RNG _rng,
44-
uint256 _rngLookahead,
42+
IRNG _rng,
4543
uint256 _maxStakePerJuror,
4644
uint256 _maxTotalStaked
4745
) external reinitializer(2) {
48-
__SortitionModuleBase_initialize(_governor, _core, _minStakingTime, _maxDrawingTime, _rng, _rngLookahead);
46+
__SortitionModuleBase_initialize(_governor, _core, _minStakingTime, _maxDrawingTime, _rng);
4947
maxStakePerJuror = _maxStakePerJuror;
5048
maxTotalStaked = _maxTotalStaked;
5149
}

0 commit comments

Comments
 (0)