-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathindex.ts
343 lines (295 loc) · 14 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
import { expect } from "chai";
import { deployments, ethers, getNamedAccounts, network } from "hardhat";
import { BigNumber } from "ethers";
import {
PNK,
KlerosCore,
ForeignGateway,
ArbitrableExample,
HomeGateway,
VeaMock,
DisputeKitClassic,
RandomizerRNG,
RandomizerMock,
SortitionModule,
VRFConsumerV2,
VRFCoordinatorV2Mock,
} from "../../typechain-types";
/* eslint-disable no-unused-vars */
/* eslint-disable no-unused-expressions */ // https://github.com/standard/standard/issues/690#issuecomment-278533482
describe("Integration tests", async () => {
const ONE_TENTH_ETH = BigNumber.from(10).pow(17);
const ONE_ETH = BigNumber.from(10).pow(18);
const ONE_HUNDRED_PNK = BigNumber.from(10).pow(20);
const ONE_THOUSAND_PNK = BigNumber.from(10).pow(21);
const enum Period {
evidence, // Evidence can be submitted. This is also when drawing has to take place.
commit, // Jurors commit a hashed vote. This is skipped for courts without hidden votes.
vote, // Jurors reveal/cast their vote depending on whether the court has hidden votes or not.
appeal, // The dispute can be appealed.
execution, // Tokens are redistributed and the ruling is executed.
}
const enum Phase {
staking, // No disputes that need drawing.
generating, // Waiting for a random number. Pass as soon as it is ready.
drawing, // Jurors can be drawn.
}
let deployer;
let rng,
randomizer,
vrfConsumer,
vrfCoordinator,
disputeKit,
pnk,
core,
vea,
foreignGateway,
arbitrable,
homeGateway,
sortitionModule;
beforeEach("Setup", async () => {
({ deployer } = await getNamedAccounts());
await deployments.fixture(["Arbitration", "VeaMock"], {
fallbackToGlobal: true,
keepExistingDeployments: false,
});
rng = (await ethers.getContract("RandomizerRNG")) as RandomizerRNG;
randomizer = (await ethers.getContract("RandomizerMock")) as RandomizerMock;
vrfCoordinator = (await ethers.getContract("VRFCoordinatorV2Mock")) as VRFCoordinatorV2Mock;
vrfConsumer = (await ethers.getContract("VRFConsumerV2")) as VRFConsumerV2;
disputeKit = (await ethers.getContract("DisputeKitClassic")) as DisputeKitClassic;
pnk = (await ethers.getContract("PNK")) as PNK;
core = (await ethers.getContract("KlerosCore")) as KlerosCore;
vea = (await ethers.getContract("VeaMock")) as VeaMock;
foreignGateway = (await ethers.getContract("ForeignGatewayOnEthereum")) as ForeignGateway;
arbitrable = (await ethers.getContract("ArbitrableExample")) as ArbitrableExample;
homeGateway = (await ethers.getContract("HomeGatewayToEthereum")) as HomeGateway;
sortitionModule = (await ethers.getContract("SortitionModule")) as SortitionModule;
});
it("Resolves a dispute on the home chain with no appeal - Randomizer", async () => {
const arbitrationCost = ONE_TENTH_ETH.mul(3);
const [, , relayer] = await ethers.getSigners();
await pnk.approve(core.address, ONE_THOUSAND_PNK.mul(100));
await core.setStake(1, ONE_THOUSAND_PNK);
await core.getJurorBalance(deployer, 1).then((result) => {
expect(result.totalStaked).to.equal(ONE_THOUSAND_PNK);
expect(result.totalLocked).to.equal(0);
logJurorBalance(result);
});
await core.setStake(1, ONE_HUNDRED_PNK.mul(5));
await core.getJurorBalance(deployer, 1).then((result) => {
expect(result.totalStaked).to.equal(ONE_HUNDRED_PNK.mul(5));
expect(result.totalLocked).to.equal(0);
logJurorBalance(result);
});
await core.setStake(1, 0);
await core.getJurorBalance(deployer, 1).then((result) => {
expect(result.totalStaked).to.equal(0);
expect(result.totalLocked).to.equal(0);
logJurorBalance(result);
});
await core.setStake(1, ONE_THOUSAND_PNK.mul(4));
await core.getJurorBalance(deployer, 1).then((result) => {
expect(result.totalStaked).to.equal(ONE_THOUSAND_PNK.mul(4));
expect(result.totalLocked).to.equal(0);
logJurorBalance(result);
});
const tx = await arbitrable.functions["createDispute(string)"]("future of france", {
value: arbitrationCost,
});
const trace = await network.provider.send("debug_traceTransaction", [tx.hash]);
const [disputeId] = ethers.utils.defaultAbiCoder.decode(["uint"], `0x${trace.returnValue}`); // get returned value from createDispute()
console.log("Dispute Created with disputeId: %d", disputeId);
await expect(tx)
.to.emit(foreignGateway, "CrossChainDisputeOutgoing")
.withArgs(anyValue, arbitrable.address, 1, 2, "0x00");
await expect(tx)
.to.emit(arbitrable, "DisputeRequest")
.withArgs(
foreignGateway.address,
1,
BigNumber.from("46619385602526556702049273755915206310773794210139929511467397410441395547901"),
0,
""
);
const lastBlock = await ethers.provider.getBlock(tx.blockNumber - 1);
const disputeHash = ethers.utils.solidityKeccak256(
["bytes", "bytes32", "uint256", "address", "uint256", "uint256", "bytes"],
[ethers.utils.toUtf8Bytes("createDispute"), lastBlock.hash, 31337, arbitrable.address, disputeId, 2, "0x00"]
);
console.log("dispute hash: ", disputeHash);
// Relayer tx
const tx2 = await homeGateway
.connect(relayer)
.functions["relayCreateDispute((bytes32,uint256,address,uint256,uint256,uint256,string,uint256,bytes))"](
{
foreignBlockHash: lastBlock.hash,
foreignChainID: 31337,
foreignArbitrable: arbitrable.address,
foreignDisputeID: disputeId,
externalDisputeID: ethers.utils.keccak256(ethers.utils.toUtf8Bytes("future of france")),
templateId: 0,
templateUri: "",
choices: 2,
extraData: "0x00",
},
{ value: arbitrationCost }
);
expect(tx2).to.emit(homeGateway, "Dispute");
const events2 = (await tx2.wait()).events;
await network.provider.send("evm_increaseTime", [2000]); // Wait for minStakingTime
await network.provider.send("evm_mine");
expect(await sortitionModule.phase()).to.equal(Phase.staking);
expect(await sortitionModule.disputesWithoutJurors()).to.equal(1);
console.log("KC phase: %d", await sortitionModule.phase());
await sortitionModule.passPhase(); // Staking -> Generating
expect(await sortitionModule.phase()).to.equal(Phase.generating);
console.log("KC phase: %d", await sortitionModule.phase());
await randomizer.relay(rng.address, 0, ethers.utils.randomBytes(32));
await sortitionModule.passPhase(); // Generating -> Drawing
expect(await sortitionModule.phase()).to.equal(Phase.drawing);
console.log("KC phase: %d", await sortitionModule.phase());
const tx3 = await core.draw(0, 1000);
console.log("draw successful");
const events3 = (await tx3.wait()).events;
const roundInfo = await core.getRoundInfo(0, 0);
expect(roundInfo.drawnJurors).deep.equal([deployer, deployer, deployer]);
expect(roundInfo.pnkAtStakePerJuror).to.equal(ONE_HUNDRED_PNK.mul(2));
expect(roundInfo.totalFeesForJurors).to.equal(arbitrationCost);
expect(roundInfo.feeToken).to.equal(ethers.constants.AddressZero);
expect((await core.disputes(0)).period).to.equal(Period.evidence);
await core.passPeriod(0);
expect((await core.disputes(0)).period).to.equal(Period.vote);
await disputeKit.connect(await ethers.getSigner(deployer)).castVote(0, [0, 1, 2], 0, 0, "");
await core.passPeriod(0);
await network.provider.send("evm_increaseTime", [100]); // Wait for the appeal period
await network.provider.send("evm_mine");
await core.passPeriod(0);
expect((await core.disputes(0)).period).to.equal(Period.execution);
expect(await core.execute(0, 0, 1000)).to.emit(core, "TokenAndETHShift");
const tx4 = await core.executeRuling(0, { gasLimit: 10000000, gasPrice: 5000000000 });
console.log("Ruling executed on KlerosCore");
expect(tx4).to.emit(core, "Ruling").withArgs(homeGateway.address, 0, 0);
expect(tx4).to.emit(arbitrable, "Ruling").withArgs(foreignGateway.address, 1, 0); // The ForeignGateway starts counting disputeID from 1.
});
it("Resolves a dispute on the home chain with no appeal - Chainlink VRF v2", async () => {
const arbitrationCost = ONE_TENTH_ETH.mul(3);
const [bridger, challenger, relayer] = await ethers.getSigners();
await sortitionModule.changeRandomNumberGenerator(vrfConsumer.address);
await pnk.approve(core.address, ONE_THOUSAND_PNK.mul(100));
await core.setStake(1, ONE_THOUSAND_PNK);
await core.getJurorBalance(deployer, 1).then((result) => {
expect(result.totalStaked).to.equal(ONE_THOUSAND_PNK);
expect(result.totalLocked).to.equal(0);
logJurorBalance(result);
});
await core.setStake(1, ONE_HUNDRED_PNK.mul(5));
await core.getJurorBalance(deployer, 1).then((result) => {
expect(result.totalStaked).to.equal(ONE_HUNDRED_PNK.mul(5));
expect(result.totalLocked).to.equal(0);
logJurorBalance(result);
});
await core.setStake(1, 0);
await core.getJurorBalance(deployer, 1).then((result) => {
expect(result.totalStaked).to.equal(0);
expect(result.totalLocked).to.equal(0);
logJurorBalance(result);
});
await core.setStake(1, ONE_THOUSAND_PNK.mul(4));
await core.getJurorBalance(deployer, 1).then((result) => {
expect(result.totalStaked).to.equal(ONE_THOUSAND_PNK.mul(4));
expect(result.totalLocked).to.equal(0);
logJurorBalance(result);
});
const tx = await arbitrable.functions["createDispute(string)"]("RNG test", {
value: arbitrationCost,
});
const trace = await network.provider.send("debug_traceTransaction", [tx.hash]);
const [disputeId] = ethers.utils.defaultAbiCoder.decode(["uint"], `0x${trace.returnValue}`); // get returned value from createDispute()
console.log("Dispute Created with disputeId: %d", disputeId);
await expect(tx)
.to.emit(foreignGateway, "CrossChainDisputeOutgoing")
.withArgs(anyValue, arbitrable.address, 1, 2, "0x00");
await expect(tx)
.to.emit(arbitrable, "DisputeRequest")
.withArgs(
foreignGateway.address,
1,
BigNumber.from("100587076116875319099890440047601180158236049259177371049006183970829186180694"),
0,
""
);
const lastBlock = await ethers.provider.getBlock(tx.blockNumber - 1);
const disputeHash = ethers.utils.solidityKeccak256(
["bytes", "bytes32", "uint256", "address", "uint256", "uint256", "bytes"],
[ethers.utils.toUtf8Bytes("createDispute"), lastBlock.hash, 31337, arbitrable.address, disputeId, 2, "0x00"]
);
console.log("dispute hash: ", disputeHash);
// Relayer tx
const tx2 = await homeGateway
.connect(relayer)
.functions["relayCreateDispute((bytes32,uint256,address,uint256,uint256,uint256,string,uint256,bytes))"](
{
foreignBlockHash: lastBlock.hash,
foreignChainID: 31337,
foreignArbitrable: arbitrable.address,
foreignDisputeID: disputeId,
externalDisputeID: ethers.utils.keccak256(ethers.utils.toUtf8Bytes("RNG test")),
templateId: 0,
templateUri: "",
choices: 2,
extraData: "0x00",
},
{ value: arbitrationCost }
);
expect(tx2).to.emit(homeGateway, "Dispute");
const events2 = (await tx2.wait()).events;
await network.provider.send("evm_increaseTime", [2000]); // Wait for minStakingTime
await network.provider.send("evm_mine");
expect(await sortitionModule.phase()).to.equal(Phase.staking);
expect(await sortitionModule.disputesWithoutJurors()).to.equal(1);
console.log("KC phase: %d", await sortitionModule.phase());
await sortitionModule.passPhase(); // Staking -> Generating
expect(await sortitionModule.phase()).to.equal(Phase.generating);
console.log("KC phase: %d", await sortitionModule.phase());
const requestId = await vrfConsumer.lastRequestId(); // Needed as we emulate the vrfCoordinator manually
await vrfCoordinator.fulfillRandomWords(requestId, vrfConsumer.address); // The callback calls sortitionModule.passPhase(); // Generating -> Drawing
expect(await sortitionModule.phase()).to.equal(Phase.drawing);
console.log("KC phase: %d", await sortitionModule.phase());
const tx3 = await core.draw(0, 1000);
console.log("draw successful");
const events3 = (await tx3.wait()).events;
const roundInfo = await core.getRoundInfo(0, 0);
expect(roundInfo.drawnJurors).deep.equal([deployer, deployer, deployer]);
expect(roundInfo.pnkAtStakePerJuror).to.equal(ONE_HUNDRED_PNK.mul(2));
expect(roundInfo.totalFeesForJurors).to.equal(arbitrationCost);
expect(roundInfo.feeToken).to.equal(ethers.constants.AddressZero);
expect((await core.disputes(0)).period).to.equal(Period.evidence);
await core.passPeriod(0);
expect((await core.disputes(0)).period).to.equal(Period.vote);
await disputeKit.connect(await ethers.getSigner(deployer)).castVote(0, [0, 1, 2], 0, 0, "");
await core.passPeriod(0);
await network.provider.send("evm_increaseTime", [100]); // Wait for the appeal period
await network.provider.send("evm_mine");
await core.passPeriod(0);
expect((await core.disputes(0)).period).to.equal(Period.execution);
expect(await core.execute(0, 0, 1000)).to.emit(core, "TokenAndETHShift");
const tx4 = await core.executeRuling(0, { gasLimit: 10000000, gasPrice: 5000000000 });
console.log("Ruling executed on KlerosCore");
expect(tx4).to.emit(core, "Ruling").withArgs(homeGateway.address, 0, 0);
expect(tx4).to.emit(arbitrable, "Ruling").withArgs(foreignGateway.address, 1, 0); // The ForeignGateway starts counting disputeID from 1.
});
const mineBlocks = async (n: number) => {
for (let index = 0; index < n; index++) {
await network.provider.send("evm_mine");
}
};
});
const logJurorBalance = async (result) => {
console.log(
"staked=%s, locked=%s",
ethers.utils.formatUnits(result.totalStaked),
ethers.utils.formatUnits(result.totalLocked)
);
};