2017-09-22 15:59:15 +10:00
|
|
|
# Eos - Verifiable elections
|
2018-01-11 22:06:21 +11:00
|
|
|
# Copyright © 2017-18 RunasSudo (Yingtong Li)
|
2017-09-22 15:59:15 +10:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from eos.core.objects import *
|
2017-12-12 20:32:02 +11:00
|
|
|
from eos.core.tasks import *
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2018-01-11 22:06:21 +11:00
|
|
|
class WorkflowTaskStatus(EosEnum):
|
|
|
|
UNKNOWN = 0
|
|
|
|
NOT_READY = 10
|
|
|
|
READY = 20
|
|
|
|
ENTERED = 30
|
|
|
|
#COMPLETE = 40
|
|
|
|
EXITED = 50
|
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
class WorkflowTask(EmbeddedObject):
|
|
|
|
depends_on = []
|
|
|
|
provides = []
|
|
|
|
|
2018-01-11 22:06:21 +11:00
|
|
|
status = EnumField(WorkflowTaskStatus, is_hashed=False)
|
2017-11-24 20:26:18 +11:00
|
|
|
exited_at = DateTimeField(is_hashed=False)
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2017-09-24 13:25:16 +10:00
|
|
|
def __init__(self, *args, **kwargs):
|
2017-09-22 15:59:15 +10:00
|
|
|
super().__init__(*args, **kwargs)
|
2017-09-24 13:25:16 +10:00
|
|
|
|
|
|
|
def post_init(self):
|
|
|
|
super().post_init()
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2017-09-24 13:25:16 +10:00
|
|
|
self.workflow = self.recurse_parents(Workflow)
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2018-01-11 22:06:21 +11:00
|
|
|
if self.status == WorkflowTaskStatus.UNKNOWN:
|
|
|
|
self.status = WorkflowTaskStatus.READY if self.are_dependencies_met() else WorkflowTaskStatus.NOT_READY
|
2017-09-22 15:59:15 +10:00
|
|
|
|
|
|
|
self.listeners = {
|
2017-09-28 16:46:30 +10:00
|
|
|
'enter': [],
|
2017-09-22 15:59:15 +10:00
|
|
|
'exit': []
|
|
|
|
}
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
|
|
|
|
def on_dependency_exit():
|
2018-01-11 22:06:21 +11:00
|
|
|
self.status = WorkflowTaskStatus.READY if self.are_dependencies_met() else WorkflowTaskStatus.NOT_READY
|
2017-09-22 15:59:15 +10:00
|
|
|
for depends_on_desc in self.depends_on:
|
|
|
|
for depends_on_task in self.workflow.get_tasks(depends_on_desc):
|
|
|
|
depends_on_task.listeners['exit'].append(on_dependency_exit)
|
|
|
|
|
|
|
|
def are_dependencies_met(self):
|
|
|
|
for depends_on_desc in self.depends_on:
|
|
|
|
for depends_on_task in self.workflow.get_tasks(depends_on_desc):
|
2018-01-11 22:06:21 +11:00
|
|
|
if depends_on_task.status is not WorkflowTaskStatus.EXITED:
|
2017-09-22 15:59:15 +10:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def satisfies(cls, descriptor):
|
2017-12-12 20:32:02 +11:00
|
|
|
return cls._name == descriptor or descriptor in cls.provides or (descriptor in EosObject.objects and issubclass(cls, EosObject.lookup(descriptor)))
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2017-09-28 16:46:30 +10:00
|
|
|
def on_enter(self):
|
|
|
|
self.exit()
|
|
|
|
|
|
|
|
def enter(self):
|
2018-01-11 22:06:21 +11:00
|
|
|
if self.status is not WorkflowTaskStatus.READY:
|
2017-09-28 16:46:30 +10:00
|
|
|
raise Exception('Attempted to enter a task when not ready')
|
|
|
|
|
2018-01-11 22:06:21 +11:00
|
|
|
self.status = WorkflowTaskStatus.ENTERED
|
2017-09-28 16:46:30 +10:00
|
|
|
self.fire_event('enter')
|
|
|
|
self.on_enter()
|
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
def fire_event(self, event):
|
|
|
|
for listener in self.listeners[event]:
|
|
|
|
listener()
|
|
|
|
|
2017-09-28 16:46:30 +10:00
|
|
|
def on_exit(self):
|
2017-11-24 20:26:18 +11:00
|
|
|
self.exited_at = DateTimeField.now()
|
2017-09-28 16:46:30 +10:00
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
def exit(self):
|
2018-01-11 22:06:21 +11:00
|
|
|
if self.status is not WorkflowTaskStatus.ENTERED:
|
2017-09-28 16:46:30 +10:00
|
|
|
raise Exception('Attempted to exit a task when not entered')
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2018-01-11 22:06:21 +11:00
|
|
|
self.status = WorkflowTaskStatus.EXITED
|
2017-09-22 15:59:15 +10:00
|
|
|
self.fire_event('exit')
|
2017-09-28 16:46:30 +10:00
|
|
|
self.on_exit()
|
2017-12-12 21:19:02 +11:00
|
|
|
|
|
|
|
def get_entry_task(self):
|
|
|
|
election = self.recurse_parents('eos.base.election.Election')
|
|
|
|
|
|
|
|
for task in WorkflowTaskEntryTask.get_all():
|
|
|
|
if task.election_id == election._id and task.workflow_task == self._name:
|
|
|
|
return task
|
|
|
|
|
|
|
|
return None
|
2017-09-22 15:59:15 +10:00
|
|
|
|
|
|
|
class Workflow(EmbeddedObject):
|
2017-09-22 16:57:40 +10:00
|
|
|
tasks = EmbeddedObjectListField()
|
2017-09-22 15:59:15 +10:00
|
|
|
meta = {
|
|
|
|
'abstract': True
|
|
|
|
}
|
|
|
|
|
2017-09-24 13:25:16 +10:00
|
|
|
def __init__(self, *args, **kwargs):
|
2017-09-22 15:59:15 +10:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_tasks(self, descriptor):
|
2017-09-25 15:19:48 +10:00
|
|
|
#yield from (task for task in self.tasks if task.satisfies(descriptor))
|
2017-12-04 13:51:45 +11:00
|
|
|
for task in self.tasks:
|
2017-09-25 15:19:48 +10:00
|
|
|
if task.satisfies(descriptor):
|
|
|
|
yield task
|
2017-09-22 15:59:15 +10:00
|
|
|
|
|
|
|
def get_task(self, descriptor):
|
|
|
|
try:
|
|
|
|
return next(self.get_tasks(descriptor))
|
|
|
|
except StopIteration:
|
|
|
|
return None
|
|
|
|
|
2017-12-12 20:32:02 +11:00
|
|
|
class WorkflowTaskEntryTask(Task):
|
|
|
|
election_id = UUIDField()
|
|
|
|
workflow_task = StringField()
|
|
|
|
|
|
|
|
def _run(self):
|
|
|
|
election = EosObject.lookup('eos.base.election.Election').get_by_id(self.election_id)
|
|
|
|
task = election.workflow.get_task(self.workflow_task)
|
|
|
|
task.enter()
|
|
|
|
election.save()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def label(self):
|
|
|
|
election = EosObject.lookup('eos.base.election.Election').get_by_id(self.election_id)
|
|
|
|
task = election.workflow.get_task(self.workflow_task)
|
|
|
|
return task.label + ' – ' + election.name
|
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
# Concrete tasks
|
|
|
|
# ==============
|
|
|
|
|
|
|
|
class TaskConfigureElection(WorkflowTask):
|
2017-12-07 16:04:24 +11:00
|
|
|
label = 'Freeze the election'
|
2017-12-07 15:33:11 +11:00
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
#def on_enter(self):
|
2018-01-11 22:06:21 +11:00
|
|
|
# self.status = WorkflowTaskStatus.COMPLETE
|
2017-09-22 15:59:15 +10:00
|
|
|
|
|
|
|
class TaskOpenVoting(WorkflowTask):
|
2017-12-07 15:33:11 +11:00
|
|
|
label = 'Open voting'
|
2017-09-22 15:59:15 +10:00
|
|
|
depends_on = ['eos.base.workflow.TaskConfigureElection']
|
|
|
|
|
2017-09-24 13:25:16 +10:00
|
|
|
class TaskCloseVoting(WorkflowTask):
|
2017-12-07 15:33:11 +11:00
|
|
|
label = 'Close voting'
|
2017-09-24 13:25:16 +10:00
|
|
|
depends_on = ['eos.base.workflow.TaskOpenVoting']
|
|
|
|
|
2017-09-28 16:46:30 +10:00
|
|
|
class TaskDecryptVotes(WorkflowTask):
|
2017-12-07 15:33:11 +11:00
|
|
|
label = 'Decrypt the votes'
|
2017-09-28 16:46:30 +10:00
|
|
|
depends_on = ['eos.base.workflow.TaskCloseVoting']
|
|
|
|
|
|
|
|
def on_enter(self):
|
|
|
|
election = self.recurse_parents('eos.base.election.Election')
|
|
|
|
|
|
|
|
for _ in range(len(election.questions)):
|
2017-12-12 20:32:02 +11:00
|
|
|
election.results.append(EosObject.lookup('eos.base.election.RawResult')())
|
2017-09-28 16:46:30 +10:00
|
|
|
|
|
|
|
for voter in election.voters:
|
2017-12-15 20:51:57 +11:00
|
|
|
if len(voter.votes.get_all()) > 0:
|
|
|
|
vote = voter.votes.get_all()[-1]
|
2017-11-23 23:10:57 +11:00
|
|
|
ballot = vote.ballot
|
2017-12-04 13:51:45 +11:00
|
|
|
for q_num in range(len(ballot.encrypted_answers)):
|
|
|
|
plaintexts, answer = ballot.encrypted_answers[q_num].decrypt()
|
|
|
|
election.results[q_num].plaintexts.append(plaintexts)
|
|
|
|
election.results[q_num].answers.append(answer)
|
2017-09-28 16:46:30 +10:00
|
|
|
|
|
|
|
self.exit()
|
|
|
|
|
|
|
|
class TaskReleaseResults(WorkflowTask):
|
2017-12-07 15:33:11 +11:00
|
|
|
label = 'Release the results'
|
2017-09-28 16:46:30 +10:00
|
|
|
depends_on = ['eos.base.workflow.TaskDecryptVotes']
|
|
|
|
|
2017-09-22 15:59:15 +10:00
|
|
|
# Concrete workflows
|
|
|
|
# ==================
|
|
|
|
|
|
|
|
class WorkflowBase(Workflow):
|
2017-09-24 13:25:16 +10:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2017-09-24 13:25:16 +10:00
|
|
|
self.tasks.append(TaskConfigureElection())
|
|
|
|
self.tasks.append(TaskOpenVoting())
|
|
|
|
self.tasks.append(TaskCloseVoting())
|
2017-09-28 16:46:30 +10:00
|
|
|
self.tasks.append(TaskDecryptVotes())
|
|
|
|
self.tasks.append(TaskReleaseResults())
|