|
| 1 | +import { expect } from "chai"; |
| 2 | +import { deployments, ethers, getNamedAccounts, network } from "hardhat"; |
| 3 | +import { BigNumber } from "ethers"; |
| 4 | +import { |
| 5 | + PNK, |
| 6 | + KlerosCore, |
| 7 | + ArbitrableExample, |
| 8 | + HomeGatewayToEthereum, |
| 9 | + DisputeKitClassic, |
| 10 | +} from "../../typechain-types"; |
| 11 | + |
| 12 | +/* eslint-disable no-unused-vars */ |
| 13 | +/* eslint-disable no-unused-expressions */ // https://github.com/standard/standard/issues/690#issuecomment-278533482 |
| 14 | + |
| 15 | +//describe.only("Draw Benchmark", function () { // To run benchmark in isolation. |
| 16 | +describe("Draw Benchmark", function () { |
| 17 | + const ONE_TENTH_ETH = BigNumber.from(10).pow(17); |
| 18 | + const ONE_THOUSAND_PNK = BigNumber.from(10).pow(21); |
| 19 | + |
| 20 | + const enum Period { |
| 21 | + evidence, // Evidence can be submitted. This is also when drawing has to take place. |
| 22 | + commit, // Jurors commit a hashed vote. This is skipped for courts without hidden votes. |
| 23 | + vote, // Jurors reveal/cast their vote depending on whether the court has hidden votes or not. |
| 24 | + appeal, // The dispute can be appealed. |
| 25 | + execution, // Tokens are redistributed and the ruling is executed. |
| 26 | + } |
| 27 | + |
| 28 | + const enum Phase { |
| 29 | + staking, // Stake can be updated during this phase. |
| 30 | + freezing, // Phase during which the dispute kits can undergo the drawing process. Staking is not allowed during this phase. |
| 31 | + } |
| 32 | + |
| 33 | + const enum DisputeKitPhase { |
| 34 | + resolving, // No disputes that need drawing. |
| 35 | + generating, // Waiting for a random number. Pass as soon as it is ready. |
| 36 | + drawing, // Jurors can be drawn. |
| 37 | + } |
| 38 | + |
| 39 | + let deployer, relayer; |
| 40 | + let ng, disputeKit, pnk, core, arbitrable, homeGateway; |
| 41 | + |
| 42 | + beforeEach("Setup", async () => { |
| 43 | + deployer = (await getNamedAccounts()).deployer; |
| 44 | + relayer = (await getNamedAccounts()).relayer; |
| 45 | + |
| 46 | + console.log("deployer:%s", deployer); |
| 47 | + console.log("named accounts: %O", await getNamedAccounts()); |
| 48 | + |
| 49 | + await deployments.fixture(["Arbitration", "ForeignGateway", "HomeGateway"], { |
| 50 | + fallbackToGlobal: true, |
| 51 | + keepExistingDeployments: false, |
| 52 | + }); |
| 53 | + disputeKit = <DisputeKitClassic>await ethers.getContract("DisputeKitClassic"); |
| 54 | + pnk = <PNK>await ethers.getContract("PNK"); |
| 55 | + core = <KlerosCore>await ethers.getContract("KlerosCore"); |
| 56 | + homeGateway = <HomeGatewayToEthereum>await ethers.getContract("HomeGatewayToEthereum"); |
| 57 | + arbitrable = <ArbitrableExample>await ethers.getContract("ArbitrableExample"); |
| 58 | + }); |
| 59 | + |
| 60 | + |
| 61 | + it("Draw Benchmark", async () => { |
| 62 | + const arbitrationCost = ONE_TENTH_ETH.mul(3); |
| 63 | + const [bridger] = await ethers.getSigners(); |
| 64 | + |
| 65 | + for (let i = 0; i < 16; i++) { |
| 66 | + let wallet = ethers.Wallet.createRandom() |
| 67 | + wallet = wallet.connect(ethers.provider); |
| 68 | + await bridger.sendTransaction({to: wallet.address, value: ethers.utils.parseEther("0.1")}); |
| 69 | + await pnk.transfer(wallet.address, ONE_THOUSAND_PNK) |
| 70 | + await pnk.connect(wallet).approve(core.address, ONE_THOUSAND_PNK); |
| 71 | + await core.connect(wallet).setStake(0, ONE_THOUSAND_PNK); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + // create a dispute |
| 76 | + const tx = await arbitrable.createDispute(2, "0x00", 0, { value: arbitrationCost }); |
| 77 | + const trace = await network.provider.send("debug_traceTransaction", [tx.hash]); |
| 78 | + const [disputeId] = ethers.utils.defaultAbiCoder.decode(["uint"], `0x${trace.returnValue}`); |
| 79 | + const lastBlock = await ethers.provider.getBlock(tx.blockNumber - 1); |
| 80 | + |
| 81 | + // Relayer tx |
| 82 | + const tx2 = await homeGateway |
| 83 | + .connect(await ethers.getSigner(relayer)) |
| 84 | + .relayCreateDispute(31337, lastBlock.hash, disputeId, 2, "0x00", arbitrable.address, { |
| 85 | + value: arbitrationCost, |
| 86 | + }); |
| 87 | + |
| 88 | + await network.provider.send("evm_increaseTime", [130]); // Wait for minStakingTime |
| 89 | + await network.provider.send("evm_mine"); |
| 90 | + await core.passPhase(); // Staking -> Freezing |
| 91 | + await mineNBlocks(20); // Wait for 20 blocks finality |
| 92 | + await disputeKit.passPhase(); // Resolving -> Generating |
| 93 | + await disputeKit.passPhase(); // Generating -> Drawing |
| 94 | + |
| 95 | + const tx3 = await core.draw(0, 1000); |
| 96 | + }); |
| 97 | + |
| 98 | + async function mineNBlocks(n) { |
| 99 | + for (let index = 0; index < n; index++) { |
| 100 | + await network.provider.send("evm_mine"); |
| 101 | + } |
| 102 | + } |
| 103 | +}); |
| 104 | + |
| 105 | +const logJurorBalance = function (result) { |
| 106 | + console.log("staked=%s, locked=%s", ethers.utils.formatUnits(result.staked), ethers.utils.formatUnits(result.locked)); |
| 107 | +}; |
0 commit comments