resolved linting errors

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2022-10-12 12:45:12 +02:00
parent 2649b59794
commit 33a1cbb548
No known key found for this signature in database
GPG Key ID: FA5EE906EB55316A
3 changed files with 40 additions and 18 deletions

View File

@ -19,9 +19,20 @@ from hashlib import sha3_256
import requests import requests
from transactions import Transaction, Vote from transactions import Transaction, Vote
from transactions.common.crypto import public_key_from_ed25519_key from transactions.common.crypto import public_key_from_ed25519_key
from transactions.common.exceptions import SchemaValidationError, ValidationError, DuplicateTransaction, \ from transactions.common.exceptions import (
InvalidSignature, DoubleSpend, InputDoesNotExist, AssetIdMismatch, AmountError, MultipleInputsError, \ SchemaValidationError,
InvalidProposer, UnequalValidatorSet, InvalidPowerChange ValidationError,
DuplicateTransaction,
InvalidSignature,
DoubleSpend,
InputDoesNotExist,
AssetIdMismatch,
AmountError,
MultipleInputsError,
InvalidProposer,
UnequalValidatorSet,
InvalidPowerChange,
)
from transactions.common.transaction import VALIDATOR_ELECTION, CHAIN_MIGRATION_ELECTION from transactions.common.transaction import VALIDATOR_ELECTION, CHAIN_MIGRATION_ELECTION
from transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT, BROADCAST_TX_ASYNC, BROADCAST_TX_SYNC from transactions.common.transaction_mode_types import BROADCAST_TX_COMMIT, BROADCAST_TX_ASYNC, BROADCAST_TX_SYNC
from transactions.types.elections.election import Election from transactions.types.elections.election import Election
@ -30,7 +41,14 @@ from transactions.types.elections.validator_utils import election_id_to_public_k
import planetmint import planetmint
from planetmint.config import Config from planetmint.config import Config
from planetmint import backend, config_utils, fastquery from planetmint import backend, config_utils, fastquery
from planetmint.tendermint_utils import encode_transaction, merkleroot, key_from_base64, public_key_to_base64, encode_validator, new_validator_set from planetmint.tendermint_utils import (
encode_transaction,
merkleroot,
key_from_base64,
public_key_to_base64,
encode_validator,
new_validator_set,
)
from planetmint import exceptions as core_exceptions from planetmint import exceptions as core_exceptions
from planetmint.validation import BaseValidationRules from planetmint.validation import BaseValidationRules
@ -616,7 +634,7 @@ class Planetmint(object):
return validators return validators
def validate_election(self, transaction, current_transactions=[]): # TODO: move somewhere else def validate_election(self, transaction, current_transactions=[]): # TODO: move somewhere else
"""Validate election transaction """Validate election transaction
NOTE: NOTE:
@ -664,8 +682,7 @@ class Planetmint(object):
return transaction return transaction
def validate_validator_election(self, transaction, current_transactions=[]): # TODO: move somewhere else
def validate_validator_election(self, transaction, current_transactions=[]): # TODO: move somewhere else
"""For more details refer BEP-21: https://github.com/planetmint/BEPs/tree/master/21""" """For more details refer BEP-21: https://github.com/planetmint/BEPs/tree/master/21"""
current_validators = self.get_validators_dict() current_validators = self.get_validators_dict()
@ -683,7 +700,7 @@ class Planetmint(object):
return Election.INCONCLUSIVE if self.has_validator_set_changed(transaction) else Election.ONGOING return Election.INCONCLUSIVE if self.has_validator_set_changed(transaction) else Election.ONGOING
def has_validator_set_changed(self, transaction): # TODO: move somewhere else def has_validator_set_changed(self, transaction): # TODO: move somewhere else
latest_change = self.get_validator_change() latest_change = self.get_validator_change()
if latest_change is None: if latest_change is None:
return False return False
@ -694,7 +711,7 @@ class Planetmint(object):
return latest_change_height > election["height"] return latest_change_height > election["height"]
def get_validator_change(self): # TODO: move somewhere else def get_validator_change(self): # TODO: move somewhere else
"""Return the validator set from the most recent approved block """Return the validator set from the most recent approved block
:return: { :return: {
@ -790,13 +807,13 @@ class Planetmint(object):
votes = votes + int(getter(output, "amount")) votes = votes + int(getter(output, "amount"))
return votes return votes
def get_commited_votes(self, transaction, election_pk=None): # TODO: move somewhere else def get_commited_votes(self, transaction, election_pk=None): # TODO: move somewhere else
if election_pk is None: if election_pk is None:
election_pk = election_id_to_public_key(transaction.id) 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)) 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) return self.count_votes(election_pk, txns, dict.get)
def _get_initiated_elections(self, height, txns): # TODO: move somewhere else def _get_initiated_elections(self, height, txns): # TODO: move somewhere else
elections = [] elections = []
for tx in txns: for tx in txns:
if not isinstance(tx, Election): if not isinstance(tx, Election):
@ -805,7 +822,7 @@ class Planetmint(object):
elections.append({"election_id": tx.id, "height": height, "is_concluded": False}) elections.append({"election_id": tx.id, "height": height, "is_concluded": False})
return elections return elections
def _get_votes(self, txns): # TODO: move somewhere else def _get_votes(self, txns): # TODO: move somewhere else
elections = OrderedDict() elections = OrderedDict()
for tx in txns: for tx in txns:
if not isinstance(tx, Vote): if not isinstance(tx, Vote):
@ -817,7 +834,7 @@ class Planetmint(object):
elections[election_id].append(tx) elections[election_id].append(tx)
return elections return elections
def process_block(self, new_height, txns): # TODO: move somewhere else def process_block(self, new_height, txns): # TODO: move somewhere else
"""Looks for election and vote transactions inside the block, records """Looks for election and vote transactions inside the block, records
and processes elections. and processes elections.
@ -863,7 +880,7 @@ class Planetmint(object):
return [validator_update] if validator_update else [] return [validator_update] if validator_update else []
def has_election_concluded(self, transaction, current_votes=[]): # TODO: move somewhere else def has_election_concluded(self, transaction, current_votes=[]): # TODO: move somewhere else
"""Check if the election can be concluded or not. """Check if the election can be concluded or not.
* Elections can only be concluded if the validator set has not changed * Elections can only be concluded if the validator set has not changed
@ -893,7 +910,7 @@ class Planetmint(object):
return False return False
def has_validator_election_concluded(self): # TODO: move somewhere else def has_validator_election_concluded(self): # TODO: move somewhere else
latest_block = self.get_latest_block() latest_block = self.get_latest_block()
if latest_block is not None: if latest_block is not None:
latest_block_height = latest_block["height"] latest_block_height = latest_block["height"]
@ -906,7 +923,7 @@ class Planetmint(object):
return True return True
def has_chain_migration_concluded(self): # TODO: move somewhere else def has_chain_migration_concluded(self): # TODO: move somewhere else
chain = self.get_latest_abci_chain() chain = self.get_latest_abci_chain()
if chain is not None and not chain["is_synced"]: if chain is not None and not chain["is_synced"]:
# do not conclude the migration election if # do not conclude the migration election if
@ -915,7 +932,7 @@ class Planetmint(object):
return True return True
def rollback_election(self, new_height, txn_ids): # TODO: move somewhere else def rollback_election(self, new_height, txn_ids): # TODO: move somewhere else
"""Looks for election and vote transactions inside the block and """Looks for election and vote transactions inside the block and
cleans up the database artifacts possibly created in `process_blocks`. cleans up the database artifacts possibly created in `process_blocks`.
@ -955,4 +972,5 @@ class Planetmint(object):
self.store_validator_set(new_height + 1, updated_validator_set) self.store_validator_set(new_height + 1, updated_validator_set)
return encode_validator(election.asset["data"]) return encode_validator(election.asset["data"])
Block = namedtuple("Block", ("app_hash", "height", "transactions")) Block = namedtuple("Block", ("app_hash", "height", "transactions"))

View File

@ -16,6 +16,7 @@ from hashlib import sha3_256
from transactions.common.exceptions import InvalidPublicKey from transactions.common.exceptions import InvalidPublicKey
def encode_validator(v): def encode_validator(v):
ed25519_public_key = v["public_key"]["value"] ed25519_public_key = v["public_key"]["value"]
pub_key = keys_pb2.PublicKey(ed25519=bytes.fromhex(ed25519_public_key)) pub_key = keys_pb2.PublicKey(ed25519=bytes.fromhex(ed25519_public_key))
@ -50,6 +51,7 @@ def new_validator_set(validators, updates):
new_validators_dict = {**validators_dict, **updates_dict} new_validators_dict = {**validators_dict, **updates_dict}
return list(new_validators_dict.values()) return list(new_validators_dict.values())
def get_public_key_decoder(pk): def get_public_key_decoder(pk):
encoding = pk["type"] encoding = pk["type"]
decoder = base64.b64decode decoder = base64.b64decode
@ -65,6 +67,7 @@ def get_public_key_decoder(pk):
return decoder return decoder
def encode_transaction(value): def encode_transaction(value):
"""Encode a transaction (dict) to Base64.""" """Encode a transaction (dict) to Base64."""

View File

@ -152,6 +152,7 @@ def test_valid_election_votes_received(b_mock, valid_upsert_validator_election,
b_mock.store_bulk_transactions([key0_casted_vote]) b_mock.store_bulk_transactions([key0_casted_vote])
assert b_mock.get_commited_votes(valid_upsert_validator_election) == votes - 2 assert b_mock.get_commited_votes(valid_upsert_validator_election) == votes - 2
@pytest.mark.bdb @pytest.mark.bdb
def test_valid_election_conclude(b_mock, valid_upsert_validator_election, ed25519_node_keys): def test_valid_election_conclude(b_mock, valid_upsert_validator_election, ed25519_node_keys):