This repository was archived by the owner on Aug 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathe2e.deploy.test.ts
More file actions
330 lines (279 loc) · 10 KB
/
Copy pathe2e.deploy.test.ts
File metadata and controls
330 lines (279 loc) · 10 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { Keypair } from "@maci-protocol/domainobjs";
import { isArm, joinPoll, signup, sleepUntil, ESupportedChains, ContractStorage } from "@maci-protocol/sdk";
import { ValidationPipe, type INestApplication } from "@nestjs/common";
import { Test } from "@nestjs/testing";
import dotenv from "dotenv";
import { type Signer } from "ethers";
import { type Socket, io } from "socket.io-client";
import { type Hex, zeroAddress } from "viem";
import path from "path";
import type { IGenerateData } from "../ts/proof/types";
import { AppModule } from "../ts/app.module";
import { getSigner } from "../ts/common";
import {
pollDuration,
pollStartDateExtraSeconds,
testMaciDeploymentConfig,
testPollDeploymentConfig,
} from "../ts/deployer/__tests__/utils";
import { DeployerModule } from "../ts/deployer/deployer.module";
import { FileModule } from "../ts/file/file.module";
import { type IGetPublicKeyData } from "../ts/file/types";
import { type IHealthCheckResponse } from "../ts/health/types";
import { ProofModule } from "../ts/proof/proof.module";
import { SessionKeysModule } from "../ts/sessionKeys/sessionKeys.module";
import { type IGenerateSessionKeyReturn } from "../ts/sessionKeys/types";
import {
pollJoiningTestZkeyPath,
testPollJoiningWasmPath,
testPollJoiningWitnessPath,
testRapidsnarkPath,
zeroUint256Encoded,
} from "./constants";
import { getAuthorizationHeader } from "./utils";
dotenv.config();
jest.setTimeout(700000); // Sets timeout to 700 seconds
const PORT = process.env.COORDINATOR_PORT || 3001;
const TEST_URL = `http://localhost:${PORT}/v1`;
const CHAIN = ESupportedChains.Localhost;
const REGEX_SUBGRAPH = /^https:\/\/api\.studio\.thegraph\.com\/query\/\d+\/maci-subgraph\/v0\.0\.\d+$/;
const NUM_USERS = 1;
const VOTE_OPTIONS: Record<string, number> = {
"0": 0,
"1": 0,
};
const useWasm = isArm();
describe("E2E Deployment Tests", () => {
let signer: Signer;
let encryptedHeader: string;
let app: INestApplication;
let socket: Socket;
let maciAddress: Hex;
let maciCreatedAt: bigint;
let pollId: bigint;
// set up coordinator address
beforeAll(async () => {
signer = await getSigner(CHAIN);
encryptedHeader = await getAuthorizationHeader(signer);
process.env.COORDINATOR_ADDRESSES = await signer.getAddress();
const moduleFixture = await Test.createTestingModule({
imports: [AppModule, FileModule, DeployerModule, ProofModule, SessionKeysModule],
}).compile();
app = moduleFixture.createNestApplication();
app.useGlobalPipes(new ValidationPipe({ transform: true }));
await app.init();
await app.listen(PORT);
});
afterAll(async () => {
await app.close();
const storageInstance = ContractStorage.getInstance(path.join(process.cwd(), "deployed-contracts.json"));
storageInstance.cleanup(CHAIN);
});
// set up auth header in WS connection
beforeEach(async () => {
const authorization = await getAuthorizationHeader(signer);
await new Promise((resolve) => {
app.getUrl().then((url) => {
socket = io(url, {
extraHeaders: {
authorization,
},
});
socket.on("connect", () => {
resolve(true);
});
});
});
});
afterEach(() => {
socket.disconnect();
});
test("should return true in the health check", async () => {
const response = await fetch(`${TEST_URL}/health/check`, { method: "GET" });
const body = (await response.json()) as IHealthCheckResponse;
expect(response.status).toBe(200);
expect(body.rapidsnark.rapidsnarkIsExecutable).toBe(true);
expect(body.zkeysDirectory.zkeysDirectoryExists).toBe(true);
expect(body.coordinatorWalletFunds.fundsInNetworks[0].address).not.toBe(zeroAddress);
});
test("should retrieve RSA public key", async () => {
const response = await fetch(`${TEST_URL}/proof/publicKey`, {
method: "GET",
});
const body = (await response.json()) as IGetPublicKeyData;
expect(response.status).toBe(200);
expect(body.publicKey).toBeDefined();
});
test("should retrieve the session key address", async () => {
const response = await fetch(`${TEST_URL}/session-keys/generate`, {
method: "GET",
headers: {
Authorization: encryptedHeader,
},
});
const body = (await response.json()) as IGenerateSessionKeyReturn;
expect(response.status).toBe(200);
expect(body.sessionKeyAddress).not.toBe(zeroAddress);
});
test("should deploy MACI correctly", async () => {
const response = await fetch(`${TEST_URL}/deploy/maci`, {
method: "POST",
headers: {
Authorization: encryptedHeader,
"Content-Type": "application/json",
},
body: JSON.stringify({
chain: CHAIN,
config: testMaciDeploymentConfig,
}),
});
const body = (await response.json()) as { address: string };
expect(response.status).toBe(201);
expect(body.address).not.toBe(zeroAddress);
// save them for next tests
const blockNumber = await signer.provider!.getBlockNumber();
maciCreatedAt = BigInt(blockNumber);
maciAddress = body.address as Hex;
});
test("should deploy a poll correctly", async () => {
testPollDeploymentConfig.voteOptions = testPollDeploymentConfig.voteOptions.toString();
const startDate = Math.floor(Date.now() / 1000) + pollStartDateExtraSeconds;
testPollDeploymentConfig.startDate = startDate;
testPollDeploymentConfig.endDate = startDate + pollDuration;
const response = await fetch(`${TEST_URL}/deploy/poll`, {
method: "POST",
headers: {
Authorization: encryptedHeader,
"Content-Type": "application/json",
},
body: JSON.stringify({
chain: CHAIN,
config: testPollDeploymentConfig,
}),
});
const body = (await response.json()) as { pollId: string };
expect(response.status).toBe(201);
expect(body.pollId).toBeDefined();
// save them for next tests
pollId = BigInt(body.pollId);
});
test("should allow voting on a poll", async () => {
await sleepUntil(testPollDeploymentConfig.startDate * 1000);
for (let i = 0; i < NUM_USERS; i += 1) {
const keypairUser = new Keypair();
const userPublicKey = keypairUser.publicKey.serialize();
const userPrivateKey = keypairUser.privateKey.serialize();
const vote = i % 2;
VOTE_OPTIONS[String(vote)] += 1;
// user signs up to MACI
// eslint-disable-next-line no-await-in-loop
await signup({
maciAddress,
maciPublicKey: userPublicKey,
sgData: zeroUint256Encoded,
signer,
});
// user joins the poll
// eslint-disable-next-line no-await-in-loop
await joinPoll({
maciAddress,
privateKey: userPrivateKey,
pollId: BigInt(pollId),
pollJoiningZkey: pollJoiningTestZkeyPath,
useWasm,
pollJoiningWasm: testPollJoiningWasmPath,
pollWitnessGenerator: testPollJoiningWitnessPath,
rapidsnark: testRapidsnarkPath,
sgDataArg: zeroUint256Encoded,
ivcpDataArg: zeroUint256Encoded,
signer,
});
}
});
test("should deploy a subgraph correctly", async () => {
const response = await fetch(`${TEST_URL}/subgraph/deploy`, {
method: "POST",
headers: {
Authorization: encryptedHeader,
"Content-Type": "application/json",
},
body: JSON.stringify({
maciContractAddress: maciAddress,
startBlock: Number(maciCreatedAt),
network: ESupportedChains.OptimismSepolia,
name: process.env.SUBGRAPH_NAME,
tag: `v0.0.${+new Date()}`, // different versions per test using timestamp
}),
});
const body = await response.json();
if (response.status === 400) {
const errorMessage = (body as { message: string }).message;
if (errorMessage.includes("api.thegraph.com | 502: Bad gateway")) {
// eslint-disable-next-line no-console
console.info("It looks like there was a problem with The Graph endpoint. We are passing this test.");
return;
}
}
const { url } = body as { url: string };
expect(response.status).toBe(201);
expect(url).toBeDefined();
expect(REGEX_SUBGRAPH.test(url)).toBe(true);
});
test("should merge correctly", async () => {
await sleepUntil((testPollDeploymentConfig.endDate + 13) * 1000); // 13 seconds of grace time after the poll end date
const response = await fetch(`${TEST_URL}/proof/merge`, {
method: "POST",
headers: {
Authorization: encryptedHeader,
"Content-Type": "application/json",
},
body: JSON.stringify({
maciContractAddress: maciAddress,
pollId: Number(pollId),
chain: CHAIN,
}),
});
expect(response.status).toBe(201);
});
test("should generate proofs correctly", async () => {
const response = await fetch(`${TEST_URL}/proof/generate`, {
method: "POST",
headers: {
Authorization: encryptedHeader,
"Content-Type": "application/json",
},
body: JSON.stringify({
poll: Number(pollId),
maciContractAddress: maciAddress,
mode: testPollDeploymentConfig.mode,
startBlock: Number(maciCreatedAt),
blocksPerBatch: 500,
chain: CHAIN,
useWasm,
}),
});
const body = (await response.json()) as IGenerateData;
expect(response.status).toBe(201);
expect(body.processProofs).toBeDefined();
expect(body.processProofs.length).toBeGreaterThan(0);
expect(body.tallyProofs).toBeDefined();
expect(body.tallyProofs.length).toBeGreaterThan(0);
expect(body.tallyData).toBeDefined();
expect(body.tallyData.results).toBeDefined();
});
test("should submit results on-chain correctly", async () => {
const response = await fetch(`${TEST_URL}/proof/submit`, {
method: "POST",
headers: {
Authorization: encryptedHeader,
"Content-Type": "application/json",
},
body: JSON.stringify({
pollId: Number(pollId),
maciContractAddress: maciAddress,
chain: CHAIN,
}),
});
expect(response.status).toBe(201);
});
});