Skip to content

Commit

Permalink
refactor(core): refactor the core package subsidy and tally functions
Browse files Browse the repository at this point in the history
Refactor the last few functions left inside the Poll.ts file, as well as add interfaces for circuit
inputs
  • Loading branch information
ctrlc03 committed Jan 6, 2024
1 parent 2c5e633 commit 0af7dd2
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 180 deletions.
2 changes: 1 addition & 1 deletion circuits/ts/__tests__/CeremonyParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ describe("Ceremony param tests", () => {
randIdx = generateRandomIndex(Object.keys(generatedInputs).length);
}

(generatedInputs.currentResults as string[])[randIdx] = "1";
generatedInputs.currentResults[randIdx] = "1";
const witness = await testCircuit.calculateWitness(generatedInputs);
await testCircuit.checkConstraints(witness);
});
Expand Down
6 changes: 3 additions & 3 deletions circuits/ts/__tests__/TallyVotes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe("TallyVotes circuit", function test() {
randIdx = generateRandomIndex(Object.keys(generatedInputs).length);
}

(generatedInputs.currentResults as string[])[randIdx] = "1";
generatedInputs.currentResults[randIdx] = "1";
const witness = await circuit.calculateWitness(generatedInputs);
await circuit.checkConstraints(witness);
});
Expand Down Expand Up @@ -176,9 +176,9 @@ describe("TallyVotes circuit", function test() {
// currentSpentVoiceCreditSubtotal, and
// currentPerVOSpentVoiceCredits
if (i === 0) {
(generatedInputs.currentResults as string[])[0] = "123";
generatedInputs.currentResults[0] = "123";
generatedInputs.currentSpentVoiceCreditSubtotal = "456";
(generatedInputs.currentPerVOSpentVoiceCredits as string[])[0] = "789";
generatedInputs.currentPerVOSpentVoiceCredits[0] = "789";
}

// eslint-disable-next-line no-await-in-loop
Expand Down
10 changes: 4 additions & 6 deletions cli/ts/commands/genProofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export const genProofs = async (

let maciState: MaciState | undefined;
if (stateFile) {
const content = JSON.parse(fs.readFileSync(stateFile).toString()) as IJsonMaciState;
const content = JSON.parse(fs.readFileSync(stateFile).toString()) as unknown as IJsonMaciState;
const serializedPrivateKey = maciPrivKey.serialize();

try {
Expand Down Expand Up @@ -287,7 +287,7 @@ export const genProofs = async (
// while we have unprocessed messages, process them
while (poll.hasUnprocessedMessages()) {
// process messages in batches
const circuitInputs = poll.processMessages(pollId);
const circuitInputs = poll.processMessages(pollId) as unknown as CircuitInputs;
try {
// generate the proof for this batch
// eslint-disable-next-line no-await-in-loop
Expand Down Expand Up @@ -345,13 +345,11 @@ export const genProofs = async (

let numBatchesCalulated = 0;

// @todo fix types in the circuits package
// @todo why this next part works
let subsidyCircuitInputs: CircuitInputs;
// calculate the subsidy for each batch
while (poll.hasUnfinishedSubsidyCalculation()) {
// calculate subsidy in batches
subsidyCircuitInputs = poll.subsidyPerBatch();
subsidyCircuitInputs = poll.subsidyPerBatch() as unknown as CircuitInputs;
try {
// generate proof for this batch
// eslint-disable-next-line no-await-in-loop
Expand Down Expand Up @@ -423,7 +421,7 @@ export const genProofs = async (
// tally all ballots for this poll
while (poll.hasUntalliedBallots()) {
// tally votes in batches
tallyCircuitInputs = poll.tallyVotes();
tallyCircuitInputs = poll.tallyVotes() as unknown as CircuitInputs;

try {
// generate the proof
Expand Down
12 changes: 9 additions & 3 deletions contracts/tests/MessageProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import { expect } from "chai";
import { BaseContract, Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";
import { MaciState, Poll, STATE_TREE_DEPTH, packProcessMessageSmallVals } from "maci-core";
import {
MaciState,
Poll,
STATE_TREE_DEPTH,
packProcessMessageSmallVals,
IProcessMessagesCircuitInputs,
} from "maci-core";
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, Message, PubKey } from "maci-domainobjs";

Expand Down Expand Up @@ -36,7 +42,7 @@ describe("MessageProcessor", () => {
const maciState = new MaciState(STATE_TREE_DEPTH);

let signer: Signer;
let generatedInputs: { newSbCommitment: bigint };
let generatedInputs: IProcessMessagesCircuitInputs;
const coordinator = new Keypair();

const users = [new Keypair(), new Keypair()];
Expand Down Expand Up @@ -87,7 +93,7 @@ describe("MessageProcessor", () => {
maciState.polls[pollId].publishMessage(message, padKey);

poll = maciState.polls[pollId];
generatedInputs = poll.processMessages(pollId) as typeof generatedInputs;
generatedInputs = poll.processMessages(pollId);

// set the verification keys on the vk smart contract
const vkContract = r.vkRegistryContract;
Expand Down
10 changes: 5 additions & 5 deletions contracts/tests/Subsidy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { expect } from "chai";
import { BaseContract, Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";
import { MaciState, Poll, packSubsidySmallVals } from "maci-core";
import { MaciState, Poll, packSubsidySmallVals, IProcessMessagesCircuitInputs, ISubsidyCircuitInputs } from "maci-core";
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, Message, PubKey } from "maci-domainobjs";

Expand Down Expand Up @@ -37,7 +37,7 @@ describe("Subsidy", () => {
let pollId: number;
let poll: Poll;

let generatedInputs: { newSbCommitment: bigint };
let generatedInputs: IProcessMessagesCircuitInputs;

before(async () => {
signer = await getDefaultSigner();
Expand Down Expand Up @@ -87,7 +87,7 @@ describe("Subsidy", () => {
poll = maciState.polls[pollId];

// process messages locally
generatedInputs = poll.processMessages(pollId) as typeof generatedInputs;
generatedInputs = poll.processMessages(pollId);

// set the verification keys on the vk smart contract
const vkContract = r.vkRegistryContract;
Expand Down Expand Up @@ -145,14 +145,14 @@ describe("Subsidy", () => {
});

describe("after merging acc queues", () => {
let subsidyGeneratedInputs: { newSubsidyCommitment: bigint };
let subsidyGeneratedInputs: ISubsidyCircuitInputs;
before(async () => {
await pollContract.mergeMaciStateAqSubRoots(0, pollId);
await pollContract.mergeMaciStateAq(0);

await pollContract.mergeMessageAqSubRoots(0);
await pollContract.mergeMessageAq();
subsidyGeneratedInputs = poll.subsidyPerBatch() as { newSubsidyCommitment: bigint };
subsidyGeneratedInputs = poll.subsidyPerBatch();
});
it("updateSubsidy() should update the tally commitment", async () => {
// do the processing on the message processor contract
Expand Down
16 changes: 11 additions & 5 deletions contracts/tests/Tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import { expect } from "chai";
import { BaseContract, Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";
import { MaciState, Poll, packTallyVotesSmallVals } from "maci-core";
import {
MaciState,
Poll,
packTallyVotesSmallVals,
IProcessMessagesCircuitInputs,
ITallyCircuitInputs,
} from "maci-core";
import { NOTHING_UP_MY_SLEEVE } from "maci-crypto";
import { Keypair, Message, PubKey } from "maci-domainobjs";

Expand Down Expand Up @@ -39,7 +45,7 @@ describe("TallyVotes", () => {
let pollId: number;
let poll: Poll;

let generatedInputs: { newSbCommitment: bigint };
let generatedInputs: IProcessMessagesCircuitInputs;

before(async () => {
signer = await getDefaultSigner();
Expand Down Expand Up @@ -89,7 +95,7 @@ describe("TallyVotes", () => {
poll = maciState.polls[pollId];

// process messages locally
generatedInputs = poll.processMessages(pollId) as typeof generatedInputs;
generatedInputs = poll.processMessages(pollId);

// set the verification keys on the vk smart contract
const vkContract = r.vkRegistryContract;
Expand Down Expand Up @@ -140,14 +146,14 @@ describe("TallyVotes", () => {
});

describe("after merging acc queues", () => {
let tallyGeneratedInputs: { newTallyCommitment: bigint };
let tallyGeneratedInputs: ITallyCircuitInputs;
before(async () => {
await pollContract.mergeMaciStateAqSubRoots(0, pollId);
await pollContract.mergeMaciStateAq(0);

await pollContract.mergeMessageAqSubRoots(0);
await pollContract.mergeMessageAq();
tallyGeneratedInputs = poll.tallyVotes() as { newTallyCommitment: bigint };
tallyGeneratedInputs = poll.tallyVotes();
});
it("tallyVotes() should update the tally commitment", async () => {
// do the processing on the message processor contract
Expand Down
Loading

0 comments on commit 0af7dd2

Please sign in to comment.