Skip to content

Commit

Permalink
Merge pull request #2113 from privacy-scaling-explorations/feat/poll-…
Browse files Browse the repository at this point in the history
…start-date

feat: add start and end timestmap for poll
  • Loading branch information
0xmad authored Feb 7, 2025
2 parents 4886574 + cf67b53 commit 2d4c13a
Show file tree
Hide file tree
Showing 43 changed files with 387 additions and 192 deletions.
5 changes: 4 additions & 1 deletion apps/relayer/tests/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ export class TestDeploy {

const maciAddresses = await deploy({ stateTreeDepth: 10, signer });

const startDate = (await signer.provider.getBlock("latest"))?.timestamp ?? Math.floor(Date.now() / 1000);

await deployPoll({
pollDuration: 30,
pollStartDate: startDate,
pollEndDate: startDate + 30,
intStateTreeDepth: INT_STATE_TREE_DEPTH,
messageBatchSize: MESSAGE_BATCH_SIZE,
voteOptionTreeDepth: VOTE_OPTION_TREE_DEPTH,
Expand Down
2 changes: 2 additions & 0 deletions apps/subgraph/schemas/schema.v1.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Account @entity {
type Poll @entity {
id: Bytes! # poll address
pollId: BigInt! # uint256
startDate: BigInt! # uint256
endDate: BigInt! # uint256
duration: BigInt! # uint256
treeDepth: BigInt! # uint8
maxVoteOptions: BigInt!
Expand Down
7 changes: 5 additions & 2 deletions apps/subgraph/src/maci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ export function handleDeployPoll(event: DeployPollEvent): void {
const pollContract = PollContract.bind(contracts.poll);
const maxVoteOptions = pollContract.maxVoteOptions();
const treeDepths = pollContract.treeDepths();
const durations = pollContract.getDeployTimeAndDuration();
const durations = pollContract.getStartAndEndDate();
const duration = durations.value1.minus(durations.value0);

poll.pollId = event.params._pollId;
poll.messageProcessor = contracts.messageProcessor;
poll.tally = contracts.tally;
poll.maxVoteOptions = maxVoteOptions;
poll.treeDepth = GraphBN.fromI32(treeDepths.value0);
poll.duration = durations.value1;
poll.duration = duration;
poll.startDate = durations.value0;
poll.endDate = durations.value1;
poll.mode = GraphBN.fromI32(event.params._mode);

poll.createdAt = event.block.timestamp;
Expand Down
5 changes: 5 additions & 0 deletions apps/subgraph/tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export function mockPollContract(): void {
"getDeployTimeAndDuration():(uint256,uint256)",
).returns([ethereum.Value.fromI32(30), ethereum.Value.fromI32(40)]);

createMockedFunction(DEFAULT_POLL_ADDRESS, "getStartAndEndDate", "getStartAndEndDate():(uint256,uint256)").returns([
ethereum.Value.fromI32(30),
ethereum.Value.fromI32(40),
]);

createMockedFunction(
DEFAULT_POLL_ADDRESS,
"hashMessageAndEncPubKey",
Expand Down
4 changes: 4 additions & 0 deletions packages/circuits/ts/__tests__/ProcessMessages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { MaciState, Poll, STATE_TREE_ARITY } from "maci-core";
import { IncrementalQuinTree, hash2, poseidon } from "maci-crypto";
import { PrivKey, Keypair, PCommand, Message, Ballot, PubKey } from "maci-domainobjs";

import fs from "fs";

import { IProcessMessagesInputs } from "../types";

import { STATE_TREE_DEPTH, duration, messageBatchSize, treeDepths, voiceCreditBalance } from "./utils/constants";
Expand Down Expand Up @@ -138,6 +140,8 @@ describe("ProcessMessage circuit", function test() {
it("should produce a proof", async () => {
const inputs = poll.processMessages(pollId) as unknown as IProcessMessagesInputs;

fs.writeFileSync("inputs.json", JSON.stringify(inputs, null, 2));

// Calculate the witness
const witness = await circuit.calculateWitness(inputs);
await circuit.expectConstraintPass(witness);
Expand Down
47 changes: 40 additions & 7 deletions packages/cli/tests/ceremony-params/ceremonyParams.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { genRandomSalt } from "maci-crypto";
import { Keypair } from "maci-domainobjs";
import { getDefaultSigner } from "maci-sdk";
import { getBlockTimestamp, getDefaultSigner } from "maci-sdk";

import type { Signer } from "ethers";

Expand Down Expand Up @@ -81,8 +81,7 @@ describe("Stress tests with ceremony params (6,3,2,20)", function test() {
stateTreeDepth,
};

const deployPollArgs: Omit<DeployPollArgs, "signer"> = {
pollDuration,
const deployPollArgs: Omit<DeployPollArgs, "signer" | "pollStartDate" | "pollEndDate"> = {
intStateTreeDepth,
messageBatchSize,
voteOptionTreeDepth,
Expand Down Expand Up @@ -129,8 +128,16 @@ describe("Stress tests with ceremony params (6,3,2,20)", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...ceremonyDeployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({
...deployPollArgs,
signer,
pollStartDate: startDate,
pollEndDate: startDate + pollDuration,
});
});

it("should signup 1 user", async () => {
Expand Down Expand Up @@ -175,8 +182,16 @@ describe("Stress tests with ceremony params (6,3,2,20)", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...ceremonyDeployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({
...deployPollArgs,
signer,
pollStartDate: startDate,
pollEndDate: startDate + pollDuration,
});
});

it("should signup 25 users", async () => {
Expand Down Expand Up @@ -255,8 +270,17 @@ describe("Stress tests with ceremony params (6,3,2,20)", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...ceremonyDeployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer, useQuadraticVoting: false });
await deployPoll({
...deployPollArgs,
signer,
useQuadraticVoting: false,
pollStartDate: startDate,
pollEndDate: startDate + pollDuration,
});
});

it("should signup one user", async () => {
Expand Down Expand Up @@ -300,8 +324,17 @@ describe("Stress tests with ceremony params (6,3,2,20)", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...ceremonyDeployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer, useQuadraticVoting: false });
await deployPoll({
...deployPollArgs,
signer,
useQuadraticVoting: false,
pollStartDate: startDate,
pollEndDate: startDate + pollDuration,
});
});

it("should signup 25 users", async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ export const deployArgs: Omit<DeployArgs, "signer"> = {
stateTreeDepth: STATE_TREE_DEPTH,
};

export const deployPollArgs: Omit<DeployPollArgs, "signer"> = {
pollDuration,
export const deployPollArgs: Omit<DeployPollArgs, "signer" | "pollStartDate" | "pollEndDate"> = {
intStateTreeDepth: INT_STATE_TREE_DEPTH,
messageBatchSize: MESSAGE_BATCH_SIZE,
voteOptionTreeDepth: VOTE_OPTION_TREE_DEPTH,
Expand Down
13 changes: 11 additions & 2 deletions packages/cli/tests/e2e/e2e.nonQv.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { genRandomSalt } from "maci-crypto";
import { Keypair } from "maci-domainobjs";
import { getDefaultSigner } from "maci-sdk";
import { getBlockTimestamp, getDefaultSigner } from "maci-sdk";

import type { Signer } from "ethers";

Expand Down Expand Up @@ -90,8 +90,17 @@ describe("e2e tests with non quadratic voting", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer, useQuadraticVoting: false });
await deployPoll({
...deployPollArgs,
signer,
useQuadraticVoting: false,
pollStartDate: startDate,
pollEndDate: startDate + pollDuration,
});
});

it("should signup one user", async () => {
Expand Down
69 changes: 54 additions & 15 deletions packages/cli/tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { genRandomSalt } from "maci-crypto";
import { Keypair } from "maci-domainobjs";
import { getDefaultSigner } from "maci-sdk";
import { getBlockTimestamp, getDefaultSigner } from "maci-sdk";

import fs from "fs";

Expand Down Expand Up @@ -111,8 +111,15 @@ describe("e2e tests", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });
const startDate = await getBlockTimestamp(signer);
// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer, useQuadraticVoting: true });
await deployPoll({
...deployPollArgs,
signer,
useQuadraticVoting: true,
pollStartDate: startDate,
pollEndDate: startDate + pollDuration,
});
});

it("should get the correct gatekeeper trait", async () => {
Expand Down Expand Up @@ -178,8 +185,11 @@ describe("e2e tests", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
});

it("should signup one user", async () => {
Expand Down Expand Up @@ -242,8 +252,11 @@ describe("e2e tests", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
});

it("should signup four users", async () => {
Expand Down Expand Up @@ -402,8 +415,11 @@ describe("e2e tests", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
});

it("should signup nine users", async () => {
Expand Down Expand Up @@ -468,8 +484,11 @@ describe("e2e tests", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
});

it("should signup eight users (same pub key)", async () => {
Expand Down Expand Up @@ -533,8 +552,11 @@ describe("e2e tests", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
});

it("should signup thirty users", async () => {
Expand Down Expand Up @@ -649,8 +671,11 @@ describe("e2e tests", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
// signup
await signup({ maciAddress: maciAddresses.maciAddress, maciPubKey: user.pubKey.serialize(), signer });
// joinPoll
Expand Down Expand Up @@ -691,7 +716,9 @@ describe("e2e tests", function test() {
});

it("should deploy a new poll", async () => {
await deployPoll({ ...deployPollArgs, signer });
const startDate = await getBlockTimestamp(signer);

await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
});

it("should join to new poll", async () => {
Expand Down Expand Up @@ -745,8 +772,11 @@ describe("e2e tests", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
// signup
await signup({ maciAddress: maciAddresses.maciAddress, maciPubKey: users[0].pubKey.serialize(), signer });
// joinPoll
Expand Down Expand Up @@ -808,7 +838,9 @@ describe("e2e tests", function test() {
});

it("should deploy a new poll", async () => {
await deployPoll({ ...deployPollArgs, signer });
const startDate = await getBlockTimestamp(signer);

await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
});

it("should signup four new users", async () => {
Expand Down Expand Up @@ -909,8 +941,10 @@ describe("e2e tests", function test() {
});

it("should run the first poll", async () => {
const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });

// signup
// eslint-disable-next-line @typescript-eslint/prefer-for-of
Expand Down Expand Up @@ -985,9 +1019,11 @@ describe("e2e tests", function test() {
});

it("should deploy two more polls", async () => {
const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
});

it("join the second and third polls", async () => {
Expand Down Expand Up @@ -1161,8 +1197,11 @@ describe("e2e tests", function test() {
before(async () => {
// deploy the smart contracts
maciAddresses = await deploy({ ...deployArgs, signer });

const startDate = await getBlockTimestamp(signer);

// deploy a poll contract
await deployPoll({ ...deployPollArgs, signer });
await deployPoll({ ...deployPollArgs, signer, pollStartDate: startDate, pollEndDate: startDate + pollDuration });
});

it("should signup one user", async () => {
Expand Down
Loading

0 comments on commit 2d4c13a

Please sign in to comment.