diff --git a/hathor/transaction/base_transaction.py b/hathor/transaction/base_transaction.py index 27790c1e2..75bb3de43 100644 --- a/hathor/transaction/base_transaction.py +++ b/hathor/transaction/base_transaction.py @@ -873,7 +873,7 @@ def get_metadata(self, *, force_reload: bool = False, use_storage: bool = True) # FIXME: there is code that set use_storage=False but relies on correct height being calculated # which requires the use of a storage, this is a workaround that should be fixed, places where this # happens include generating new mining blocks and some tests - height = self.calculate_height() if self.storage else 0 + height = self.calculate_height() if self.storage else None score = self.weight if self.is_genesis else 0 kwargs: dict[str, Any] = {} @@ -913,6 +913,9 @@ def reset_metadata(self) -> None: self._metadata.validation = ValidationState.INITIAL self._metadata.voided_by = {settings.PARTIALLY_VALIDATED_ID} self._metadata._tx_ref = weakref.ref(self) + + self._update_height_metadata() + self.storage.save_transaction(self, only_metadata=True) def update_accumulated_weight(self, *, stop_value: float = inf, save_file: bool = True) -> TransactionMetadata: diff --git a/hathor/transaction/storage/transaction_storage.py b/hathor/transaction/storage/transaction_storage.py index f87ed3a0e..126eccdf3 100644 --- a/hathor/transaction/storage/transaction_storage.py +++ b/hathor/transaction/storage/transaction_storage.py @@ -423,6 +423,7 @@ def pre_save_validation(self, tx: BaseTransaction, tx_meta: TransactionMetadata) assert tx.hash == tx_meta.hash, f'{tx.hash.hex()} != {tx_meta.hash.hex()}' self._validate_partial_marker_consistency(tx_meta) self._validate_transaction_in_scope(tx) + self._validate_block_height_metadata(tx) def post_get_validation(self, tx: BaseTransaction) -> None: """ Must be run before every save, will raise AssertionError or TransactionNotInAllowedScopeError @@ -433,6 +434,7 @@ def post_get_validation(self, tx: BaseTransaction) -> None: tx_meta = tx.get_metadata() self._validate_partial_marker_consistency(tx_meta) self._validate_transaction_in_scope(tx) + self._validate_block_height_metadata(tx) def _validate_partial_marker_consistency(self, tx_meta: TransactionMetadata) -> None: voided_by = tx_meta.get_frozen_voided_by() @@ -447,6 +449,11 @@ def _validate_transaction_in_scope(self, tx: BaseTransaction) -> None: tx_meta = tx.get_metadata() raise TransactionNotInAllowedScopeError(tx.hash_hex, self.get_allow_scope().name, tx_meta.validation.name) + def _validate_block_height_metadata(self, tx: BaseTransaction) -> None: + if tx.is_block: + tx_meta = tx.get_metadata() + assert tx_meta.height is not None + @abstractmethod def remove_transaction(self, tx: BaseTransaction) -> None: """Remove the tx.