Skip to content

Commit

Permalink
Convert to asyncio
Browse files Browse the repository at this point in the history
  • Loading branch information
txus committed Apr 16, 2021
1 parent 134d8a1 commit 67204d4
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 297 deletions.
27 changes: 24 additions & 3 deletions .code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"name": "Bulletin Board Gem",
"path": "bulletin_board/ruby-client"
},
{
"name": "Bulletin Board JS Client",
"path": "bulletin_board/js-client"
},
{
"name": "Documentation",
"path": "docs"
Expand All @@ -20,13 +24,30 @@
"name": "Dummy JS Adapter",
"path": "voting_schemes/dummy/js-adapter"
},
{
"name": "ElectionGuard Wrapper",
"path": "voting_schemes/electionguard/python-wrapper"
},
{
"name": "ElectionGuard JS Adapter",
"path": "voting_schemes/electionguard/js-adapter"
},
{
"name": "ElectionGuard Wrapper",
"path": "voting_schemes/electionguard/python-wrapper"
"name": "ElectionGuard Ruby Adapter",
"path": "voting_schemes/electionguard/ruby-adapter"
},
{
"name": "ElectionGuard Python2JS",
"path": "voting_schemes/electionguard/python-to-js"
},
{
"name": "ElectionGuard Pyodide Base Packages",
"path": "voting_schemes/electionguard/pyodide-base-packages"
},
{
"name": "ElectionGuard Pyodide Verifier",
"path": "voting_schemes/electionguard/verifier"
}
]
],
"settings": {}
}
1 change: 1 addition & 0 deletions voting_schemes/electionguard/python-wrapper/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ verify_ssl = true
[dev-packages]
flake8 = ">=3.8.3"
pytest = "*"
pytest-asyncio = "*"
black = "*"

[packages]
Expand Down
258 changes: 133 additions & 125 deletions voting_schemes/electionguard/python-wrapper/Pipfile.lock

Large diffs are not rendered by default.

116 changes: 58 additions & 58 deletions voting_schemes/electionguard/python-wrapper/integration_results.jsonl

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from electionguard.types import GUARDIAN_ID
from electionguard.utils import get_optional

from .common import Content, Context, ElectionStep, Wrapper
from .common import Content, Context, ElectionStep, Wrapper, async_wrap
from .dummy_scheduler import DummyScheduler
from .messages import (
Compensation,
Expand Down Expand Up @@ -292,6 +292,7 @@ def __init__(self, recorder=None) -> None:
BulletinBoardContext(), ProcessCreateElection(), recorder=recorder
)

@async_wrap
def add_ballots(self, ballots: List[str]):
submitted_ballots: List[Tuple[None, SubmittedBallot]] = []
for ballot in ballots:
Expand All @@ -302,6 +303,7 @@ def add_ballots(self, ballots: List[str]):

self.context.tally.batch_append(submitted_ballots, DummyScheduler()) # type: ignore

@async_wrap
def add_ballot(self, ballot: str):
ciphertext_ballot = deserialize(ballot, CiphertextBallot)
submitted_ballot = from_ciphertext_ballot(
Expand All @@ -312,6 +314,7 @@ def add_ballot(self, ballot: str):
DummyScheduler(), # type: ignore
)

@async_wrap
def get_tally_cast(self) -> Dict:
return {
"message_type": "tally.cast",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import asyncio
from functools import partial, wraps
import json
import logging as log
from pathlib import Path
Expand All @@ -9,8 +11,8 @@
from electionguard.election_builder import ElectionBuilder
from electionguard.manifest import InternalManifest, Manifest

from .utils import InvalidElectionDescription
from .election import parse_description
from .utils import InvalidElectionDescription

try:
import cPickle as pickle # type: ignore
Expand All @@ -27,6 +29,17 @@ def unwrap(x: Optional[X], message="You messed up") -> X:
return x



def async_wrap(func):
@wraps(func)
async def run(*args, loop=None, executor=None, **kwargs):
if loop is None:
loop = asyncio.get_event_loop()
pfunc = partial(func, *args, **kwargs)
return await loop.run_in_executor(executor, pfunc)
return run


class Context:
election: Manifest
election_builder: ElectionBuilder
Expand Down Expand Up @@ -123,6 +136,7 @@ def __init__(
def skip_message(self, message_type: str) -> bool:
return self.step.skip_message(message_type)

@async_wrap
def process_message(
self, message_type: str, message: Optional[Message] | dict
) -> List[Message]:
Expand Down Expand Up @@ -156,9 +170,12 @@ def is_key_ceremony_done(self) -> bool:
def is_tally_done(self) -> bool:
raise NotImplementedError


@async_wrap
def backup(self) -> bytes:
return pickle.dumps(self)

@classmethod
@async_wrap
def restore(cls: Type[T], backup: bytes) -> T:
return pickle.loads(backup)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from electionguard.types import CONTEST_ID, GUARDIAN_ID, SELECTION_ID
from electionguard.utils import get_optional

from .common import Content, Context, ElectionStep, Wrapper, unwrap
from .common import Content, Context, ElectionStep, Wrapper, async_wrap, unwrap
from .messages import (
Compensation,
KeyCeremonyResults,
Expand Down Expand Up @@ -416,12 +416,14 @@ def __init__(self, guardian_id: GUARDIAN_ID, recorder=None) -> None:
TrusteeContext(guardian_id), self.starting_step(), recorder=recorder
)

@async_wrap
def is_key_ceremony_done(self) -> bool:
return self.step.__class__ in [
ProcessTallyCast,
ProcessEndTally,
ProcessPublishResults,
]

@async_wrap
def is_tally_done(self) -> bool:
return self.step.__class__ in [ProcessPublishResults]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from electionguard.key_ceremony import ElectionJointKey
from electionguard.utils import get_optional

from .common import Content, Context, ElectionStep, Wrapper, unwrap
from .common import Content, Context, ElectionStep, Wrapper, async_wrap, unwrap
from .messages import KeyCeremonyResults
from .utils import MissingJointKey, deserialize, remove_nonces, serialize

Expand Down Expand Up @@ -66,6 +66,7 @@ def __init__(self, ballot_id: str, recorder=None) -> None:
super().__init__(VoterContext(), ProcessCreateElection(), recorder=recorder)
self.ballot_id = ballot_id

@async_wrap
def encrypt(
self, ballot: dict, ballot_style: Optional[str] = None, master_nonce: int = None
) -> Tuple[str, str]:
Expand Down
Loading

0 comments on commit 67204d4

Please sign in to comment.