@maci-protocol/testing 0.0.0-ci.fd5247e → 0.0.0-ci.ffabe48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,851 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const core_1 = require("@maci-protocol/core");
4
+ const crypto_1 = require("@maci-protocol/crypto");
5
+ const domainobjs_1 = require("@maci-protocol/domainobjs");
6
+ const sdk_1 = require("@maci-protocol/sdk");
7
+ const chai_1 = require("chai");
8
+ const constants_1 = require("../constants");
9
+ const utils_1 = require("../utils");
10
+ /**
11
+ Test scenarios:
12
+ 1 signup, 1 message
13
+ 1 signup, 1 relayed message
14
+ 1 signup, 2 valid messages
15
+ 1 signup, 2 valid and 2 invalid messages
16
+ 4 signups, 8 messages, 16 relayed messages
17
+ 30 signups (31 ballots), 21 messages
18
+ 30 signups, 30 invalid and 1 valid messages
19
+ */
20
+ describe("e2e tests with full credits voting", function test() {
21
+ const useWasm = (0, sdk_1.isArm)();
22
+ this.timeout(900000);
23
+ let maciAddresses;
24
+ let initialVoiceCreditProxyContractAddress;
25
+ let verifierContractAddress;
26
+ let signer;
27
+ let verifyingKeysRegistryAddress;
28
+ const generateProofsArgs = {
29
+ outputDir: constants_1.testProofsDirPath,
30
+ tallyFile: constants_1.testTallyFilePath,
31
+ tallyZkey: constants_1.testTallyVotesFullZkeyPath,
32
+ processZkey: constants_1.testProcessMessageFullZkeyPath,
33
+ pollId: 0n,
34
+ rapidsnark: constants_1.testRapidsnarkPath,
35
+ processWitgen: constants_1.testProcessMessagesFullWitnessPath,
36
+ processDatFile: constants_1.testProcessMessagesFullWitnessDatPath,
37
+ tallyWitgen: constants_1.testTallyVotesFullWitnessPath,
38
+ tallyDatFile: constants_1.testTallyVotesFullWitnessDatPath,
39
+ coordinatorPrivateKey: constants_1.coordinatorPrivateKey,
40
+ processWasm: constants_1.testProcessMessagesFullWasmPath,
41
+ tallyWasm: constants_1.testTallyVotesFullWasmPath,
42
+ useWasm,
43
+ mode: sdk_1.EMode.FULL,
44
+ };
45
+ const prepareTest = async () => {
46
+ const [signupPolicy, , signupPolicyFactory, signupCheckerFactory] = await (0, sdk_1.deployFreeForAllSignUpPolicy)({}, signer, true);
47
+ const signupPolicyContractAddress = await signupPolicy.getAddress();
48
+ const [pollPolicy] = await (0, sdk_1.deployFreeForAllSignUpPolicy)({ policy: signupPolicyFactory, checker: signupCheckerFactory }, signer, true);
49
+ const pollPolicyContractAddress = await pollPolicy.getAddress();
50
+ // deploy the smart contracts
51
+ const maciContractsAddresses = await (0, sdk_1.deployMaci)({
52
+ ...constants_1.deployArgs,
53
+ signer,
54
+ signupPolicyAddress: signupPolicyContractAddress,
55
+ });
56
+ const startDate = await (0, sdk_1.getBlockTimestamp)(signer);
57
+ // deploy a poll contract
58
+ await (0, sdk_1.deployPoll)({
59
+ ...constants_1.deployPollArgs,
60
+ signer,
61
+ pollStartTimestamp: startDate,
62
+ pollEndTimestamp: startDate + constants_1.pollDuration,
63
+ relayers: [await signer.getAddress()],
64
+ maciAddress: maciContractsAddresses.maciContractAddress,
65
+ verifierContractAddress,
66
+ verifyingKeysRegistryContractAddress: verifyingKeysRegistryAddress,
67
+ policyContractAddress: pollPolicyContractAddress,
68
+ initialVoiceCreditProxyContractAddress,
69
+ mode: sdk_1.EMode.FULL,
70
+ });
71
+ return maciContractsAddresses;
72
+ };
73
+ // before all tests we deploy the verifying keys registry contract and set the verifying keys
74
+ before(async () => {
75
+ signer = await (0, sdk_1.getDefaultSigner)();
76
+ // we deploy the verifying keys registry contract
77
+ verifyingKeysRegistryAddress = await (0, sdk_1.deployVerifyingKeysRegistryContract)({ signer });
78
+ const [initialVoiceCreditProxy] = await (0, sdk_1.deployConstantInitialVoiceCreditProxy)({ amount: constants_1.DEFAULT_INITIAL_VOICE_CREDITS }, signer, undefined, true);
79
+ initialVoiceCreditProxyContractAddress = await initialVoiceCreditProxy.getAddress();
80
+ const verifier = await (0, sdk_1.deployVerifier)(signer, true);
81
+ verifierContractAddress = await verifier.getAddress();
82
+ // we deploy the verifying keys registry contract
83
+ verifyingKeysRegistryAddress = await (0, sdk_1.deployVerifyingKeysRegistryContract)({ signer });
84
+ // we set the verifying keys
85
+ await (0, sdk_1.setVerifyingKeys)({ ...(await (0, constants_1.verifyingKeysArgs)(signer, sdk_1.EMode.FULL)), verifyingKeysRegistryAddress });
86
+ });
87
+ describe("1 signup, 1 message", () => {
88
+ after(async () => {
89
+ await (0, utils_1.clean)();
90
+ });
91
+ const user = new domainobjs_1.Keypair();
92
+ before(async () => {
93
+ // deploy the smart contracts
94
+ maciAddresses = await prepareTest();
95
+ });
96
+ it("should signup one user", async () => {
97
+ await (0, sdk_1.signup)({
98
+ maciAddress: maciAddresses.maciContractAddress,
99
+ maciPublicKey: user.publicKey.serialize(),
100
+ sgData: constants_1.DEFAULT_SG_DATA,
101
+ signer,
102
+ });
103
+ });
104
+ it("should join one user", async () => {
105
+ await (0, sdk_1.joinPoll)({
106
+ maciAddress: maciAddresses.maciContractAddress,
107
+ privateKey: user.privateKey.serialize(),
108
+ stateIndex: 1n,
109
+ pollId: 0n,
110
+ pollJoiningZkey: constants_1.testPollJoiningZkeyPath,
111
+ useWasm: true,
112
+ pollWasm: constants_1.testPollJoiningWasmPath,
113
+ pollWitgen: constants_1.testPollJoiningWitnessPath,
114
+ rapidsnark: constants_1.testRapidsnarkPath,
115
+ sgDataArg: constants_1.DEFAULT_SG_DATA,
116
+ ivcpDataArg: constants_1.DEFAULT_IVCP_DATA,
117
+ signer,
118
+ });
119
+ });
120
+ it("should publish one message", async () => {
121
+ await (0, sdk_1.publish)({
122
+ publicKey: user.publicKey.serialize(),
123
+ stateIndex: 1n,
124
+ voteOptionIndex: 0n,
125
+ nonce: 1n,
126
+ pollId: 0n,
127
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
128
+ maciAddress: maciAddresses.maciContractAddress,
129
+ salt: (0, crypto_1.generateRandomSalt)(),
130
+ privateKey: user.privateKey.serialize(),
131
+ signer,
132
+ });
133
+ });
134
+ it("should generate zk-SNARK proofs and verify them", async () => {
135
+ await (0, sdk_1.timeTravel)({ seconds: constants_1.pollDuration, signer });
136
+ await (0, sdk_1.mergeSignups)({ ...constants_1.mergeSignupsArgs, maciAddress: maciAddresses.maciContractAddress, signer });
137
+ const { tallyData: tallyFileData } = await (0, sdk_1.generateProofs)({
138
+ ...generateProofsArgs,
139
+ signer,
140
+ maciAddress: maciAddresses.maciContractAddress,
141
+ mode: sdk_1.EMode.FULL,
142
+ });
143
+ await (0, sdk_1.proveOnChain)({ ...constants_1.proveOnChainArgs, maciAddress: maciAddresses.maciContractAddress, signer });
144
+ await (0, sdk_1.verify)({
145
+ ...(await (0, constants_1.verifyArgs)(signer)),
146
+ tallyData: tallyFileData,
147
+ maciAddress: tallyFileData.maci,
148
+ });
149
+ (0, chai_1.expect)(tallyFileData.totalSpentVoiceCredits.spent.toString()).to.eq(constants_1.DEFAULT_INITIAL_VOICE_CREDITS.toString());
150
+ tallyFileData.results.tally.forEach((result, index) => {
151
+ (0, chai_1.expect)(result.toString()).to.eq(index === 0 ? constants_1.DEFAULT_INITIAL_VOICE_CREDITS.toString() : "0");
152
+ });
153
+ });
154
+ });
155
+ describe("1 signup, 1 relayed message", () => {
156
+ after(async () => {
157
+ await (0, utils_1.clean)();
158
+ });
159
+ const user = new domainobjs_1.Keypair();
160
+ before(async () => {
161
+ // deploy the smart contracts
162
+ maciAddresses = await prepareTest();
163
+ });
164
+ it("should signup one user", async () => {
165
+ await (0, sdk_1.signup)({
166
+ maciAddress: maciAddresses.maciContractAddress,
167
+ maciPublicKey: user.publicKey.serialize(),
168
+ sgData: constants_1.DEFAULT_SG_DATA,
169
+ signer,
170
+ });
171
+ });
172
+ it("should join one user", async () => {
173
+ await (0, sdk_1.joinPoll)({
174
+ maciAddress: maciAddresses.maciContractAddress,
175
+ privateKey: user.privateKey.serialize(),
176
+ stateIndex: 1n,
177
+ pollId: 0n,
178
+ pollJoiningZkey: constants_1.testPollJoiningZkeyPath,
179
+ useWasm: true,
180
+ pollWasm: constants_1.testPollJoiningWasmPath,
181
+ pollWitgen: constants_1.testPollJoiningWitnessPath,
182
+ rapidsnark: constants_1.testRapidsnarkPath,
183
+ sgDataArg: constants_1.DEFAULT_SG_DATA,
184
+ ivcpDataArg: constants_1.DEFAULT_IVCP_DATA,
185
+ signer,
186
+ });
187
+ });
188
+ it("should relay one message", async () => {
189
+ const { message, ephemeralKeypair } = (0, sdk_1.generateVote)({
190
+ pollId: 0n,
191
+ voteOptionIndex: 0n,
192
+ salt: (0, crypto_1.generateRandomSalt)(),
193
+ nonce: 1n,
194
+ privateKey: user.privateKey,
195
+ stateIndex: 1n,
196
+ voteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
197
+ coordinatorPublicKey: constants_1.coordinatorKeypair.publicKey,
198
+ maxVoteOption: BigInt(core_1.VOTE_OPTION_TREE_ARITY ** constants_1.deployPollArgs.voteOptionTreeDepth),
199
+ newPublicKey: user.publicKey,
200
+ });
201
+ const messages = [
202
+ {
203
+ maciAddress: maciAddresses.maciContractAddress,
204
+ poll: 0,
205
+ data: message.data.map(String),
206
+ publicKey: ephemeralKeypair.publicKey.asArray().map(String),
207
+ hash: message.hash(ephemeralKeypair.publicKey).toString(),
208
+ },
209
+ ];
210
+ await (0, utils_1.relayTestMessages)({ messages, signer, pollId: 0, maciAddress: maciAddresses.maciContractAddress });
211
+ });
212
+ it("should generate zk-SNARK proofs and verify them", async () => {
213
+ const ipfsMessageBackupFiles = await (0, utils_1.getBackupFilenames)();
214
+ await (0, sdk_1.timeTravel)({ seconds: constants_1.pollDuration, signer });
215
+ await (0, sdk_1.mergeSignups)({ ...constants_1.mergeSignupsArgs, maciAddress: maciAddresses.maciContractAddress, signer });
216
+ const { tallyData: tallyFileData } = await (0, sdk_1.generateProofs)({
217
+ ...generateProofsArgs,
218
+ signer,
219
+ maciAddress: maciAddresses.maciContractAddress,
220
+ ipfsMessageBackupFiles,
221
+ mode: sdk_1.EMode.FULL,
222
+ });
223
+ await (0, sdk_1.proveOnChain)({ ...constants_1.proveOnChainArgs, maciAddress: maciAddresses.maciContractAddress, signer });
224
+ await (0, sdk_1.verify)({
225
+ ...(await (0, constants_1.verifyArgs)(signer)),
226
+ tallyData: tallyFileData,
227
+ maciAddress: tallyFileData.maci,
228
+ });
229
+ (0, chai_1.expect)(tallyFileData.totalSpentVoiceCredits.spent.toString()).to.eq(constants_1.DEFAULT_INITIAL_VOICE_CREDITS.toString());
230
+ tallyFileData.results.tally.forEach((result, index) => {
231
+ (0, chai_1.expect)(result.toString()).to.eq(index === 0 ? constants_1.DEFAULT_INITIAL_VOICE_CREDITS.toString() : "0");
232
+ });
233
+ });
234
+ });
235
+ describe("1 signup, 2 valid messages", () => {
236
+ after(async () => {
237
+ await (0, utils_1.clean)();
238
+ });
239
+ const user = new domainobjs_1.Keypair();
240
+ before(async () => {
241
+ // deploy the smart contracts
242
+ maciAddresses = await prepareTest();
243
+ });
244
+ it("should signup one user", async () => {
245
+ await (0, sdk_1.signup)({
246
+ maciAddress: maciAddresses.maciContractAddress,
247
+ maciPublicKey: user.publicKey.serialize(),
248
+ sgData: constants_1.DEFAULT_SG_DATA,
249
+ signer,
250
+ });
251
+ });
252
+ it("should join one user", async () => {
253
+ await (0, sdk_1.joinPoll)({
254
+ maciAddress: maciAddresses.maciContractAddress,
255
+ privateKey: user.privateKey.serialize(),
256
+ stateIndex: 1n,
257
+ pollId: 0n,
258
+ pollJoiningZkey: constants_1.testPollJoiningZkeyPath,
259
+ useWasm: true,
260
+ pollWasm: constants_1.testPollJoiningWasmPath,
261
+ pollWitgen: constants_1.testPollJoiningWitnessPath,
262
+ rapidsnark: constants_1.testRapidsnarkPath,
263
+ sgDataArg: constants_1.DEFAULT_SG_DATA,
264
+ ivcpDataArg: constants_1.DEFAULT_IVCP_DATA,
265
+ signer,
266
+ });
267
+ });
268
+ it("should publish two messages", async () => {
269
+ await (0, sdk_1.publish)({
270
+ publicKey: user.publicKey.serialize(),
271
+ stateIndex: 1n,
272
+ voteOptionIndex: 0n,
273
+ nonce: 1n,
274
+ pollId: 0n,
275
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
276
+ maciAddress: maciAddresses.maciContractAddress,
277
+ salt: (0, crypto_1.generateRandomSalt)(),
278
+ privateKey: user.privateKey.serialize(),
279
+ signer,
280
+ });
281
+ await (0, sdk_1.publish)({
282
+ publicKey: user.publicKey.serialize(),
283
+ stateIndex: 1n,
284
+ voteOptionIndex: 1n,
285
+ nonce: 1n,
286
+ pollId: 0n,
287
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
288
+ maciAddress: maciAddresses.maciContractAddress,
289
+ salt: (0, crypto_1.generateRandomSalt)(),
290
+ privateKey: user.privateKey.serialize(),
291
+ signer,
292
+ });
293
+ });
294
+ it("should generate zk-SNARK proofs and verify them", async () => {
295
+ await (0, sdk_1.timeTravel)({ seconds: constants_1.pollDuration, signer });
296
+ await (0, sdk_1.mergeSignups)({ ...constants_1.mergeSignupsArgs, maciAddress: maciAddresses.maciContractAddress, signer });
297
+ const { tallyData: tallyFileData } = await (0, sdk_1.generateProofs)({
298
+ ...generateProofsArgs,
299
+ signer,
300
+ maciAddress: maciAddresses.maciContractAddress,
301
+ mode: sdk_1.EMode.FULL,
302
+ });
303
+ await (0, sdk_1.proveOnChain)({ ...constants_1.proveOnChainArgs, maciAddress: maciAddresses.maciContractAddress, signer });
304
+ await (0, sdk_1.verify)({
305
+ ...(await (0, constants_1.verifyArgs)(signer)),
306
+ tallyData: tallyFileData,
307
+ maciAddress: tallyFileData.maci,
308
+ });
309
+ (0, chai_1.expect)(tallyFileData.totalSpentVoiceCredits.spent.toString()).to.eq(constants_1.DEFAULT_INITIAL_VOICE_CREDITS.toString());
310
+ tallyFileData.results.tally.forEach((result, index) => {
311
+ (0, chai_1.expect)(result.toString()).to.eq(index === 1 ? constants_1.DEFAULT_INITIAL_VOICE_CREDITS.toString() : "0");
312
+ });
313
+ });
314
+ });
315
+ describe("1 signup, 2 valid and 2 invalid message", () => {
316
+ after(async () => {
317
+ await (0, utils_1.clean)();
318
+ });
319
+ const user = new domainobjs_1.Keypair();
320
+ before(async () => {
321
+ // deploy the smart contracts
322
+ maciAddresses = await prepareTest();
323
+ });
324
+ it("should signup one user", async () => {
325
+ await (0, sdk_1.signup)({
326
+ maciAddress: maciAddresses.maciContractAddress,
327
+ maciPublicKey: user.publicKey.serialize(),
328
+ sgData: constants_1.DEFAULT_SG_DATA,
329
+ signer,
330
+ });
331
+ });
332
+ it("should join one user", async () => {
333
+ await (0, sdk_1.joinPoll)({
334
+ maciAddress: maciAddresses.maciContractAddress,
335
+ privateKey: user.privateKey.serialize(),
336
+ stateIndex: 1n,
337
+ pollId: 0n,
338
+ pollJoiningZkey: constants_1.testPollJoiningZkeyPath,
339
+ useWasm: true,
340
+ pollWasm: constants_1.testPollJoiningWasmPath,
341
+ pollWitgen: constants_1.testPollJoiningWitnessPath,
342
+ rapidsnark: constants_1.testRapidsnarkPath,
343
+ sgDataArg: constants_1.DEFAULT_SG_DATA,
344
+ ivcpDataArg: constants_1.DEFAULT_IVCP_DATA,
345
+ signer,
346
+ });
347
+ });
348
+ it("should publish two valid and two invalid messages", async () => {
349
+ await (0, sdk_1.publish)({
350
+ publicKey: user.publicKey.serialize(),
351
+ stateIndex: 1n,
352
+ voteOptionIndex: 0n,
353
+ nonce: 1n,
354
+ pollId: 0n,
355
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
356
+ maciAddress: maciAddresses.maciContractAddress,
357
+ salt: (0, crypto_1.generateRandomSalt)(),
358
+ privateKey: user.privateKey.serialize(),
359
+ signer,
360
+ });
361
+ await (0, sdk_1.publish)({
362
+ publicKey: user.publicKey.serialize(),
363
+ stateIndex: 1n,
364
+ voteOptionIndex: 1n,
365
+ nonce: 1n,
366
+ pollId: 0n,
367
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
368
+ maciAddress: maciAddresses.maciContractAddress,
369
+ salt: (0, crypto_1.generateRandomSalt)(),
370
+ privateKey: user.privateKey.serialize(),
371
+ signer,
372
+ });
373
+ await (0, sdk_1.publish)({
374
+ publicKey: user.publicKey.serialize(),
375
+ stateIndex: 1n,
376
+ voteOptionIndex: 1n,
377
+ nonce: 1n,
378
+ pollId: 0n,
379
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS - 10),
380
+ maciAddress: maciAddresses.maciContractAddress,
381
+ salt: (0, crypto_1.generateRandomSalt)(),
382
+ privateKey: user.privateKey.serialize(),
383
+ signer,
384
+ });
385
+ await (0, sdk_1.publish)({
386
+ publicKey: user.publicKey.serialize(),
387
+ stateIndex: 1n,
388
+ voteOptionIndex: 1n,
389
+ nonce: 1n,
390
+ pollId: 0n,
391
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS + 10),
392
+ maciAddress: maciAddresses.maciContractAddress,
393
+ salt: (0, crypto_1.generateRandomSalt)(),
394
+ privateKey: user.privateKey.serialize(),
395
+ signer,
396
+ });
397
+ });
398
+ it("should generate zk-SNARK proofs and verify them", async () => {
399
+ await (0, sdk_1.timeTravel)({ seconds: constants_1.pollDuration, signer });
400
+ await (0, sdk_1.mergeSignups)({ ...constants_1.mergeSignupsArgs, maciAddress: maciAddresses.maciContractAddress, signer });
401
+ const { tallyData: tallyFileData } = await (0, sdk_1.generateProofs)({
402
+ ...generateProofsArgs,
403
+ signer,
404
+ maciAddress: maciAddresses.maciContractAddress,
405
+ mode: sdk_1.EMode.FULL,
406
+ });
407
+ await (0, sdk_1.proveOnChain)({ ...constants_1.proveOnChainArgs, maciAddress: maciAddresses.maciContractAddress, signer });
408
+ await (0, sdk_1.verify)({
409
+ ...(await (0, constants_1.verifyArgs)(signer)),
410
+ tallyData: tallyFileData,
411
+ maciAddress: tallyFileData.maci,
412
+ });
413
+ (0, chai_1.expect)(tallyFileData.totalSpentVoiceCredits.spent.toString()).to.eq(constants_1.DEFAULT_INITIAL_VOICE_CREDITS.toString());
414
+ tallyFileData.results.tally.forEach((result, index) => {
415
+ (0, chai_1.expect)(result.toString()).to.eq(index === 1 ? constants_1.DEFAULT_INITIAL_VOICE_CREDITS.toString() : "0");
416
+ });
417
+ });
418
+ });
419
+ describe("4 signups, 8 messages, 16 relayed messages", () => {
420
+ after(async () => {
421
+ await (0, utils_1.clean)();
422
+ });
423
+ const users = [new domainobjs_1.Keypair(), new domainobjs_1.Keypair(), new domainobjs_1.Keypair(), new domainobjs_1.Keypair()];
424
+ before(async () => {
425
+ // deploy the smart contracts
426
+ maciAddresses = await prepareTest();
427
+ });
428
+ it("should signup four users", async () => {
429
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
430
+ for (let i = 0; i < users.length; i += 1) {
431
+ // eslint-disable-next-line no-await-in-loop
432
+ await (0, sdk_1.signup)({
433
+ maciAddress: maciAddresses.maciContractAddress,
434
+ maciPublicKey: users[i].publicKey.serialize(),
435
+ sgData: constants_1.DEFAULT_SG_DATA,
436
+ signer,
437
+ });
438
+ }
439
+ });
440
+ it("should join four users", async () => {
441
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
442
+ for (let i = 0; i < users.length; i += 1) {
443
+ // eslint-disable-next-line no-await-in-loop
444
+ await (0, sdk_1.joinPoll)({
445
+ maciAddress: maciAddresses.maciContractAddress,
446
+ privateKey: users[i].privateKey.serialize(),
447
+ stateIndex: BigInt(i + 1),
448
+ pollId: 0n,
449
+ pollJoiningZkey: constants_1.testPollJoiningZkeyPath,
450
+ useWasm: true,
451
+ pollWasm: constants_1.testPollJoiningWasmPath,
452
+ pollWitgen: constants_1.testPollJoiningWitnessPath,
453
+ rapidsnark: constants_1.testRapidsnarkPath,
454
+ sgDataArg: constants_1.DEFAULT_SG_DATA,
455
+ ivcpDataArg: constants_1.DEFAULT_IVCP_DATA,
456
+ signer,
457
+ });
458
+ }
459
+ });
460
+ it("should publish eight messages", async () => {
461
+ await (0, sdk_1.publish)({
462
+ publicKey: users[0].publicKey.serialize(),
463
+ stateIndex: 1n,
464
+ voteOptionIndex: 0n,
465
+ nonce: 2n,
466
+ pollId: 0n,
467
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
468
+ maciAddress: maciAddresses.maciContractAddress,
469
+ salt: (0, crypto_1.generateRandomSalt)(),
470
+ privateKey: users[0].privateKey.serialize(),
471
+ signer,
472
+ });
473
+ await (0, sdk_1.publish)({
474
+ publicKey: users[0].publicKey.serialize(),
475
+ stateIndex: 1n,
476
+ voteOptionIndex: 0n,
477
+ nonce: 2n,
478
+ pollId: 0n,
479
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
480
+ maciAddress: maciAddresses.maciContractAddress,
481
+ salt: (0, crypto_1.generateRandomSalt)(),
482
+ privateKey: users[0].privateKey.serialize(),
483
+ signer,
484
+ });
485
+ await (0, sdk_1.publish)({
486
+ publicKey: users[0].publicKey.serialize(),
487
+ stateIndex: 1n,
488
+ voteOptionIndex: 0n,
489
+ nonce: 1n,
490
+ pollId: 0n,
491
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
492
+ maciAddress: maciAddresses.maciContractAddress,
493
+ salt: (0, crypto_1.generateRandomSalt)(),
494
+ privateKey: users[0].privateKey.serialize(),
495
+ signer,
496
+ });
497
+ await (0, sdk_1.publish)({
498
+ publicKey: users[1].publicKey.serialize(),
499
+ stateIndex: 2n,
500
+ voteOptionIndex: 2n,
501
+ nonce: 1n,
502
+ pollId: 0n,
503
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
504
+ maciAddress: maciAddresses.maciContractAddress,
505
+ salt: (0, crypto_1.generateRandomSalt)(),
506
+ privateKey: users[1].privateKey.serialize(),
507
+ signer,
508
+ });
509
+ await (0, sdk_1.publish)({
510
+ publicKey: users[2].publicKey.serialize(),
511
+ stateIndex: 3n,
512
+ voteOptionIndex: 2n,
513
+ nonce: 1n,
514
+ pollId: 0n,
515
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
516
+ maciAddress: maciAddresses.maciContractAddress,
517
+ salt: (0, crypto_1.generateRandomSalt)(),
518
+ privateKey: users[2].privateKey.serialize(),
519
+ signer,
520
+ });
521
+ await (0, sdk_1.publish)({
522
+ publicKey: users[3].publicKey.serialize(),
523
+ stateIndex: 4n,
524
+ voteOptionIndex: 2n,
525
+ nonce: 3n,
526
+ pollId: 0n,
527
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
528
+ maciAddress: maciAddresses.maciContractAddress,
529
+ salt: (0, crypto_1.generateRandomSalt)(),
530
+ privateKey: users[3].privateKey.serialize(),
531
+ signer,
532
+ });
533
+ await (0, sdk_1.publish)({
534
+ publicKey: users[3].publicKey.serialize(),
535
+ stateIndex: 4n,
536
+ voteOptionIndex: 2n,
537
+ nonce: 2n,
538
+ pollId: 0n,
539
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
540
+ maciAddress: maciAddresses.maciContractAddress,
541
+ salt: (0, crypto_1.generateRandomSalt)(),
542
+ privateKey: users[3].privateKey.serialize(),
543
+ signer,
544
+ });
545
+ await (0, sdk_1.publish)({
546
+ publicKey: users[3].publicKey.serialize(),
547
+ stateIndex: 4n,
548
+ voteOptionIndex: 1n,
549
+ nonce: 1n,
550
+ pollId: 0n,
551
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
552
+ maciAddress: maciAddresses.maciContractAddress,
553
+ salt: (0, crypto_1.generateRandomSalt)(),
554
+ privateKey: users[3].privateKey.serialize(),
555
+ signer,
556
+ });
557
+ });
558
+ it("should relay sixteen messages", async () => {
559
+ const votes = [
560
+ {
561
+ pollId: 0n,
562
+ voteOptionIndex: 1n,
563
+ salt: (0, crypto_1.generateRandomSalt)(),
564
+ nonce: 2n,
565
+ privateKey: users[0].privateKey,
566
+ stateIndex: 1n,
567
+ voteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
568
+ coordinatorPublicKey: constants_1.coordinatorKeypair.publicKey,
569
+ maxVoteOption: BigInt(core_1.VOTE_OPTION_TREE_ARITY ** constants_1.deployPollArgs.voteOptionTreeDepth),
570
+ newPublicKey: users[0].publicKey,
571
+ },
572
+ {
573
+ pollId: 0n,
574
+ voteOptionIndex: 1n,
575
+ salt: (0, crypto_1.generateRandomSalt)(),
576
+ nonce: 2n,
577
+ privateKey: users[0].privateKey,
578
+ stateIndex: 1n,
579
+ voteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
580
+ coordinatorPublicKey: constants_1.coordinatorKeypair.publicKey,
581
+ maxVoteOption: BigInt(core_1.VOTE_OPTION_TREE_ARITY ** constants_1.deployPollArgs.voteOptionTreeDepth),
582
+ newPublicKey: users[0].publicKey,
583
+ },
584
+ {
585
+ pollId: 0n,
586
+ voteOptionIndex: 1n,
587
+ salt: (0, crypto_1.generateRandomSalt)(),
588
+ nonce: 1n,
589
+ privateKey: users[0].privateKey,
590
+ stateIndex: 1n,
591
+ voteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
592
+ coordinatorPublicKey: constants_1.coordinatorKeypair.publicKey,
593
+ maxVoteOption: BigInt(core_1.VOTE_OPTION_TREE_ARITY ** constants_1.deployPollArgs.voteOptionTreeDepth),
594
+ newPublicKey: users[0].publicKey,
595
+ },
596
+ {
597
+ pollId: 0n,
598
+ voteOptionIndex: 1n,
599
+ salt: (0, crypto_1.generateRandomSalt)(),
600
+ nonce: 1n,
601
+ privateKey: users[1].privateKey,
602
+ stateIndex: 2n,
603
+ voteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
604
+ coordinatorPublicKey: constants_1.coordinatorKeypair.publicKey,
605
+ maxVoteOption: BigInt(core_1.VOTE_OPTION_TREE_ARITY ** constants_1.deployPollArgs.voteOptionTreeDepth),
606
+ newPublicKey: users[1].publicKey,
607
+ },
608
+ {
609
+ pollId: 0n,
610
+ voteOptionIndex: 1n,
611
+ salt: (0, crypto_1.generateRandomSalt)(),
612
+ nonce: 1n,
613
+ privateKey: users[2].privateKey,
614
+ stateIndex: 3n,
615
+ voteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
616
+ coordinatorPublicKey: constants_1.coordinatorKeypair.publicKey,
617
+ maxVoteOption: BigInt(core_1.VOTE_OPTION_TREE_ARITY ** constants_1.deployPollArgs.voteOptionTreeDepth),
618
+ newPublicKey: users[2].publicKey,
619
+ },
620
+ {
621
+ pollId: 0n,
622
+ voteOptionIndex: 0n,
623
+ salt: (0, crypto_1.generateRandomSalt)(),
624
+ nonce: 3n,
625
+ privateKey: users[3].privateKey,
626
+ stateIndex: 4n,
627
+ voteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
628
+ coordinatorPublicKey: constants_1.coordinatorKeypair.publicKey,
629
+ maxVoteOption: BigInt(core_1.VOTE_OPTION_TREE_ARITY ** constants_1.deployPollArgs.voteOptionTreeDepth),
630
+ newPublicKey: users[3].publicKey,
631
+ },
632
+ {
633
+ pollId: 0n,
634
+ voteOptionIndex: 0n,
635
+ salt: (0, crypto_1.generateRandomSalt)(),
636
+ nonce: 2n,
637
+ privateKey: users[3].privateKey,
638
+ stateIndex: 4n,
639
+ voteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
640
+ coordinatorPublicKey: constants_1.coordinatorKeypair.publicKey,
641
+ maxVoteOption: BigInt(core_1.VOTE_OPTION_TREE_ARITY ** constants_1.deployPollArgs.voteOptionTreeDepth),
642
+ newPublicKey: users[3].publicKey,
643
+ },
644
+ {
645
+ pollId: 0n,
646
+ voteOptionIndex: 0n,
647
+ salt: (0, crypto_1.generateRandomSalt)(),
648
+ nonce: 1n,
649
+ privateKey: users[3].privateKey,
650
+ stateIndex: 4n,
651
+ voteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
652
+ coordinatorPublicKey: constants_1.coordinatorKeypair.publicKey,
653
+ maxVoteOption: BigInt(core_1.VOTE_OPTION_TREE_ARITY ** constants_1.deployPollArgs.voteOptionTreeDepth),
654
+ newPublicKey: users[3].publicKey,
655
+ },
656
+ ];
657
+ const messages = votes
658
+ .map((vote) => (0, sdk_1.generateVote)(vote))
659
+ .map(({ message, ephemeralKeypair }) => ({
660
+ maciAddress: maciAddresses.maciContractAddress,
661
+ poll: 0,
662
+ data: message.data.map(String),
663
+ publicKey: ephemeralKeypair.publicKey.asArray().map(String),
664
+ hash: message.hash(ephemeralKeypair.publicKey).toString(),
665
+ }));
666
+ await (0, utils_1.relayTestMessages)({ messages, signer, pollId: 0, maciAddress: maciAddresses.maciContractAddress });
667
+ });
668
+ it("should generate zk-SNARK proofs and verify them", async () => {
669
+ const ipfsMessageBackupFiles = await (0, utils_1.getBackupFilenames)();
670
+ await (0, sdk_1.timeTravel)({ seconds: constants_1.pollDuration, signer });
671
+ await (0, sdk_1.mergeSignups)({ ...constants_1.mergeSignupsArgs, maciAddress: maciAddresses.maciContractAddress, signer });
672
+ const { tallyData: tallyFileData } = await (0, sdk_1.generateProofs)({
673
+ ...generateProofsArgs,
674
+ signer,
675
+ maciAddress: maciAddresses.maciContractAddress,
676
+ ipfsMessageBackupFiles,
677
+ });
678
+ await (0, sdk_1.proveOnChain)({ ...constants_1.proveOnChainArgs, maciAddress: maciAddresses.maciContractAddress, signer });
679
+ await (0, sdk_1.verify)({ ...(await (0, constants_1.verifyArgs)(signer)) });
680
+ (0, chai_1.expect)(tallyFileData.totalSpentVoiceCredits.spent.toString()).to.eq((constants_1.DEFAULT_INITIAL_VOICE_CREDITS * users.length).toString());
681
+ (0, chai_1.expect)(tallyFileData.results.tally[0].toString()).to.eq("100");
682
+ (0, chai_1.expect)(tallyFileData.results.tally[1].toString()).to.eq("300");
683
+ tallyFileData.results.tally.forEach((result, index) => {
684
+ if (index > 1) {
685
+ (0, chai_1.expect)(result).to.eq("0");
686
+ }
687
+ });
688
+ });
689
+ });
690
+ describe("30 signups (31 ballots), 30 messages", () => {
691
+ after(async () => {
692
+ await (0, utils_1.clean)();
693
+ });
694
+ const users = Array.from({ length: 30 }, () => new domainobjs_1.Keypair());
695
+ before(async () => {
696
+ // deploy the smart contracts
697
+ maciAddresses = await prepareTest();
698
+ });
699
+ it("should signup thirty users", async () => {
700
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
701
+ for (let i = 0; i < users.length; i += 1) {
702
+ // eslint-disable-next-line no-await-in-loop
703
+ await (0, sdk_1.signup)({
704
+ maciAddress: maciAddresses.maciContractAddress,
705
+ maciPublicKey: users[i].publicKey.serialize(),
706
+ sgData: constants_1.DEFAULT_SG_DATA,
707
+ signer,
708
+ });
709
+ }
710
+ });
711
+ it("should join thirty users", async () => {
712
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
713
+ for (let i = 0; i < users.length; i += 1) {
714
+ // eslint-disable-next-line no-await-in-loop
715
+ await (0, sdk_1.joinPoll)({
716
+ maciAddress: maciAddresses.maciContractAddress,
717
+ privateKey: users[i].privateKey.serialize(),
718
+ stateIndex: BigInt(i + 1),
719
+ pollId: 0n,
720
+ pollJoiningZkey: constants_1.testPollJoiningZkeyPath,
721
+ useWasm: true,
722
+ pollWasm: constants_1.testPollJoiningWasmPath,
723
+ pollWitgen: constants_1.testPollJoiningWitnessPath,
724
+ rapidsnark: constants_1.testRapidsnarkPath,
725
+ sgDataArg: constants_1.DEFAULT_SG_DATA,
726
+ ivcpDataArg: constants_1.DEFAULT_IVCP_DATA,
727
+ signer,
728
+ });
729
+ }
730
+ });
731
+ it("should publish 30 messages", async () => {
732
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
733
+ for (let i = 0; i < users.length; i += 1) {
734
+ // eslint-disable-next-line no-await-in-loop
735
+ await (0, sdk_1.publish)({
736
+ maciAddress: maciAddresses.maciContractAddress,
737
+ publicKey: users[i].publicKey.serialize(),
738
+ stateIndex: BigInt(i + 1),
739
+ voteOptionIndex: 0n,
740
+ nonce: 1n,
741
+ pollId: 0n,
742
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
743
+ salt: (0, crypto_1.generateRandomSalt)(),
744
+ privateKey: users[i].privateKey.serialize(),
745
+ signer,
746
+ });
747
+ }
748
+ });
749
+ it("should generate zk-SNARK proofs and verify them", async () => {
750
+ await (0, sdk_1.timeTravel)({ seconds: constants_1.pollDuration, signer });
751
+ await (0, sdk_1.mergeSignups)({ ...constants_1.mergeSignupsArgs, maciAddress: maciAddresses.maciContractAddress, signer });
752
+ const { tallyData: tallyFileData } = await (0, sdk_1.generateProofs)({
753
+ ...generateProofsArgs,
754
+ maciAddress: maciAddresses.maciContractAddress,
755
+ signer,
756
+ });
757
+ await (0, sdk_1.proveOnChain)({ ...constants_1.proveOnChainArgs, maciAddress: maciAddresses.maciContractAddress, signer });
758
+ await (0, sdk_1.verify)({ ...(await (0, constants_1.verifyArgs)(signer)), tallyData: tallyFileData });
759
+ const totalSpent = (constants_1.DEFAULT_INITIAL_VOICE_CREDITS * users.length).toString();
760
+ (0, chai_1.expect)(tallyFileData.totalSpentVoiceCredits.spent.toString()).to.eq(totalSpent);
761
+ (0, chai_1.expect)(tallyFileData.results.tally[0].toString()).to.eq(totalSpent);
762
+ });
763
+ });
764
+ describe("30 signups, 20 invalid and 1 valid messages", () => {
765
+ after(async () => {
766
+ await (0, utils_1.clean)();
767
+ });
768
+ const users = Array.from({ length: 30 }, () => new domainobjs_1.Keypair());
769
+ before(async () => {
770
+ // deploy the smart contracts
771
+ maciAddresses = await prepareTest();
772
+ });
773
+ it("should signup thirty users", async () => {
774
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
775
+ for (let i = 0; i < users.length; i += 1) {
776
+ // eslint-disable-next-line no-await-in-loop
777
+ await (0, sdk_1.signup)({
778
+ maciAddress: maciAddresses.maciContractAddress,
779
+ maciPublicKey: users[i].publicKey.serialize(),
780
+ sgData: constants_1.DEFAULT_SG_DATA,
781
+ signer,
782
+ });
783
+ }
784
+ });
785
+ it("should join thirty users", async () => {
786
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
787
+ for (let i = 0; i < users.length; i += 1) {
788
+ // eslint-disable-next-line no-await-in-loop
789
+ await (0, sdk_1.joinPoll)({
790
+ maciAddress: maciAddresses.maciContractAddress,
791
+ privateKey: users[i].privateKey.serialize(),
792
+ stateIndex: BigInt(i + 1),
793
+ pollId: 0n,
794
+ pollJoiningZkey: constants_1.testPollJoiningZkeyPath,
795
+ useWasm: true,
796
+ pollWasm: constants_1.testPollJoiningWasmPath,
797
+ pollWitgen: constants_1.testPollJoiningWitnessPath,
798
+ rapidsnark: constants_1.testRapidsnarkPath,
799
+ sgDataArg: constants_1.DEFAULT_SG_DATA,
800
+ ivcpDataArg: constants_1.DEFAULT_IVCP_DATA,
801
+ signer,
802
+ });
803
+ }
804
+ });
805
+ it("should publish 30 invalid and 1 valid messages", async () => {
806
+ // eslint-disable-next-line @typescript-eslint/prefer-for-of
807
+ for (let i = 0; i < users.length; i += 1) {
808
+ // eslint-disable-next-line no-await-in-loop
809
+ await (0, sdk_1.publish)({
810
+ maciAddress: maciAddresses.maciContractAddress,
811
+ publicKey: users[i].publicKey.serialize(),
812
+ stateIndex: BigInt(i + 1),
813
+ voteOptionIndex: 0n,
814
+ nonce: 1n,
815
+ pollId: 0n,
816
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS + 10),
817
+ salt: (0, crypto_1.generateRandomSalt)(),
818
+ privateKey: users[i].privateKey.serialize(),
819
+ signer,
820
+ });
821
+ }
822
+ await (0, sdk_1.publish)({
823
+ maciAddress: maciAddresses.maciContractAddress,
824
+ publicKey: users[0].publicKey.serialize(),
825
+ stateIndex: 1n,
826
+ voteOptionIndex: 0n,
827
+ nonce: 1n,
828
+ pollId: 0n,
829
+ newVoteWeight: BigInt(constants_1.DEFAULT_INITIAL_VOICE_CREDITS),
830
+ salt: (0, crypto_1.generateRandomSalt)(),
831
+ privateKey: users[0].privateKey.serialize(),
832
+ signer,
833
+ });
834
+ });
835
+ it("should generate zk-SNARK proofs and verify them", async () => {
836
+ await (0, sdk_1.timeTravel)({ seconds: constants_1.pollDuration, signer });
837
+ await (0, sdk_1.mergeSignups)({ ...constants_1.mergeSignupsArgs, maciAddress: maciAddresses.maciContractAddress, signer });
838
+ const { tallyData: tallyFileData } = await (0, sdk_1.generateProofs)({
839
+ ...generateProofsArgs,
840
+ maciAddress: maciAddresses.maciContractAddress,
841
+ signer,
842
+ });
843
+ await (0, sdk_1.proveOnChain)({ ...constants_1.proveOnChainArgs, maciAddress: maciAddresses.maciContractAddress, signer });
844
+ await (0, sdk_1.verify)({ ...(await (0, constants_1.verifyArgs)(signer)), tallyData: tallyFileData });
845
+ const totalSpent = constants_1.DEFAULT_INITIAL_VOICE_CREDITS.toString();
846
+ (0, chai_1.expect)(tallyFileData.totalSpentVoiceCredits.spent.toString()).to.eq(totalSpent);
847
+ (0, chai_1.expect)(tallyFileData.results.tally[0].toString()).to.eq(totalSpent);
848
+ });
849
+ });
850
+ });
851
+ //# sourceMappingURL=e2e.full.test.js.map