1
1
const BN = web3 . utils . BN
2
2
const { expect } = require ( 'chai' )
3
- const { constants, expectEvent, expectRevert } = require ( '@openzeppelin/test-helpers' )
3
+ const { constants, expectEvent, expectRevert, time } = require ( '@openzeppelin/test-helpers' )
4
4
const { ZERO_ADDRESS } = constants
5
5
6
6
// helpers
@@ -12,9 +12,23 @@ const { defaults } = require('./lib/testHelpers')
12
12
const MAX_PPM = 1000000
13
13
const NON_EXISTING_DISPUTE_ID = '0x0'
14
14
15
+ function toGRT ( value ) {
16
+ return new BN ( web3 . utils . toWei ( value ) )
17
+ }
18
+
15
19
contract (
16
20
'Disputes' ,
17
21
( [ me , other , governor , arbitrator , indexer , fisherman , otherIndexer , channelProxy ] ) => {
22
+ before ( async function ( ) {
23
+ // Helpers
24
+ this . advanceToNextEpoch = async ( ) => {
25
+ const currentBlock = await time . latestBlock ( )
26
+ const epochLength = await this . epochManager . epochLength ( )
27
+ const nextEpochBlock = currentBlock . add ( epochLength )
28
+ await time . advanceBlockTo ( nextEpochBlock )
29
+ }
30
+ } )
31
+
18
32
beforeEach ( async function ( ) {
19
33
// Channel keys for account #4
20
34
this . indexerChannelPrivKey =
@@ -200,8 +214,8 @@ contract(
200
214
describe ( 'dispute lifecycle' , function ( ) {
201
215
beforeEach ( async function ( ) {
202
216
// Give some funds to the fisherman
203
- this . fishermanTokens = web3 . utils . toWei ( new BN ( '100000' ) )
204
- this . fishermanDeposit = web3 . utils . toWei ( new BN ( '1000' ) )
217
+ this . fishermanTokens = toGRT ( '100000' )
218
+ this . fishermanDeposit = toGRT ( '1000' )
205
219
await this . grt . mint ( fisherman , this . fishermanTokens , {
206
220
from : governor ,
207
221
} )
@@ -223,26 +237,77 @@ contract(
223
237
)
224
238
} )
225
239
226
- it ( 'reject create a dispute' , async function ( ) {
240
+ it ( 'reject create a dispute if attestation does not refer to valid indexer ' , async function ( ) {
227
241
// Create dispute
228
242
await expectRevert (
229
243
this . disputeManager . createDispute ( this . dispute . attestation , this . fishermanDeposit , {
230
244
from : fisherman ,
231
245
} ) ,
232
- 'Indexer cannot be found with the attestation' ,
246
+ 'Indexer cannot be found for the attestation' ,
233
247
)
234
248
} )
235
249
236
- context ( '> when stake does exist' , function ( ) {
250
+ it ( 'reject create a dispute if indexer has no stake' , async function ( ) {
251
+ // This tests reproduce the case when someones present a dispute after
252
+ // an indexer removed his stake completely and find nothing to slash
253
+
254
+ const indexerTokens = toGRT ( '100000' )
255
+ const indexerAllocatedTokens = toGRT ( '10000' )
256
+ const indexerSettledTokens = toGRT ( '10' )
257
+
258
+ // Give some funds to the indexer
259
+ await this . grt . mint ( indexer , indexerTokens , {
260
+ from : governor ,
261
+ } )
262
+ await this . grt . approve ( this . staking . address , indexerTokens , {
263
+ from : indexer ,
264
+ } )
265
+
266
+ // Give some funds to the channel
267
+ await this . grt . mint ( channelProxy , indexerSettledTokens , {
268
+ from : governor ,
269
+ } )
270
+ await this . grt . approve ( this . staking . address , indexerSettledTokens , {
271
+ from : channelProxy ,
272
+ } )
273
+
274
+ // Set the thawing period to zero to make the test easier
275
+ await this . staking . setThawingPeriod ( new BN ( '0' ) , { from : governor } )
276
+
277
+ // Indexer stake funds, allocate, settle, unstake and withdraw the stake fully
278
+ await this . staking . stake ( indexerTokens , { from : indexer } )
279
+ await this . staking . allocate (
280
+ this . dispute . receipt . subgraphID ,
281
+ indexerAllocatedTokens ,
282
+ this . indexerChannelPubKey ,
283
+ channelProxy ,
284
+ new BN ( '0' ) ,
285
+ { from : indexer } ,
286
+ )
287
+ await this . advanceToNextEpoch ( ) // wait the required one epoch to settle
288
+ await this . staking . settle ( indexerSettledTokens , { from : channelProxy } )
289
+ await this . staking . unstake ( indexerTokens , { from : indexer } )
290
+ await this . staking . withdraw ( { from : indexer } ) // no thawing period so we are good
291
+
292
+ // Create dispute
293
+ await expectRevert (
294
+ this . disputeManager . createDispute ( this . dispute . attestation , this . fishermanDeposit , {
295
+ from : fisherman ,
296
+ } ) ,
297
+ 'Dispute has no stake by the indexer' ,
298
+ )
299
+ } )
300
+
301
+ context ( '> when indexer has staked' , function ( ) {
237
302
beforeEach ( async function ( ) {
238
303
// Dispute manager is allowed to slash
239
304
await this . staking . setSlasher ( this . disputeManager . address , true , {
240
305
from : governor ,
241
306
} )
242
307
243
308
// Stake
244
- this . indexerTokens = web3 . utils . toWei ( new BN ( '100000' ) )
245
- this . indexerAllocatedTokens = web3 . utils . toWei ( new BN ( '10000' ) )
309
+ this . indexerTokens = toGRT ( '100000' )
310
+ this . indexerAllocatedTokens = toGRT ( '10000' )
246
311
const indexerList = [
247
312
[ indexer , this . indexerChannelPubKey ] ,
248
313
[ otherIndexer , this . otherIndexerChannelPubKey ] ,
@@ -319,6 +384,39 @@ contract(
319
384
} )
320
385
} )
321
386
387
+ describe ( 'accept a dispute' , function ( ) {
388
+ it ( 'reject to accept a non-existing dispute' , async function ( ) {
389
+ await expectRevert (
390
+ this . disputeManager . acceptDispute ( NON_EXISTING_DISPUTE_ID , {
391
+ from : arbitrator ,
392
+ } ) ,
393
+ 'Dispute does not exist' ,
394
+ )
395
+ } )
396
+ } )
397
+
398
+ describe ( 'reject a dispute' , function ( ) {
399
+ it ( 'reject to reject a non-existing dispute' , async function ( ) {
400
+ await expectRevert (
401
+ this . disputeManager . rejectDispute ( NON_EXISTING_DISPUTE_ID , {
402
+ from : arbitrator ,
403
+ } ) ,
404
+ 'Dispute does not exist' ,
405
+ )
406
+ } )
407
+ } )
408
+
409
+ describe ( 'draw a dispute' , function ( ) {
410
+ it ( 'reject to draw a non-existing dispute' , async function ( ) {
411
+ await expectRevert (
412
+ this . disputeManager . drawDispute ( NON_EXISTING_DISPUTE_ID , {
413
+ from : arbitrator ,
414
+ } ) ,
415
+ 'Dispute does not exist' ,
416
+ )
417
+ } )
418
+ } )
419
+
322
420
context ( '> when dispute is created' , function ( ) {
323
421
beforeEach ( async function ( ) {
324
422
// Create dispute
@@ -330,7 +428,7 @@ contract(
330
428
} )
331
429
332
430
describe ( 'create a dispute' , function ( ) {
333
- it ( 'should create dispute if receipt is equal but for different indexer' , async function ( ) {
431
+ it ( 'should create dispute if receipt is equal but for other indexer' , async function ( ) {
334
432
// Create dispute (same receipt but different indexer)
335
433
const newDispute = await attestation . createDispute (
336
434
this . dispute . receipt ,
@@ -366,15 +464,6 @@ contract(
366
464
} )
367
465
368
466
describe ( 'accept a dispute' , function ( ) {
369
- it ( 'reject to accept a non-existing dispute' , async function ( ) {
370
- await expectRevert (
371
- this . disputeManager . acceptDispute ( NON_EXISTING_DISPUTE_ID , {
372
- from : arbitrator ,
373
- } ) ,
374
- 'Dispute does not exist' ,
375
- )
376
- } )
377
-
378
467
it ( 'reject to accept a dispute if not the arbitrator' , async function ( ) {
379
468
await expectRevert (
380
469
this . disputeManager . acceptDispute ( this . dispute . id , {
@@ -448,15 +537,6 @@ contract(
448
537
} )
449
538
450
539
describe ( 'reject a dispute' , async function ( ) {
451
- it ( 'reject to reject a non-existing dispute' , async function ( ) {
452
- await expectRevert (
453
- this . disputeManager . rejectDispute ( NON_EXISTING_DISPUTE_ID , {
454
- from : arbitrator ,
455
- } ) ,
456
- 'Dispute does not exist' ,
457
- )
458
- } )
459
-
460
540
it ( 'reject to reject a dispute if not the arbitrator' , async function ( ) {
461
541
await expectRevert (
462
542
this . disputeManager . rejectDispute ( this . dispute . id , {
@@ -481,7 +561,7 @@ contract(
481
561
482
562
// Burn fisherman deposit
483
563
const totalSupplyAfter = await this . grt . totalSupply ( )
484
- const burnedTokens = web3 . utils . toBN ( this . fishermanDeposit )
564
+ const burnedTokens = new BN ( this . fishermanDeposit )
485
565
expect ( totalSupplyAfter ) . to . be . bignumber . eq ( totalSupplyBefore . sub ( burnedTokens ) )
486
566
487
567
// Event emitted
@@ -496,15 +576,6 @@ contract(
496
576
} )
497
577
498
578
describe ( 'draw a dispute' , async function ( ) {
499
- it ( 'reject to draw a non-existing dispute' , async function ( ) {
500
- await expectRevert (
501
- this . disputeManager . drawDispute ( NON_EXISTING_DISPUTE_ID , {
502
- from : arbitrator ,
503
- } ) ,
504
- 'Dispute does not exist' ,
505
- )
506
- } )
507
-
508
579
it ( 'reject to draw a dispute if not the arbitrator' , async function ( ) {
509
580
await expectRevert (
510
581
this . disputeManager . drawDispute ( this . dispute . id , {
0 commit comments