This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispute-manager.test.ts
226 lines (196 loc) · 7.4 KB
/
dispute-manager.test.ts
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
import {ethers} from 'hardhat';
import {expect, use} from 'chai';
import {generateWitness, hash, WitnessProof} from '../../ts-prototype/merkle';
import _ from 'lodash';
import {ContractFactory} from '@ethersproject/contracts';
import {solidity} from 'ethereum-waffle';
function getElements(array: string[], indices: number[]): string[] {
return indices.map(i => array[i]);
}
enum ChallengeStatus {
InProgress = 0,
FraudDetected = 1,
Forfeited = 2
}
enum Mover {
Challenger,
Responder
}
// This allows us to use things like .to.be.revertedWith
use(solidity);
describe('Dispute Manager Contract', () => {
let disputeManagerFactory: ContractFactory;
const correctStates = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const correctHashes = correctStates.map(num => hash(num.toString()));
const incorrectStates = [0, 1, 2, 3, 4, 5.1, 6.1, 7.1, 8.1, 9.1];
const incorrectHashes = incorrectStates.map(num => hash(num.toString()));
beforeEach(async () => {
const merkleUtils = await (await ethers.getContractFactory('MerkleUtils')).deploy();
disputeManagerFactory = await ethers.getContractFactory('DisputeManager', {
libraries: {MerkleUtils: merkleUtils.address}
});
});
it('can perform a basic bisection', async () => {
const manager = await disputeManagerFactory.deploy(
getElements(correctHashes, [0, 4, 9]),
9,
Mover.Challenger,
2
);
let status = await manager.currentStatus();
expect(status).to.eq(ChallengeStatus.InProgress);
await manager.split(
generateWitness(getElements(correctHashes, [0, 4, 9]), 1),
getElements(incorrectHashes, [6, 9]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
Mover.Responder
);
await manager.split(
generateWitness(getElements(incorrectHashes, [4, 6, 9]), 1),
getElements(correctHashes, [5, 6]),
generateWitness(getElements(incorrectHashes, [4, 6, 9]), 2),
Mover.Challenger
);
await expect(
manager.split(
generateWitness(getElements(correctHashes, [0, 4, 9]), 1),
getElements(incorrectHashes, [6, 9]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
Mover.Responder
)
).to.be.revertedWith('States cannot be split further');
await manager.claimFraud(1, Mover.Responder);
status = await manager.currentStatus();
expect(status).to.eq(ChallengeStatus.FraudDetected);
const fraudIndex = await manager.fraudIndex();
expect(fraudIndex).to.eq(1);
});
it('can perform a basic trisection', async () => {
const manager = await disputeManagerFactory.deploy(
getElements(correctHashes, [0, 3, 6, 9]),
9,
Mover.Challenger,
3
);
let status = await manager.currentStatus();
expect(status).to.eq(ChallengeStatus.InProgress);
await manager.split(
generateWitness(getElements(correctHashes, [0, 3, 6, 9]), 1),
getElements(incorrectHashes, [4, 5, 7]),
generateWitness(getElements(correctHashes, [0, 3, 6, 9]), 2),
Mover.Responder
);
await manager.claimFraud(1, Mover.Challenger);
status = await manager.currentStatus();
expect(status).to.eq(ChallengeStatus.FraudDetected);
const fraudIndex = await manager.fraudIndex();
expect(fraudIndex).to.eq(1);
});
it('Invalid DisputeManager instanatiation', async () => {
expect(
disputeManagerFactory.deploy(getElements(correctHashes, [0, 3, 4, 9]), 9, Mover.Challenger, 2)
).to.revertedWith('There must be k+1 values');
expect(
disputeManagerFactory.deploy(getElements(correctHashes, [0, 5, 9]), 9, Mover.Challenger, 1)
).to.revertedWith('The splitting factor must be above 1');
});
it('Invalid splits, invalid detect fraud', async () => {
const manager = await disputeManagerFactory.deploy(
getElements(correctHashes, [0, 4, 9]),
9,
Mover.Challenger,
2
);
await expect(
manager.split(
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
getElements(correctHashes, [9]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
Mover.Responder
)
).to.revertedWith('Consensus witness cannot be the last stored state');
await expect(
manager.split(
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
getElements(correctHashes, [9]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
Mover.Challenger
)
).to.revertedWith('The mover cannot be the same as the last mover');
const leafWintness = generateWitness(getElements(correctHashes, [0, 4, 9]), 0);
const lastNode = leafWintness.nodes.pop() as string;
const nonLeafWitness: WitnessProof = {witness: lastNode, nodes: leafWintness.nodes, index: 0};
await expect(
manager.split(
nonLeafWitness,
getElements(correctHashes, [9]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
Mover.Responder
)
).to.revertedWith('The witness provided is not for a leaf node');
await expect(
manager.split(
generateWitness(getElements(correctHashes, [5, 6, 7]), 1),
getElements(correctHashes, [2, 4]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
Mover.Responder
)
).to.revertedWith('Invalid consensus witness proof');
await expect(
manager.split(
generateWitness(getElements(correctHashes, [0, 4, 9]), 1),
getElements(correctHashes, [2, 4]),
generateWitness(getElements(correctHashes, [5, 6, 7]), 2),
Mover.Responder
)
).to.revertedWith('Invalid dispute witness proof');
await expect(
manager.split(
generateWitness(getElements(correctHashes, [0, 4, 9]), 0),
getElements(correctHashes, [2, 4]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
Mover.Responder
)
).to.revertedWith('Disputed state hash must be the next leaf after consensus state hash');
await expect(
manager.split(
generateWitness(getElements(correctHashes, [0, 4, 9]), 1),
getElements(correctHashes, [6, 9]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
Mover.Responder
)
).to.revertedWith('The last state supplied must differ from the disputed witness');
await expect(
manager.split(
generateWitness(getElements(correctHashes, [0, 4, 9]), 0),
getElements(correctHashes, [2, 3, 5]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 1),
Mover.Responder
)
).to.revertedWith('Incorrect amount of state hashes');
await expect(manager.claimFraud(1, Mover.Challenger)).to.revertedWith(
'The mover cannot be the same as the last mover'
);
});
it('forfeits', async () => {
const manager = await disputeManagerFactory.deploy(
getElements(correctHashes, [0, 4, 9]),
9,
Mover.Challenger,
2
);
let status = await manager.currentStatus();
expect(status).to.eq(ChallengeStatus.InProgress);
await manager.forfeit(Mover.Responder);
status = await manager.currentStatus();
expect(status).to.eq(ChallengeStatus.Forfeited);
expect(
manager.split(
generateWitness(getElements(correctHashes, [0, 4, 9]), 1),
getElements(incorrectHashes, [6, 9]),
generateWitness(getElements(correctHashes, [0, 4, 9]), 2),
Mover.Responder
)
).to.revertedWith('The challenge is already complete');
});
});