def castVote(self, vote): for i in range(10): vote = Vote(Voter("Bad Voter {}".format(i)), Candidate("Bad Candidate")) block = block(vote, self.chain.last) # mostly invalid proof proof = hash(block) self.chain.addBlock(block, proof) self.announceNewBlock() return true
def test_duplicateVoters(self): for voter in self.voters: node = self.network.nodes[randint(0,self.nNodes-1)] candidate = choice(self.candidates) vote = Vote(voter, candidate) print("{} goes to node {} and chooses to vote for candidate {}".format(voter, node, candidate)) self.assertEqual(node.castVote(vote), True) duplicateVoter = self.voters[0] node = self.network.nodes[randint(0,self.nNodes-1)] candidate = choice(self.candidates) vote = Vote(voter, candidate) print("\n\n\nNow {} goes to node {} and attempts to vote again, now for candidate {}".format(duplicateVoter, node, candidate)) self.assertEqual(node.castVote(vote), False) print("\nTEST 3 PASSED\n=============") print("{} attempted to duplicate vote \nbut node disallowed".format(duplicateVoter))
def _vote(self): ''' Place a vote. ''' self._process_menu() cmd = 0x24 self._send_str(cmd) if True == self._is_menu_id_valid_op(cmd): # check to see if there are any candidates to vote for if 0 == self.state['e'].get_candidate_count(): self._receive_by_len(self.EMPTY_CANDIDATES, term=CONFIG['TERM']) else: # read candidate list self._receive_by_len(self.SELECT_CANDIDATE, term=CONFIG['TERM']) fmt = "\t{0}: {1} {2}\n" for c in self.state['e'].candidates: line = fmt.format(c.id, c.f_name, c.l_name) self._receive_by_len(line) self._receive_by_len(self.CHOOSE, term=CONFIG['TERM']) # make selection c = self.state['e'].get_random_candidate() self._send_str(c.id) # receive voting receipt p1 = "Confirmation #" p2 = " Vote recorded for candidate: {0} {1}.\n" self._receive_by_len(p1) vote_id = Variable('voteid{0}'.format( self.state['e'].authd_user.id)) vote_id.set_slice(0) self.read(delim='.', assign=vote_id) self._receive_by_len(p2.format(c.f_name, c.l_name)) # record vote v = Vote(vote_id, self.state['e'].authd_user, c) self.state['e'].add_vote(v) self.state['e'].authd_user = None return 0 else: self._receive_fail() return -1
def test_badNode(self): self.network.addBadNode() for voter in self.voters: node = self.network.nodes[randint(0,self.nNodes-1)] candidate = choice(self.candidates) vote = Vote(voter, candidate) print("{} goes to node {} and chooses to vote for candidate {}".format(voter, node, candidate)) self.assertNotEqual(self.network.nodes[0], self.network.nodes[-1]) print("\n\n\nTEST 4 PASSED\n=============") print("All good nodes rejected\ninvalid chain")
def test_RunElection(self): print("\n\n#### COMMENCE VOTING ####\n") for voter in self.voters: node = self.network.nodes[randint(0,self.nNodes-1)] candidate = choice(self.candidates) vote = Vote(voter, candidate) print("{} goes to node {} and chooses to vote for candidate {}".format(voter, node, candidate)) self.assertEqual(node.castVote(vote), True) print("\n\n#### COMPARING EACH NODE'S CHAIN ####\n") for i in range(self.nNodes): for j in range(self.nNodes): if i != j: self.assertEqual(self.network.nodes[i].chain, self.network.nodes[j].chain) print("{} == {} with Length: {}".format(self.network.nodes[i], self.network.nodes[j], self.network.nodes[i].chain.length)) print("\nTEST 2 PASSED\n=============") print("{} votes successfully cast".format(self.nVoters)) print("All nodes have equal chains\n".format(self.nVoters))
from block import Block from election import Vote, Voter, Candidate from blockchain import Blockchain, InvalidBlockchain # Voters Amany = Voter("Amany") Anthony = Voter("Anthony") Owen = Voter("Owen") # Candidates James = Candidate("James") Marcus = Candidate("Marcus") Ronny = Candidate("Ronny") ab = { 'jy': Vote(Amany, James), 'mc': Vote(Amany, Marcus), 'rk': Vote(Amany, Ronny) } ak = { 'jy': Vote(Anthony, James), 'mc': Vote(Anthony, Marcus), 'rk': Vote(Anthony, Ronny) } on = { 'jy': Vote(Owen, James), 'mc': Vote(Owen, Marcus), 'rk': Vote(Owen, Ronny) }