From 4b46d3d87c20b5c44c6a95e40c58948bb6d9b6c2 Mon Sep 17 00:00:00 2001 From: Vanshdeep Singh Date: Mon, 20 Aug 2018 13:50:11 +0200 Subject: [PATCH] Problem: method name `conclude` isn't appropriate Solution: The `conclude` function isn't actually concluding the election but check if the election can be concluded or not. So the method name has been changed to `has_concluded` --- .../upsert_validator/validator_election.py | 11 +++--- .../test_validator_election_vote.py | 36 +++++++++---------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/bigchaindb/upsert_validator/validator_election.py b/bigchaindb/upsert_validator/validator_election.py index 18ddd82c..1e19ec1f 100644 --- a/bigchaindb/upsert_validator/validator_election.py +++ b/bigchaindb/upsert_validator/validator_election.py @@ -178,14 +178,14 @@ class ValidatorElection(Transaction): return self.count_votes(election_pk, txns, dict.get) @classmethod - def conclude(cls, bigchain, txn_id, current_votes=[], height=None): - """Check if the given election has concluded or not + def has_concluded(cls, bigchain, election_id, current_votes=[], height=None): + """Check if the given `election_id` can be concluded or not NOTE: * Election is concluded iff the current validator set is exactly equal to the validator set encoded in election outputs * Election can concluded only if the current votes achieves a supermajority """ - election = bigchain.get_transaction(txn_id) + election = bigchain.get_transaction(election_id) if election: election_pk = election.to_public_key(election.id) @@ -196,9 +196,8 @@ class ValidatorElection(Transaction): if election.is_same_topology(current_validators, election.outputs): total_votes = sum(current_validators.values()) if (votes_commited < (2/3)*total_votes) and \ - (votes_commited + votes_current > (2/3)*total_votes): + (votes_commited + votes_current >= (2/3)*total_votes): return election - return False @classmethod @@ -213,7 +212,7 @@ class ValidatorElection(Transaction): election_votes.append(txn) votes[election_id] = election_votes - election = cls.conclude(bigchain, election_id, election_votes, new_height) + election = cls.has_concluded(bigchain, election_id, election_votes, new_height) # Once an election concludes any other conclusion for the same # or any other election is invalidated if election: diff --git a/tests/upsert_validator/test_validator_election_vote.py b/tests/upsert_validator/test_validator_election_vote.py index b9de10b4..c0798224 100644 --- a/tests/upsert_validator/test_validator_election_vote.py +++ b/tests/upsert_validator/test_validator_election_vote.py @@ -172,14 +172,14 @@ def test_valid_election_conclude(b_mock, valid_election, ed25519_node_keys): # store election b_mock.store_bulk_transactions([valid_election]) # cannot conclude election as not votes exist - assert not ValidatorElection.conclude(b_mock, valid_election.id) + assert not ValidatorElection.has_concluded(b_mock, valid_election.id) # validate vote assert tx_vote0.validate(b_mock) - assert not ValidatorElection.conclude(b_mock, valid_election.id, [tx_vote0]) + assert not ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote0]) b_mock.store_bulk_transactions([tx_vote0]) - assert not ValidatorElection.conclude(b_mock, valid_election.id) + assert not ValidatorElection.has_concluded(b_mock, valid_election.id) # Node 1: cast vote tx_vote1 = gen_vote(valid_election, 1, ed25519_node_keys) @@ -191,31 +191,31 @@ def test_valid_election_conclude(b_mock, valid_election, ed25519_node_keys): tx_vote3 = gen_vote(valid_election, 3, ed25519_node_keys) assert tx_vote1.validate(b_mock) - assert not ValidatorElection.conclude(b_mock, valid_election.id, [tx_vote1]) + assert not ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote1]) - # 2/3 is achieved in the same block so the election can be concluded - assert ValidatorElection.conclude(b_mock, valid_election.id, [tx_vote1, tx_vote2]) + # 2/3 is achieved in the same block so the election can be.has_concludedd + assert ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote1, tx_vote2]) b_mock.store_bulk_transactions([tx_vote1]) - assert not ValidatorElection.conclude(b_mock, valid_election.id) + assert not ValidatorElection.has_concluded(b_mock, valid_election.id) assert tx_vote2.validate(b_mock) assert tx_vote3.validate(b_mock) # conclusion can be triggered my different votes in the same block - assert ValidatorElection.conclude(b_mock, valid_election.id, [tx_vote2]) - assert ValidatorElection.conclude(b_mock, valid_election.id, [tx_vote2, tx_vote3]) + assert ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote2]) + assert ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote2, tx_vote3]) b_mock.store_bulk_transactions([tx_vote2]) - # Once the blockchain records >2/3 of the votes the election is assumed to be concluded - # so any invocation of `.conclude` for that election should return False - assert not ValidatorElection.conclude(b_mock, valid_election.id) + # Once the blockchain records >2/3 of the votes the election is assumed to be.has_concludedd + # so any invocation of `.has_concluded` for that election should return False + assert not ValidatorElection.has_concluded(b_mock, valid_election.id) - # Vote is still valid but the election cannot be concluded as it it assmed that it has - # been concluded before + # Vote is still valid but the election cannot be.has_concludedd as it it assmed that it has + # been.has_concludedd before assert tx_vote3.validate(b_mock) - assert not ValidatorElection.conclude(b_mock, valid_election.id, [tx_vote3]) + assert not ValidatorElection.has_concluded(b_mock, valid_election.id, [tx_vote3]) @pytest.mark.abci @@ -292,9 +292,9 @@ def test_get_validator_update(b, node_keys, node_key, ed25519_node_keys): tx_vote1 = gen_vote(election, 1, ed25519_node_keys) tx_vote2 = gen_vote(election, 2, ed25519_node_keys) - assert not ValidatorElection.conclude(b, election.id, [tx_vote0]) - assert not ValidatorElection.conclude(b, election.id, [tx_vote0, tx_vote1]) - assert ValidatorElection.conclude(b, election.id, [tx_vote0, tx_vote1, tx_vote2]) + assert not ValidatorElection.has_concluded(b, election.id, [tx_vote0]) + assert not ValidatorElection.has_concluded(b, election.id, [tx_vote0, tx_vote1]) + assert ValidatorElection.has_concluded(b, election.id, [tx_vote0, tx_vote1, tx_vote2]) assert ValidatorElection.get_validator_update(b, 4, [tx_vote0]) == [] assert ValidatorElection.get_validator_update(b, 4, [tx_vote0, tx_vote1]) == []