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 29, 2023
1 parent c5d373f commit 823cacd
Show file tree
Hide file tree
Showing 16 changed files with 348 additions and 285 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 @@ -61,8 +61,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 @@ -153,7 +151,12 @@ describe("ProcessMessage circuit", function () {
expect(newStateRoot.toString()).not.to.be.eq(currentStateRoot.toString());
expect(newBallotRoot.toString()).not.to.be.eq(currentBallotRoot.toString());

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 @@ -196,7 +199,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 @@ -291,7 +293,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 @@ -387,7 +388,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 @@ -217,7 +217,6 @@ describe("MACI", () => {
pollId = event.args._pollId;

const p = maciState.deployPoll(
duration,
BigInt(deployTime + duration),
maxValues,
treeDepths,
Expand Down
9 changes: 1 addition & 8 deletions contracts/tests/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,7 @@ describe("MessageProcessor", () => {
const deployTime = block!.timestamp;

// deploy local poll
const p = maciState.deployPoll(
duration,
BigInt(deployTime + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinator,
);
const p = maciState.deployPoll(BigInt(deployTime + duration), maxValues, treeDepths, messageBatchSize, coordinator);
expect(p.toString()).to.eq(pollId.toString());

// publish the NOTHING_UP_MY_SLEEVE message
Expand Down
9 changes: 1 addition & 8 deletions contracts/tests/Poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,7 @@ describe("Poll", () => {
pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract;

// deploy local poll
const p = maciState.deployPoll(
duration,
BigInt(deployTime + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinator,
);
const p = maciState.deployPoll(BigInt(deployTime + duration), maxValues, treeDepths, messageBatchSize, coordinator);
expect(p.toString()).to.eq(pollId.toString());
// publish the NOTHING_UP_MY_SLEEVE message
const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
Expand Down
9 changes: 1 addition & 8 deletions contracts/tests/Tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,7 @@ describe("TallyVotes", () => {
pollContract = new BaseContract(pollContractAddress, pollAbi, signer) as PollContract;

// deploy local poll
const p = maciState.deployPoll(
duration,
BigInt(deployTime + duration),
maxValues,
treeDepths,
messageBatchSize,
coordinator,
);
const p = maciState.deployPoll(BigInt(deployTime + duration), maxValues, treeDepths, messageBatchSize, coordinator);
expect(p.toString()).to.eq(pollId.toString());
// publish the NOTHING_UP_MY_SLEEVE message
const messageData = [NOTHING_UP_MY_SLEEVE, BigInt(0)];
Expand Down
4 changes: 1 addition & 3 deletions contracts/ts/genMaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ export 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 @@ -338,8 +337,7 @@ export const genMaciStateFromContract = async (

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

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

// we need to ensure that the stateRoot is correct
assert(maciState.stateTree.root.toString() === (await maciContract.getStateAqRoot()).toString());
Expand Down
14 changes: 7 additions & 7 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 All @@ -80,7 +81,6 @@ export class MaciState implements IMaciState {
},
maxValues,
this,
this.stateTreeDepth,
);

this.polls.push(poll);
Expand Down
Loading

0 comments on commit 823cacd

Please sign in to comment.