Skip to content

Commit

Permalink
fix(contracts): fix wrong tally processing check
Browse files Browse the repository at this point in the history
Ensure we stop tallying ballots correctly, and that an event is emitted even if batchStartIndex ==
number of ballots

fix #1137
  • Loading branch information
ctrlc03 committed Jan 31, 2024
1 parent ede35ff commit a90bcf8
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 83 deletions.
157 changes: 79 additions & 78 deletions cli/tests/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,84 +190,6 @@ describe("e2e tests", function test() {
});
});

describe("4 signups, 4 messages", () => {
after(() => {
cleanVanilla();
});

const users = [new Keypair(), new Keypair(), new Keypair(), new Keypair()];

before(async () => {
// deploy the smart contracts
maciAddresses = await deploy(deployArgs);
// deploy a poll contract
pollAddresses = await deployPoll(deployPollArgs);
});

it("should signup four users", async () => {
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < users.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
await signup({ maciPubKey: users[i].pubKey.serialize() });
}
});

it("should publish four messages", async () => {
await publish({
pubkey: users[0].pubKey.serialize(),
stateIndex: 1n,
voteOptionIndex: 0n,
nonce: 1n,
pollId: 0n,
newVoteWeight: 9n,
salt: genRandomSalt(),
privateKey: users[0].privKey.serialize(),
});

await publish({
pubkey: users[1].pubKey.serialize(),
stateIndex: 2n,
voteOptionIndex: 1n,
nonce: 1n,
pollId: 0n,
newVoteWeight: 9n,
salt: genRandomSalt(),
privateKey: users[1].privKey.serialize(),
});

await publish({
pubkey: users[2].pubKey.serialize(),
stateIndex: 3n,
voteOptionIndex: 2n,
nonce: 1n,
pollId: 0n,
newVoteWeight: 9n,
salt: genRandomSalt(),
privateKey: users[2].privKey.serialize(),
});

await publish({
pubkey: users[3].pubKey.serialize(),
stateIndex: 4n,
voteOptionIndex: 3n,
nonce: 1n,
pollId: 0n,
newVoteWeight: 9n,
salt: genRandomSalt(),
privateKey: users[3].privKey.serialize(),
});
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
const tallyFileData = await genProofs(genProofsArgs);
await proveOnChain(proveOnChainArgs);
await verify({ ...verifyArgs, tallyData: tallyFileData });
});
});

describe("4 signups, 6 messages", () => {
after(() => {
cleanVanilla();
Expand Down Expand Up @@ -473,6 +395,85 @@ describe("e2e tests", function test() {
});
});

describe("30 signups (31 ballots), 4 messages", () => {
after(() => {
cleanVanilla();
});

const users = Array.from({ length: 30 }, () => new Keypair());

before(async () => {
// deploy the smart contracts
maciAddresses = await deploy(deployArgs);
// deploy a poll contract
pollAddresses = await deployPoll(deployPollArgs);
});

it("should signup thirty users", async () => {
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < users.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
await signup({ maciPubKey: users[i].pubKey.serialize() });
}
});

it("should publish 4 messages", async () => {
// publish four different messages
await publish({
pubkey: users[0].pubKey.serialize(),
stateIndex: 1n,
voteOptionIndex: 0n,
nonce: 1n,
pollId: 0n,
newVoteWeight: 9n,
salt: genRandomSalt(),
privateKey: users[0].privKey.serialize(),
});

await publish({
pubkey: users[1].pubKey.serialize(),
stateIndex: 2n,
voteOptionIndex: 1n,
nonce: 1n,
pollId: 0n,
newVoteWeight: 9n,
salt: genRandomSalt(),
privateKey: users[1].privKey.serialize(),
});

await publish({
pubkey: users[2].pubKey.serialize(),
stateIndex: 3n,
voteOptionIndex: 2n,
nonce: 1n,
pollId: 0n,
newVoteWeight: 9n,
salt: genRandomSalt(),
privateKey: users[2].privKey.serialize(),
});

await publish({
pubkey: users[3].pubKey.serialize(),
stateIndex: 4n,
voteOptionIndex: 3n,
nonce: 1n,
pollId: 0n,
newVoteWeight: 9n,
salt: genRandomSalt(),
privateKey: users[3].privKey.serialize(),
});
});

it("should generate zk-SNARK proofs and verify them", async () => {
await timeTravel(timeTravelArgs);
await mergeMessages(mergeMessagesArgs);
await mergeSignups(mergeSignupsArgs);
const tallyFileData = await genProofs(genProofsArgs);
await proveOnChain(proveOnChainArgs);
await verify({ ...verifyArgs, tallyData: tallyFileData });
});
});

describe("checkKeys", () => {
before(async () => {
// deploy maci as we need the address
Expand Down
4 changes: 2 additions & 2 deletions contracts/contracts/Tally.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher {
(uint256 numSignUps, ) = poll.numSignUpsAndMessages();

// Require that there are untalied ballots left
if (batchStartIndex > numSignUps) {
if (batchStartIndex >= numSignUps) {
revert AllBallotsTallied();
}

Expand All @@ -148,7 +148,7 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher {
// Update the tally commitment and the tally batch num
tallyCommitment = _newTallyCommitment;

if ((cachedBatchNum + 1) * tallyBatchSize > numSignUps) {
if ((cachedBatchNum + 1) * tallyBatchSize >= numSignUps) {
emit BallotsTallied(address(poll));
}
}
Expand Down
Loading

0 comments on commit a90bcf8

Please sign in to comment.