Skip to content

Commit cb59730

Browse files
unknownunknown1jaybuidl
authored andcommitted
feat(RNG): governor for RNG and lookahead
1 parent 22b8e27 commit cb59730

File tree

8 files changed

+64
-29
lines changed

8 files changed

+64
-29
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
2222
const { deployments, getNamedAccounts, getChainId } = hre;
2323
const { deploy, execute } = deployments;
2424
const { AddressZero } = hre.ethers.constants;
25+
const RNG_LOOKAHEAD = 20;
2526

2627
// fallback to hardhat node signers on local network
2728
const deployer = (await getNamedAccounts()).deployer ?? (await hre.ethers.getSigners())[0].address;
@@ -65,13 +66,13 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
6566
const randomizer = randomizerByChain.get(Number(await getChainId())) ?? AddressZero;
6667
const rng = await deploy("RandomizerRNG", {
6768
from: deployer,
68-
args: [randomizer],
69+
args: [randomizer, deployer],
6970
log: true,
7071
});
7172

7273
const disputeKit = await deploy("DisputeKitClassic", {
7374
from: deployer,
74-
args: [deployer, AddressZero, rng.address],
75+
args: [deployer, AddressZero, rng.address, RNG_LOOKAHEAD],
7576
log: true,
7677
});
7778

contracts/src/arbitration/dispute-kits/DisputeKitClassic.sol

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ contract DisputeKitClassic is BaseDisputeKit, IEvidence {
7171
uint256 public constant LOSER_STAKE_MULTIPLIER = 20000; // Multiplier of the appeal cost that the loser has to pay as fee stake for a round in basis points. Default is 2x of appeal fee.
7272
uint256 public constant LOSER_APPEAL_PERIOD_MULTIPLIER = 5000; // Multiplier of the appeal period for the choice that wasn't voted for in the previous round, in basis points. Default is 1/2 of original appeal period.
7373
uint256 public constant ONE_BASIS_POINT = 10000; // One basis point, for scaling.
74-
uint256 public constant RNG_LOOKAHEAD = 20; // Minimum block distance between requesting and obtaining a random number. Arbitrum sequencer finality = 20 blocks.
7574

7675
RNG public rng; // The random number generator
7776
uint256 public rngRequestedBlock; // The block number requested to the random number.
77+
uint256 public rngLookahead; // Minimum block distance between requesting and obtaining a random number.
7878
uint256 public randomNumber; // The current random number.
7979
Phase public phase; // Current phase of this dispute kit.
8080
uint256 public disputesWithoutJurors; // The number of disputes that have not finished drawing jurors.
@@ -117,9 +117,11 @@ contract DisputeKitClassic is BaseDisputeKit, IEvidence {
117117
* @param _governor The governor's address.
118118
* @param _core The KlerosCore arbitrator.
119119
* @param _rng The random number generator.
120+
* @param _rngLookahead Lookahead value for rng.
120121
*/
121-
constructor(address _governor, KlerosCore _core, RNG _rng) BaseDisputeKit(_governor, _core) {
122+
constructor(address _governor, KlerosCore _core, RNG _rng, uint256 _rngLookahead) BaseDisputeKit(_governor, _core) {
122123
rng = _rng;
124+
rngLookahead = _rngLookahead;
123125
}
124126

125127
// ************************ //
@@ -142,11 +144,13 @@ contract DisputeKitClassic is BaseDisputeKit, IEvidence {
142144

143145
/** @dev Changes the `_rng` storage variable.
144146
* @param _rng The new value for the `RNGenerator` storage variable.
147+
* @param _rngLookahead The new value for the `rngLookahead` storage variable.
145148
*/
146-
function changeRandomNumberGenerator(RNG _rng) external onlyByGovernor {
149+
function changeRandomNumberGenerator(RNG _rng, uint256 _rngLookahead) external onlyByGovernor {
147150
rng = _rng;
151+
rngLookahead = _rngLookahead;
148152
if (phase == Phase.generating) {
149-
rngRequestedBlock = block.number + RNG_LOOKAHEAD;
153+
rngRequestedBlock = block.number + rngLookahead;
150154
rng.requestRandomness(rngRequestedBlock);
151155
}
152156
}
@@ -193,7 +197,7 @@ contract DisputeKitClassic is BaseDisputeKit, IEvidence {
193197
} else if (core.phase() == KlerosCore.Phase.freezing) {
194198
if (phase == Phase.resolving) {
195199
require(disputesWithoutJurors > 0, "All the disputes have jurors");
196-
rngRequestedBlock = core.freezeBlock() + RNG_LOOKAHEAD;
200+
rngRequestedBlock = core.freezeBlock() + rngLookahead;
197201
rng.requestRandomness(rngRequestedBlock);
198202
phase = Phase.generating;
199203
} else if (phase == Phase.generating) {

contracts/src/arbitration/dispute-kits/DisputeKitSybilResistant.sol

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ contract DisputeKitSybilResistant is BaseDisputeKit, IEvidence {
8080
uint256 public constant LOSER_STAKE_MULTIPLIER = 20000; // Multiplier of the appeal cost that the loser has to pay as fee stake for a round in basis points. Default is 2x of appeal fee.
8181
uint256 public constant LOSER_APPEAL_PERIOD_MULTIPLIER = 5000; // Multiplier of the appeal period for the choice that wasn't voted for in the previous round, in basis points. Default is 1/2 of original appeal period.
8282
uint256 public constant ONE_BASIS_POINT = 10000; // One basis point, for scaling.
83-
uint256 public constant RNG_LOOKAHEAD = 20; // Minimum block distance between requesting and obtaining a random number. Arbitrum sequencer finality = 20 blocks.
8483

8584
RNG public rng; // The random number generator
8685
IProofOfHumanity public poh; // The Proof of Humanity registry
8786
uint256 public rngRequestedBlock; // The block number requested to the random number.
87+
uint256 public rngLookahead; // Minimum block distance between requesting and obtaining a random number.
8888
uint256 public randomNumber; // The current random number.
8989
Phase public phase; // Current phase of this dispute kit.
9090
uint256 public disputesWithoutJurors; // The number of disputes that have not finished drawing jurors.
@@ -127,9 +127,18 @@ contract DisputeKitSybilResistant is BaseDisputeKit, IEvidence {
127127
* @param _governor The governor's address.
128128
* @param _core The KlerosCore arbitrator.
129129
* @param _rng The random number generator.
130+
* @param _rngLookahead Lookahead value for rng.
131+
* @param _poh ProofOfHumanity contract.
130132
*/
131-
constructor(address _governor, KlerosCore _core, RNG _rng, IProofOfHumanity _poh) BaseDisputeKit(_governor, _core) {
133+
constructor(
134+
address _governor,
135+
KlerosCore _core,
136+
RNG _rng,
137+
uint256 _rngLookahead,
138+
IProofOfHumanity _poh
139+
) BaseDisputeKit(_governor, _core) {
132140
rng = _rng;
141+
rngLookahead = _rngLookahead;
133142
poh = _poh;
134143
}
135144

@@ -153,11 +162,13 @@ contract DisputeKitSybilResistant is BaseDisputeKit, IEvidence {
153162

154163
/** @dev Changes the `_rng` storage variable.
155164
* @param _rng The new value for the `RNGenerator` storage variable.
165+
* @param _rngLookahead The new value for the `rngLookahead` storage variable.
156166
*/
157-
function changeRandomNumberGenerator(RNG _rng) external onlyByGovernor {
167+
function changeRandomNumberGenerator(RNG _rng, uint256 _rngLookahead) external onlyByGovernor {
158168
rng = _rng;
169+
rngLookahead = _rngLookahead;
159170
if (phase == Phase.generating) {
160-
rngRequestedBlock = block.number + RNG_LOOKAHEAD;
171+
rngRequestedBlock = block.number + rngLookahead;
161172
rng.requestRandomness(rngRequestedBlock);
162173
}
163174
}
@@ -211,7 +222,7 @@ contract DisputeKitSybilResistant is BaseDisputeKit, IEvidence {
211222
} else if (core.phase() == KlerosCore.Phase.freezing) {
212223
if (phase == Phase.resolving) {
213224
require(disputesWithoutJurors > 0, "All the disputes have jurors");
214-
rngRequestedBlock = core.freezeBlock() + RNG_LOOKAHEAD;
225+
rngRequestedBlock = core.freezeBlock() + rngLookahead;
215226
rng.requestRandomness(rngRequestedBlock);
216227
phase = Phase.generating;
217228
} else if (phase == Phase.generating) {

contracts/src/rng/RandomizerRNG.sol

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,31 @@ import "./IRandomizer.sol";
1111
*/
1212
contract RandomizerRNG is RNG {
1313
uint256 public constant CALLBACK_GAS_LIMIT = 50000;
14-
// TODO: should the owner be KlerosLiquid by default?
15-
address public owner = msg.sender; // The address that can withdraw funds.
14+
address public governor; // The address that can withdraw funds.
1615

1716
IRandomizer public randomizer; // Randomizer address.
1817
mapping(uint128 => uint256) public randomNumbers; // randomNumbers[requestID] is the random number for this request id, 0 otherwise.
1918
mapping(address => uint128) public requesterToID; // Maps the requester to his latest request ID.
2019

20+
modifier onlyByGovernor() {
21+
require(governor == msg.sender, "Governor only");
22+
_;
23+
}
24+
2125
/** @dev Constructor.
2226
* @param _randomizer Randomizer contract.
27+
* @param _governor Governor of the contract.
2328
*/
24-
constructor(IRandomizer _randomizer) {
29+
constructor(IRandomizer _randomizer, address _governor) {
2530
randomizer = _randomizer;
31+
governor = _governor;
32+
}
33+
34+
/** @dev Changes the governor of the contract.
35+
* @param _governor The new governor.
36+
*/
37+
function changeGovernor(address _governor) external onlyByGovernor {
38+
governor = _governor;
2639
}
2740

2841
/**
@@ -52,11 +65,10 @@ contract RandomizerRNG is RNG {
5265
}
5366

5467
/**
55-
* @dev Allows the owner to withdraw randomizer funds.
68+
* @dev Allows the governor to withdraw randomizer funds.
5669
* @param _amount Amount to withdraw in wei.
5770
*/
58-
function randomizerWithdraw(uint256 _amount) external {
59-
require(msg.sender == owner, "Only owner allowed");
71+
function randomizerWithdraw(uint256 _amount) external onlyByGovernor {
6072
randomizer.clientWithdrawTo(msg.sender, _amount);
6173
}
6274
}

contracts/test/arbitration/draw.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ describe("Draw Benchmark", async () => {
103103
await network.provider.send("evm_increaseTime", [2000]); // Wait for minStakingTime
104104
await network.provider.send("evm_mine");
105105
await core.passPhase(); // Staking -> Freezing
106-
for (let index = 0; index < 20; index++) {
107-
await network.provider.send("evm_mine"); // Wait for 20 blocks finality
106+
107+
const lookahead = await disputeKit.rngLookahead();
108+
for (let index = 0; index < lookahead; index++) {
109+
await network.provider.send("evm_mine");
108110
}
109111
await disputeKit.passPhase(); // Resolving -> Generating
110-
for (let index = 0; index < 20; index++) {
111-
await network.provider.send("evm_mine"); // RNG lookahead, TODO: remove this for RandomizerRNG
112+
for (let index = 0; index < lookahead; index++) {
113+
await network.provider.send("evm_mine");
112114
}
113115
await randomizer.relay(rng.address, 0, ethers.utils.randomBytes(32));
114116
await disputeKit.passPhase(); // Generating -> Drawing

contracts/test/arbitration/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const WINNER_STAKE_MULTIPLIER = 3000;
77
const LOSER_STAKE_MULTIPLIER = 7000;
88
const MULTIPLIER_DENOMINATOR = 10000;
99

10+
const RNG_LOOKAHEAD = 20;
11+
1012
describe("DisputeKitClassic", async () => {
1113
// eslint-disable-next-line no-unused-vars
1214
let deployer;
@@ -78,7 +80,8 @@ async function deployContracts(deployer) {
7880
const disputeKit = await disputeKitFactory.deploy(
7981
deployer.address,
8082
ethers.constants.AddressZero, // KlerosCore is set later once it is deployed
81-
rng.address
83+
rng.address,
84+
RNG_LOOKAHEAD
8285
);
8386
await disputeKit.deployed();
8487

contracts/test/arbitration/unstake.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ describe("Unstake juror", async () => {
5353

5454
await network.provider.send("evm_increaseTime", [2000]); // Wait for minStakingTime
5555
await network.provider.send("evm_mine");
56+
57+
const lookahead = await disputeKit.rngLookahead();
5658
await core.passPhase(); // Staking -> Freezing
57-
for (let index = 0; index < 20; index++) {
58-
await network.provider.send("evm_mine"); // Wait for 20 blocks finality
59+
for (let index = 0; index < lookahead; index++) {
60+
await network.provider.send("evm_mine");
5961
}
6062
await disputeKit.passPhase(); // Resolving -> Generating
61-
for (let index = 0; index < 20; index++) {
62-
await network.provider.send("evm_mine"); // RNG lookahead, TODO: remove this for RandomizerRNG
63+
for (let index = 0; index < lookahead; index++) {
64+
await network.provider.send("evm_mine");
6365
}
6466
await randomizer.relay(rng.address, 0, ethers.utils.randomBytes(32));
6567
await disputeKit.passPhase(); // Generating -> Drawing

contracts/test/integration/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ describe("Integration tests", async () => {
138138
expect(await core.phase()).to.equal(Phase.freezing);
139139
console.log("KC phase: %d, DK phase: ", await core.phase(), await disputeKit.phase());
140140

141-
await mineBlocks(20); // Wait for 20 blocks finality
141+
await mineBlocks(await disputeKit.rngLookahead());
142142
await disputeKit.passPhase(); // Resolving -> Generating
143143
expect(await disputeKit.phase()).to.equal(DisputeKitPhase.generating);
144144
console.log("KC phase: %d, DK phase: ", await core.phase(), await disputeKit.phase());
145145

146-
await mineBlocks(20); // Wait for RNG lookahead, TODO: remove this for RandomizerRNG
146+
await mineBlocks(await disputeKit.rngLookahead());
147147
await randomizer.relay(rng.address, 0, ethers.utils.randomBytes(32));
148148
await disputeKit.passPhase(); // Generating -> Drawing
149149
expect(await disputeKit.phase()).to.equal(DisputeKitPhase.drawing);

0 commit comments

Comments
 (0)