From d569b9a2faa2c4055b160903c7e048b9ce4b1b60 Mon Sep 17 00:00:00 2001 From: Gabriel Levcovitz Date: Fri, 18 Oct 2024 16:14:25 -0300 Subject: [PATCH] refactor: move can_validate_full method (#1153) --- hathor/manager.py | 2 +- hathor/p2p/sync_v2/agent.py | 2 +- .../sync_v2/blockchain_streaming_client.py | 2 +- hathor/transaction/base_transaction.py | 34 ------------------- .../storage/transaction_storage.py | 23 ++++++++++++- 5 files changed, 25 insertions(+), 38 deletions(-) diff --git a/hathor/manager.py b/hathor/manager.py index a385fb3d8..0b6bd94b1 100644 --- a/hathor/manager.py +++ b/hathor/manager.py @@ -483,7 +483,7 @@ def _initialize_components_full_verification(self) -> None: # TODO: deal with invalid tx tx._update_parents_children_metadata() - if tx.can_validate_full(): + if self.tx_storage.can_validate_full(tx): tx.update_initial_metadata() if tx.is_genesis: assert tx.validate_checkpoint(self.checkpoints) diff --git a/hathor/p2p/sync_v2/agent.py b/hathor/p2p/sync_v2/agent.py index 7046f755c..b27ced303 100644 --- a/hathor/p2p/sync_v2/agent.py +++ b/hathor/p2p/sync_v2/agent.py @@ -1171,7 +1171,7 @@ def handle_data(self, payload: str) -> None: else: # If we have not requested the data, it is a new transaction being propagated # in the network, thus, we propagate it as well. - if tx.can_validate_full(): + if self.tx_storage.can_validate_full(tx): self.log.debug('tx received in real time from peer', tx=tx.hash_hex, peer=self.protocol.get_peer_id()) try: self.vertex_handler.on_new_vertex(tx, propagate_to_peers=True, fails_silently=False) diff --git a/hathor/p2p/sync_v2/blockchain_streaming_client.py b/hathor/p2p/sync_v2/blockchain_streaming_client.py index f00395d79..6f0a3f236 100644 --- a/hathor/p2p/sync_v2/blockchain_streaming_client.py +++ b/hathor/p2p/sync_v2/blockchain_streaming_client.py @@ -130,7 +130,7 @@ def handle_blocks(self, blk: Block) -> None: else: self.log.debug('block received', blk_id=blk.hash.hex()) - if blk.can_validate_full(): + if self.tx_storage.can_validate_full(blk): try: self.vertex_handler.on_new_vertex(blk, propagate_to_peers=False, fails_silently=False) except HathorError: diff --git a/hathor/transaction/base_transaction.py b/hathor/transaction/base_transaction.py index db6fce85b..c85e40618 100644 --- a/hathor/transaction/base_transaction.py +++ b/hathor/transaction/base_transaction.py @@ -461,29 +461,6 @@ def add_address_from_output(output: 'TxOutput') -> None: return addresses - def can_validate_full(self) -> bool: - """ Check if this transaction is ready to be fully validated, either all deps are full-valid or one is invalid. - """ - assert self.storage is not None - assert self._hash is not None - if self.is_genesis: - return True - deps = self.get_all_dependencies() - all_exist = True - all_valid = True - # either they all exist and are fully valid - for dep in deps: - meta = self.storage.get_metadata(dep) - if meta is None: - all_exist = False - continue - if not meta.validation.is_fully_connected(): - all_valid = False - if meta.validation.is_invalid(): - # or any of them is invalid (which would make this one invalid too) - return True - return all_exist and all_valid - def set_validation(self, validation: ValidationState) -> None: """ This method will set the internal validation state AND the appropriate voided_by marker. @@ -850,17 +827,6 @@ def clone(self, *, include_metadata: bool = True, include_storage: bool = True) def get_token_uid(self, index: int) -> TokenUid: raise NotImplementedError - def is_ready_for_validation(self) -> bool: - """Check whether the transaction is ready to be validated: all dependencies exist and are fully connected.""" - assert self.storage is not None - for dep_hash in self.get_all_dependencies(): - dep_meta = self.storage.get_metadata(dep_hash) - if dep_meta is None: - return False - if not dep_meta.validation.is_fully_connected(): - return False - return True - @property def static_metadata(self) -> StaticMetadataT: """Get this vertex's static metadata. Assumes it has been initialized.""" diff --git a/hathor/transaction/storage/transaction_storage.py b/hathor/transaction/storage/transaction_storage.py index aa367fa8c..f91e3b795 100644 --- a/hathor/transaction/storage/transaction_storage.py +++ b/hathor/transaction/storage/transaction_storage.py @@ -30,7 +30,7 @@ from hathor.indexes.height_index import HeightInfo from hathor.profiler import get_cpu_profiler from hathor.pubsub import PubSubManager -from hathor.transaction.base_transaction import BaseTransaction, TxOutput +from hathor.transaction.base_transaction import BaseTransaction, TxOutput, Vertex from hathor.transaction.block import Block from hathor.transaction.exceptions import RewardLocked from hathor.transaction.storage.exceptions import ( @@ -1133,6 +1133,27 @@ def migrate_static_metadata(self, log: BoundLogger) -> None: """ raise NotImplementedError + def can_validate_full(self, vertex: Vertex) -> bool: + """ Check if a vertex is ready to be fully validated, either all deps are full-valid or one is invalid. + """ + if vertex.is_genesis: + return True + deps = vertex.get_all_dependencies() + all_exist = True + all_valid = True + # either they all exist and are fully valid + for dep in deps: + meta = self.get_metadata(dep) + if meta is None: + all_exist = False + continue + if not meta.validation.is_fully_connected(): + all_valid = False + if meta.validation.is_invalid(): + # or any of them is invalid (which would make this one invalid too) + return True + return all_exist and all_valid + class BaseTransactionStorage(TransactionStorage): indexes: Optional[IndexesManager]