Skip to content

Commit

Permalink
fix(isregistereduser): fetch events in batches
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed May 28, 2024
1 parent 0c18913 commit 6319120
Show file tree
Hide file tree
Showing 26 changed files with 286 additions and 70 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)


### Bug Fixes

* **isregistereduser:** fetch events in batches ([1e71fa6](https://github.com/privacy-scaling-explorations/maci/commit/1e71fa65ecf2b79a504541ea7e1392eebd8119c4))



## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)

**Note:** Version bump only for package maci
Expand Down
8 changes: 8 additions & 0 deletions circuits/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)

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





## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)

**Note:** Version bump only for package maci-circuits
Expand Down
8 changes: 4 additions & 4 deletions circuits/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maci-circuits",
"version": "1.2.2",
"version": "1.2.3",
"description": "zk-SNARK circuits for MACI",
"main": "build/ts/index.js",
"files": [
Expand Down Expand Up @@ -38,9 +38,9 @@
"@zk-kit/circuits": "^0.4.0",
"circomkit": "^0.1.0",
"circomlib": "^2.0.5",
"maci-core": "^1.2.2",
"maci-crypto": "^1.2.2",
"maci-domainobjs": "^1.2.2",
"maci-core": "^1.2.3",
"maci-crypto": "^1.2.3",
"maci-domainobjs": "^1.2.3",
"snarkjs": "^0.7.4"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)


### Bug Fixes

* **isregistereduser:** fetch events in batches ([1e71fa6](https://github.com/privacy-scaling-explorations/maci/commit/1e71fa65ecf2b79a504541ea7e1392eebd8119c4))



## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)

**Note:** Version bump only for package maci-cli
Expand Down
13 changes: 7 additions & 6 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maci-cli",
"version": "1.2.2",
"version": "1.2.3",
"description": "CLI utilities for MACI",
"main": "build/ts/index.js",
"exports": {
Expand Down Expand Up @@ -38,6 +38,7 @@
"test:timeTravel": "ts-mocha --exit tests/unit/timeTravel.test.ts",
"test:fundWallet": "ts-mocha --exit tests/unit/fundWallet.test.ts",
"test:utils": "ts-mocha --exit tests/unit/utils.test.ts",
"test:signup": "ts-mocha --exit tests/unit/signup.test.ts",
"docs": "typedoc --plugin typedoc-plugin-markdown --options ./typedoc.json"
},
"dependencies": {
Expand All @@ -47,11 +48,11 @@
"dotenv": "^16.4.5",
"ethers": "^6.12.0",
"hardhat": "^2.22.3",
"maci-circuits": "^1.2.2",
"maci-contracts": "^1.2.2",
"maci-core": "^1.2.2",
"maci-crypto": "^1.2.2",
"maci-domainobjs": "^1.2.2",
"maci-circuits": "^1.2.3",
"maci-contracts": "^1.2.3",
"maci-core": "^1.2.3",
"maci-crypto": "^1.2.3",
"maci-domainobjs": "^1.2.3",
"prompt": "^1.3.0"
},
"devDependencies": {
Expand Down
70 changes: 70 additions & 0 deletions cli/tests/unit/signup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { expect } from "chai";
import { Signer } from "ethers";
import { getDefaultSigner } from "maci-contracts";
import { Keypair } from "maci-domainobjs";

import {
deploy,
DeployedContracts,
deployVkRegistryContract,
isRegisteredUser,
setVerifyingKeys,
signup,
} from "../../ts";
import { deployArgs, setVerifyingKeysArgs } from "../constants";

describe("signup", () => {
let signer: Signer;
let maciAddresses: DeployedContracts;
const user = new Keypair();
// before all tests we deploy the vk registry contract and set the verifying keys
before(async () => {
signer = await getDefaultSigner();

// we deploy the vk registry contract
await deployVkRegistryContract({ signer });
// we set the verifying keys
await setVerifyingKeys({ ...setVerifyingKeysArgs, signer });
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });
});

it("should allow to signup and return the user data", async () => {
const signUpData = await signup({
maciAddress: maciAddresses.maciAddress,
maciPubKey: user.pubKey.serialize(),
signer,
});

const registeredUserData = await isRegisteredUser({
maciAddress: maciAddresses.maciAddress,
startBlock: await signer.provider?.getBlockNumber(),
maciPubKey: user.pubKey.serialize(),
signer,
});

expect(registeredUserData.isRegistered).to.eq(true);
expect(registeredUserData.stateIndex).to.eq(signUpData.stateIndex);
});

it("should not get the user data if the user is not registered", async () => {
const registeredUserData = await isRegisteredUser({
maciAddress: maciAddresses.maciAddress,
startBlock: await signer.provider?.getBlockNumber(),
maciPubKey: new Keypair().pubKey.serialize(),
signer,
});

expect(registeredUserData.isRegistered).to.eq(false);
});

it("should start fetchig from block zero if the start block is not provided", async () => {
const registeredUserData = await isRegisteredUser({
maciAddress: maciAddresses.maciAddress,
maciPubKey: user.pubKey.serialize(),
signer,
});

expect(registeredUserData.isRegistered).to.eq(true);
});
});
50 changes: 41 additions & 9 deletions cli/ts/commands/signup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MACI__factory as MACIFactory } from "maci-contracts/typechain-types";
import { PubKey } from "maci-domainobjs";

import type { IRegisteredUserArgs, ISignupData, SignupArgs } from "../utils/interfaces";
import type { IParseSignupEventsArgs, IRegisteredUserArgs, ISignupData, SignupArgs } from "../utils/interfaces";
import type { ContractTransactionReceipt } from "ethers";

import { banner } from "../utils/banner";
Expand Down Expand Up @@ -86,6 +86,36 @@ export const signup = async ({
};
};

/**
* Parse the signup events from the MACI contract
*/
const parseSignupEvents = async ({ maciContract, startBlock, currentBlock, publicKey }: IParseSignupEventsArgs) => {
// 1000 blocks at a time
for (let block = startBlock; block <= currentBlock; block += 1000) {
const toBlock = Math.min(block + 999, currentBlock);
// eslint-disable-next-line no-await-in-loop
const newEvents = await maciContract.queryFilter(
maciContract.filters.SignUp(undefined, publicKey.rawPubKey[0], publicKey.rawPubKey[1]),
block,
toBlock,
);

if (newEvents.length > 0) {
const [event] = newEvents;

return {
stateIndex: event.args[0].toString(),
voiceCredits: event.args[3].toString(),
};
}
}

return {
stateIndex: undefined,
voiceCredits: undefined,
};
};

/**
* Checks if user is registered with public key
* @param IRegisteredArgs - The arguments for the register check command
Expand All @@ -101,14 +131,16 @@ export const isRegisteredUser = async ({
banner(quiet);

const maciContract = MACIFactory.connect(maciAddress, signer);
const publicKey = PubKey.deserialize(maciPubKey).asContractParam();

const events = await maciContract.queryFilter(
maciContract.filters.SignUp(undefined, publicKey.x, publicKey.y),
startBlock,
);
const stateIndex = events[0]?.args[0].toString() as string | undefined;
const voiceCredits = events[0]?.args[3].toString() as string | undefined;
const publicKey = PubKey.deserialize(maciPubKey);
const startBlockNumber = startBlock || 0;
const currentBlock = await signer.provider!.getBlockNumber();

const { stateIndex, voiceCredits } = await parseSignupEvents({
maciContract,
startBlock: startBlockNumber,
currentBlock,
publicKey,
});

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

Expand Down
1 change: 1 addition & 0 deletions cli/ts/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type {
IPublishBatchArgs,
IPublishBatchData,
IPublishMessage,
IParseSignupEventsArgs,
} from "./interfaces";
export { compareVks } from "./vks";
export { delay } from "./time";
Expand Down
28 changes: 28 additions & 0 deletions cli/ts/utils/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { MACI } from "maci-contracts/typechain-types";
import { PubKey } from "maci-domainobjs";

import type { Provider, Signer } from "ethers";
import type { SnarkProof } from "maci-contracts";
import type { CircuitInputs } from "maci-core";
Expand Down Expand Up @@ -1134,3 +1137,28 @@ export interface DeployVkRegistryArgs {
*/
quiet?: boolean;
}

/**
* Interface for the arguments to the parseSignupEvents function
*/
export interface IParseSignupEventsArgs {
/**
* The MACI contract
*/
maciContract: MACI;

/**
* The start block
*/
startBlock: number;

/**
* The current block
*/
currentBlock: number;

/**
* The public key
*/
publicKey: PubKey;
}
8 changes: 8 additions & 0 deletions contracts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)

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





## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)

**Note:** Version bump only for package maci-contracts
Expand Down
10 changes: 5 additions & 5 deletions contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maci-contracts",
"version": "1.2.2",
"version": "1.2.3",
"description": "Solidity Smart Contracts for MACI (Minimal Anti-Collusion Infrastructure)",
"main": "build/ts/index.js",
"exports": {
Expand Down Expand Up @@ -77,10 +77,10 @@
"ethers": "^6.12.0",
"hardhat": "^2.22.3",
"lowdb": "^1.0.0",
"maci-circuits": "^1.2.2",
"maci-core": "^1.2.2",
"maci-crypto": "^1.2.2",
"maci-domainobjs": "^1.2.2",
"maci-circuits": "^1.2.3",
"maci-core": "^1.2.3",
"maci-crypto": "^1.2.3",
"maci-domainobjs": "^1.2.3",
"solidity-docgen": "^0.6.0-beta.36"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions coordinator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)

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





## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)

**Note:** Version bump only for package maci-coordinator
Expand Down
10 changes: 5 additions & 5 deletions coordinator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maci-coordinator",
"version": "0.1.0",
"version": "1.2.3",
"private": true,
"description": "Coordinator service for MACI",
"main": "build/ts/main.js",
Expand Down Expand Up @@ -28,10 +28,10 @@
"ethers": "^6.12.0",
"hardhat": "^2.22.3",
"helmet": "^7.1.0",
"maci-circuits": "^1.2.2",
"maci-cli": "^1.2.2",
"maci-contracts": "^1.2.2",
"maci-domainobjs": "^1.2.2",
"maci-circuits": "^1.2.3",
"maci-cli": "^1.2.3",
"maci-contracts": "^1.2.3",
"maci-domainobjs": "^1.2.3",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
},
Expand Down
8 changes: 8 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.2.3](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.2...v1.2.3) (2024-05-28)

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





## [1.2.2](https://github.com/privacy-scaling-explorations/maci/compare/v1.2.1...v1.2.2) (2024-05-15)

**Note:** Version bump only for package maci-core
Expand Down
6 changes: 3 additions & 3 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maci-core",
"version": "1.2.2",
"version": "1.2.3",
"description": "",
"main": "build/ts/index.js",
"types": "build/ts/index.d.ts",
Expand All @@ -22,8 +22,8 @@
"docs": "typedoc --plugin typedoc-plugin-markdown --options ./typedoc.json"
},
"dependencies": {
"maci-crypto": "^1.2.2",
"maci-domainobjs": "^1.2.2"
"maci-crypto": "^1.2.3",
"maci-domainobjs": "^1.2.3"
},
"devDependencies": {
"@types/chai": "^4.3.11",
Expand Down
Loading

0 comments on commit 6319120

Please sign in to comment.