Skip to content

Commit caa79d3

Browse files
committed
review changes
1 parent ee60c0d commit caa79d3

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

hathor/transaction/storage/transaction_storage.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,9 @@ def compute_transactions_that_became_invalid(self, new_best_height: int) -> list
11001100
to_remove: list[BaseTransaction] = []
11011101
for tx in self.iter_mempool_from_best_index():
11021102
try:
1103-
TransactionVerifier.verify_reward_locked_for_height(tx, new_best_height, verify_min_height=True)
1103+
TransactionVerifier.verify_reward_locked_for_height(
1104+
tx, new_best_height, assert_min_height_verification=False
1105+
)
11041106
except RewardLocked:
11051107
tx.set_validation(ValidationState.INVALID)
11061108
to_remove.append(tx)

hathor/verification/transaction_verifier.py

+28-9
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,29 @@ def verify_reward_locked(self, tx: Transaction) -> None:
149149
self.verify_reward_locked_for_height(tx, best_height)
150150

151151
@staticmethod
152-
def verify_reward_locked_for_height(tx: Transaction, best_height: int, *, verify_min_height: bool = False) -> None:
153-
"""Will raise `RewardLocked` if any reward is spent before the best block height is enough, considering both
154-
the block rewards spent by this tx itself, and the inherited `min_height`."""
152+
def verify_reward_locked_for_height(
153+
tx: Transaction,
154+
best_height: int,
155+
*,
156+
assert_min_height_verification: bool = True
157+
) -> None:
158+
"""
159+
Will raise `RewardLocked` if any reward is spent before the best block height is enough, considering both
160+
the block rewards spent by this tx itself, and the inherited `min_height`.
161+
162+
Args:
163+
tx: the transaction to be verified.
164+
best_height: the height of the best chain to be used for verification.
165+
assert_min_height_verification: whether the inherited `min_height` verification must pass.
166+
167+
Note: for verification of new transactions, `assert_min_height_verification` must be `True`. This
168+
verification is always expected to pass for new txs, as a failure would mean one of its dependencies would
169+
have failed too. So an `AssertionError` is raised if it fails.
170+
171+
However, when txs are being re-verified for Reward Lock during a reorg, it's possible that txs may fail
172+
their inherited `min_height` verification. So in that case `assert_min_height_verification` is `False`,
173+
and a normal `RewardLocked` exception is raised instead.
174+
"""
155175
assert tx.storage is not None
156176
info = get_spent_reward_locked_info(tx, tx.storage)
157177
if info is not None:
@@ -161,12 +181,11 @@ def verify_reward_locked_for_height(tx: Transaction, best_height: int, *, verify
161181
assert meta.min_height is not None
162182
# We use +1 here because a tx is valid if it can be confirmed by the next block
163183
if best_height + 1 < meta.min_height:
164-
if verify_min_height:
165-
raise RewardLocked(
166-
f'Tx {tx.hash_hex} has min_height={meta.min_height}, but the best_height={best_height}.'
167-
)
168-
else:
169-
raise AssertionError('a new tx should never be invalid by its inherited min_height')
184+
if assert_min_height_verification:
185+
raise AssertionError('a new tx should never be invalid by its inherited min_height.')
186+
raise RewardLocked(
187+
f'Tx {tx.hash_hex} has min_height={meta.min_height}, but the best_height={best_height}.'
188+
)
170189

171190
def verify_number_of_inputs(self, tx: Transaction) -> None:
172191
"""Verify number of inputs is in a valid range"""

0 commit comments

Comments
 (0)