From 11083c725fdb8514ea85428d2c84786ba5c01fc3 Mon Sep 17 00:00:00 2001 From: Lorenz Herzberger Date: Thu, 29 Sep 2022 18:22:56 +0200 Subject: [PATCH] moved vote methods to planetmint Signed-off-by: Lorenz Herzberger --- planetmint/lib.py | 20 ++++++++++++++++ .../transactions/types/elections/election.py | 24 ++----------------- .../test_upsert_validator_vote.py | 15 ++++-------- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/planetmint/lib.py b/planetmint/lib.py index b9d192a..5d60bb3 100644 --- a/planetmint/lib.py +++ b/planetmint/lib.py @@ -46,6 +46,8 @@ from planetmint.transactions.common.transaction_mode_types import ( from planetmint.tendermint_utils import encode_transaction, merkleroot, key_from_base64, public_key_to_base64 from planetmint import exceptions as core_exceptions from planetmint.transactions.types.elections.election import Election +from planetmint.transactions.types.elections.vote import Vote +from planetmint.transactions.types.elections.validator_utils import election_id_to_public_key from planetmint.validation import BaseValidationRules logger = logging.getLogger(__name__) @@ -792,4 +794,22 @@ class Planetmint(object): # validators and their voting power in the network return current_topology == voters + def count_votes(self, election_pk, transactions, getter=getattr): + votes = 0 + for txn in transactions: + if getter(txn, "operation") == Vote.OPERATION: + for output in getter(txn, "outputs"): + # NOTE: We enforce that a valid vote to election id will have only + # election_pk in the output public keys, including any other public key + # along with election_pk will lead to vote being not considered valid. + if len(getter(output, "public_keys")) == 1 and [election_pk] == getter(output, "public_keys"): + votes = votes + int(getter(output, "amount")) + return votes + + def get_commited_votes(self, transaction, election_pk=None): # TODO: move somewhere else + if election_pk is None: + election_pk = election_id_to_public_key(transaction.id) + txns = list(backend.query.get_asset_tokens_for_public_key(self.connection, transaction.id, election_pk)) + return self.count_votes(election_pk, txns, dict.get) + Block = namedtuple("Block", ("app_hash", "height", "transactions")) diff --git a/planetmint/transactions/types/elections/election.py b/planetmint/transactions/types/elections/election.py index 981258b..e002f53 100644 --- a/planetmint/transactions/types/elections/election.py +++ b/planetmint/transactions/types/elections/election.py @@ -73,26 +73,6 @@ class Election(Transaction): if cls.TX_SCHEMA_CUSTOM: _validate_schema(cls.TX_SCHEMA_CUSTOM, tx) - - @classmethod - def count_votes(cls, election_pk, transactions, getter=getattr): # TODO: move somewhere else - votes = 0 - for txn in transactions: - if getter(txn, "operation") == Vote.OPERATION: - for output in getter(txn, "outputs"): - # NOTE: We enforce that a valid vote to election id will have only - # election_pk in the output public keys, including any other public key - # along with election_pk will lead to vote being not considered valid. - if len(getter(output, "public_keys")) == 1 and [election_pk] == getter(output, "public_keys"): - votes = votes + int(getter(output, "amount")) - return votes - - def get_commited_votes(self, planet, election_pk=None): # TODO: move somewhere else - if election_pk is None: - election_pk = election_id_to_public_key(self.id) - txns = list(backend.query.get_asset_tokens_for_public_key(planet.connection, self.id, election_pk)) - return self.count_votes(election_pk, txns, dict.get) - def has_concluded(self, planet, current_votes=[]): # TODO: move somewhere else """Check if the election can be concluded or not. @@ -106,8 +86,8 @@ class Election(Transaction): return False election_pk = election_id_to_public_key(self.id) - votes_committed = self.get_commited_votes(planet, election_pk) - votes_current = self.count_votes(election_pk, current_votes) + votes_committed = planet.get_commited_votes(self, election_pk) + votes_current = planet.count_votes(election_pk, current_votes) total_votes = sum(output.amount for output in self.outputs) if (votes_committed < (2 / 3) * total_votes) and (votes_committed + votes_current >= (2 / 3) * total_votes): diff --git a/tests/upsert_validator/test_upsert_validator_vote.py b/tests/upsert_validator/test_upsert_validator_vote.py index 9313c95..e5e81c8 100644 --- a/tests/upsert_validator/test_upsert_validator_vote.py +++ b/tests/upsert_validator/test_upsert_validator_vote.py @@ -34,7 +34,6 @@ def test_upsert_validator_valid_election_vote(b_mock, valid_upsert_validator_ele vote = Vote.generate([input0], [([election_pub_key], votes)], election_id=valid_upsert_validator_election.id).sign( [key0.private_key] ) - # assert vote.validate(b_mock) assert b_mock.validate_transaction(vote) @@ -73,7 +72,6 @@ def test_upsert_validator_delegate_election_vote(b_mock, valid_upsert_validator_ election_id=valid_upsert_validator_election.id, ).sign([key0.private_key]) - # assert delegate_vote.validate(b_mock) assert b_mock.validate_transaction(delegate_vote) b_mock.store_bulk_transactions([delegate_vote]) @@ -83,14 +81,12 @@ def test_upsert_validator_delegate_election_vote(b_mock, valid_upsert_validator_ alice_casted_vote = Vote.generate( [alice_votes], [([election_pub_key], 3)], election_id=valid_upsert_validator_election.id ).sign([alice.private_key]) - # assert alice_casted_vote.validate(b_mock) assert b_mock.validate_transaction(alice_casted_vote) key0_votes = delegate_vote.to_inputs()[1] key0_casted_vote = Vote.generate( [key0_votes], [([election_pub_key], votes - 3)], election_id=valid_upsert_validator_election.id ).sign([key0.private_key]) - # assert key0_casted_vote.validate(b_mock) assert b_mock.validate_transaction(key0_casted_vote) @@ -110,7 +106,6 @@ def test_upsert_validator_invalid_election_vote(b_mock, valid_upsert_validator_e ).sign([key0.private_key]) with pytest.raises(AmountError): - # assert vote.validate(b_mock) assert b_mock.validate_transaction(vote) @@ -118,7 +113,7 @@ def test_upsert_validator_invalid_election_vote(b_mock, valid_upsert_validator_e def test_valid_election_votes_received(b_mock, valid_upsert_validator_election, ed25519_node_keys): alice = generate_key_pair() b_mock.store_bulk_transactions([valid_upsert_validator_election]) - assert valid_upsert_validator_election.get_commited_votes(b_mock) == 0 + assert b_mock.get_commited_votes(valid_upsert_validator_election) == 0 input0 = valid_upsert_validator_election.to_inputs()[0] votes = valid_upsert_validator_election.outputs[0].amount @@ -132,7 +127,7 @@ def test_valid_election_votes_received(b_mock, valid_upsert_validator_election, election_id=valid_upsert_validator_election.id, ).sign([key0.private_key]) b_mock.store_bulk_transactions([delegate_vote]) - assert valid_upsert_validator_election.get_commited_votes(b_mock) == 0 + assert b_mock.get_commited_votes(valid_upsert_validator_election) == 0 election_public_key = election_id_to_public_key(valid_upsert_validator_election.id) alice_votes = delegate_vote.to_inputs()[0] @@ -148,7 +143,7 @@ def test_valid_election_votes_received(b_mock, valid_upsert_validator_election, b_mock.store_bulk_transactions([alice_casted_vote]) # Check if the delegated vote is count as valid vote - assert valid_upsert_validator_election.get_commited_votes(b_mock) == 2 + assert b_mock.get_commited_votes(valid_upsert_validator_election) == 2 key0_casted_vote = Vote.generate( [key0_votes], [([election_public_key], votes - 4)], election_id=valid_upsert_validator_election.id @@ -156,9 +151,7 @@ def test_valid_election_votes_received(b_mock, valid_upsert_validator_election, assert b_mock.validate_transaction(key0_casted_vote) b_mock.store_bulk_transactions([key0_casted_vote]) - - assert valid_upsert_validator_election.get_commited_votes(b_mock) == votes - 2 - + assert b_mock.get_commited_votes(valid_upsert_validator_election) == votes - 2 @pytest.mark.bdb def test_valid_election_conclude(b_mock, valid_upsert_validator_election, ed25519_node_keys):