-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
362 lines (334 loc) · 11.4 KB
/
main.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
from utilities.verification import Initialiser
from utilities.verification import Authentication
from node import Node
from blockchain import Blockchain
import socket
import pickle as pk
from bson.json_util import loads
from bson.json_util import dumps
app = Flask(__name__)
CORS(app)
@app.route('/', methods=['GET'])
def get_node_ui():
return send_from_directory('ui', 'index.html')
@app.route('/vote', methods=['GET'])
def get_vote_ui():
return send_from_directory('ui', 'voting.html')
@app.route('/results', methods=['GET'])
def get_result_ui():
return send_from_directory('ui', 'results.html')
@app.route('/node', methods=['POST'])
def create_node():
node.create_keys()
if(node.save_keys()):
global blockchain
blockchain = Blockchain(node.public_key, port)
response = {
'public_key': node.public_key,
'private_key': node.private_key,
'votes': blockchain.count_votes(),
'node_id': node.node_id
}
return jsonify(response), 201
else:
response = {
'message': 'Saving the keys failed'
}
return jsonify(response), 500
@app.route('/node', methods=['GET'])
def load_node():
if(node.load_keys()):
global blockchain
blockchain = Blockchain(node.public_key, port)
response = {
'public_key': node.public_key,
'private_key': node.private_key,
'votes': blockchain.count_votes(),
'node_id': node.node_id
}
return jsonify(response), 201
else:
response = {
'message': 'Keys not available.'
}
return jsonify(response), 500
@app.route('/votes', methods=['GET'])
def get_votes():
total_votes = blockchain.count_votes()
if(total_votes != None):
response = {
'message': 'Fetched total_votes successfully.',
'votes': total_votes
}
return jsonify(response), 200
else:
response = {
'message': 'Loading balance failed',
'wallet_set_up': node.public_key != None
}
return jsonify(response), 500
@app.route('/broadcast-transaction', methods=['POST'])
def broadcast_transaction():
values = request.get_json()
if(not values):
response = {'message':'No data found.'}
return jsonify(response), 400
required_fields = ['nodeId', 'candidateId', 'partyId', 'signature']
if not all(field in values for field in required_fields):
response = {
'message': 'Required data missing.'
}
return jsonify(response), 400
success = blockchain.add_transaction(values['nodeId'], values['candidateId'], values['partyId'], values['signature'], is_recieving=True)
if(success):
response = {
'message': 'Successfully added transaction.',
'transaction': {
'nodeId': values['nodeId'],
'candidateId': values['candidateId'],
'partyId': values['partyId'],
'signature': values['signature']
}
}
return jsonify(response), 201
else:
response = {
'message': 'Creation of transaction failed.'
}
return jsonify(response), 500
@app.route('/broadcast-block', methods=['POST'])
def broadcast_block():
values = request.get_json()
if(not values):
response = {'message':'No data found.'}
return jsonify(response), 400
if 'block' not in values:
response = {'message': 'Required data missing.'}
return jsonify(response), 400
block = values['block']
if block['index'] == blockchain.get_chain()[-1].index + 1:
if(blockchain.add_block(block)):
response = {'message': 'Block added.'}
return jsonify(response), 201
else:
response = {'message': 'Block seems invalid.'}
return jsonify(response), 409
elif block['index'] > blockchain.get_chain()[-1].index:
response = {'message':'Blockchain seems to be differ from local blockchain'}
blockchain.resolve_conflicts = True
return jsonify(response), 200
else:
response = {'message':'Blockchain seems to be shorter, block not added'}
return jsonify(response), 409
@app.route('/transaction', methods=['POST'])
def add_transaction():
if(node.public_key == None):
response = {
'message': 'No wallet set up.'
}
return jsonify(response), 400
values = request.get_json()
if(not values):
response = {
'message': 'No data found.'
}
return jsonify(response), 400
required_fields = ['candidateId', 'partyId']
if not all(field in values for field in required_fields):
response = {
'message': 'Required data missing.'
}
return jsonify(response), 400
candidateId = values['candidateId']
partyId = values['partyId']
signature = node.sign_transaction(node.public_key, candidateId, partyId)
success = blockchain.add_transaction(node.public_key, candidateId, partyId, signature)
if(success):
response = {
'message': 'Successfully added transaction.',
'transaction': {
'nodeId': node.public_key,
'candidateId': candidateId,
'partyId': partyId,
'signature': signature
},
'votes': blockchain.count_votes()
}
return jsonify(response), 201
else:
response = {
'message': 'Creation of transaction failed.'
}
return jsonify(response), 500
@app.route('/mine', methods=['POST'])
def mine():
if(blockchain.resolve_conflicts == True):
response = {'message':'Resolve conflicts first, block not added!'}
return jsonify(response), 500
block = blockchain.mine_block()
if(block != None):
dict_block = block.__dict__.copy()
dict_block['transactions'] = [tx.__dict__ for tx in dict_block['transactions']]
response = {
'message': 'Block added sucessfully',
'block': dict_block,
'votes': blockchain.count_votes()
}
return jsonify(response), 201
else:
response = {
'message': 'Adding a block failed',
'node_set_up': node.public_key != None
}
return jsonify(response), 500
@app.route('/resolve-conflicts', methods=['POST'])
def resolve_conflicts():
replaced = blockchain.resolve()
if(replaced):
response = {'message': 'Chain was replaced!'}
else:
response = {'message': 'Local chain kept!'}
return jsonify(response), 200
@app.route('/transactions', methods=['GET'])
def get_open_transactions():
transactions = blockchain.get_open_transactions()
dict_transactions = [tx.__dict__ for tx in transactions]
return jsonify(dict_transactions), 200
@app.route('/chain', methods=['GET'])
def get_chain():
chain_snapshot = blockchain.get_chain()
dict_chain = [block.__dict__.copy() for block in chain_snapshot]
for dict_block in dict_chain:
dict_block['transactions'] = [tx.__dict__ for tx in dict_block['transactions']]
return jsonify(dict_chain), 200
@app.route('/peer', methods=['POST'])
def add_peer():
values = request.get_json()
if not (values):
response = {
'message': 'No data attached.'
}
return jsonify(response), 400
if('peer' not in values):
response = {
'message': 'No peer data found.'
}
return jsonify(response), 400
peer = values['peer']
blockchain.add_peer_node(peer)
response = {
'message': 'Peer added successfully.',
'all_peers': blockchain.get_peer_nodes()
}
return jsonify(response), 201
@app.route('/peer/<peer_url>', methods=['DELETE'])
def remove_peer(peer_url):
if(peer_url == '' or peer_url == None):
response = {
'message': 'No peer found.'
}
return jsonify(response), 400
blockchain.remove_peer_node(peer_url)
response = {
'message': 'Peer removed.',
'all_peers': blockchain.get_peer_nodes()
}
return jsonify(response), 200
@app.route('/peer', methods=['GET'])
def get_peers():
peers = blockchain.get_peer_nodes()
response = {
'all_peers': peers
}
return jsonify(response), 200
@app.route('/data', methods=['GET'])
def send_data():
response = {
'constituencyData': dumps(loads(node.data)[0])
}
return jsonify(response), 200
@app.route('/authenticate', methods=['POST'])
def authenticate_voter():
values = request.get_json()
scannedId = values['scannedId']
validity, voterData = Authentication.authenticate(scannedId, node)
response = {
'validVoter' : validity,
'voterData': voterData
}
return jsonify(response), 200
@app.route('/mark-attendence', methods=['POST'])
def mark_voter_attendence():
values = request.get_json()
roll_no = values['attendenceRollNo']
mark = node.mark_attendence(roll_no)
response = {
'marked': mark
}
return jsonify(response), 200
@app.route('/upload-attendence', methods=['POST'])
def upload_attendence():
attendedVoters = loads(node.data)[0]['Voters']
ConstId = loads(node.data)[0]['ConstId']
s = socket.socket()
s.connect(('localhost', 12345))
s.send('upload-attendence'.encode())
print(s.recv(1024).decode())
s.send(pk.dumps((ConstId,attendedVoters)))
serverResponse = s.recv(1024).decode()
s.close()
response = {
'serverResponse': serverResponse
}
return jsonify(response), 200
@app.route('/upload-chain', methods=['POST'])
def upload_chain():
chain_snapshot = blockchain.get_chain()
dict_chain = [block.__dict__.copy() for block in chain_snapshot]
for dict_block in dict_chain:
dict_block['transactions'] = [tx.__dict__ for tx in dict_block['transactions']]
ConstId = loads(node.data)[0]['ConstId']
s = socket.socket()
s.connect(('localhost', 12345))
s.send('upload-chain'.encode())
print(s.recv(1024).decode())
s.send(pk.dumps((ConstId,dict_chain)))
serverResponse = s.recv(1024).decode()
s.close()
response = {
'serverResponse': serverResponse
}
return jsonify(response), 200
@app.route('/result-data', methods=['POST'])
def download_result_data():
s = socket.socket()
s.connect(('localhost', 12345))
s.send('download-results'.encode())
serverResponse = pk.loads(s.recv(100000))
tableData = [list(tup) for tup in serverResponse[0]]
chain = serverResponse[1]
s.close()
response = {
'chain': chain,
'tableData': tableData
}
return jsonify(response), 200
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('-p', '--port', type=int, default=5000)
parser.add_argument('-m', '--mode', type=str, default='initialize')
args = parser.parse_args()
port = args.port
mode = args.mode
if(mode == 'initialize'):
print('\n'+(5*'=')+'System Initalization'+(5*'='))
print('\nPlease Scan constituency smart card...')
secret = Initialiser.scan_card()
elif(mode == 'restart'):
secret = 'NA'
node = Node(port, secret)
blockchain = Blockchain(node.public_key, port)
app.run(host='0.0.0.0', port=port)