Skip to content

Commit

Permalink
refactor(core): refactor the process message functions and general cl…
Browse files Browse the repository at this point in the history
…eanup

Remove unnecessary code, fix types, remove unused variables or duplicate ones, refactor the process
messages functions to ensure they can be tested individually, add custom error messages for message
processing debugging, ensure code is well commented and explained
  • Loading branch information
ctrlc03 committed Dec 14, 2023
1 parent 5781a44 commit 65a139b
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 238 deletions.
12 changes: 6 additions & 6 deletions circuits/ts/__tests__/ProcessMessages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ describe("ProcessMessage circuit", function () {
);

pollId = maciState.deployPoll(
duration,
// BigInt(2 + duration),
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
Expand Down Expand Up @@ -158,7 +156,12 @@ describe("ProcessMessage circuit", function () {

writeFileSync("witness.json", JSON.stringify(witness));

const packedVals = packProcessMessageSmallVals(BigInt(maxValues.maxVoteOptions), BigInt(poll.numSignUps), 0, 2);
const packedVals = packProcessMessageSmallVals(
BigInt(maxValues.maxVoteOptions),
BigInt(poll.maciStateRef.numSignUps),
0,
2,
);

// Test the ProcessMessagesInputHasher circuit
const hasherCircuitInputs = stringifyBigInts({
Expand Down Expand Up @@ -201,7 +204,6 @@ describe("ProcessMessage circuit", function () {
);

pollId = maciState.deployPoll(
duration,
BigInt(2 + duration), //BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
Expand Down Expand Up @@ -296,7 +298,6 @@ describe("ProcessMessage circuit", function () {
);

pollId = maciState.deployPoll(
duration,
BigInt(2 + duration), //BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
Expand Down Expand Up @@ -392,7 +393,6 @@ describe("ProcessMessage circuit", function () {

// Sign up and publish
const pollId = maciState.deployPoll(
duration,
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
Expand Down
2 changes: 0 additions & 2 deletions circuits/ts/__tests__/TallyVotes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ describe("TallyVotes circuit", function () {
);

pollId = maciState.deployPoll(
duration,
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
Expand Down Expand Up @@ -137,7 +136,6 @@ describe("TallyVotes circuit", function () {
}

const pollId = maciState.deployPoll(
duration,
BigInt(Math.floor(Date.now() / 1000) + duration),
maxValues,
treeDepths,
Expand Down
2 changes: 1 addition & 1 deletion circuits/ts/types/snarkjs.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module "snarkjs" {
export type NumericString = `${number}` | string;
export type PublicSignals = Record<string, string | bigint | bigint[] | string[]>;
export type PublicSignals = Record<string, string | bigint | bigint[] | string[] | bigint[][] | bigint[][][]>;
export type BigNumberish = number | string | bigint;

export interface ISnarkJSVerificationKey {
Expand Down
1 change: 0 additions & 1 deletion contracts/tests/MACI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ describe("MACI", () => {
pollId = event.args._pollId;

const p = maciState.deployPoll(
duration,
BigInt(deployTime + duration),
maxValues,
treeDepths,
Expand Down
3 changes: 1 addition & 2 deletions contracts/ts/genMaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ const genMaciStateFromContract = async (

case action.type === "DeployPoll" && action.data.pollId?.toString() === pollId.toString(): {
maciState.deployPoll(
duration,
BigInt(deployTime + duration),
maxValues,
treeDepths,
Expand Down Expand Up @@ -343,8 +342,8 @@ const genMaciStateFromContract = async (

const poll = maciState.polls[pollId];
assert(Number(numSignUpsAndMessages[1]) === poll.messages.length);
assert(Number(numSignUpsAndMessages[0]) === maciState.numSignUps);

poll.numSignUps = Number(numSignUpsAndMessages[0]);
maciState.polls[pollId] = poll;

return maciState;
Expand Down
13 changes: 7 additions & 6 deletions core/ts/MaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import { IJsonMaciState, IJsonPoll, IMaciState, MaxValues, TreeDepths } from "./
* A representation of the MACI contract.
*/
export class MaciState implements IMaciState {
// a MaciState can hold multiple polls
public polls: Poll[] = [];

// in this quinary tree we hold all signups (hash of a state leaf)
public stateTree: IncrementalQuinTree;
// the leaves of the state tree
public stateLeaves: StateLeaf[] = [];
// how deep the state tree is
public stateTreeDepth: number;

public numSignUps = 0;

public stateTreeDepth: number;

// to keep track if a poll is currently being processed
public pollBeingProcessed: boolean;
public currentPollBeingProcessed: number;

Expand All @@ -29,6 +33,7 @@ export class MaciState implements IMaciState {
this.stateTreeDepth = stateTreeDepth;
this.stateTree = new IncrementalQuinTree(this.stateTreeDepth, blankStateLeafHash, STATE_TREE_ARITY, hash5);

// we put a blank state leaf to prevent a DoS attack
this.stateLeaves.push(blankStateLeaf);
this.stateTree.insert(blankStateLeafHash);
}
Expand All @@ -51,7 +56,6 @@ export class MaciState implements IMaciState {

/**
* Deploy a new poll with the given parameters.
* @param duration - The duration of the poll in seconds.
* @param pollEndTimestamp - The Unix timestamp at which the poll ends.
* @param maxValues - The maximum number of values for each vote option.
* @param treeDepths - The depths of the tree.
Expand All @@ -60,16 +64,13 @@ export class MaciState implements IMaciState {
* @returns The index of the newly deployed poll.
*/
public deployPoll(
duration: number,
pollEndTimestamp: bigint,
maxValues: MaxValues,
treeDepths: TreeDepths,
messageBatchSize: number,
coordinatorKeypair: Keypair,
): number {
// TODO: fix the order of the arguments
const poll: Poll = new Poll(
duration,
pollEndTimestamp,
coordinatorKeypair,
treeDepths,
Expand Down
Loading

0 comments on commit 65a139b

Please sign in to comment.