-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
138 lines (100 loc) · 3.4 KB
/
app.py
File metadata and controls
138 lines (100 loc) · 3.4 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from blockchain import *
from collections import Counter
import requests
def create(server):
name = input('Name? ')
sk, vk = generate_key_pair()
resp = requests.post(server + '/register',
params={'vk': vk, 'name': name}).json()
if resp['success']:
print('Successfully registered.')
else:
print('Registration Failed. Are you already registered?')
with open('key', 'w+') as f:
f.write(sk)
f.write('\n')
f.write(vk)
def vote(server):
with open('key') as f:
lines = f.readlines()
[sk, vk] = [x.strip() for x in lines]
choices = requests.get(server + '/choices').json()
print('Available candidates are:', choices)
choice = input('Who do you vote for? ')
message = generate_vote_message(sk, vk, choice)
resp = requests.post(server + '/vote', params={'vote': message})
resp = resp.json()
if resp['success']:
print('Successfully voted.')
else:
print('Voting failed. :(')
def fetch_ledger(server):
resp = requests.get(server + '/ledger').json()
return resp['vks'], resp['ledger']
def results(server):
servks, ledger = fetch_ledger(server)
keys = {}
results = Counter()
for message in ledger:
message = json.loads(message)
if not verify_message(message):
continue
vk = message['vk']
payload = json.loads(message['payload'])
if payload['action'] == 'vote' and vk in keys:
new_choice = payload['choice']
new_time = payload['timestamp']
old_time, old_choice = keys[vk]
if old_time < new_time:
keys[vk] = new_time, new_choice
results[new_choice] += 1
if old_choice:
results[old_choice] -= 1
elif payload['action'] == 'register' and vk in servks:
new_vk = payload['vk']
keys[new_vk] = 0, None
for cand, votes in results.items():
print(cand, 'got', votes, 'votes')
def checkvote(server):
servks, ledger = fetch_ledger(server)
with open('key') as f:
lines = f.readlines()
[_, vk] = [x.strip() for x in lines]
curr_time, curr_choice = 0, None
for message in ledger:
message = json.loads(message)
if not verify_message(message):
continue
if message['vk'] != vk:
continue
payload = json.loads(message['payload'])
if payload['action'] == 'vote':
new_choice = payload['choice']
new_time = payload['timestamp']
if curr_time < new_time:
curr_time = new_time
curr_choice = new_choice
print('You chose: ', curr_choice)
def main():
server = input('Server: ')
if not requests.get(server + '/status').ok:
print('Server not active')
return
while True:
action = input('Action (register, vote, results, checkvote): ')
action = action.lower()
if action == 'register':
create(server)
elif action == 'results':
results(server)
elif action == 'vote':
vote(server)
elif action == 'checkvote':
checkvote(server)
else:
print('Option not recognized')
cont = input('Perform another action (y/n)?')
if cont != 'y':
break
if __name__ == '__main__':
main()