Skip to content

Commit

Permalink
feat(tally): remove ballotsTallied event and add view function
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlc03 committed Feb 2, 2024
1 parent b43ec20 commit 32f840e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
30 changes: 16 additions & 14 deletions contracts/contracts/Tally.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher {
error BatchStartIndexTooLarge();
error TallyBatchSizeTooLarge();

/// @notice events
event BallotsTallied(address poll);

/// @notice Create a new Tally contract
/// @param _verifier The Verifier contract
/// @param _vkRegistry The VkRegistry contract
Expand Down Expand Up @@ -81,6 +78,16 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher {
result = (_batchStartIndex / _tallyBatchSize) + (_numSignUps << uint256(50));
}

/// @notice Check if all ballots are tallied
/// @return tallied whether all ballots are tallied
function isTallied() external view returns (bool tallied) {
(uint8 intStateTreeDepth, , , ) = poll.treeDepths();
(uint256 numSignUps, ) = poll.numSignUpsAndMessages();

// Require that there are untallied ballots left
tallied = tallyBatchNum * (TREE_ARITY ** intStateTreeDepth) >= numSignUps;
}

Check warning

Code scanning / Slither

Unused return Medium

Check warning

Code scanning / Slither

Unused return Medium


/// @notice generate hash of public inputs for tally circuit
/// @param _numSignUps: number of signups
/// @param _batchStartIndex: the start index of given batch
Expand Down Expand Up @@ -121,20 +128,19 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher {
_votingPeriodOver(poll);
updateSbCommitment();

uint256 cachedBatchNum = tallyBatchNum;
// get the batch size and start index
(uint8 intStateTreeDepth, , , ) = poll.treeDepths();
uint256 tallyBatchSize = TREE_ARITY ** intStateTreeDepth;
uint256 batchStartIndex = tallyBatchNum * tallyBatchSize;

// save some gas because we won't overflow uint256
unchecked {
tallyBatchNum++;
}

// get the batch size and start index
(uint8 intStateTreeDepth, , , ) = poll.treeDepths();
uint256 tallyBatchSize = TREE_ARITY ** intStateTreeDepth;
uint256 batchStartIndex = cachedBatchNum * tallyBatchSize;

(uint256 numSignUps, ) = poll.numSignUpsAndMessages();

// Require that there are untalied ballots left
// Require that there are untallied ballots left
if (batchStartIndex >= numSignUps) {
revert AllBallotsTallied();
}
Expand All @@ -147,10 +153,6 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher {

// Update the tally commitment and the tally batch num
tallyCommitment = _newTallyCommitment;

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

/// @notice Verify the tally proof using the verifying key
Expand Down
22 changes: 13 additions & 9 deletions contracts/tests/Tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,26 @@ describe("TallyVotes", () => {
tallyGeneratedInputs = poll.tallyVotes();
});

it("isTallied should return false", async () => {
const isTallied = await tallyContract.isTallied();
expect(isTallied).to.eq(false);
});

it("tallyVotes() should update the tally commitment", async () => {
// do the processing on the message processor contract
await mpContract.processMessages(generatedInputs.newSbCommitment, [0, 0, 0, 0, 0, 0, 0, 0]);

await expect(tallyContract.tallyVotes(tallyGeneratedInputs.newTallyCommitment, [0, 0, 0, 0, 0, 0, 0, 0]))
.to.emit(tallyContract, "BallotsTallied")
.withArgs(await pollContract.getAddress());
await tallyContract.tallyVotes(tallyGeneratedInputs.newTallyCommitment, [0, 0, 0, 0, 0, 0, 0, 0]);

const onChainNewTallyCommitment = await tallyContract.tallyCommitment();
expect(tallyGeneratedInputs.newTallyCommitment).to.eq(onChainNewTallyCommitment.toString());
});

it("isTallied should return true", async () => {
const isTallied = await tallyContract.isTallied();
expect(isTallied).to.eq(true);
});

it("tallyVotes() should revert when votes have already been tallied", async () => {
await expect(
tallyContract.tallyVotes(tallyGeneratedInputs.newTallyCommitment, [0, 0, 0, 0, 0, 0, 0, 0]),
Expand Down Expand Up @@ -334,9 +342,7 @@ describe("TallyVotes", () => {

it("should tally votes correctly", async () => {
const tallyGeneratedInputs = poll.tallyVotes();
await expect(tallyContract.tallyVotes(tallyGeneratedInputs.newTallyCommitment, [0, 0, 0, 0, 0, 0, 0, 0]))
.to.emit(tallyContract, "BallotsTallied")
.withArgs(await pollContract.getAddress());
await tallyContract.tallyVotes(tallyGeneratedInputs.newTallyCommitment, [0, 0, 0, 0, 0, 0, 0, 0]);

const onChainNewTallyCommitment = await tallyContract.tallyCommitment();
expect(tallyGeneratedInputs.newTallyCommitment).to.eq(onChainNewTallyCommitment.toString());
Expand Down Expand Up @@ -488,9 +494,7 @@ describe("TallyVotes", () => {
// tally second batch
tallyGeneratedInputs = poll.tallyVotes();

await expect(tallyContract.tallyVotes(tallyGeneratedInputs.newTallyCommitment, [0, 0, 0, 0, 0, 0, 0, 0]))
.to.emit(tallyContract, "BallotsTallied")
.withArgs(await pollContract.getAddress());
await tallyContract.tallyVotes(tallyGeneratedInputs.newTallyCommitment, [0, 0, 0, 0, 0, 0, 0, 0]);

// check that it fails to tally again
await expect(
Expand Down

0 comments on commit 32f840e

Please sign in to comment.