-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
exitHandler.js
656 lines (494 loc) · 26 KB
/
exitHandler.js
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/**
* Copyright (c) 2018-present, Leap DAO (leapdao.org)
*
* This source code is licensed under the Mozilla Public License, version 2,
* found in the LICENSE file in the root directory of this source tree.
*/
import { Tx, Input, Output, Outpoint } from 'leap-core';
import { EVMRevert, submitNewPeriodWithTx } from './helpers';
const ethUtil = require('ethereumjs-util');
const time = require('./helpers/time');
require('./helpers/setup');
const { BN } = web3.utils;
const AdminableProxy = artifacts.require('AdminableProxy');
const Bridge = artifacts.require('Bridge');
const ExitHandler = artifacts.require('ExitHandler');
const PriorityQueue = artifacts.require('PriorityQueue');
const SimpleToken = artifacts.require('SimpleToken');
const SpaceDustNFT = artifacts.require('SpaceDustNFT');
const ERC1949 = artifacts.require('ERC1949');
const SpendingCondition = artifacts.require('SpendingCondition');
const aSecond = async () => new Promise(resolve => setTimeout(resolve, 1000));
const exitUtxoId = (exitEvent) => {
const { txHash, outIndex } = exitEvent.logs[0].args;
return new Outpoint(txHash, outIndex.toNumber()).getUtxoId();
};
contract('ExitHandler', (accounts) => {
const alice = accounts[0];
// This is from ganache GUI version
const alicePriv = '0x278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25383f';
const bob = accounts[1];
const bobPriv = '0x7bc8feb5e1ce2927480de19d8bc1dc6874678c016ae53a2eec6a6e9df717bfac';
const charlie = accounts[2];
describe('Test', () => {
// contracts
let bridge;
let exitHandler;
let proxy;
// ERC20 token stuff
let nativeToken;
const nativeTokenColor = 0;
let depositTx;
let transferTx;
const depositAmount = new BN(100);
let depositId = 1;
// ERC721 token stuff
let nftToken;
let nftColor;
let nftTokenId;
let nftTokenIdStr;
let nftDepositTx;
let nftTransferTx;
const parentBlockInterval = 0;
const exitDuration = 5;
const exitStake = '100000000000000000'; // 0.1 ETH
const seedTxs = async () => {
await nativeToken.approve(exitHandler.address, 1000);
await exitHandler.deposit(alice, depositAmount, nativeTokenColor).should.be.fulfilled;
depositTx = Tx.deposit(depositId, depositAmount.toNumber(), alice);
transferTx = Tx.transfer(
[new Input(new Outpoint(depositTx.hash(), 0))],
[new Output(50, bob), new Output(40, alice), new Output(10, alice)]
).sign([alicePriv]);
};
const seedNftTxs = async () => {
nftToken = await SpaceDustNFT.new();
let receipt = await nftToken.mint(alice, 10, true, 2);
nftTokenId = receipt.logs[0].args.tokenId;
nftTokenIdStr = nftTokenId.toString(10);
const data = await exitHandler.contract.methods.registerToken(nftToken.address, 1).encodeABI();
receipt = await proxy.applyProposal(data, {from: accounts[2]}).should.be.fulfilled;
nftColor = Buffer.from(receipt.receipt.rawLogs[0].data.replace('0x', ''), 'hex').readUInt32BE(28);
// deposit
await nftToken.approve(exitHandler.address, nftTokenId);
receipt = await exitHandler.deposit(alice, nftTokenId, nftColor, { from: alice }).should.be.fulfilled;
const nftDepositId = receipt.logs[0].args.depositId.toNumber();
nftDepositTx = Tx.deposit(nftDepositId, nftTokenIdStr, alice, nftColor);
// transfer to bob
nftTransferTx = Tx.transfer(
[new Input(new Outpoint(nftDepositTx.hash(), 0))],
[new Output(nftTokenIdStr, bob, nftColor)]
).sign([alicePriv]);
}
const submitNewPeriod = txs => submitNewPeriodWithTx(txs, bridge, { from: bob });
beforeEach(async () => {
const pqLib = await PriorityQueue.new();
ExitHandler.link('PriorityQueue', pqLib.address);
nativeToken = await SimpleToken.new();
const bridgeCont = await Bridge.new();
let data = await bridgeCont.contract.methods.initialize(parentBlockInterval).encodeABI();
proxy = await AdminableProxy.new(bridgeCont.address, data, {from: accounts[2]});
bridge = await Bridge.at(proxy.address);
data = await bridge.contract.methods.setOperator(bob).encodeABI();
await proxy.applyProposal(data, {from: accounts[2]});
const vaultCont = await ExitHandler.new();
data = await vaultCont.contract.methods.initializeWithExit(bridge.address, exitDuration, exitStake).encodeABI();
proxy = await AdminableProxy.new(vaultCont.address, data, {from: accounts[2]});
exitHandler = await ExitHandler.at(proxy.address);
// register first token
data = await exitHandler.contract.methods.registerToken(nativeToken.address, 0).encodeABI();
await proxy.applyProposal(data, {from: accounts[2]});
// At this point alice is the owner of bridge and depositHandler and has 10000 tokens
// Bob is the bridge operator and exitHandler and has 0 tokens
// Note: all txs in these tests originate from alice unless otherwise specified
await seedTxs();
await seedNftTxs();
});
describe('Start exit', async () => {
it('Should allow to exit valid utxo', async () => {
const period1 = await submitNewPeriod([depositTx, transferTx]);
const youngerTx = Tx.transfer(
[new Input(new Outpoint(transferTx.hash(), 1))],
[new Output(30, alice), new Output(10, alice)]
).sign([alicePriv]);
const period2 = await submitNewPeriod([youngerTx]);
const evenYoungerTx = Tx.transfer(
[
new Input(new Outpoint(transferTx.hash(), 2)),
new Input(new Outpoint(youngerTx.hash(), 0))
],
[new Output(20, bob), new Output(20, alice)]
).sign([alicePriv, alicePriv]);
const period3 = await submitNewPeriod([evenYoungerTx]);
// exit with younger input 0
let transferProof = period2.proof(youngerTx);
let inputProof = period1.proof(transferTx);
let inputIndex = 0;
let outputIndex = 1;
await exitHandler.startExit(inputProof, transferProof, outputIndex, inputIndex, {value: exitStake});
// exit with younger input 1
transferProof = period3.proof(evenYoungerTx);
inputProof = period2.proof(youngerTx);
inputIndex = 1;
outputIndex = 1;
await exitHandler.startExit(inputProof, transferProof, outputIndex, inputIndex, {value: exitStake});
const aliceBalanceBefore = await nativeToken.balanceOf(alice);
const exitTime = (await time.latest()) + (2 * time.duration.seconds(exitDuration));
await time.increaseTo(exitTime);
await exitHandler.finalizeTopExit(nativeTokenColor);
const aliceBalanceAfter = await nativeToken.balanceOf(alice);
assert(aliceBalanceBefore.add(new BN(30)).eq(aliceBalanceAfter));
});
it('Should not allow to exit utxo from deleted period', async () => {
const period = await submitNewPeriod([depositTx, transferTx]);
const transferProof = period.proof(transferTx);
const outputIndex = 1;
const inputProof = period.proof(depositTx); // transferTx spends depositTx
const inputIndex = 0;
await exitHandler.startExit(inputProof, transferProof, outputIndex, inputIndex, {value: exitStake});
// delete period
await bridge.deletePeriod(transferProof[0], {from: accounts[1]});
const aliceBalanceBefore = await nativeToken.balanceOf(alice);
const govBalanceBefore = await web3.eth.getBalance(accounts[2]);
const exitTime = (await time.latest()) + (2 * time.duration.seconds(exitDuration));
await time.increaseTo(exitTime);
await exitHandler.finalizeTopExit(nativeTokenColor);
const govBalanceAfter = await web3.eth.getBalance(accounts[2]);
assert(new BN(govBalanceAfter).gt(new BN(govBalanceBefore)));
const aliceBalanceAfter = await nativeToken.balanceOf(alice);
assert(aliceBalanceBefore.eq(aliceBalanceAfter));
});
it('Should allow to exit valid utxo to spending condition', async () => {
const condition = await SpendingCondition.new();
const codeBuf = Buffer.from(condition.constructor._json.deployedBytecode.replace('0x', ''), 'hex'); // eslint-disable-line no-underscore-dangle
const codeHash = ethUtil.ripemd160(ethUtil.keccak256(codeBuf));
const transferToSpTx = Tx.transfer(
[new Input(new Outpoint(depositTx.hash(), 0))],
[new Output(50, bob), new Output(50, `0x${codeHash.toString('hex')}`)]
).sign([alicePriv]);
const period = await submitNewPeriod([depositTx, transferToSpTx]);
const transferProof = period.proof(transferToSpTx);
const outputIndex = 1;
const inputProof = period.proof(depositTx); // transferTx spends depositTx
const inputIndex = 0;
await condition.startExit(inputProof, transferProof, outputIndex, inputIndex, exitHandler.address, {value: exitStake});
const aliceBalanceBefore = await nativeToken.balanceOf(condition.address);
const exitTime = (await time.latest()) + (2 * time.duration.seconds(exitDuration));
await time.increaseTo(exitTime);
await exitHandler.finalizeTopExit(nativeTokenColor);
const aliceBalanceAfter = await nativeToken.balanceOf(condition.address);
assert(aliceBalanceBefore.add(new BN(50)).eq(aliceBalanceAfter));
});
it('Finalise exit should revert if queue is empty', async () => {
await exitHandler.finalizeTopExit(nativeTokenColor).should.be.rejectedWith(EVMRevert);
});
it('Should allow to exit deposit utxo', async () => {
const period = await submitNewPeriod([depositTx]);
const proof = period.proof(depositTx);
const outputIndex = 0;
const inputIndex = 0;
await exitHandler.startExit([], proof, outputIndex, inputIndex, {value: exitStake});
const aliceBalanceBefore = await nativeToken.balanceOf(alice);
const exitTime = (await time.latest()) + (2 * time.duration.seconds(exitDuration));
await time.increaseTo(exitTime);
await exitHandler.finalizeTopExit(nativeTokenColor);
const aliceBalanceAfter = await nativeToken.balanceOf(alice);
assert(aliceBalanceBefore.add(depositAmount).eq(aliceBalanceAfter));
});
it('Should allow to exit deposit', async () => {
await exitHandler.startDepositExit(1, {value: exitStake});
const aliceBalanceBefore = await nativeToken.balanceOf(alice);
const exitTime = (await time.latest()) + (2 * time.duration.seconds(exitDuration));
await time.increaseTo(exitTime);
await exitHandler.finalizeTopExit(nativeTokenColor);
const aliceBalanceAfter = await nativeToken.balanceOf(alice);
assert(aliceBalanceBefore.add(depositAmount).eq(aliceBalanceAfter));
});
it('Should allow to challenge exiting deposit', async () => {
await exitHandler.startDepositExit(1, {value: exitStake});
const period = await submitNewPeriod([depositTx]);
const one = '0x0000000000000000000000000000000000000000000000000000000000000001';
const proof = period.proof(depositTx);
// challenge exit and make sure exit is removed
let exit = await exitHandler.exits(one);
assert.equal(exit[2], alice);
await exitHandler.challengeExit([], proof, 0, 0, alice);
exit = await exitHandler.exits(one);
// check that exit was deleted
assert.equal(exit[2], '0x0000000000000000000000000000000000000000');
});
it('Should allow to exit NFT utxo', async () => {
const period = await submitNewPeriod([nftDepositTx, nftTransferTx]);
const proof = period.proof(nftTransferTx);
const inputProof = period.proof(nftDepositTx);
// withdraw output
assert.equal(await nftToken.ownerOf(nftTokenId), exitHandler.address);
const event = await exitHandler.startExit(inputProof, proof, 0, 0, { from: bob, value: exitStake});
await exitHandler.finalizeTopExit(nftColor);
assert.equal(await nftToken.ownerOf(nftTokenId), bob);
// exit was markeed as finalized
assert.equal((await exitHandler.exits(exitUtxoId(event)))[3], true);
});
it('Should allow to finalize delayed breeder', async () => {
// deposit queen
const breedToken = await ERC1949.new("yo", "YO");
await breedToken.mintDelegate(exitHandler.address);
// register token
const data = await exitHandler.contract.methods.registerToken(breedToken.address, 2).encodeABI();
const receipt = await proxy.applyProposal(data, {from: accounts[2]}).should.be.fulfilled;
const nstColor = Buffer.from(receipt.receipt.rawLogs[0].data.replace('0x', ''), 'hex').readUInt32BE(28);
// deposit
const workerId = 1337;
const workerData = '0x0101010101010101010101010101010101010101010101010101010101010101';
const nstDepositTx = Tx.deposit(13, workerId, alice, nstColor, workerData);
// transfer
const nstTransferTx = Tx.transfer(
[new Input(new Outpoint(nstDepositTx.hash(), 0))],
[new Output(workerId, bob, nstColor, workerData)]
).sign([alicePriv]);
const period = await submitNewPeriod([nstDepositTx, nstTransferTx]);
const proof = period.proof(nstTransferTx);
const inputProof = period.proof(nstDepositTx);
// withdraw output
const event = await exitHandler.startExit(inputProof, proof, 0, 0, { from: bob, value: exitStake});
await exitHandler.finalizeTopExit(nstColor);
assert.equal(await breedToken.ownerOf(workerId), bob);
assert.equal((await exitHandler.exits(exitUtxoId(event)))[3], true);
assert.equal(await breedToken.readData(workerId), workerData);
});
it('Should allow to exit only for utxo owner', async () => {
const period = await submitNewPeriod([depositTx, transferTx]);
const transferProof = period.proof(transferTx);
const outputIndex = 1;
const inputProof = period.proof(depositTx);
await exitHandler.startExit(inputProof, transferProof, outputIndex, 0, { from: bob, value: exitStake }).should.be.rejectedWith(EVMRevert);
});
it('Should allow to challenge exit', async () => {
// utxo that will have spend exit utxo
const spendTx = Tx.transfer(
[new Input(new Outpoint(transferTx.hash(), 0))],
[new Output(50, charlie)]
).sign([bobPriv]);
const period = await submitNewPeriod([ depositTx, transferTx, spendTx]);
const transferProof = period.proof(transferTx);
const spendProof = period.proof(spendTx);
const inputProof = period.proof(depositTx);
// withdraw output
const event = await exitHandler.startExit(inputProof, transferProof, 0, 0, { from: bob, value: exitStake });
const utxoId = exitUtxoId(event);
assert.equal(utxoId, spendTx.inputs[0].prevout.getUtxoId());
// challenge exit and make sure exit is removed
assert.equal((await exitHandler.exits(utxoId))[2], bob);
// should prevent simple front-runs
await exitHandler.challengeExit(
spendProof, transferProof, 0, 0, alice, { from: bob }
).should.be.rejectedWith(EVMRevert);
await exitHandler.challengeExit(spendProof, transferProof, 0, 0, alice);
assert.equal((await exitHandler.exits(utxoId))[2], '0x0000000000000000000000000000000000000000');
assert.equal((await exitHandler.tokens(0))[1], 1);
const bal1 = await nativeToken.balanceOf(bob);
const exitTime = (await time.latest()) + (2 * time.duration.seconds(exitDuration));
await time.increaseTo(exitTime);
await exitHandler.finalizeTopExit(0);
const bal2 = await nativeToken.balanceOf(bob);
// check transfer didn't happen
assert.equal(bal1.toNumber(), bal2.toNumber());
// check exit was evicted from PriorityQueue
assert.equal((await exitHandler.tokens(0))[1], 0);
});
it('should not allow to challenge exit from deleted period', async () => {
// utxo that will have spend exit utxo
const spendTx = Tx.transfer(
[new Input(new Outpoint(transferTx.hash(), 0))],
[new Output(50, charlie)]
).sign([bobPriv]);
const period = await submitNewPeriod([ depositTx, transferTx, spendTx]);
const transferProof = period.proof(transferTx);
const spendProof = period.proof(spendTx);
const inputProof = period.proof(depositTx);
// withdraw output
const event = await exitHandler.startExit(inputProof, transferProof, 0, 0, { from: bob, value: exitStake });
const utxoId = exitUtxoId(event);
assert.equal(utxoId, spendTx.inputs[0].prevout.getUtxoId());
// challenge exit and make sure exit is removed
assert.equal((await exitHandler.exits(utxoId))[2], bob);
// delete _proof period
await bridge.deletePeriod(spendProof[0], {from: accounts[1]});
// should prevent challenge exit from a deleted period
await exitHandler.challengeExit(spendProof, transferProof, 0, 0, alice).should.be.rejectedWith(EVMRevert);
});
it('Should allow to challenge exit by spending condition', async () => {
const spendTx = Tx.spendCond(
[new Input({
prevout: new Outpoint(transferTx.hash(), 0),
script: '0x123456',
})], [new Output(50, charlie)],
);
spendTx.inputs[0].setMsgData('0xabcdef');
const period = await submitNewPeriod([ depositTx, transferTx, spendTx]);
const transferProof = period.proof(transferTx);
const spendProof = period.proof(spendTx);
const inputProof = period.proof(depositTx);
// withdraw output
const event = await exitHandler.startExit(inputProof, transferProof, 0, 0, { from: bob, value: exitStake });
const utxoId = exitUtxoId(event);
assert.equal(utxoId, spendTx.inputs[0].prevout.getUtxoId());
// challenge exit and make sure exit is removed
assert.equal((await exitHandler.exits(utxoId))[2], bob);
await exitHandler.challengeExit(spendProof, transferProof, 0, 0, alice);
assert.equal((await exitHandler.exits(utxoId))[2], '0x0000000000000000000000000000000000000000');
assert.equal((await exitHandler.tokens(0))[1], 1);
const bal1 = await nativeToken.balanceOf(bob);
const exitTime = (await time.latest()) + (2 * time.duration.seconds(exitDuration));
await time.increaseTo(exitTime);
await exitHandler.finalizeTopExit(0);
const bal2 = await nativeToken.balanceOf(bob);
// check transfer didn't happen
assert.equal(bal1.toNumber(), bal2.toNumber());
// check exit was evicted from PriorityQueue
assert.equal((await exitHandler.tokens(0))[1], 0);
});
it('Should allow to challenge youngest input for exit', async () => {
// period1: depositTx, anotherDepositTx
// period2: tranferTx spending depositTx (priority 1)
// period3: spendTx spending transferTx (priority 2) and anotherDepositTx (priority 1). Thus spendTx has priority 2 (youngest)
const anotherDepositTx = Tx.deposit(depositId + 1, 50, bob);
const spendTx = Tx.transfer(
[
new Input(new Outpoint(transferTx.hash(), 0)), // from period2 thus youngest
new Input(new Outpoint(anotherDepositTx.hash(), 0)), // from period1
],
[new Output(100, alice)]
).sign([bobPriv, bobPriv]);
const period1 = await submitNewPeriod([depositTx, anotherDepositTx]);
await aSecond();
const period2 = await submitNewPeriod([transferTx]);
await aSecond();
const period3 = await submitNewPeriod([spendTx]);
const spendProof = period3.proof(spendTx);
const notReallyYoungestInputProof = period1.proof(anotherDepositTx);
const notReallyYoungestInputId = 1;
const youngestInputProof = period2.proof(transferTx);
const youngestInputId = 0;
// start exit with older input (priority 1)
const exitingOutput = 0;
const event = await exitHandler.startExit(
notReallyYoungestInputProof, spendProof, exitingOutput, notReallyYoungestInputId,
{ from: alice, value: exitStake},
);
const utxoId = exitUtxoId(event);
assert.equal(utxoId, new Outpoint(spendTx.hash(), exitingOutput).getUtxoId());
let exit = await exitHandler.exits(utxoId);
assert.equal(exit[2], alice);
// should fail on a simple front-run (sender is different than challenger param)
await exitHandler.challengeYoungestInput(
youngestInputProof, spendProof, exitingOutput, youngestInputId, alice, { from: bob }
).should.be.rejectedWith(EVMRevert);
// challenge exit with youngest input
await exitHandler.challengeYoungestInput(
youngestInputProof, spendProof, exitingOutput, youngestInputId, alice
);
exit = await exitHandler.exits(utxoId);
assert.equal((await exitHandler.tokens(0))[1], 1);
const bal1 = await nativeToken.balanceOf(alice);
const exitTime = (await time.latest()) + time.duration.seconds(2 * exitDuration);
await time.increaseTo(exitTime);
await exitHandler.finalizeTopExit(0);
const bal2 = await nativeToken.balanceOf(alice);
// check transfer didn't happen
assert.equal(bal1.toNumber(), bal2.toNumber());
// check exit was evicted from PriorityQueue
assert.equal((await exitHandler.tokens(0))[1], 0);
assert.equal(exit[2], '0x0000000000000000000000000000000000000000');
});
it('Should allow to challenge NFT exit', async () => {
// utxo that will have spend exit utxo
let spend = Tx.transfer(
[new Input(new Outpoint(nftTransferTx.hash(), 0))],
[new Output(nftTokenIdStr, charlie, nftColor)]
);
spend = spend.sign([bobPriv]);
const period = await submitNewPeriod([nftDepositTx, nftTransferTx, spend]);
const transferProof = period.proof(nftTransferTx);
const spendProof = period.proof(spend);
const inputProof = period.proof(nftDepositTx);
// withdraw output
const event = await exitHandler.startExit(inputProof, transferProof, 0, 0, { from: bob, value: exitStake});
const utxoId = exitUtxoId(event);
assert.equal(utxoId, spend.inputs[0].prevout.getUtxoId());
// challenge exit and make sure exit is removed
let exit = await exitHandler.exits(utxoId);
assert.equal(exit[2], bob);
await exitHandler.challengeExit(spendProof, transferProof, 0, 0, alice);
exit = await exitHandler.exits(utxoId);
// check that exit was deleted
assert.equal(exit[2], '0x0000000000000000000000000000000000000000');
});
it("Should allow to finalise multiple exits", async () => {
const exits = [];
let event;
/* eslint-disable */
for (let i=0; i<10; i++) {
await seedTxs();
depositId ++;
const period = await submitNewPeriod([depositTx, transferTx]);
const transferProof = period.proof(transferTx);
const outputIndex = 1;
const inputProof = period.proof(depositTx); // transferTx spends depositTx
const inputIndex = 0;
event = await exitHandler.startExit(inputProof, transferProof, outputIndex, inputIndex, {value: exitStake});
exits.push(exitUtxoId(event));
}
/* eslint-enable */
(await Promise.all(exits.map(utxoId => exitHandler.exits(utxoId)))).forEach(exit => {
assert.equal(exit.finalized, false);
})
const exitTime = (await time.latest()) + (2 * time.duration.seconds(exitDuration));
await time.increaseTo(exitTime);
await exitHandler.finalizeTopExit(nativeTokenColor);
(await Promise.all(exits.map(utxoId => exitHandler.exits(utxoId)))).forEach(exit => {
assert.equal(exit.finalized, true);
})
});
it("Finalise exit should cost less than 6M gas", async () => {
depositId = 1;
/* eslint-disable */
for (let i=0; i<30; i++) {
await seedTxs();
depositId ++;
const period = await submitNewPeriod([depositTx, transferTx]);
const transferProof = period.proof(transferTx);
const outputIndex = 1;
const inputProof = period.proof(depositTx); // transferTx spends depositTx
const inputIndex = 0;
await exitHandler.startExit(inputProof, transferProof, outputIndex, inputIndex, {value: exitStake});
}
/* eslint-enable */
const exitTime = (await time.latest()) + (2 * time.duration.seconds(exitDuration));
await time.increaseTo(exitTime);
const finEvent = await exitHandler.finalizeTopExit(nativeTokenColor);
assert(finEvent.receipt.gasUsed < 6000000);
});
});
});
describe('Governance', () => {
let proxy;
let exitHandler;
const exitDuration = 5;
const exitStake = 0;
it('should allow to change exit params', async () => {
const vaultCont = await ExitHandler.new();
let data = await vaultCont.contract.methods.initializeWithExit(accounts[0], exitDuration, exitStake).encodeABI();
proxy = await AdminableProxy.new(vaultCont.address, data, {from: accounts[2]});
exitHandler = await ExitHandler.at(proxy.address);
// set exit duration
data = await exitHandler.contract.methods.setExitDuration(100).encodeABI();
await proxy.applyProposal(data, {from: accounts[2]});
assert.equal(await exitHandler.exitDuration(), 100);
// set exit stake
data = await exitHandler.contract.methods.setExitStake(200).encodeABI();
await proxy.applyProposal(data, {from: accounts[2]});
assert.equal(await exitHandler.exitStake(), 200);
});
});
});