mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
generalize election status
This commit is contained in:
parent
cea2f6f554
commit
eb444c0771
@ -476,16 +476,22 @@ class Bigchain(object):
|
||||
|
||||
return unvoted
|
||||
|
||||
def block_voted_invalid(self, block):
|
||||
def block_election_status(self, block):
|
||||
"""
|
||||
Checks if block has enough invalid votes to make a decision
|
||||
Tallies the votes on a block, and returns the status: valid, invalid, or undecided
|
||||
"""
|
||||
n_voters = len(block['block']['voters'])
|
||||
|
||||
vote_list = [vote['vote']['is_block_valid'] for vote in block['block']['votes']]
|
||||
|
||||
# validate votes here
|
||||
|
||||
n_invalid_votes = vote_list.count(False)
|
||||
n_valid_votes = vote_list.count(True)
|
||||
|
||||
if n_invalid_votes >= int(n_voters/2):
|
||||
return True
|
||||
return 'invalid'
|
||||
elif n_valid_votes > int(n_voters/2):
|
||||
return 'valid'
|
||||
else:
|
||||
return False
|
||||
return 'undecided'
|
||||
|
@ -218,7 +218,7 @@ class Election(object):
|
||||
logger.info('clean exit')
|
||||
return
|
||||
|
||||
if b.block_voted_invalid(next_block):
|
||||
if b.block_election_status(next_block) == 'invalid':
|
||||
self.q_invalid_blocks.put(next_block)
|
||||
|
||||
def requeue_transactions(self):
|
||||
|
@ -314,15 +314,19 @@ class TestBlockElection(object):
|
||||
|
||||
# test unanimously valid block
|
||||
test_block['block']['votes'] = [valid_vote, valid_vote, valid_vote, valid_vote]
|
||||
assert not b.block_voted_invalid(test_block)
|
||||
assert b.block_election_status(test_block) == 'valid'
|
||||
|
||||
# test block with minority invalid vote
|
||||
test_block['block']['votes'] = [invalid_vote, valid_vote, valid_vote, valid_vote]
|
||||
assert not b.block_voted_invalid(test_block)
|
||||
assert b.block_election_status(test_block) == 'valid'
|
||||
|
||||
# test split vote -- block_voted_invalid should return True
|
||||
# test split vote
|
||||
test_block['block']['votes'] = [invalid_vote, invalid_vote, valid_vote, valid_vote]
|
||||
assert b.block_voted_invalid(test_block)
|
||||
assert b.block_election_status(test_block) == 'invalid'
|
||||
|
||||
# test undecided
|
||||
test_block['block']['votes'] = [valid_vote, valid_vote]
|
||||
assert b.block_election_status(test_block) == 'undecided'
|
||||
|
||||
def test_tx_rewritten_after_invalid(self, b, user_public_key):
|
||||
q_block_new_vote = mp.Queue()
|
||||
|
Loading…
x
Reference in New Issue
Block a user