Skip to content

Commit

Permalink
refactor(core): remove unnecessary stateAq from MaciState
Browse files Browse the repository at this point in the history
  • Loading branch information
baumstern committed Dec 7, 2023
1 parent ddc1061 commit 22a4f28
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 80 deletions.
11 changes: 0 additions & 11 deletions circuits/ts/__tests__/ProcessMessages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ describe("ProcessMessage circuit", function () {
BigInt(Math.floor(Date.now() / 1000)),
);

maciState.stateAq.mergeSubRoots(0);
maciState.stateAq.merge(STATE_TREE_DEPTH);

pollId = maciState.deployPoll(
duration,
// BigInt(2 + duration),
Expand Down Expand Up @@ -197,9 +194,6 @@ describe("ProcessMessage circuit", function () {
BigInt(1), //BigInt(Math.floor(Date.now() / 1000)),
);

maciState.stateAq.mergeSubRoots(0);
maciState.stateAq.merge(STATE_TREE_DEPTH);

pollId = maciState.deployPoll(
duration,
BigInt(2 + duration), //BigInt(Math.floor(Date.now() / 1000) + duration),
Expand Down Expand Up @@ -288,9 +282,6 @@ describe("ProcessMessage circuit", function () {
BigInt(1), //BigInt(Math.floor(Date.now() / 1000)),
);

maciState.stateAq.mergeSubRoots(0);
maciState.stateAq.merge(STATE_TREE_DEPTH);

pollId = maciState.deployPoll(
duration,
BigInt(2 + duration), //BigInt(Math.floor(Date.now() / 1000) + duration),
Expand Down Expand Up @@ -410,8 +401,6 @@ describe("ProcessMessage circuit", function () {
BigInt(Math.floor(Date.now() / 1000)),
);

maciState.stateAq.mergeSubRoots(0);
maciState.stateAq.merge(STATE_TREE_DEPTH);
// Sign up and publish
const pollId = maciState.deployPoll(
duration,
Expand Down
6 changes: 0 additions & 6 deletions circuits/ts/__tests__/TallyVotes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ describe("TallyVotes circuit", function () {
const userKeypair = new Keypair();
stateIndex = maciState.signUp(userKeypair.pubKey, voiceCreditBalance, BigInt(Math.floor(Date.now() / 1000)));

maciState.stateAq.mergeSubRoots(0);
maciState.stateAq.merge(STATE_TREE_DEPTH);

pollId = maciState.deployPoll(
duration,
BigInt(Math.floor(Date.now() / 1000) + duration),
Expand Down Expand Up @@ -173,9 +170,6 @@ describe("TallyVotes circuit", function () {
maciState.signUp(k.pubKey, voiceCreditBalance, BigInt(Math.floor(Date.now() / 1000) + duration));
}

maciState.stateAq.mergeSubRoots(0);
maciState.stateAq.merge(STATE_TREE_DEPTH);

const pollId = maciState.deployPoll(
duration,
BigInt(Math.floor(Date.now() / 1000) + duration),
Expand Down
5 changes: 1 addition & 4 deletions contracts/tests/MACI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,11 @@ describe("MACI", () => {
});
receipt = await tx.wait();
expect(receipt.status).to.eq(1);

maciState.stateAq.mergeSubRoots(0);
maciState.stateAq.merge(STATE_TREE_DEPTH);
});

it("the state root must be correct", async () => {
const onChainStateRoot = await stateAqContract.getMainRoot(STATE_TREE_DEPTH);
expect(onChainStateRoot.toString()).to.eq(maciState.stateAq.mainRoots[STATE_TREE_DEPTH].toString());
expect(onChainStateRoot.toString()).to.eq(maciState.stateTree.root.toString());
});
});

Expand Down
6 changes: 0 additions & 6 deletions contracts/ts/genMaciState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,6 @@ const genMaciStateFromContract = async (
maciState.polls[pollId].publishMessage(action.data.message, action.data.encPubKey);
} else if (action["type"] === "TopupMessage") {
maciState.polls[pollId].topupMessage(action.data.message);
} else if (action["type"] === "MergeMaciStateAqSubRoots") {
maciState.stateAq.mergeSubRoots(action.data.numSrQueueOps);
} else if (action["type"] === "MergeMaciStateAq") {
if (pollId == 0) {
maciState.stateAq.merge(stateTreeDepth);
}
} else if (action["type"] === "MergeMessageAqSubRoots") {
maciState.polls[pollId].messageAq.mergeSubRoots(action.data.numSrQueueOps);
} else if (action["type"] === "MergeMessageAq") {
Expand Down
50 changes: 20 additions & 30 deletions core/ts/MaciState.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AccQueue, IncrementalQuinTree, hash5 } from "maci-crypto";
import { IncrementalQuinTree, hash5 } from "maci-crypto";
import { PubKey, Keypair, StateLeaf } from "maci-domainobjs";

// import order?
import { Poll } from "./Poll";
import { TreeDepths, MaxValues } from "./utils/utils";
import { STATE_TREE_ARITY } from "./utils/constants";

// todo: organize this in domainobjs
const blankStateLeaf = StateLeaf.genBlankLeaf();
Expand All @@ -17,6 +17,7 @@ const blankStateLeafHash = blankStateLeaf.hash();

interface IMaciState {
signUp(_pubKey: PubKey, _initialVoiceCreditBalance: bigint, _timestamp: bigint): number;

deployPoll(
_duration: number,
_pollEndTimestamp: bigint,
Expand All @@ -34,35 +35,34 @@ interface IMaciState {
// A representation of the MACI contract
// Also see MACI.sol
class MaciState implements IMaciState {
public STATE_TREE_ARITY = 5;
public STATE_TREE_SUBDEPTH = 2;
public MESSAGE_TREE_ARITY = 5;
public VOTE_OPTION_TREE_ARITY = 5;

public stateTreeDepth: number;
public polls: Poll[] = [];
public stateLeaves: StateLeaf[] = [];

public stateTree: IncrementalQuinTree;
public stateAq: AccQueue = new AccQueue(this.STATE_TREE_SUBDEPTH, this.STATE_TREE_ARITY, blankStateLeafHash);
public pollBeingProcessed = true;
public currentPollBeingProcessed;
public stateLeaves: StateLeaf[] = [];

public numSignUps = 0;

public stateTreeDepth: number;

public pollBeingProcessed: boolean;
public currentPollBeingProcessed: number;

constructor(_stateTreeDepth: number) {
this.stateTreeDepth = _stateTreeDepth;
this.stateTree = new IncrementalQuinTree(this.stateTreeDepth, blankStateLeafHash, this.STATE_TREE_ARITY, hash5);
this.stateTree = new IncrementalQuinTree(this.stateTreeDepth, blankStateLeafHash, STATE_TREE_ARITY, hash5);

this.stateLeaves.push(blankStateLeaf);
this.stateTree.insert(blankStateLeafHash);
this.stateAq.enqueue(blankStateLeafHash);
}

public signUp(_pubKey: PubKey, _initialVoiceCreditBalance: bigint, _timestamp: bigint): number {
this.numSignUps++;
const stateLeaf = new StateLeaf(_pubKey, _initialVoiceCreditBalance, _timestamp);
const h = stateLeaf.hash();
const leafIndex = this.stateAq.enqueue(h);

this.stateTree.insert(h);
this.stateLeaves.push(stateLeaf.copy());
this.numSignUps++;
const arrayLength = this.stateLeaves.push(stateLeaf.copy());
const leafIndex = arrayLength - 1;
return leafIndex;
}

Expand All @@ -74,15 +74,16 @@ class MaciState implements IMaciState {
_messageBatchSize: number,
_coordinatorKeypair: Keypair,
): number {
// TODO: fix the order of the arguments
const poll: Poll = new Poll(
_duration,
_pollEndTimestamp,
_coordinatorKeypair,
_treeDepths,
{
messageBatchSize: _messageBatchSize,
subsidyBatchSize: this.STATE_TREE_ARITY ** _treeDepths.intStateTreeDepth,
tallyBatchSize: this.STATE_TREE_ARITY ** _treeDepths.intStateTreeDepth,
subsidyBatchSize: STATE_TREE_ARITY ** _treeDepths.intStateTreeDepth,
tallyBatchSize: STATE_TREE_ARITY ** _treeDepths.intStateTreeDepth,
},
_maxValues,
this,
Expand Down Expand Up @@ -111,9 +112,6 @@ class MaciState implements IMaciState {

public equals = (m: MaciState): boolean => {
const result =
this.STATE_TREE_ARITY === m.STATE_TREE_ARITY &&
this.MESSAGE_TREE_ARITY === m.MESSAGE_TREE_ARITY &&
this.VOTE_OPTION_TREE_ARITY === m.VOTE_OPTION_TREE_ARITY &&
this.stateTreeDepth === m.stateTreeDepth &&
this.polls.length === m.polls.length &&
this.stateLeaves.length === m.stateLeaves.length;
Expand All @@ -140,10 +138,6 @@ class MaciState implements IMaciState {
*/
toJSON() {
return {
STATE_TREE_ARITY: this.STATE_TREE_ARITY,
STATE_TREE_SUBDEPTH: this.STATE_TREE_SUBDEPTH,
MESSAGE_TREE_ARITY: this.MESSAGE_TREE_ARITY,
VOTE_OPTION_TREE_ARITY: this.VOTE_OPTION_TREE_ARITY,
stateTreeDepth: this.stateTreeDepth,
polls: this.polls.map((poll) => poll.toJSON()),
stateLeaves: this.stateLeaves.map((leaf) => leaf.toJSON()),
Expand All @@ -163,10 +157,6 @@ class MaciState implements IMaciState {
maciState.pollBeingProcessed = json.pollBeingProcessed;
maciState.currentPollBeingProcessed = json.currentPollBeingProcessed;
maciState.numSignUps = json.numSignUps;
maciState.STATE_TREE_ARITY = json.STATE_TREE_ARITY;
maciState.STATE_TREE_SUBDEPTH = json.STATE_TREE_SUBDEPTH;
maciState.MESSAGE_TREE_ARITY = json.MESSAGE_TREE_ARITY;
maciState.VOTE_OPTION_TREE_ARITY = json.VOTE_OPTION_TREE_ARITY;

// re create the state tree (start from index 1 as in the constructor we already add the blank leaf)
for (let i = 1; i < json.stateLeaves.length; i++) {
Expand Down
41 changes: 18 additions & 23 deletions core/ts/__tests__/MaciState.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { MaciState, packProcessMessageSmallVals, unpackProcessMessageSmallVals } from "../";
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs";

import { expect } from "chai";

import { PCommand, Message, Keypair, VerifyingKey, StateLeaf } from "maci-domainobjs";
import { hash5, G1Point, G2Point, NOTHING_UP_MY_SLEEVE, IncrementalQuinTree, AccQueue } from "maci-crypto";

import { hash5, G1Point, G2Point, NOTHING_UP_MY_SLEEVE, IncrementalQuinTree } from "maci-crypto";
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs";
import {
STATE_TREE_ARITY,
STATE_TREE_SUBDEPTH,
BlankStateLeafHash,
MaciState,
packProcessMessageSmallVals,
unpackProcessMessageSmallVals,
} from "../";

const voiceCreditBalance = BigInt(100);

Expand Down Expand Up @@ -43,15 +52,11 @@ const testTallyVk = new VerifyingKey(

const coordinatorKeypair = new Keypair();

const blankStateLeaf = StateLeaf.genBlankLeaf();
const blankStateLeafHash = blankStateLeaf.hash();

describe("MaciState", function () {
this.timeout(100000);
describe("Process and tally 1 message from 1 user", () => {
let maciState: MaciState;
let pollId;
let stateTree;
let msgTree;
const voteWeight = BigInt(9);
const voteOptionIndex = BigInt(0);
Expand All @@ -60,31 +65,25 @@ describe("MaciState", function () {

before(() => {
maciState = new MaciState(STATE_TREE_DEPTH);
stateTree = new IncrementalQuinTree(STATE_TREE_DEPTH, blankStateLeafHash, 5, hash5);

stateTree.insert(blankStateLeafHash);

msgTree = new IncrementalQuinTree(treeDepths.messageTreeDepth, NOTHING_UP_MY_SLEEVE, 5, hash5);
});

// The end result should be that option 0 gets 3 votes
// because the user spends 9 voice credits on it
it("the state root should be correct", () => {
const accumulatorQueue: AccQueue = new AccQueue(STATE_TREE_SUBDEPTH, STATE_TREE_ARITY, BlankStateLeafHash);
const timestamp = BigInt(Math.floor(Date.now() / 1000));

const stateLeaf = new StateLeaf(userKeypair.pubKey, voiceCreditBalance, timestamp);

stateTree.insert(stateLeaf.hash());
accumulatorQueue.enqueue(BlankStateLeafHash);
accumulatorQueue.enqueue(stateLeaf.hash());
accumulatorQueue.mergeSubRoots(0);
accumulatorQueue.merge(STATE_TREE_DEPTH);

stateIndex = maciState.signUp(userKeypair.pubKey, voiceCreditBalance, timestamp);

expect(stateIndex.toString()).to.eq("1");

maciState.stateAq.mergeSubRoots(0);
maciState.stateAq.merge(STATE_TREE_DEPTH);
console.log(`root=${stateTree.root.toString()}`);

expect(maciState.stateAq.getRoot(STATE_TREE_DEPTH).toString()).to.eq(stateTree.root.toString());
expect(accumulatorQueue.getRoot(STATE_TREE_DEPTH).toString()).to.eq(maciState.stateTree.root.toString());
});

it("the message root should be correct", () => {
Expand Down Expand Up @@ -246,10 +245,6 @@ describe("MaciState", function () {
maciState.polls[pollId].processMessages();
}).to.throw;

// Merge the state aq
maciState.stateAq.mergeSubRoots(0);
maciState.stateAq.merge(STATE_TREE_DEPTH);

expect(() => {
maciState.polls[pollId].processMessages();
}).to.throw;
Expand Down
2 changes: 2 additions & 0 deletions core/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export {
unpackTallyVotesSmallVals,
packSubsidySmallVals,
} from "./utils/utils";

export { STATE_TREE_ARITY, STATE_TREE_SUBDEPTH, BlankStateLeaf, BlankStateLeafHash } from "./utils/constants";
10 changes: 10 additions & 0 deletions core/ts/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StateLeaf } from "maci-domainobjs";

const STATE_TREE_ARITY = 5;
const STATE_TREE_SUBDEPTH = 2;

// todo: organize this in domainobjs
const BlankStateLeaf = StateLeaf.genBlankLeaf();
const BlankStateLeafHash = BlankStateLeaf.hash();

export { STATE_TREE_ARITY, STATE_TREE_SUBDEPTH, BlankStateLeaf, BlankStateLeafHash };

0 comments on commit 22a4f28

Please sign in to comment.