-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.py
More file actions
95 lines (83 loc) · 3.42 KB
/
Handler.py
File metadata and controls
95 lines (83 loc) · 3.42 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
from Merkle import *
from hashlib import sha256
hash_function = sha256
# Handler (i.e. blockchain) for each election.
# Manages opening, closing of elections
# Adds votes, verifies votes, etc
# holds the chain and all the pointers
class Handler:
def __init__(self, name, el_id, options):
self.name = name # name of the election
self.id = el_id # id of the election
self.options = options # choices for the election
self.chain = [] # list of trees
self.snapshot = [] # snapshot of last time chain was counted
self.active_tree = Merkle(True) # pointer to open tree, setting first block as root
# add vote to tree
# need to get the id of the tree
def vote(self, v):
return_str = self.active_tree.add_leaf(v)
if return_str[0] == "full":
self.cap_tree()
return return_str[1] #return hash of vote
def count(self):
"""
1 - Check each block is linked correctly
2 - Compare current hashes against snapshot hashes
Take snapshot of block hashes
3 - In each block sum votes
"""
chain = True
proof = True
hashes = True
votes = []
# verifying all proof of work is still valid
if len(self.chain) > 1:
for i in range(1, len(self.chain)):
if self.chain[i - 1].proof_work != self.chain[i].prev_block:
chain = False
else:
print "chain is still too short to properly verify"
# verifying the node proof of work match their stored value
for b in self.chain:
if b.validate() == False:
proof = False
# verifying the tree values match stored values
if len(self.snapshot) > 1:
for i in range(len(self.snapshot)):
print "%s - snapshot:%s chain:%s" % (i, len(self.snapshot), len(self.chain))
if self.snapshot[i] != self.chain[i].proof_work:
hashes = False
print "don't match"
else:
print "hashes match"
else:
for b in self.chain:
self.snapshot.append(b.proof_work)
print self.snapshot
# if any of the checks return false then return false and empty votes
if chain != True or proof != True or hashes != True:
return {'secure':False,'votes':votes}
else:
# run through all trees and count their votes to return with True condition
for b in self.chain:
votes.append(b.count())
return {'secure':True,'votes':votes}
# if tree is length of 10
def cap_tree(self):
# build the tree
print self.active_tree.build()
# add previous blocks hash to this block
# self.active_tree.prev_block = self.chain[-1].proof_work
# inert into chain
self.chain.append(self.active_tree)
# create a new merkle tree
self.active_tree = Merkle(False, self.active_tree.proof_work)
print 'Number of blocks', len(self.chain)
# change the name of the election
def change_name(self, new_name):
self.name = new_name
# change the options for this elections
# takes the whole set of options, NOT a single one to add or remove
def change_options(self, new_opt):
self.options = new_opt