-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclaim.test.ts
More file actions
309 lines (287 loc) · 11.6 KB
/
claim.test.ts
File metadata and controls
309 lines (287 loc) · 11.6 KB
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
import { ethers, getAddress } from "ethers";
import { ClaimStruct } from "@kleros/vea-contracts/typechain-types/arbitrumToEth/VeaInboxArbToEth";
import { getClaim, hashClaim, getClaimResolveState, ClaimResolveStateParams } from "./claim";
import { ClaimNotFoundError } from "./errors";
let mockClaim: ClaimStruct;
// Pre calculated from the deployed contracts
const hashedMockClaim = "0xfee47661ef0432da320c3b4706ff7d412f421b9d1531c33ce8f2e03bfe5dcfa2";
const mockBlockTag = "latest";
const mockFromBlock = 0;
describe("snapshotClaim", () => {
describe("getClaim", () => {
let veaOutbox: any;
let veaOutboxProvider: any;
const epoch = 1;
let getVerificationForClaim = jest.fn();
let getChallengerForClaim = jest.fn();
let mockClaimParams: any;
beforeEach(() => {
mockClaim = {
stateRoot: "0xeac817ed5c5b3d1c2c548f231b7cf9a0dfd174059f450ec6f0805acf6a16a551",
claimer: "0xFa00D29d378EDC57AA1006946F0fc6230a5E3288",
timestampClaimed: 1730276784,
timestampVerification: 0,
blocknumberVerification: 0,
honest: 0,
challenger: ethers.ZeroAddress,
};
veaOutbox = {
queryFilter: jest.fn(),
filters: {
VerificationStarted: jest.fn(),
Challenged: jest.fn(),
Claimed: jest.fn(),
},
claimHashes: jest.fn(),
getAddress: jest.fn(),
};
veaOutboxProvider = {
getBlock: jest.fn().mockResolvedValueOnce({ timestamp: mockClaim.timestampClaimed, number: 1234 }),
};
mockClaimParams = {
veaOutbox,
veaOutboxProvider,
epoch,
fromBlock: mockFromBlock,
toBlock: mockBlockTag,
fetchClaimForEpoch: getClaim,
fetchVerificationForClaim: getVerificationForClaim,
fetchChallengerForClaim: getChallengerForClaim,
};
});
it("should return a valid claim", async () => {
veaOutbox.claimHashes = jest.fn().mockResolvedValueOnce(hashedMockClaim);
veaOutbox.queryFilter
.mockImplementationOnce(() =>
Promise.resolve([
{
data: mockClaim.stateRoot,
topics: [null, `0x000000000000000000000000${mockClaim.claimer.toString().slice(2)}`],
blockNumber: 1234,
},
])
) // For Claimed
.mockImplementationOnce(() => []) // For Challenged
.mockImplementationOnce(() => Promise.resolve([])); // For VerificationStarted
mockClaimParams.veaOutbox = veaOutbox;
const claim = await getClaim(mockClaimParams);
expect(claim).toBeDefined();
expect(claim).toEqual(mockClaim);
});
it("should return a valid claim with challenger", async () => {
mockClaim.challenger = "0xFa00D29d378EDC57AA1006946F0fc6230a5E3288";
veaOutbox.claimHashes.mockResolvedValueOnce(hashClaim(mockClaim));
veaOutbox.queryFilter
.mockImplementationOnce(() =>
Promise.resolve([
{
data: mockClaim.stateRoot,
topics: [null, `0x000000000000000000000000${mockClaim.claimer.toString().slice(2)}`],
blockNumber: 1234,
},
])
) // For Claimed
.mockImplementationOnce(() =>
Promise.resolve([
{
blockHash: "0x1234",
topics: [null, null, `0x000000000000000000000000${mockClaim.challenger.toString().slice(2)}`],
},
])
) // For Challenged
.mockImplementationOnce(() => Promise.resolve([])); // For VerificationStartedß
mockClaimParams.veaOutbox = veaOutbox;
const claim = await getClaim(mockClaimParams);
expect(claim).toBeDefined();
expect(claim).toEqual(mockClaim);
});
it("should return a valid claim with verification", async () => {
mockClaim.timestampVerification = 1234;
mockClaim.blocknumberVerification = 1234;
veaOutbox.claimHashes.mockResolvedValueOnce(hashClaim(mockClaim));
veaOutboxProvider.getBlock.mockResolvedValueOnce({ timestamp: mockClaim.timestampVerification });
veaOutbox.queryFilter
.mockImplementationOnce(() =>
Promise.resolve([
{
data: mockClaim.stateRoot,
topics: [null, `0x000000000000000000000000${mockClaim.claimer.toString().slice(2)}`],
blockNumber: 1234,
},
])
) // For Claimed
.mockImplementationOnce(() => Promise.resolve([])) // For Challenged
.mockImplementationOnce(() =>
Promise.resolve([
{
blockNumber: 1234,
},
])
); // For VerificationStarted
mockClaimParams.veaOutbox = veaOutbox;
mockClaimParams.veaOutboxProvider = veaOutboxProvider;
const claim = await getClaim(mockClaimParams);
expect(claim).toBeDefined();
expect(claim).toEqual(mockClaim);
});
it("should return a claim with fallback on subgraphs", async () => {
veaOutbox.claimHashes.mockResolvedValueOnce(hashedMockClaim);
veaOutbox.queryFilter.mockImplementationOnce(() => {
throw new Error("Logs not available");
});
const claimFromGraph = {
id: "1",
stateroot: mockClaim.stateRoot,
bridger: mockClaim.claimer,
timestamp: mockClaim.timestampClaimed,
};
const verificationFromGraph = null;
const challengeFromGraph = null;
mockClaimParams.veaOutbox = veaOutbox;
mockClaimParams.fetchClaimForEpoch = jest.fn().mockResolvedValueOnce(claimFromGraph);
mockClaimParams.fetchVerificationForClaim = jest.fn().mockResolvedValueOnce(verificationFromGraph);
mockClaimParams.fetchChallengerForClaim = jest.fn().mockResolvedValueOnce(challengeFromGraph);
const claim = await getClaim(mockClaimParams);
expect(claim).toBeDefined();
expect(claim).toEqual(mockClaim);
});
it("should return null if no claim is found", async () => {
veaOutbox.claimHashes.mockResolvedValueOnce(ethers.ZeroHash);
mockClaimParams.veaOutbox = veaOutbox;
const claim = await getClaim(mockClaimParams);
expect(claim).toBeNull();
expect(veaOutbox.queryFilter).toHaveBeenCalledTimes(0);
});
it("should throw an error if no claim is found", async () => {
veaOutbox.claimHashes.mockResolvedValueOnce(hashedMockClaim);
veaOutbox.queryFilter
.mockImplementationOnce(() => Promise.resolve([]))
.mockImplementationOnce(() => Promise.resolve([]))
.mockImplementationOnce(() => Promise.resolve([]));
mockClaimParams.fetchClaimForEpoch = jest.fn().mockResolvedValueOnce(null);
mockClaimParams.fetchVerificationForClaim = jest.fn().mockResolvedValueOnce(null);
mockClaimParams.fetchChallengerForClaim = jest.fn().mockResolvedValueOnce(null);
mockClaimParams.veaOutbox = veaOutbox;
await expect(async () => {
await getClaim(mockClaimParams);
}).rejects.toThrow(new ClaimNotFoundError(epoch));
});
it("should throw an error if the claim is not valid", async () => {
veaOutbox.claimHashes.mockResolvedValueOnce(hashClaim(mockClaim));
mockClaim.honest = 1;
veaOutbox.queryFilter
.mockImplementationOnce(() =>
Promise.resolve([
{
data: mockClaim.stateRoot,
topics: [null, `0x000000000000000000000000${ethers.ZeroAddress.toString().slice(2)}`],
blockNumber: 1234,
},
])
) // For Claimed
.mockImplementationOnce(() => []) // For Challenged
.mockImplementationOnce(() => Promise.resolve([])); // For VerificationStarted
mockClaimParams.veaOutbox = veaOutbox;
await expect(async () => {
await getClaim(mockClaimParams);
}).rejects.toThrow(new ClaimNotFoundError(epoch));
});
});
describe("hashClaim", () => {
beforeEach(() => {
mockClaim = {
stateRoot: "0xeac817ed5c5b3d1c2c548f231b7cf9a0dfd174059f450ec6f0805acf6a16a551",
claimer: "0xFa00D29d378EDC57AA1006946F0fc6230a5E3288",
timestampClaimed: 1730276784,
timestampVerification: 0,
blocknumberVerification: 0,
honest: 0,
challenger: ethers.ZeroAddress,
};
});
it("should return a valid hash", () => {
const hash = hashClaim(mockClaim);
expect(hash).toBeDefined();
expect(hash).toEqual(hashedMockClaim);
});
it("should not return a valid hash", () => {
mockClaim.honest = 1;
const hash = hashClaim(mockClaim);
expect(hash).toBeDefined();
expect(hash).not.toEqual(hashedMockClaim);
});
});
describe("getClaimResolveState", () => {
let veaInbox: any;
let veaOutbox: any;
let fetchSentSnapshotData: any;
const epoch = 1;
const blockNumberOutboxLowerBound = 1234;
const toBlock = "latest";
let mockClaimResolveStateParams: any;
beforeEach(() => {
mockClaim = {
stateRoot: "0xeac817ed5c5b3d1c2c548f231b7cf9a0dfd174059f450ec6f0805acf6a16a551",
claimer: "0xFa00D29d378EDC57AA1006946F0fc6230a5E3288",
timestampClaimed: 1730276784,
timestampVerification: 0,
blocknumberVerification: 0,
honest: 0,
challenger: ethers.ZeroAddress,
};
veaInbox = {
queryFilter: jest.fn(),
filters: {
SnapshotSent: jest.fn(),
},
getAddress: jest.fn(),
};
veaOutbox = {
claimHashes: jest.fn().mockResolvedValueOnce(hashedMockClaim),
getAddress: jest.fn(),
};
fetchSentSnapshotData = jest.fn().mockResolvedValueOnce(hashedMockClaim);
mockClaimResolveStateParams = {
chainId: 11155111,
veaInbox,
veaOutbox,
veaInboxProvider: {
getBlock: jest.fn().mockResolvedValueOnce({ timestamp: mockClaim.timestampClaimed, number: 1234 }),
} as any,
veaOutboxProvider: {
getBlock: jest.fn().mockResolvedValueOnce({ timestamp: mockClaim.timestampClaimed, number: 1234 }),
} as any,
epoch,
fromBlock: blockNumberOutboxLowerBound,
toBlock,
fetchMessageStatus: jest.fn(),
fetchSentSnapshotData,
};
});
it("should return pending state for both", async () => {
veaInbox.queryFilter.mockResolvedValueOnce([]);
const claimResolveState = await getClaimResolveState(mockClaimResolveStateParams);
expect(claimResolveState).toBeDefined();
expect(claimResolveState.sendSnapshot.status).toBeFalsy();
expect(claimResolveState.execution.status).toBe(0);
});
it("should return pending state for execution", async () => {
veaInbox.queryFilter.mockResolvedValueOnce([{ transactionHash: "0x1234" }]);
const mockGetMessageStatus = jest.fn().mockResolvedValueOnce(0);
mockClaimResolveStateParams.fetchMessageStatus = mockGetMessageStatus;
const claimResolveState = await getClaimResolveState(mockClaimResolveStateParams);
expect(claimResolveState).toBeDefined();
expect(claimResolveState.sendSnapshot.status).toBeTruthy();
expect(claimResolveState.execution.status).toBe(0);
});
it("should return false state if incorrect snapshot sent", async () => {
veaInbox.queryFilter.mockResolvedValueOnce([{ transactionHash: "0x1234" }]);
fetchSentSnapshotData = jest.fn().mockResolvedValueOnce("0xincorrecthash");
mockClaimResolveStateParams.fetchSentSnapshotData = fetchSentSnapshotData;
const claimResolveState = await getClaimResolveState(mockClaimResolveStateParams);
expect(claimResolveState).toBeDefined();
expect(claimResolveState.sendSnapshot.status).toBeFalsy();
expect(claimResolveState.execution.status).toBe(0);
});
});
});