forked from ron-rivest/audit-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saved_state.py
101 lines (73 loc) · 2.97 KB
/
saved_state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# saved_state.py
# Ronald L. Rivest
# August 1, 2017
# Routines to save and restore some stage between audit stages.
import json
import os
import multi
import utils
def write_initial_saved_state(e):
"""
Write the first saved-state, after the election-spec has been read."
"""
initial_stage_time = "0000-00-00-00-00-00" # stage_time for initial saved-state
e.sn_tp[initial_stage_time] = {}
for pbcid in e.pbcids:
# no sampling done yet
e.sn_tp[initial_stage_time][pbcid] = 0
e.plan_tp[initial_stage_time] = {}
for pbcid in e.pbcids:
e.plan_tp[initial_stage_time][pbcid] = int(e.max_audit_rate_p[pbcid])
e.status_tm[initial_stage_time] = {}
for mid in e.mids:
# initial contest state
e.status_tm[initial_stage_time][mid] = e.initial_status_m[mid]
ss = {} # saved state dict, to be written out
ss["stage_time"] = initial_stage_time
ss["sn_tp"] = e.sn_tp # sample sizes, by stage and pbcid
ss["status_tm"] = e.status_tm # measurement statuses, by stage and mid
ss["plan_tp"] = e.plan_tp # plan for next stage of audit
write_state(e, ss)
def write_intermediate_saved_state(e):
"""
Write an intermediate saved-state,
after the election-spec has been read and the first audit stage done.
"""
ss = {} # saved state dict, to be written out
ss["stage_time"] = e.stage_time
ss["sn_tp"] = e.sn_tp # sample sizes, by stage and pbcid
ss["status_tm"] = e.status_tm # measurement statuses, by stage and mid
ss["plan_tp"] = e.plan_tp # plan for next stage of audit
write_state(e, ss)
def write_state(e, ss):
"""
Save some state to 3-audit/34-audit-output/audit-output-saved-state.json
Data ss saved is needed in the next audit stage.
ss is a dict with the saved-state information, including
the stage_time.
"""
dirpath = os.path.join(multi.ELECTIONS_ROOT,
e.election_dirname,
"3-audit",
"34-audit-output")
os.makedirs(dirpath, exist_ok=True)
filename = os.path.join(dirpath,
"audit-output-saved-state-"+ss["stage_time"]+".json")
with open(filename, "w") as file:
json.dump(ss, file, indent=2)
def read_saved_state(e):
"""
Read state from latest 3-audit/34-audit-output/audit-output-saved-state.json
"""
dirpath = os.path.join(multi.ELECTIONS_ROOT,
e.election_dirname,
"3-audit",
"34-audit-output")
filename = utils.greatest_name(dirpath,
"audit-output-saved-state",
".json")
file_pathname = os.path.join(dirpath, filename)
file = open(file_pathname, "r")
e.saved_state = json.load(file)
if __name__ == "__main__":
pass