Skip to content

Commit

Permalink
fix(isregistered): use a block range to fetch events
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed May 28, 2024
1 parent 9daa5e2 commit d59959f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
1 change: 1 addition & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"test:genKeypair": "ts-mocha --exit tests/unit/genKeyPair.test.ts",
"test:timeTravel": "ts-mocha --exit tests/unit/timeTravel.test.ts",
"test:fundWallet": "ts-mocha --exit tests/unit/fundWallet.test.ts",
"test:signup": "ts-mocha --exit tests/unit/signup.test.ts",
"test:utils": "ts-mocha --exit tests/unit/utils.test.ts",
"docs": "typedoc --plugin typedoc-plugin-markdown --options ./typedoc.json"
},
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);
});
});
29 changes: 23 additions & 6 deletions cli/ts/commands/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,29 @@ export const isRegisteredUser = async ({
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;
// if no start block is provided we start from 0
const startBlockNumber = startBlock || 0;
const currentBlock = await signer.provider!.getBlockNumber();

let event;
// 1000 blocks at a time
for (let block = startBlockNumber; 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.x, publicKey.y),
block,
toBlock,
);

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

const stateIndex = event?.args[0].toString();
const voiceCredits = event?.args[3].toString();

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

Expand Down

0 comments on commit d59959f

Please sign in to comment.