|
| 1 | +from electionguard.ballot import CiphertextAcceptedBallot |
| 2 | +from electionguard.election import ( |
| 3 | + CiphertextElectionContext, |
| 4 | + ElectionDescription, |
| 5 | + InternalElectionDescription, |
| 6 | +) |
| 7 | +from electionguard.tally import ( |
| 8 | + publish_ciphertext_tally, |
| 9 | + CiphertextTally, |
| 10 | + PublishedCiphertextTally, |
| 11 | +) |
| 12 | +from fastapi import APIRouter, Body, HTTPException |
| 13 | +from pydantic import BaseModel |
| 14 | +from typing import Any, List, Optional |
| 15 | + |
| 16 | +router = APIRouter() |
| 17 | + |
| 18 | + |
| 19 | +class AccumulateTallyRequest(BaseModel): |
| 20 | + ballots: List[Any] |
| 21 | + encrypted_tally: Optional[Any] = None |
| 22 | + description: Any |
| 23 | + context: Any |
| 24 | + |
| 25 | + |
| 26 | +@router.post("/accumulate") |
| 27 | +def accumulate_tally(request: AccumulateTallyRequest = Body(...)) -> Any: |
| 28 | + """ |
| 29 | + Accumulate ballots into a new or existing tally |
| 30 | + """ |
| 31 | + |
| 32 | + ballots = [ |
| 33 | + CiphertextAcceptedBallot.from_json_object(ballot) for ballot in request.ballots |
| 34 | + ] |
| 35 | + description = ElectionDescription.from_json_object(request.description) |
| 36 | + internal_description = InternalElectionDescription(description) |
| 37 | + context = CiphertextElectionContext.from_json_object(request.context) |
| 38 | + published_tally: Optional[PublishedCiphertextTally] = ( |
| 39 | + PublishedCiphertextTally.from_json_object(request.encrypted_tally) |
| 40 | + if request.encrypted_tally |
| 41 | + else None |
| 42 | + ) |
| 43 | + |
| 44 | + tally = _get_new_or_existing_tally(internal_description, context, published_tally) |
| 45 | + |
| 46 | + tally_succeeded = tally.batch_append(ballots) |
| 47 | + |
| 48 | + if tally_succeeded: |
| 49 | + published_tally = publish_ciphertext_tally(tally) |
| 50 | + return published_tally.to_json_object() |
| 51 | + else: |
| 52 | + raise HTTPException( |
| 53 | + status_code=500, detail="Tally accumulation was unsuccessful" |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def _get_new_or_existing_tally( |
| 58 | + description: ElectionDescription, |
| 59 | + context: CiphertextElectionContext, |
| 60 | + published_tally: PublishedCiphertextTally, |
| 61 | +) -> CiphertextTally: |
| 62 | + if not published_tally: |
| 63 | + return CiphertextTally("election-results", description, context) |
| 64 | + |
| 65 | + full_tally = CiphertextTally(published_tally.object_id, description, context) |
| 66 | + full_tally.cast = published_tally.cast |
| 67 | + |
| 68 | + return full_tally |
0 commit comments