Skip to content

Commit 7dd0b6d

Browse files
committed
refactor: move can_validate_full method
1 parent 026cb7b commit 7dd0b6d

File tree

5 files changed

+25
-38
lines changed

5 files changed

+25
-38
lines changed

hathor/manager.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def _initialize_components_full_verification(self) -> None:
483483
# TODO: deal with invalid tx
484484
tx._update_parents_children_metadata()
485485

486-
if tx.can_validate_full():
486+
if self.tx_storage.can_validate_full(tx):
487487
tx.update_initial_metadata()
488488
if tx.is_genesis:
489489
assert tx.validate_checkpoint(self.checkpoints)

hathor/p2p/sync_v2/agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ def handle_data(self, payload: str) -> None:
11711171
else:
11721172
# If we have not requested the data, it is a new transaction being propagated
11731173
# in the network, thus, we propagate it as well.
1174-
if tx.can_validate_full():
1174+
if self.tx_storage.can_validate_full(tx):
11751175
self.log.debug('tx received in real time from peer', tx=tx.hash_hex, peer=self.protocol.get_peer_id())
11761176
try:
11771177
self.vertex_handler.on_new_vertex(tx, propagate_to_peers=True, fails_silently=False)

hathor/p2p/sync_v2/blockchain_streaming_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def handle_blocks(self, blk: Block) -> None:
130130
else:
131131
self.log.debug('block received', blk_id=blk.hash.hex())
132132

133-
if blk.can_validate_full():
133+
if self.tx_storage.can_validate_full(blk):
134134
try:
135135
self.vertex_handler.on_new_vertex(blk, propagate_to_peers=False, fails_silently=False)
136136
except HathorError:

hathor/transaction/base_transaction.py

-34
Original file line numberDiff line numberDiff line change
@@ -461,29 +461,6 @@ def add_address_from_output(output: 'TxOutput') -> None:
461461

462462
return addresses
463463

464-
def can_validate_full(self) -> bool:
465-
""" Check if this transaction is ready to be fully validated, either all deps are full-valid or one is invalid.
466-
"""
467-
assert self.storage is not None
468-
assert self._hash is not None
469-
if self.is_genesis:
470-
return True
471-
deps = self.get_all_dependencies()
472-
all_exist = True
473-
all_valid = True
474-
# either they all exist and are fully valid
475-
for dep in deps:
476-
meta = self.storage.get_metadata(dep)
477-
if meta is None:
478-
all_exist = False
479-
continue
480-
if not meta.validation.is_fully_connected():
481-
all_valid = False
482-
if meta.validation.is_invalid():
483-
# or any of them is invalid (which would make this one invalid too)
484-
return True
485-
return all_exist and all_valid
486-
487464
def set_validation(self, validation: ValidationState) -> None:
488465
""" This method will set the internal validation state AND the appropriate voided_by marker.
489466
@@ -850,17 +827,6 @@ def clone(self, *, include_metadata: bool = True, include_storage: bool = True)
850827
def get_token_uid(self, index: int) -> TokenUid:
851828
raise NotImplementedError
852829

853-
def is_ready_for_validation(self) -> bool:
854-
"""Check whether the transaction is ready to be validated: all dependencies exist and are fully connected."""
855-
assert self.storage is not None
856-
for dep_hash in self.get_all_dependencies():
857-
dep_meta = self.storage.get_metadata(dep_hash)
858-
if dep_meta is None:
859-
return False
860-
if not dep_meta.validation.is_fully_connected():
861-
return False
862-
return True
863-
864830
@property
865831
def static_metadata(self) -> StaticMetadataT:
866832
"""Get this vertex's static metadata. Assumes it has been initialized."""

hathor/transaction/storage/transaction_storage.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from hathor.indexes.height_index import HeightInfo
3131
from hathor.profiler import get_cpu_profiler
3232
from hathor.pubsub import PubSubManager
33-
from hathor.transaction.base_transaction import BaseTransaction, TxOutput
33+
from hathor.transaction.base_transaction import BaseTransaction, TxOutput, Vertex
3434
from hathor.transaction.block import Block
3535
from hathor.transaction.exceptions import RewardLocked
3636
from hathor.transaction.storage.exceptions import (
@@ -1133,6 +1133,27 @@ def migrate_static_metadata(self, log: BoundLogger) -> None:
11331133
"""
11341134
raise NotImplementedError
11351135

1136+
def can_validate_full(self, vertex: Vertex) -> bool:
1137+
""" Check if a vertex is ready to be fully validated, either all deps are full-valid or one is invalid.
1138+
"""
1139+
if vertex.is_genesis:
1140+
return True
1141+
deps = vertex.get_all_dependencies()
1142+
all_exist = True
1143+
all_valid = True
1144+
# either they all exist and are fully valid
1145+
for dep in deps:
1146+
meta = self.get_metadata(dep)
1147+
if meta is None:
1148+
all_exist = False
1149+
continue
1150+
if not meta.validation.is_fully_connected():
1151+
all_valid = False
1152+
if meta.validation.is_invalid():
1153+
# or any of them is invalid (which would make this one invalid too)
1154+
return True
1155+
return all_exist and all_valid
1156+
11361157

11371158
class BaseTransactionStorage(TransactionStorage):
11381159
indexes: Optional[IndexesManager]

0 commit comments

Comments
 (0)