Skip to content

Commit 6319120

Browse files
committed
fix(isregistereduser): fetch events in batches
1 parent 0c18913 commit 6319120

26 files changed

+286
-70
lines changed

Diff for: CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)
7+
8+
9+
### Bug Fixes
10+
11+
* **isregistereduser:** fetch events in batches ([1e71fa6](https://github.com/privacy-scaling-explorations/maci/commit/1e71fa65ecf2b79a504541ea7e1392eebd8119c4))
12+
13+
14+
615
## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)
716

817
**Note:** Version bump only for package maci

Diff for: circuits/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)
7+
8+
**Note:** Version bump only for package maci-circuits
9+
10+
11+
12+
13+
614
## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)
715

816
**Note:** Version bump only for package maci-circuits

Diff for: circuits/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "maci-circuits",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "zk-SNARK circuits for MACI",
55
"main": "build/ts/index.js",
66
"files": [
@@ -38,9 +38,9 @@
3838
"@zk-kit/circuits": "^0.4.0",
3939
"circomkit": "^0.1.0",
4040
"circomlib": "^2.0.5",
41-
"maci-core": "^1.2.2",
42-
"maci-crypto": "^1.2.2",
43-
"maci-domainobjs": "^1.2.2",
41+
"maci-core": "^1.2.3",
42+
"maci-crypto": "^1.2.3",
43+
"maci-domainobjs": "^1.2.3",
4444
"snarkjs": "^0.7.4"
4545
},
4646
"devDependencies": {

Diff for: cli/CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)
7+
8+
9+
### Bug Fixes
10+
11+
* **isregistereduser:** fetch events in batches ([1e71fa6](https://github.com/privacy-scaling-explorations/maci/commit/1e71fa65ecf2b79a504541ea7e1392eebd8119c4))
12+
13+
14+
615
## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)
716

817
**Note:** Version bump only for package maci-cli

Diff for: cli/package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "maci-cli",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "CLI utilities for MACI",
55
"main": "build/ts/index.js",
66
"exports": {
@@ -38,6 +38,7 @@
3838
"test:timeTravel": "ts-mocha --exit tests/unit/timeTravel.test.ts",
3939
"test:fundWallet": "ts-mocha --exit tests/unit/fundWallet.test.ts",
4040
"test:utils": "ts-mocha --exit tests/unit/utils.test.ts",
41+
"test:signup": "ts-mocha --exit tests/unit/signup.test.ts",
4142
"docs": "typedoc --plugin typedoc-plugin-markdown --options ./typedoc.json"
4243
},
4344
"dependencies": {
@@ -47,11 +48,11 @@
4748
"dotenv": "^16.4.5",
4849
"ethers": "^6.12.0",
4950
"hardhat": "^2.22.3",
50-
"maci-circuits": "^1.2.2",
51-
"maci-contracts": "^1.2.2",
52-
"maci-core": "^1.2.2",
53-
"maci-crypto": "^1.2.2",
54-
"maci-domainobjs": "^1.2.2",
51+
"maci-circuits": "^1.2.3",
52+
"maci-contracts": "^1.2.3",
53+
"maci-core": "^1.2.3",
54+
"maci-crypto": "^1.2.3",
55+
"maci-domainobjs": "^1.2.3",
5556
"prompt": "^1.3.0"
5657
},
5758
"devDependencies": {

Diff for: cli/tests/unit/signup.test.ts

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { expect } from "chai";
2+
import { Signer } from "ethers";
3+
import { getDefaultSigner } from "maci-contracts";
4+
import { Keypair } from "maci-domainobjs";
5+
6+
import {
7+
deploy,
8+
DeployedContracts,
9+
deployVkRegistryContract,
10+
isRegisteredUser,
11+
setVerifyingKeys,
12+
signup,
13+
} from "../../ts";
14+
import { deployArgs, setVerifyingKeysArgs } from "../constants";
15+
16+
describe("signup", () => {
17+
let signer: Signer;
18+
let maciAddresses: DeployedContracts;
19+
const user = new Keypair();
20+
// before all tests we deploy the vk registry contract and set the verifying keys
21+
before(async () => {
22+
signer = await getDefaultSigner();
23+
24+
// we deploy the vk registry contract
25+
await deployVkRegistryContract({ signer });
26+
// we set the verifying keys
27+
await setVerifyingKeys({ ...setVerifyingKeysArgs, signer });
28+
// deploy the smart contracts
29+
maciAddresses = await deploy({ ...deployArgs, signer });
30+
});
31+
32+
it("should allow to signup and return the user data", async () => {
33+
const signUpData = await signup({
34+
maciAddress: maciAddresses.maciAddress,
35+
maciPubKey: user.pubKey.serialize(),
36+
signer,
37+
});
38+
39+
const registeredUserData = await isRegisteredUser({
40+
maciAddress: maciAddresses.maciAddress,
41+
startBlock: await signer.provider?.getBlockNumber(),
42+
maciPubKey: user.pubKey.serialize(),
43+
signer,
44+
});
45+
46+
expect(registeredUserData.isRegistered).to.eq(true);
47+
expect(registeredUserData.stateIndex).to.eq(signUpData.stateIndex);
48+
});
49+
50+
it("should not get the user data if the user is not registered", async () => {
51+
const registeredUserData = await isRegisteredUser({
52+
maciAddress: maciAddresses.maciAddress,
53+
startBlock: await signer.provider?.getBlockNumber(),
54+
maciPubKey: new Keypair().pubKey.serialize(),
55+
signer,
56+
});
57+
58+
expect(registeredUserData.isRegistered).to.eq(false);
59+
});
60+
61+
it("should start fetchig from block zero if the start block is not provided", async () => {
62+
const registeredUserData = await isRegisteredUser({
63+
maciAddress: maciAddresses.maciAddress,
64+
maciPubKey: user.pubKey.serialize(),
65+
signer,
66+
});
67+
68+
expect(registeredUserData.isRegistered).to.eq(true);
69+
});
70+
});

Diff for: cli/ts/commands/signup.ts

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MACI__factory as MACIFactory } from "maci-contracts/typechain-types";
22
import { PubKey } from "maci-domainobjs";
33

4-
import type { IRegisteredUserArgs, ISignupData, SignupArgs } from "../utils/interfaces";
4+
import type { IParseSignupEventsArgs, IRegisteredUserArgs, ISignupData, SignupArgs } from "../utils/interfaces";
55
import type { ContractTransactionReceipt } from "ethers";
66

77
import { banner } from "../utils/banner";
@@ -86,6 +86,36 @@ export const signup = async ({
8686
};
8787
};
8888

89+
/**
90+
* Parse the signup events from the MACI contract
91+
*/
92+
const parseSignupEvents = async ({ maciContract, startBlock, currentBlock, publicKey }: IParseSignupEventsArgs) => {
93+
// 1000 blocks at a time
94+
for (let block = startBlock; block <= currentBlock; block += 1000) {
95+
const toBlock = Math.min(block + 999, currentBlock);
96+
// eslint-disable-next-line no-await-in-loop
97+
const newEvents = await maciContract.queryFilter(
98+
maciContract.filters.SignUp(undefined, publicKey.rawPubKey[0], publicKey.rawPubKey[1]),
99+
block,
100+
toBlock,
101+
);
102+
103+
if (newEvents.length > 0) {
104+
const [event] = newEvents;
105+
106+
return {
107+
stateIndex: event.args[0].toString(),
108+
voiceCredits: event.args[3].toString(),
109+
};
110+
}
111+
}
112+
113+
return {
114+
stateIndex: undefined,
115+
voiceCredits: undefined,
116+
};
117+
};
118+
89119
/**
90120
* Checks if user is registered with public key
91121
* @param IRegisteredArgs - The arguments for the register check command
@@ -101,14 +131,16 @@ export const isRegisteredUser = async ({
101131
banner(quiet);
102132

103133
const maciContract = MACIFactory.connect(maciAddress, signer);
104-
const publicKey = PubKey.deserialize(maciPubKey).asContractParam();
105-
106-
const events = await maciContract.queryFilter(
107-
maciContract.filters.SignUp(undefined, publicKey.x, publicKey.y),
108-
startBlock,
109-
);
110-
const stateIndex = events[0]?.args[0].toString() as string | undefined;
111-
const voiceCredits = events[0]?.args[3].toString() as string | undefined;
134+
const publicKey = PubKey.deserialize(maciPubKey);
135+
const startBlockNumber = startBlock || 0;
136+
const currentBlock = await signer.provider!.getBlockNumber();
137+
138+
const { stateIndex, voiceCredits } = await parseSignupEvents({
139+
maciContract,
140+
startBlock: startBlockNumber,
141+
currentBlock,
142+
publicKey,
143+
});
112144

113145
logGreen(quiet, success(`State index: ${stateIndex?.toString()}, registered: ${stateIndex !== undefined}`));
114146

Diff for: cli/ts/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export type {
4545
IPublishBatchArgs,
4646
IPublishBatchData,
4747
IPublishMessage,
48+
IParseSignupEventsArgs,
4849
} from "./interfaces";
4950
export { compareVks } from "./vks";
5051
export { delay } from "./time";

Diff for: cli/ts/utils/interfaces.ts

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { MACI } from "maci-contracts/typechain-types";
2+
import { PubKey } from "maci-domainobjs";
3+
14
import type { Provider, Signer } from "ethers";
25
import type { SnarkProof } from "maci-contracts";
36
import type { CircuitInputs } from "maci-core";
@@ -1134,3 +1137,28 @@ export interface DeployVkRegistryArgs {
11341137
*/
11351138
quiet?: boolean;
11361139
}
1140+
1141+
/**
1142+
* Interface for the arguments to the parseSignupEvents function
1143+
*/
1144+
export interface IParseSignupEventsArgs {
1145+
/**
1146+
* The MACI contract
1147+
*/
1148+
maciContract: MACI;
1149+
1150+
/**
1151+
* The start block
1152+
*/
1153+
startBlock: number;
1154+
1155+
/**
1156+
* The current block
1157+
*/
1158+
currentBlock: number;
1159+
1160+
/**
1161+
* The public key
1162+
*/
1163+
publicKey: PubKey;
1164+
}

Diff for: contracts/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)
7+
8+
**Note:** Version bump only for package maci-contracts
9+
10+
11+
12+
13+
614
## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)
715

816
**Note:** Version bump only for package maci-contracts

Diff for: contracts/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "maci-contracts",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Solidity Smart Contracts for MACI (Minimal Anti-Collusion Infrastructure)",
55
"main": "build/ts/index.js",
66
"exports": {
@@ -77,10 +77,10 @@
7777
"ethers": "^6.12.0",
7878
"hardhat": "^2.22.3",
7979
"lowdb": "^1.0.0",
80-
"maci-circuits": "^1.2.2",
81-
"maci-core": "^1.2.2",
82-
"maci-crypto": "^1.2.2",
83-
"maci-domainobjs": "^1.2.2",
80+
"maci-circuits": "^1.2.3",
81+
"maci-core": "^1.2.3",
82+
"maci-crypto": "^1.2.3",
83+
"maci-domainobjs": "^1.2.3",
8484
"solidity-docgen": "^0.6.0-beta.36"
8585
},
8686
"devDependencies": {

Diff for: coordinator/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)
7+
8+
**Note:** Version bump only for package maci-coordinator
9+
10+
11+
12+
13+
614
## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)
715

816
**Note:** Version bump only for package maci-coordinator

Diff for: coordinator/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "maci-coordinator",
3-
"version": "0.1.0",
3+
"version": "1.2.3",
44
"private": true,
55
"description": "Coordinator service for MACI",
66
"main": "build/ts/main.js",
@@ -28,10 +28,10 @@
2828
"ethers": "^6.12.0",
2929
"hardhat": "^2.22.3",
3030
"helmet": "^7.1.0",
31-
"maci-circuits": "^1.2.2",
32-
"maci-cli": "^1.2.2",
33-
"maci-contracts": "^1.2.2",
34-
"maci-domainobjs": "^1.2.2",
31+
"maci-circuits": "^1.2.3",
32+
"maci-cli": "^1.2.3",
33+
"maci-contracts": "^1.2.3",
34+
"maci-domainobjs": "^1.2.3",
3535
"reflect-metadata": "^0.2.0",
3636
"rxjs": "^7.8.1"
3737
},

Diff for: core/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)
7+
8+
**Note:** Version bump only for package maci-core
9+
10+
11+
12+
13+
614
## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)
715

816
**Note:** Version bump only for package maci-core

Diff for: core/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "maci-core",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "",
55
"main": "build/ts/index.js",
66
"types": "build/ts/index.d.ts",
@@ -22,8 +22,8 @@
2222
"docs": "typedoc --plugin typedoc-plugin-markdown --options ./typedoc.json"
2323
},
2424
"dependencies": {
25-
"maci-crypto": "^1.2.2",
26-
"maci-domainobjs": "^1.2.2"
25+
"maci-crypto": "^1.2.3",
26+
"maci-domainobjs": "^1.2.3"
2727
},
2828
"devDependencies": {
2929
"@types/chai": "^4.3.11",

0 commit comments

Comments
 (0)