1
1
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs" ;
2
2
import { deployments , ethers , getNamedAccounts , network } from "hardhat" ;
3
- import { BigNumber , ContractReceipt , ContractTransaction , Wallet } from "ethers" ;
3
+ import { toBigInt , ContractTransactionResponse , HDNodeWallet } from "ethers" ;
4
4
import {
5
5
PNK ,
6
6
KlerosCore ,
7
7
ArbitrableExample ,
8
8
HomeGateway ,
9
9
DisputeKitClassic ,
10
10
SortitionModule ,
11
+ IncrementalNG ,
11
12
} from "../../typechain-types" ;
12
13
import { expect } from "chai" ;
13
- import { DrawEvent } from "../../typechain-types/src/kleros-v1/kleros-liquid-xdai/XKlerosLiquidV2" ;
14
14
import { Courts } from "../../deploy/utils" ;
15
15
16
16
/* eslint-disable no-unused-vars */
17
17
/* eslint-disable no-unused-expressions */ // https://github.com/standard/standard/issues/690#issuecomment-278533482
18
18
19
19
describe ( "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 ) ;
22
22
23
23
const enum Period {
24
24
evidence , // Evidence can be submitted. This is also when drawing has to take place.
@@ -41,18 +41,19 @@ describe("Draw Benchmark", async () => {
41
41
42
42
let deployer ;
43
43
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" ) ;
54
54
const PARENT_COURT = 1 ;
55
55
const CHILD_COURT = 2 ;
56
+ const abiCoder = ethers . AbiCoder . defaultAbiCoder ( ) ;
56
57
57
58
beforeEach ( "Setup" , async ( ) => {
58
59
( { deployer, relayer } = await getNamedAccounts ( ) ) ;
@@ -70,21 +71,22 @@ describe("Draw Benchmark", async () => {
70
71
71
72
parentCourtMinStake = await core . courts ( Courts . GENERAL ) . then ( ( court ) => court . minStake ) ;
72
73
73
- childCourtMinStake = BigNumber . from ( 10 ) . pow ( 20 ) . mul ( 3 ) ; // 300 PNK
74
+ childCourtMinStake = toBigInt ( 10 ) ** toBigInt ( 20 ) * toBigInt ( 3 ) ; // 300 PNK
74
75
75
76
// Make the tests more deterministic with this dummy RNG
76
- rng = await deployments . deploy ( "IncrementalNG" , {
77
+ await deployments . deploy ( "IncrementalNG" , {
77
78
from : deployer ,
78
79
args : [ RANDOM ] ,
79
80
log : true ,
80
81
} ) ;
82
+ rng = ( await ethers . getContract ( "IncrementalNG" ) ) as IncrementalNG ;
81
83
82
- await sortitionModule . changeRandomNumberGenerator ( rng . address , 20 ) ;
84
+ await sortitionModule . changeRandomNumberGenerator ( rng . target , 20 ) ;
83
85
84
86
// 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
86
88
const alpha = 10000 ;
87
- const feeForJuror = BigNumber . from ( 10 ) . pow ( 17 ) ;
89
+ const feeForJuror = toBigInt ( 10 ) ** toBigInt ( 17 ) ;
88
90
await core . createCourt (
89
91
1 ,
90
92
false ,
@@ -93,24 +95,24 @@ describe("Draw Benchmark", async () => {
93
95
feeForJuror ,
94
96
256 ,
95
97
[ 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)
97
99
[ 1 ]
98
100
) ;
99
101
} ) ;
100
102
101
103
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 > ;
104
106
105
107
const draw = async (
106
108
stake : SetStake ,
107
109
createDisputeCourtId : number ,
108
110
expectFromDraw : ExpectFromDraw ,
109
111
unstake : SetStake
110
112
) => {
111
- const arbitrationCost = ONE_TENTH_ETH . mul ( 3 ) ;
113
+ const arbitrationCost = ONE_TENTH_ETH * toBigInt ( 3 ) ;
112
114
const [ bridger ] = await ethers . getSigners ( ) ;
113
- const wallets : Wallet [ ] = [ ] ;
115
+ const wallets : HDNodeWallet [ ] = [ ] ;
114
116
115
117
// Stake some jurors
116
118
for ( let i = 0 ; i < 16 ; i ++ ) {
@@ -119,36 +121,37 @@ describe("Draw Benchmark", async () => {
119
121
120
122
await bridger . sendTransaction ( {
121
123
to : wallet . address ,
122
- value : ethers . utils . parseEther ( "10" ) ,
124
+ value : ethers . parseEther ( "10" ) ,
123
125
} ) ;
124
- expect ( await wallet . getBalance ( ) ) . to . equal ( ethers . utils . parseEther ( "10" ) ) ;
126
+ expect ( await ethers . provider . getBalance ( wallet ) ) . to . equal ( ethers . parseEther ( "10" ) ) ;
125
127
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 ) ) ;
128
130
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 } ) ;
130
132
131
133
await stake ( wallet ) ;
132
134
}
133
135
134
136
// Create a dispute
135
- const tx = await arbitrable . functions [ "createDispute(string)" ] ( "future of france" , {
137
+ const tx = await arbitrable [ "createDispute(string)" ] ( "future of france" , {
136
138
value : arbitrationCost ,
137
139
} ) ;
140
+ if ( tx . blockNumber === null ) return ;
138
141
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 } ` ) ;
140
143
const lastBlock = await ethers . provider . getBlock ( tx . blockNumber - 1 ) ;
141
-
144
+ if ( lastBlock ?. hash === null || lastBlock ?. hash === undefined ) return ;
142
145
// Relayer tx
143
- const tx2 = await homeGateway
146
+ await homeGateway
144
147
. 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))" ] (
146
149
{
147
- foreignBlockHash : lastBlock . hash ,
150
+ foreignBlockHash : ethers . toBeHex ( lastBlock ? .hash ) ,
148
151
foreignChainID : 31337 ,
149
- foreignArbitrable : arbitrable . address ,
152
+ foreignArbitrable : arbitrable . target ,
150
153
foreignDisputeID : disputeId ,
151
- externalDisputeID : ethers . utils . keccak256 ( ethers . utils . toUtf8Bytes ( "future of france" ) ) ,
154
+ externalDisputeID : ethers . keccak256 ( ethers . toUtf8Bytes ( "future of france" ) ) ,
152
155
templateId : 0 ,
153
156
templateUri : "" ,
154
157
choices : 2 ,
@@ -180,7 +183,7 @@ describe("Draw Benchmark", async () => {
180
183
} ;
181
184
182
185
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 ) ;
184
187
return draws . reduce ( ( acc : { [ address : string ] : number } , draw ) => {
185
188
const address = draw . args . _address ;
186
189
acc [ address ] = acc [ address ] ? acc [ address ] + 1 : 1 ;
@@ -189,18 +192,19 @@ describe("Draw Benchmark", async () => {
189
192
} ;
190
193
191
194
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 } ) ;
194
197
195
198
expect ( await sortitionModule . getJurorBalance ( wallet . address , 1 ) ) . to . deep . equal ( [
196
- ONE_THOUSAND_PNK . mul ( 5 ) , // totalStaked
199
+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // totalStaked
197
200
0 , // totalLocked
198
- ONE_THOUSAND_PNK . mul ( 5 ) , // stakedInCourt
201
+ ONE_THOUSAND_PNK * toBigInt ( 5 ) , // stakedInCourt
199
202
PARENT_COURT , // nbOfCourts
200
203
] ) ;
201
204
} ;
202
205
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 ) ;
204
208
expect ( await core . getRoundInfo ( 0 , 0 ) . then ( ( round ) => round . drawIterations ) ) . to . equal ( 3 ) ;
205
209
206
210
const tx = await ( await drawTx ) . wait ( ) ;
@@ -211,27 +215,27 @@ describe("Draw Benchmark", async () => {
211
215
. withArgs ( anyValue , 0 , 0 , 1 )
212
216
. to . emit ( core , "Draw" )
213
217
. withArgs ( anyValue , 0 , 0 , 2 ) ;
214
-
218
+ if ( tx ?. blockNumber === undefined ) return ;
215
219
countedDraws = await countDraws ( tx . blockNumber ) ;
216
220
for ( const [ address , draws ] of Object . entries ( countedDraws ) ) {
217
221
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
221
225
1 , // nbOfCourts
222
226
] ) ;
223
227
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
226
230
0 , // stakedInCourt
227
231
1 , // nbOfCourts
228
232
] ) ;
229
233
}
230
234
} ;
231
235
232
- const unstake = async ( wallet : Wallet ) => {
236
+ const unstake = async ( wallet : HDNodeWallet ) => {
233
237
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 ) ;
235
239
expect (
236
240
await sortitionModule . getJurorBalance ( wallet . address , PARENT_COURT ) ,
237
241
"Drawn jurors have a locked stake in the parent court"
@@ -257,16 +261,17 @@ describe("Draw Benchmark", async () => {
257
261
258
262
// Warning: we are skipping this during `hardhat coverage` because it fails, although it passes with `hardhat test`
259
263
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 } ) ;
262
266
} ;
263
267
264
- const expectFromDraw = async ( drawTx : Promise < ContractTransaction > ) => {
268
+ const expectFromDraw = async ( drawTx : Promise < ContractTransactionResponse > ) => {
269
+ console . log ( ( await core . getRoundInfo ( 0 , 0 ) ) . drawIterations ) ;
265
270
expect ( await core . getRoundInfo ( 0 , 0 ) . then ( ( round ) => round . drawIterations ) ) . to . equal ( 20 ) ;
266
271
expect ( await drawTx ) . to . not . emit ( core , "Draw" ) ;
267
272
} ;
268
273
269
- const unstake = async ( wallet : Wallet ) => {
274
+ const unstake = async ( wallet : HDNodeWallet ) => {
270
275
await core . connect ( wallet ) . setStake ( PARENT_COURT , 0 , { gasLimit : 5000000 } ) ;
271
276
expect (
272
277
await sortitionModule . getJurorBalance ( wallet . address , PARENT_COURT ) ,
@@ -292,14 +297,16 @@ describe("Draw Benchmark", async () => {
292
297
} ) ;
293
298
294
299
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 } ) ;
297
302
} ;
298
303
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 ) ;
300
306
expect ( await core . getRoundInfo ( 0 , 0 ) . then ( ( round ) => round . drawIterations ) ) . to . equal ( 3 ) ;
301
307
302
308
const tx = await ( await drawTx ) . wait ( ) ;
309
+ if ( tx === null ) return ;
303
310
expect ( tx )
304
311
. to . emit ( core , "Draw" )
305
312
. withArgs ( anyValue , 0 , 0 , 0 )
@@ -311,23 +318,23 @@ describe("Draw Benchmark", async () => {
311
318
countedDraws = await countDraws ( tx . blockNumber ) ;
312
319
for ( const [ address , draws ] of Object . entries ( countedDraws ) ) {
313
320
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
316
323
0 , // stakedInCourt
317
324
1 , // nbOfCourts
318
325
] ) ;
319
326
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
323
330
1 , // nbOfCourts
324
331
] ) ;
325
332
}
326
333
} ;
327
334
328
- const unstake = async ( wallet : Wallet ) => {
335
+ const unstake = async ( wallet : HDNodeWallet ) => {
329
336
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 ) ;
331
338
expect (
332
339
await sortitionModule . getJurorBalance ( wallet . address , PARENT_COURT ) ,
333
340
"No locked stake in the parent court"
@@ -352,14 +359,16 @@ describe("Draw Benchmark", async () => {
352
359
} ) ;
353
360
354
361
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 } ) ;
357
364
} ;
358
365
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 ) ;
360
368
expect ( await core . getRoundInfo ( 0 , 0 ) . then ( ( round ) => round . drawIterations ) ) . to . equal ( 3 ) ;
361
369
362
370
const tx = await ( await drawTx ) . wait ( ) ;
371
+ if ( tx === null ) return ;
363
372
expect ( tx )
364
373
. to . emit ( core , "Draw" )
365
374
. withArgs ( anyValue , 0 , 0 , 0 )
@@ -371,23 +380,23 @@ describe("Draw Benchmark", async () => {
371
380
countedDraws = await countDraws ( tx . blockNumber ) ;
372
381
for ( const [ address , draws ] of Object . entries ( countedDraws ) ) {
373
382
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
376
385
0 , // stakedInCourt
377
386
1 , // nbOfCourts
378
387
] ) ;
379
388
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
383
392
1 , // nbOfCourts
384
393
] ) ;
385
394
}
386
395
} ;
387
396
388
- const unstake = async ( wallet : Wallet ) => {
397
+ const unstake = async ( wallet : HDNodeWallet ) => {
389
398
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 ) ;
391
400
expect (
392
401
await sortitionModule . getJurorBalance ( wallet . address , PARENT_COURT ) ,
393
402
"No locked stake in the parent court"
0 commit comments