Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contracts): add total spent to tally #1866

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cli/ts/commands/proveOnChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ export const proveOnChain = async ({
tallyData.results.tally.map((_, index) => index),
tallyResults,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
tallyData.perVOSpentVoiceCredits?.commitment ?? 0n,
)
Expand Down
34 changes: 28 additions & 6 deletions packages/contracts/contracts/Tally.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
// The total tally results number
uint256 public totalTallyResults;

// spent field retrieved in the totalSpentVoiceCredits object
uint256 public totalSpent;

/// @notice custom errors
error ProcessingNotComplete();
error InvalidTallyVotesProof();
Expand All @@ -75,6 +78,7 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
error TallyBatchSizeTooLarge();
error NotSupported();
error VotesNotTallied();
error IncorrectSpentVoiceCredits();

/// @notice Create a new Tally contract
/// @param _verifier The Verifier contract
Expand Down Expand Up @@ -365,17 +369,22 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
* @param _voteOptionIndices Vote option index.
* @param _tallyResults The results of vote tally for the recipients.
* @param _tallyResultProofs Proofs of correctness of the vote tally results.
* @param _totalSpent spent field retrieved in the totalSpentVoiceCredits object
* @param _tallyResultSalt the respective salt in the results object in the tally.json
* @param _spentVoiceCreditsHashes hashLeftRight(number of spent voice credits, spent salt)
* @param _perVOSpentVoiceCreditsHashes hashLeftRight(merkle root of the no spent voice credits per vote option, perVOSpentVoiceCredits salt)
* @param _newResultsCommitment The salted commitment of the vote tally for this batch of leaves plus the vote tally from currentResults
* @param _spentVoiceCreditsHash hashLeftRight(number of spent voice credits, spent salt)
* @param _perVOSpentVoiceCreditsHash hashLeftRight(merkle root of the no spent voice credits per vote option, perVOSpentVoiceCredits salt)
*/
function addTallyResults(
uint256[] calldata _voteOptionIndices,
uint256[] calldata _tallyResults,
uint256[][][] calldata _tallyResultProofs,
uint256 _totalSpent,
Dismissed Show dismissed Hide dismissed
uint256 _totalSpentSalt,
Dismissed Show dismissed Hide dismissed
uint256 _tallyResultSalt,
uint256 _spentVoiceCreditsHashes,
uint256 _perVOSpentVoiceCreditsHashes
uint256 _newResultsCommitment,
Dismissed Show dismissed Hide dismissed
uint256 _spentVoiceCreditsHash,
Dismissed Show dismissed Hide dismissed
uint256 _perVOSpentVoiceCreditsHash
Dismissed Show dismissed Hide dismissed
) public virtual onlyOwner {
if (!isTallied()) {
revert VotesNotTallied();
Expand All @@ -390,15 +399,28 @@ contract Tally is Ownable, SnarkCommon, CommonUtilities, Hasher, DomainObjs, ITa
_tallyResults[i],
_tallyResultProofs[i],
_tallyResultSalt,
_spentVoiceCreditsHashes,
_perVOSpentVoiceCreditsHashes,
_spentVoiceCreditsHash,
_perVOSpentVoiceCreditsHash,
voteOptionTreeDepth
);

unchecked {
i++;
}
}

bool verified = verifySpentVoiceCredits(
0xmad marked this conversation as resolved.
Show resolved Hide resolved
_totalSpent,
_totalSpentSalt,
_newResultsCommitment,
_perVOSpentVoiceCreditsHash
);

if (!verified) {
revert IncorrectSpentVoiceCredits();
}

totalSpent = _totalSpent;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/contracts/tasks/helpers/Prover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ export class Prover {
tallyData.results.tally.map((_, index) => index),
tallyResults,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
tallyData.perVOSpentVoiceCredits?.commitment ?? 0n,
)
Expand Down
26 changes: 26 additions & 0 deletions packages/contracts/tests/Tally.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ describe("TallyVotes", () => {
tallyData.results.tally.map((_, index) => index),
tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
0n,
),
Expand Down Expand Up @@ -372,12 +375,29 @@ describe("TallyVotes", () => {

const indices = tallyData.results.tally.map((_, index) => index);

await expect(
tallyContract.addTallyResults(
indices,
tallyData.results.tally,
tallyResultProofs,
0n,
0n,
tallyData.results.salt,
0n,
tallyData.totalSpentVoiceCredits.commitment,
newPerVOSpentVoiceCreditsCommitment,
),
).to.be.revertedWithCustomError(tallyContract, "IncorrectSpentVoiceCredits");

await tallyContract
.addTallyResults(
indices,
tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
newPerVOSpentVoiceCreditsCommitment,
)
Expand All @@ -394,7 +414,10 @@ describe("TallyVotes", () => {
indices,
tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
newPerVOSpentVoiceCreditsCommitment,
)
Expand Down Expand Up @@ -436,7 +459,10 @@ describe("TallyVotes", () => {
tallyData.results.tally.map((_, index) => index),
tallyData.results.tally,
tallyResultProofs,
tallyData.totalSpentVoiceCredits.spent,
tallyData.totalSpentVoiceCredits.salt,
tallyData.results.salt,
tallyData.results.commitment,
tallyData.totalSpentVoiceCredits.commitment,
0n,
),
Expand Down
Loading