Skip to content

Commit 1258879

Browse files
committed
chore: added RandomizerMock, migrated test and deploy scripts
1 parent 7507f77 commit 1258879

File tree

6 files changed

+125
-45
lines changed

6 files changed

+125
-45
lines changed

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ const pnkByChain = new Map<HomeChains, string>([
1313
[HomeChains.ARBITRUM_GOERLI, "0x4DEeeFD054434bf6721eF39Aa18EfB3fd0D12610"],
1414
]);
1515

16+
const randomizerByChain = new Map<HomeChains, string>([
17+
[HomeChains.ARBITRUM_ONE, "0x00"],
18+
[HomeChains.ARBITRUM_GOERLI, "0xE1B6CcAc0BB0355C01A049e78909231Bfa13620B"],
19+
]);
20+
1621
const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
1722
const { deployments, getNamedAccounts, getChainId } = hre;
1823
const { deploy, execute } = deployments;
@@ -23,18 +28,47 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
2328
const chainId = Number(await getChainId());
2429
console.log("deploying to %s with deployer %s", HomeChains[chainId], deployer);
2530

31+
if (chainId === HomeChains.HARDHAT) {
32+
pnkByChain.set(
33+
HomeChains.HARDHAT,
34+
(
35+
await deploy("PNK", {
36+
from: deployer,
37+
log: true,
38+
})
39+
).address
40+
);
41+
randomizerByChain.set(
42+
HomeChains.HARDHAT,
43+
(
44+
await deploy("RandomizerMock", {
45+
from: deployer,
46+
args: [],
47+
log: true,
48+
})
49+
).address
50+
);
51+
}
52+
2653
await deploy("PolicyRegistry", {
2754
from: deployer,
2855
args: [deployer],
2956
log: true,
3057
});
3158

32-
const rng = await deploy("BlockHashRNG", {
59+
await deploy("BlockHashRNG", {
3360
from: deployer,
3461
args: [],
3562
log: true,
3663
});
3764

65+
const randomizer = randomizerByChain.get(Number(await getChainId())) ?? AddressZero;
66+
const rng = await deploy("RandomizerRNG", {
67+
from: deployer,
68+
args: [randomizer],
69+
log: true,
70+
});
71+
3872
const disputeKit = await deploy("DisputeKitClassic", {
3973
from: deployer,
4074
args: [deployer, AddressZero, rng.address],
@@ -46,17 +80,6 @@ const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment)
4680
log: true,
4781
});
4882

49-
if (chainId === HomeChains.HARDHAT) {
50-
pnkByChain.set(
51-
HomeChains.HARDHAT,
52-
(
53-
await deploy("PNK", {
54-
from: deployer,
55-
log: true,
56-
})
57-
).address
58-
);
59-
}
6083
const pnk = pnkByChain.get(Number(await getChainId())) ?? AddressZero;
6184
const minStake = BigNumber.from(10).pow(20).mul(2);
6285
const alpha = 10000;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8;
4+
5+
import "../RandomizerRNG.sol";
6+
7+
contract RandomizerMock is IRandomizer {
8+
uint256 private id;
9+
10+
function request(uint256 callbackGasLimit) external override returns (uint256) {
11+
return id++;
12+
}
13+
14+
function clientWithdrawTo(address _to, uint256 _amount) external override {
15+
revert("Not Implemented");
16+
}
17+
18+
function relay(RandomizerRNG _rng, uint128 _id, bytes32 _value) external {
19+
_rng.randomizerCallback(_id, _value);
20+
}
21+
}

contracts/test/arbitration/draw.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
22
import { deployments, ethers, getNamedAccounts, network } from "hardhat";
33
import { BigNumber } from "ethers";
4-
import { PNK, KlerosCore, ArbitrableExample, HomeGatewayToEthereum, DisputeKitClassic } from "../../typechain-types";
4+
import {
5+
PNK,
6+
KlerosCore,
7+
ArbitrableExample,
8+
HomeGatewayToEthereum,
9+
DisputeKitClassic,
10+
RandomizerRNG,
11+
RandomizerMock,
12+
} from "../../typechain-types";
513
import { expect } from "chai";
614

715
/* eslint-disable no-unused-vars */
@@ -38,6 +46,8 @@ describe("Draw Benchmark", async () => {
3846
let core;
3947
let arbitrable;
4048
let homeGateway;
49+
let rng;
50+
let randomizer;
4151

4252
beforeEach("Setup", async () => {
4353
deployer = (await getNamedAccounts()).deployer;
@@ -55,6 +65,8 @@ describe("Draw Benchmark", async () => {
5565
core = (await ethers.getContract("KlerosCore")) as KlerosCore;
5666
homeGateway = (await ethers.getContract("HomeGatewayToEthereum")) as HomeGatewayToEthereum;
5767
arbitrable = (await ethers.getContract("ArbitrableExample")) as ArbitrableExample;
68+
rng = (await ethers.getContract("RandomizerRNG")) as RandomizerRNG;
69+
randomizer = (await ethers.getContract("RandomizerMock")) as RandomizerMock;
5870
});
5971

6072
it("Draw Benchmark", async () => {
@@ -96,8 +108,9 @@ describe("Draw Benchmark", async () => {
96108
}
97109
await disputeKit.passPhase(); // Resolving -> Generating
98110
for (let index = 0; index < 20; index++) {
99-
await network.provider.send("evm_mine"); // RNG lookahead
111+
await network.provider.send("evm_mine"); // RNG lookahead, TODO: remove this for RandomizerRNG
100112
}
113+
await randomizer.relay(rng.address, 0, ethers.utils.randomBytes(32));
101114
await disputeKit.passPhase(); // Generating -> Drawing
102115

103116
await expect(core.draw(0, 1000, { gasLimit: 1000000 }))

contracts/test/arbitration/unstake.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ethers, getNamedAccounts, network, deployments } from "hardhat";
22
import { BigNumber } from "ethers";
3-
import { PNK, KlerosCore, DisputeKitClassic } from "../../typechain-types";
3+
import { PNK, KlerosCore, DisputeKitClassic, RandomizerRNG, RandomizerMock } from "../../typechain-types";
44
import { expect } from "chai";
55

66
/* eslint-disable no-unused-vars */
@@ -18,6 +18,8 @@ describe("Unstake juror", async () => {
1818
let disputeKit;
1919
let pnk;
2020
let core;
21+
let rng;
22+
let randomizer;
2123

2224
beforeEach("Setup", async () => {
2325
({ deployer } = await getNamedAccounts());
@@ -32,6 +34,8 @@ describe("Unstake juror", async () => {
3234
disputeKit = (await ethers.getContract("DisputeKitClassic")) as DisputeKitClassic;
3335
pnk = (await ethers.getContract("PNK")) as PNK;
3436
core = (await ethers.getContract("KlerosCore")) as KlerosCore;
37+
rng = (await ethers.getContract("RandomizerRNG")) as RandomizerRNG;
38+
randomizer = (await ethers.getContract("RandomizerMock")) as RandomizerMock;
3539
});
3640

3741
it("Unstake inactive juror", async () => {
@@ -55,8 +59,9 @@ describe("Unstake juror", async () => {
5559
}
5660
await disputeKit.passPhase(); // Resolving -> Generating
5761
for (let index = 0; index < 20; index++) {
58-
await network.provider.send("evm_mine"); // RNG lookahead
62+
await network.provider.send("evm_mine"); // RNG lookahead, TODO: remove this for RandomizerRNG
5963
}
64+
await randomizer.relay(rng.address, 0, ethers.utils.randomBytes(32));
6065
await disputeKit.passPhase(); // Generating -> Drawing
6166

6267
await core.draw(0, 5000);

contracts/test/integration/index.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { expect } from "chai";
22
import { deployments, ethers, getNamedAccounts, network } from "hardhat";
3-
import { BigNumber, utils } from "ethers";
3+
import { BigNumber } from "ethers";
44
import {
5-
BlockHashRNG,
65
PNK,
76
KlerosCore,
87
ForeignGatewayOnEthereum,
98
ArbitrableExample,
109
HomeGatewayToEthereum,
1110
VeaMock,
1211
DisputeKitClassic,
12+
RandomizerRNG,
13+
RandomizerMock,
1314
} from "../../typechain-types";
1415

1516
/* eslint-disable no-unused-vars */
@@ -41,7 +42,7 @@ describe("Integration tests", async () => {
4142
}
4243

4344
let deployer;
44-
let rng, disputeKit, pnk, core, vea, foreignGateway, arbitrable, homeGateway;
45+
let rng, randomizer, disputeKit, pnk, core, vea, foreignGateway, arbitrable, homeGateway;
4546

4647
beforeEach("Setup", async () => {
4748
({ deployer } = await getNamedAccounts());
@@ -53,7 +54,8 @@ describe("Integration tests", async () => {
5354
fallbackToGlobal: true,
5455
keepExistingDeployments: false,
5556
});
56-
rng = (await ethers.getContract("BlockHashRNG")) as BlockHashRNG;
57+
rng = (await ethers.getContract("RandomizerRNG")) as RandomizerRNG;
58+
randomizer = (await ethers.getContract("RandomizerMock")) as RandomizerMock;
5759
disputeKit = (await ethers.getContract("DisputeKitClassic")) as DisputeKitClassic;
5860
pnk = (await ethers.getContract("PNK")) as PNK;
5961
core = (await ethers.getContract("KlerosCore")) as KlerosCore;
@@ -63,18 +65,6 @@ describe("Integration tests", async () => {
6365
homeGateway = (await ethers.getContract("HomeGatewayToEthereum")) as HomeGatewayToEthereum;
6466
});
6567

66-
it("RNG", async () => {
67-
let tx = await rng.receiveRandomness(9876543210);
68-
let trace = await network.provider.send("debug_traceTransaction", [tx.hash]);
69-
let [rn] = ethers.utils.defaultAbiCoder.decode(["uint"], `0x${trace.returnValue}`);
70-
expect(rn).to.equal(0); // requested a block number in the future, so return 0.
71-
72-
tx = await rng.receiveRandomness(5);
73-
trace = await network.provider.send("debug_traceTransaction", [tx.hash]);
74-
[rn] = ethers.utils.defaultAbiCoder.decode(["uint"], `0x${trace.returnValue}`);
75-
expect(rn).to.not.equal(0); // requested a block number in the past, so return non-zero.
76-
});
77-
7868
it("Honest Claim - No Challenge - Bridger paid", async () => {
7969
const arbitrationCost = ONE_TENTH_ETH.mul(3);
8070
const [bridger, challenger, relayer] = await ethers.getSigners();
@@ -153,7 +143,8 @@ describe("Integration tests", async () => {
153143
expect(await disputeKit.phase()).to.equal(DisputeKitPhase.generating);
154144
console.log("KC phase: %d, DK phase: ", await core.phase(), await disputeKit.phase());
155145

156-
await mineBlocks(20); // Wait for RNG lookahead
146+
await mineBlocks(20); // Wait for RNG lookahead, TODO: remove this for RandomizerRNG
147+
await randomizer.relay(rng.address, 0, ethers.utils.randomBytes(32));
157148
await disputeKit.passPhase(); // Generating -> Drawing
158149
expect(await disputeKit.phase()).to.equal(DisputeKitPhase.drawing);
159150
console.log("KC phase: %d, DK phase: ", await core.phase(), await disputeKit.phase());

contracts/test/rng/index.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
import { expect } from "chai";
2-
import { ethers } from "hardhat";
2+
import { ethers, network } from "hardhat";
33
import { BigNumber } from "ethers";
4+
import { IncrementalNG, BlockHashRNG } from "../../typechain-types";
5+
6+
let rng, rngFactory;
7+
const initialNg = 424242;
48

59
describe("IncrementalNG", async () => {
10+
beforeEach("Setup", async () => {
11+
rngFactory = await ethers.getContractFactory("IncrementalNG");
12+
rng = (await rngFactory.deploy(initialNg)) as IncrementalNG;
13+
await rng.deployed();
14+
});
15+
616
it("Should return a number incrementing each time", async () => {
7-
const IncrementalNG = await ethers.getContractFactory("IncrementalNG");
8-
const initialNg = 424242;
9-
const incrementalNG = await IncrementalNG.deploy(initialNg);
10-
await incrementalNG.deployed();
17+
expect(await rng.callStatic.receiveRandomness(689376)).to.equal(initialNg);
18+
await rng.receiveRandomness(29543);
19+
expect(await rng.callStatic.receiveRandomness(5894382)).to.equal(initialNg + 1);
20+
await rng.receiveRandomness(0);
21+
expect(await rng.callStatic.receiveRandomness(3465)).to.equal(initialNg + 2);
22+
await rng.receiveRandomness(BigNumber.from(2).pow(255));
23+
expect(await rng.callStatic.receiveRandomness(0)).to.equal(initialNg + 3);
24+
});
25+
});
26+
27+
describe("BlockHashRNG", async () => {
28+
beforeEach("Setup", async () => {
29+
rngFactory = await ethers.getContractFactory("BlockHashRNG");
30+
rng = (await rngFactory.deploy()) as BlockHashRNG;
31+
await rng.deployed();
32+
});
33+
34+
it("Should return a non-zero number for a block number in the past", async () => {
35+
const tx = await rng.receiveRandomness(9876543210);
36+
const trace = await network.provider.send("debug_traceTransaction", [tx.hash]);
37+
const [rn] = ethers.utils.defaultAbiCoder.decode(["uint"], `0x${trace.returnValue}`);
38+
expect(rn).to.equal(0);
39+
});
1140

12-
expect(await incrementalNG.callStatic.receiveRandomness(689376)).to.equal(initialNg);
13-
await incrementalNG.receiveRandomness(29543);
14-
expect(await incrementalNG.callStatic.receiveRandomness(5894382)).to.equal(initialNg + 1);
15-
await incrementalNG.receiveRandomness(0);
16-
expect(await incrementalNG.callStatic.receiveRandomness(3465)).to.equal(initialNg + 2);
17-
await incrementalNG.receiveRandomness(BigNumber.from(2).pow(255));
18-
expect(await incrementalNG.callStatic.receiveRandomness(0)).to.equal(initialNg + 3);
41+
it("Should return zero for a block number in the future", async () => {
42+
const tx = await rng.receiveRandomness(5);
43+
const trace = await network.provider.send("debug_traceTransaction", [tx.hash]);
44+
const [rn] = ethers.utils.defaultAbiCoder.decode(["uint"], `0x${trace.returnValue}`);
45+
expect(rn).to.not.equal(0);
1946
});
2047
});

0 commit comments

Comments
 (0)