Skip to content

Commit e5dddc4

Browse files
committed
fix(indexes): Blocks cannot be removed from block's index, even when they are voided
1 parent 2a5dc3c commit e5dddc4

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

hathor/indexes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def add_tx(self, tx: BaseTransaction) -> bool:
5959
assert r1 == r2
6060
return r1
6161

62-
def del_tx(self, tx: BaseTransaction, *, relax_assert: bool = False) -> None:
62+
def del_tx(self, tx: BaseTransaction, *, relax_assert: bool = False) -> bool:
6363
""" Delete a transaction from the indexes
6464
6565
:param tx: Transaction to be deleted

hathor/transaction/storage/transaction_storage.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ def remove_transaction(self, tx: BaseTransaction) -> None:
534534
if self.with_index:
535535
assert self.all_index is not None
536536

537-
self.del_from_indexes(tx, relax_assert=True)
537+
self.del_from_indexes(tx, relax_assert=True, del_blocks=True)
538538
# TODO Move it to self.del_from_indexes. We cannot simply do it because
539539
# this method is used by the consensus algorithm which does not
540540
# expect to have it removed from self.all_index.
@@ -810,7 +810,7 @@ def add_to_indexes(self, tx: BaseTransaction) -> None:
810810
raise NotImplementedError
811811

812812
@abstractmethod
813-
def del_from_indexes(self, tx: BaseTransaction, *, relax_assert: bool = False) -> None:
813+
def del_from_indexes(self, tx: BaseTransaction, *, relax_assert: bool = False, del_blocks: bool = False) -> None:
814814
raise NotImplementedError
815815

816816
@abstractmethod
@@ -1170,16 +1170,20 @@ def add_to_indexes(self, tx: BaseTransaction) -> None:
11701170
if self.tx_index.add_tx(tx):
11711171
self._cache_tx_count += 1
11721172

1173-
def del_from_indexes(self, tx: BaseTransaction, *, relax_assert: bool = False) -> None:
1173+
def del_from_indexes(self, tx: BaseTransaction, *, relax_assert: bool = False, del_blocks: bool = False) -> None:
11741174
if not self.with_index:
11751175
raise NotImplementedError
11761176
assert self.block_index is not None
11771177
assert self.tx_index is not None
11781178
if self.tokens_index:
11791179
self.tokens_index.del_tx(tx)
11801180
if tx.is_block:
1181-
if self.block_index.del_tx(tx, relax_assert=relax_assert):
1182-
self._cache_block_count -= 1
1181+
# This is the code to remove the block from the indexes.
1182+
# But we cannot remove blocks from index because they are used to validate
1183+
# block rewards.
1184+
if del_blocks:
1185+
if self.block_index.del_tx(tx, relax_assert=relax_assert):
1186+
self._cache_block_count -= 1
11831187
else:
11841188
if self.tx_index.del_tx(tx, relax_assert=relax_assert):
11851189
self._cache_tx_count -= 1

tests/tx/test_tx_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def validate_save(self, obj):
187187
else:
188188
self.assertTrue(obj.hash in self.tx_storage.tx_index.tips_index.tx_last_interval)
189189

190-
self.tx_storage.del_from_indexes(obj)
190+
self.tx_storage.del_from_indexes(obj, del_blocks=True)
191191

192192
if self.tx_storage.with_index:
193193
if obj.is_block:

0 commit comments

Comments
 (0)