Skip to content

Commit

Permalink
refactor: move can_validate_full method (#1153)
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco authored Oct 18, 2024
1 parent 026cb7b commit d569b9a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 38 deletions.
2 changes: 1 addition & 1 deletion hathor/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion hathor/p2p/sync_v2/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion hathor/p2p/sync_v2/blockchain_streaming_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 0 additions & 34 deletions hathor/transaction/base_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."""
Expand Down
23 changes: 22 additions & 1 deletion hathor/transaction/storage/transaction_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit d569b9a

Please sign in to comment.