11import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs" ;
22import { deployments , ethers , getNamedAccounts , network } from "hardhat" ;
3- import { BigNumber , ContractReceipt , ContractTransaction , Wallet } from "ethers" ;
3+ import { toBigInt , ContractTransactionResponse , HDNodeWallet } from "ethers" ;
44import {
55 PNK ,
66 KlerosCore ,
77 ArbitrableExample ,
88 HomeGateway ,
99 DisputeKitClassic ,
1010 SortitionModule ,
11+ IncrementalNG ,
1112} from "../../typechain-types" ;
1213import { expect } from "chai" ;
13- import { DrawEvent } from "../../typechain-types/src/kleros-v1/kleros-liquid-xdai/XKlerosLiquidV2" ;
1414import { Courts } from "../../deploy/utils" ;
1515
1616/* eslint-disable no-unused-vars */
1717/* eslint-disable no-unused-expressions */ // https://github.com/standard/standard/issues/690#issuecomment-278533482
1818
1919describe ( "Draw Benchmark" , async ( ) => {
20- const ONE_TENTH_ETH = BigNumber . from ( 10 ) . pow ( 17 ) ;
21- const ONE_THOUSAND_PNK = BigNumber . from ( 10 ) . pow ( 21 ) ;
20+ const ONE_TENTH_ETH = toBigInt ( 10 ) ** toBigInt ( 17 ) ;
21+ const ONE_THOUSAND_PNK = toBigInt ( 10 ) ** toBigInt ( 21 ) ;
2222
2323 const enum Period {
2424 evidence , // Evidence can be submitted. This is also when drawing has to take place.
@@ -41,18 +41,19 @@ describe("Draw Benchmark", async () => {
4141
4242 let deployer ;
4343 let relayer ;
44- let disputeKit ;
45- let pnk ;
46- let core ;
47- let arbitrable ;
48- let homeGateway ;
49- let sortitionModule ;
50- let rng ;
51- let parentCourtMinStake : BigNumber ;
52- let childCourtMinStake : BigNumber ;
53- const RANDOM = BigNumber . from ( "61688911660239508166491237672720926005752254046266901728404745669596507231249" ) ;
44+ let disputeKit : DisputeKitClassic ;
45+ let pnk : PNK ;
46+ let core : KlerosCore ;
47+ let arbitrable : ArbitrableExample ;
48+ let homeGateway : HomeGateway ;
49+ let sortitionModule : SortitionModule ;
50+ let rng : IncrementalNG ;
51+ let parentCourtMinStake : bigint ;
52+ let childCourtMinStake : bigint ;
53+ const RANDOM = toBigInt ( "61688911660239508166491237672720926005752254046266901728404745669596507231249" ) ;
5454 const PARENT_COURT = 1 ;
5555 const CHILD_COURT = 2 ;
56+ const abiCoder = ethers . AbiCoder . defaultAbiCoder ( ) ;
5657
5758 beforeEach ( "Setup" , async ( ) => {
5859 ( { deployer, relayer } = await getNamedAccounts ( ) ) ;
@@ -70,21 +71,22 @@ describe("Draw Benchmark", async () => {
7071
7172 parentCourtMinStake = await core . courts ( Courts . GENERAL ) . then ( ( court ) => court . minStake ) ;
7273
73- childCourtMinStake = BigNumber . from ( 10 ) . pow ( 20 ) . mul ( 3 ) ; // 300 PNK
74+ childCourtMinStake = toBigInt ( 10 ) ** toBigInt ( 20 ) * toBigInt ( 3 ) ; // 300 PNK
7475
7576 // Make the tests more deterministic with this dummy RNG
76- rng = await deployments . deploy ( "IncrementalNG" , {
77+ await deployments . deploy ( "IncrementalNG" , {
7778 from : deployer ,
7879 args : [ RANDOM ] ,
7980 log : true ,
8081 } ) ;
82+ rng = ( await ethers . getContract ( "IncrementalNG" ) ) as IncrementalNG ;
8183
82- await sortitionModule . changeRandomNumberGenerator ( rng . address , 20 ) ;
84+ await sortitionModule . changeRandomNumberGenerator ( rng . target , 20 ) ;
8385
8486 // CourtId 2 = CHILD_COURT
85- const minStake = BigNumber . from ( 10 ) . pow ( 20 ) . mul ( 3 ) ; // 300 PNK
87+ const minStake = toBigInt ( 10 ) ** toBigInt ( 20 ) * toBigInt ( 3 ) ; // 300 PNK
8688 const alpha = 10000 ;
87- const feeForJuror = BigNumber . from ( 10 ) . pow ( 17 ) ;
89+ const feeForJuror = toBigInt ( 10 ) ** toBigInt ( 17 ) ;
8890 await core . createCourt (
8991 1 ,
9092 false ,
@@ -93,24 +95,24 @@ describe("Draw Benchmark", async () => {
9395 feeForJuror ,
9496 256 ,
9597 [ 0 , 0 , 0 , 10 ] , // evidencePeriod, commitPeriod, votePeriod, appealPeriod
96- ethers . utils . hexlify ( 5 ) , // Extra data for sortition module will return the default value of K)
98+ ethers . toBeHex ( 5 ) , // Extra data for sortition module will return the default value of K)
9799 [ 1 ]
98100 ) ;
99101 } ) ;
100102
101103 type CountedDraws = { [ address : string ] : number } ;
102- type SetStake = ( wallet : Wallet ) => Promise < void > ;
103- type ExpectFromDraw = ( drawTx : Promise < ContractTransaction > ) => Promise < void > ;
104+ type SetStake = ( wallet : HDNodeWallet ) => Promise < void > ;
105+ type ExpectFromDraw = ( drawTx : Promise < ContractTransactionResponse > ) => Promise < void > ;
104106
105107 const draw = async (
106108 stake : SetStake ,
107109 createDisputeCourtId : number ,
108110 expectFromDraw : ExpectFromDraw ,
109111 unstake : SetStake
110112 ) => {
111- const arbitrationCost = ONE_TENTH_ETH . mul ( 3 ) ;
113+ const arbitrationCost = ONE_TENTH_ETH * toBigInt ( 3 ) ;
112114 const [ bridger ] = await ethers . getSigners ( ) ;
113- const wallets : Wallet [ ] = [ ] ;
115+ const wallets : HDNodeWallet [ ] = [ ] ;
114116
115117 // Stake some jurors
116118 for ( let i = 0 ; i < 16 ; i ++ ) {
@@ -119,36 +121,37 @@ describe("Draw Benchmark", async () => {
119121
120122 await bridger . sendTransaction ( {
121123 to : wallet . address ,
122- value : ethers . utils . parseEther ( "10" ) ,
124+ value : ethers . parseEther ( "10" ) ,
123125 } ) ;
124- expect ( await wallet . getBalance ( ) ) . to . equal ( ethers . utils . parseEther ( "10" ) ) ;
126+ expect ( await ethers . provider . getBalance ( wallet ) ) . to . equal ( ethers . parseEther ( "10" ) ) ;
125127
126- await pnk . transfer ( wallet . address , ONE_THOUSAND_PNK . mul ( 10 ) ) ;
127- expect ( await pnk . balanceOf ( wallet . address ) ) . to . equal ( ONE_THOUSAND_PNK . mul ( 10 ) ) ;
128+ await pnk . transfer ( wallet . address , ONE_THOUSAND_PNK * toBigInt ( 10 ) ) ;
129+ expect ( await pnk . balanceOf ( wallet . address ) ) . to . equal ( ONE_THOUSAND_PNK * toBigInt ( 10 ) ) ;
128130
129- await pnk . connect ( wallet ) . approve ( core . address , ONE_THOUSAND_PNK . mul ( 10 ) , { gasLimit : 300000 } ) ;
131+ await pnk . connect ( wallet ) . approve ( core . target , ONE_THOUSAND_PNK * toBigInt ( 10 ) , { gasLimit : 300000 } ) ;
130132
131133 await stake ( wallet ) ;
132134 }
133135
134136 // Create a dispute
135- const tx = await arbitrable . functions [ "createDispute(string)" ] ( "future of france" , {
137+ const tx = await arbitrable [ "createDispute(string)" ] ( "future of france" , {
136138 value : arbitrationCost ,
137139 } ) ;
140+ if ( tx . blockNumber === null ) return ;
138141 const trace = await network . provider . send ( "debug_traceTransaction" , [ tx . hash ] ) ;
139- const [ disputeId ] = ethers . utils . defaultAbiCoder . decode ( [ "uint" ] , `0x${ trace . returnValue } ` ) ;
142+ const [ disputeId ] = abiCoder . decode ( [ "uint" ] , `0x${ trace . returnValue } ` ) ;
140143 const lastBlock = await ethers . provider . getBlock ( tx . blockNumber - 1 ) ;
141-
144+ if ( lastBlock ?. hash === null || lastBlock ?. hash === undefined ) return ;
142145 // Relayer tx
143- const tx2 = await homeGateway
146+ await homeGateway
144147 . connect ( await ethers . getSigner ( relayer ) )
145- . functions [ "relayCreateDispute((bytes32,uint256,address,uint256,uint256,uint256,string,uint256,bytes))" ] (
148+ [ "relayCreateDispute((bytes32,uint256,address,uint256,uint256,uint256,string,uint256,bytes))" ] (
146149 {
147- foreignBlockHash : lastBlock . hash ,
150+ foreignBlockHash : ethers . toBeHex ( lastBlock ? .hash ) ,
148151 foreignChainID : 31337 ,
149- foreignArbitrable : arbitrable . address ,
152+ foreignArbitrable : arbitrable . target ,
150153 foreignDisputeID : disputeId ,
151- externalDisputeID : ethers . utils . keccak256 ( ethers . utils . toUtf8Bytes ( "future of france" ) ) ,
154+ externalDisputeID : ethers . keccak256 ( ethers . toUtf8Bytes ( "future of france" ) ) ,
152155 templateId : 0 ,
153156 templateUri : "" ,
154157 choices : 2 ,
@@ -180,7 +183,7 @@ describe("Draw Benchmark", async () => {
180183 } ;
181184
182185 const countDraws = async ( blockNumber : number ) => {
183- const draws : Array < DrawEvent > = await core . queryFilter ( core . filters . Draw ( ) , blockNumber , blockNumber ) ;
186+ const draws : Array < any > = await core . queryFilter ( core . filters . Draw ( ) , blockNumber , blockNumber ) ;
184187 return draws . reduce ( ( acc : { [ address : string ] : number } , draw ) => {
185188 const address = draw . args . _address ;
186189 acc [ address ] = acc [ address ] ? acc [ address ] + 1 : 1 ;
@@ -189,18 +192,19 @@ describe("Draw Benchmark", async () => {
189192 } ;
190193
191194 it ( "Stakes in parent court and should draw jurors in parent court" , async ( ) => {
192- const stake = async ( wallet : Wallet ) => {
193- await core . connect ( wallet ) . setStake ( PARENT_COURT , ONE_THOUSAND_PNK . mul ( 5 ) , { gasLimit : 5000000 } ) ;
195+ const stake = async ( wallet : HDNodeWallet ) => {
196+ await core . connect ( wallet ) . setStake ( PARENT_COURT , ONE_THOUSAND_PNK * toBigInt ( 5 ) , { gasLimit : 5000000 } ) ;
194197
195198 expect ( await sortitionModule . getJurorBalance ( wallet . address , 1 ) ) . to . deep . equal ( [
196- ONE_THOUSAND_PNK . mul ( 5 ) , // totalStaked
199+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // totalStaked
197200 0 , // totalLocked
198- ONE_THOUSAND_PNK . mul ( 5 ) , // stakedInCourt
201+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // stakedInCourt
199202 PARENT_COURT , // nbOfCourts
200203 ] ) ;
201204 } ;
202205 let countedDraws : CountedDraws ;
203- const expectFromDraw = async ( drawTx : Promise < ContractTransaction > ) => {
206+ const expectFromDraw = async ( drawTx : Promise < ContractTransactionResponse > ) => {
207+ console . log ( ( await core . getRoundInfo ( 0 , 0 ) ) . drawIterations ) ;
204208 expect ( await core . getRoundInfo ( 0 , 0 ) . then ( ( round ) => round . drawIterations ) ) . to . equal ( 3 ) ;
205209
206210 const tx = await ( await drawTx ) . wait ( ) ;
@@ -211,27 +215,27 @@ describe("Draw Benchmark", async () => {
211215 . withArgs ( anyValue , 0 , 0 , 1 )
212216 . to . emit ( core , "Draw" )
213217 . withArgs ( anyValue , 0 , 0 , 2 ) ;
214-
218+ if ( tx ?. blockNumber === undefined ) return ;
215219 countedDraws = await countDraws ( tx . blockNumber ) ;
216220 for ( const [ address , draws ] of Object . entries ( countedDraws ) ) {
217221 expect ( await sortitionModule . getJurorBalance ( address , PARENT_COURT ) ) . to . deep . equal ( [
218- ONE_THOUSAND_PNK . mul ( 5 ) , // totalStaked
219- parentCourtMinStake . mul ( draws ) , // totalLocked
220- ONE_THOUSAND_PNK . mul ( 5 ) , // stakedInCourt
222+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // totalStaked
223+ parentCourtMinStake * toBigInt ( draws ) , // totalLocked
224+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // stakedInCourt
221225 1 , // nbOfCourts
222226 ] ) ;
223227 expect ( await sortitionModule . getJurorBalance ( address , CHILD_COURT ) ) . to . deep . equal ( [
224- ONE_THOUSAND_PNK . mul ( 5 ) , // totalStaked
225- parentCourtMinStake . mul ( draws ) , // totalLocked
228+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // totalStaked
229+ parentCourtMinStake * toBigInt ( draws ) , // totalLocked
226230 0 , // stakedInCourt
227231 1 , // nbOfCourts
228232 ] ) ;
229233 }
230234 } ;
231235
232- const unstake = async ( wallet : Wallet ) => {
236+ const unstake = async ( wallet : HDNodeWallet ) => {
233237 await core . connect ( wallet ) . setStake ( PARENT_COURT , 0 , { gasLimit : 5000000 } ) ;
234- const locked = parentCourtMinStake . mul ( countedDraws [ wallet . address ] ?? 0 ) ;
238+ const locked = parentCourtMinStake * toBigInt ( countedDraws [ wallet . address ] ?? 0 ) ;
235239 expect (
236240 await sortitionModule . getJurorBalance ( wallet . address , PARENT_COURT ) ,
237241 "Drawn jurors have a locked stake in the parent court"
@@ -257,16 +261,17 @@ describe("Draw Benchmark", async () => {
257261
258262 // Warning: we are skipping this during `hardhat coverage` because it fails, although it passes with `hardhat test`
259263 it ( "Stakes in parent court and should draw nobody in subcourt [ @skip-on-coverage ]" , async ( ) => {
260- const stake = async ( wallet : Wallet ) => {
261- await core . connect ( wallet ) . setStake ( PARENT_COURT , ONE_THOUSAND_PNK . mul ( 5 ) , { gasLimit : 5000000 } ) ;
264+ const stake = async ( wallet : HDNodeWallet ) => {
265+ await core . connect ( wallet ) . setStake ( PARENT_COURT , ONE_THOUSAND_PNK * toBigInt ( 5 ) , { gasLimit : 5000000 } ) ;
262266 } ;
263267
264- const expectFromDraw = async ( drawTx : Promise < ContractTransaction > ) => {
268+ const expectFromDraw = async ( drawTx : Promise < ContractTransactionResponse > ) => {
269+ console . log ( ( await core . getRoundInfo ( 0 , 0 ) ) . drawIterations ) ;
265270 expect ( await core . getRoundInfo ( 0 , 0 ) . then ( ( round ) => round . drawIterations ) ) . to . equal ( 20 ) ;
266271 expect ( await drawTx ) . to . not . emit ( core , "Draw" ) ;
267272 } ;
268273
269- const unstake = async ( wallet : Wallet ) => {
274+ const unstake = async ( wallet : HDNodeWallet ) => {
270275 await core . connect ( wallet ) . setStake ( PARENT_COURT , 0 , { gasLimit : 5000000 } ) ;
271276 expect (
272277 await sortitionModule . getJurorBalance ( wallet . address , PARENT_COURT ) ,
@@ -292,14 +297,16 @@ describe("Draw Benchmark", async () => {
292297 } ) ;
293298
294299 it ( "Stakes in subcourt and should draw jurors in parent court" , async ( ) => {
295- const stake = async ( wallet : Wallet ) => {
296- await core . connect ( wallet ) . setStake ( CHILD_COURT , ONE_THOUSAND_PNK . mul ( 5 ) , { gasLimit : 5000000 } ) ;
300+ const stake = async ( wallet : HDNodeWallet ) => {
301+ await core . connect ( wallet ) . setStake ( CHILD_COURT , ONE_THOUSAND_PNK * toBigInt ( 5 ) , { gasLimit : 5000000 } ) ;
297302 } ;
298303 let countedDraws : CountedDraws ;
299- const expectFromDraw = async ( drawTx : Promise < ContractTransaction > ) => {
304+ const expectFromDraw = async ( drawTx : Promise < ContractTransactionResponse > ) => {
305+ console . log ( ( await core . getRoundInfo ( 0 , 0 ) ) . drawIterations ) ;
300306 expect ( await core . getRoundInfo ( 0 , 0 ) . then ( ( round ) => round . drawIterations ) ) . to . equal ( 3 ) ;
301307
302308 const tx = await ( await drawTx ) . wait ( ) ;
309+ if ( tx === null ) return ;
303310 expect ( tx )
304311 . to . emit ( core , "Draw" )
305312 . withArgs ( anyValue , 0 , 0 , 0 )
@@ -311,23 +318,23 @@ describe("Draw Benchmark", async () => {
311318 countedDraws = await countDraws ( tx . blockNumber ) ;
312319 for ( const [ address , draws ] of Object . entries ( countedDraws ) ) {
313320 expect ( await sortitionModule . getJurorBalance ( address , PARENT_COURT ) ) . to . deep . equal ( [
314- ONE_THOUSAND_PNK . mul ( 5 ) , // totalStaked
315- parentCourtMinStake . mul ( draws ) , // totalLocked
321+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // totalStaked
322+ parentCourtMinStake * toBigInt ( draws ) , // totalLocked
316323 0 , // stakedInCourt
317324 1 , // nbOfCourts
318325 ] ) ;
319326 expect ( await sortitionModule . getJurorBalance ( address , CHILD_COURT ) ) . to . deep . equal ( [
320- ONE_THOUSAND_PNK . mul ( 5 ) , // totalStaked
321- parentCourtMinStake . mul ( draws ) , // totalLocked
322- ONE_THOUSAND_PNK . mul ( 5 ) , // stakedInCourt
327+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // totalStaked
328+ parentCourtMinStake * toBigInt ( draws ) , // totalLocked
329+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // stakedInCourt
323330 1 , // nbOfCourts
324331 ] ) ;
325332 }
326333 } ;
327334
328- const unstake = async ( wallet : Wallet ) => {
335+ const unstake = async ( wallet : HDNodeWallet ) => {
329336 await core . connect ( wallet ) . setStake ( CHILD_COURT , 0 , { gasLimit : 5000000 } ) ;
330- const locked = parentCourtMinStake . mul ( countedDraws [ wallet . address ] ?? 0 ) ;
337+ const locked = parentCourtMinStake * toBigInt ( countedDraws [ wallet . address ] ?? 0 ) ;
331338 expect (
332339 await sortitionModule . getJurorBalance ( wallet . address , PARENT_COURT ) ,
333340 "No locked stake in the parent court"
@@ -352,14 +359,16 @@ describe("Draw Benchmark", async () => {
352359 } ) ;
353360
354361 it ( "Stakes in subcourt and should draw jurors in subcourt" , async ( ) => {
355- const stake = async ( wallet : Wallet ) => {
356- await core . connect ( wallet ) . setStake ( CHILD_COURT , ONE_THOUSAND_PNK . mul ( 5 ) , { gasLimit : 5000000 } ) ;
362+ const stake = async ( wallet : HDNodeWallet ) => {
363+ await core . connect ( wallet ) . setStake ( CHILD_COURT , ONE_THOUSAND_PNK * toBigInt ( 5 ) , { gasLimit : 5000000 } ) ;
357364 } ;
358365 let countedDraws : CountedDraws ;
359- const expectFromDraw = async ( drawTx : Promise < ContractTransaction > ) => {
366+ const expectFromDraw = async ( drawTx : Promise < ContractTransactionResponse > ) => {
367+ console . log ( ( await core . getRoundInfo ( 0 , 0 ) ) . drawIterations ) ;
360368 expect ( await core . getRoundInfo ( 0 , 0 ) . then ( ( round ) => round . drawIterations ) ) . to . equal ( 3 ) ;
361369
362370 const tx = await ( await drawTx ) . wait ( ) ;
371+ if ( tx === null ) return ;
363372 expect ( tx )
364373 . to . emit ( core , "Draw" )
365374 . withArgs ( anyValue , 0 , 0 , 0 )
@@ -371,23 +380,23 @@ describe("Draw Benchmark", async () => {
371380 countedDraws = await countDraws ( tx . blockNumber ) ;
372381 for ( const [ address , draws ] of Object . entries ( countedDraws ) ) {
373382 expect ( await sortitionModule . getJurorBalance ( address , PARENT_COURT ) ) . to . deep . equal ( [
374- ONE_THOUSAND_PNK . mul ( 5 ) , // totalStaked
375- childCourtMinStake . mul ( draws ) , // totalLocked
383+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // totalStaked
384+ childCourtMinStake * toBigInt ( draws ) , // totalLocked
376385 0 , // stakedInCourt
377386 1 , // nbOfCourts
378387 ] ) ;
379388 expect ( await sortitionModule . getJurorBalance ( address , CHILD_COURT ) ) . to . deep . equal ( [
380- ONE_THOUSAND_PNK . mul ( 5 ) , // totalStaked
381- childCourtMinStake . mul ( draws ) , // totalLocked
382- ONE_THOUSAND_PNK . mul ( 5 ) , // stakedInCourt
389+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // totalStaked
390+ childCourtMinStake * toBigInt ( draws ) , // totalLocked
391+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // stakedInCourt
383392 1 , // nbOfCourts
384393 ] ) ;
385394 }
386395 } ;
387396
388- const unstake = async ( wallet : Wallet ) => {
397+ const unstake = async ( wallet : HDNodeWallet ) => {
389398 await core . connect ( wallet ) . setStake ( CHILD_COURT , 0 , { gasLimit : 5000000 } ) ;
390- const locked = childCourtMinStake . mul ( countedDraws [ wallet . address ] ?? 0 ) ;
399+ const locked = childCourtMinStake * toBigInt ( countedDraws [ wallet . address ] ?? 0 ) ;
391400 expect (
392401 await sortitionModule . getJurorBalance ( wallet . address , PARENT_COURT ) ,
393402 "No locked stake in the parent court"
0 commit comments