Skip to content

Commit

Permalink
feat(indexes): remove sync-v1 indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
jansegre committed Jan 24, 2025
1 parent ea7d72c commit cbfd6f1
Show file tree
Hide file tree
Showing 35 changed files with 213 additions and 895 deletions.
2 changes: 1 addition & 1 deletion hathor/cli/events_simulator/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def simulate_invalid_mempool_transaction(simulator: 'Simulator', manager: 'Hatho
simulator.run(60)

# the transaction should have been removed from the mempool and the storage after the re-org
assert tx not in manager.tx_storage.iter_mempool_from_best_index()
assert tx not in manager.tx_storage.iter_mempool()
assert not manager.tx_storage.transaction_exists(tx.hash)
assert bool(tx.get_metadata().voided_by)
balance_per_address = manager.wallet.get_balance_per_address(settings.HATHOR_TOKEN_UID)
Expand Down
5 changes: 0 additions & 5 deletions hathor/consensus/block_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ def update_voided_info(self, block: Block) -> None:
meta = block.get_metadata()
if not meta.voided_by:
storage.indexes.height.add_new(block.get_height(), block.hash, block.timestamp)
storage.update_best_block_tips_cache([block.hash])
# The following assert must be true, but it is commented out for performance reasons.
if self._settings.SLOW_ASSERTS:
assert len(storage.get_best_block_tips(skip_cache=True)) == 1
else:
# Resolve all other cases, but (i).
log = self.log.new(block=block.hash_hex)
Expand Down Expand Up @@ -209,7 +205,6 @@ def update_voided_info(self, block: Block) -> None:
self.log.debug('index new winner block', height=height, block=block.hash_hex)
# We update the height cache index with the new winner chain
storage.indexes.height.update_new_chain(height, block)
storage.update_best_block_tips_cache([block.hash])
# It is only a re-org if common_block not in heads
if common_block != head:
self.context.mark_as_reorg(common_block)
Expand Down
1 change: 0 additions & 1 deletion hathor/consensus/transaction_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ def add_voided_by(self, tx: Transaction, voided_hash: bytes) -> bool:
if tx2.is_block:
assert isinstance(tx2, Block)
self.context.block_algorithm.mark_as_voided(tx2)
tx2.storage.update_best_block_tips_cache(None)

assert not meta2.voided_by or voided_hash not in meta2.voided_by
if tx2.hash != tx.hash and meta2.conflict_with and not meta2.voided_by:
Expand Down
40 changes: 7 additions & 33 deletions hathor/indexes/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from hathor.indexes.info_index import InfoIndex
from hathor.indexes.mempool_tips_index import MempoolTipsIndex
from hathor.indexes.timestamp_index import ScopeType as TimestampScopeType, TimestampIndex
from hathor.indexes.tips_index import ScopeType as TipsScopeType, TipsIndex
from hathor.indexes.tokens_index import TokensIndex
from hathor.indexes.utxo_index import UtxoIndex
from hathor.transaction import BaseTransaction
Expand All @@ -52,9 +51,6 @@ class IndexesManager(ABC):
log = get_logger()

info: InfoIndex
all_tips: TipsIndex
block_tips: TipsIndex
tx_tips: TipsIndex

sorted_all: TimestampIndex
sorted_blocks: TimestampIndex
Expand Down Expand Up @@ -82,9 +78,6 @@ def iter_all_indexes(self) -> Iterator[BaseIndex]:
""" Iterate over all of the indexes abstracted by this manager, hiding their specific implementation details"""
return filter(None, [
self.info,
self.all_tips,
self.block_tips,
self.tx_tips,
self.sorted_all,
self.sorted_blocks,
self.sorted_txs,
Expand Down Expand Up @@ -197,20 +190,12 @@ def add_tx(self, tx: BaseTransaction) -> bool:
"""
self.info.update_timestamps(tx)

# These two calls return False when a transaction changes from
# voided to executed and vice-versa.
r1 = self.all_tips.add_tx(tx)
r2 = self.sorted_all.add_tx(tx)
assert r1 == r2
r1 = self.sorted_all.add_tx(tx)

if tx.is_block:
r3 = self.block_tips.add_tx(tx)
r4 = self.sorted_blocks.add_tx(tx)
assert r3 == r4
r2 = self.sorted_blocks.add_tx(tx)
else:
r3 = self.tx_tips.add_tx(tx)
r4 = self.sorted_txs.add_tx(tx)
assert r3 == r4
r2 = self.sorted_txs.add_tx(tx)

if self.addresses:
self.addresses.add_tx(tx)
Expand All @@ -219,10 +204,10 @@ def add_tx(self, tx: BaseTransaction) -> bool:

# We need to check r1 as well to make sure we don't count twice the transactions/blocks that are
# just changing from voided to executed or vice-versa
if r1 and r3:
if r1:
self.info.update_counts(tx)

return r3
return r2

def del_tx(self, tx: BaseTransaction, *, remove_all: bool = False, relax_assert: bool = False) -> None:
""" Delete a transaction from the indexes
Expand All @@ -233,9 +218,8 @@ def del_tx(self, tx: BaseTransaction, *, remove_all: bool = False, relax_assert:

if remove_all:
# We delete from indexes in two cases: (i) mark tx as voided, and (ii) remove tx.
# We only remove tx from all_tips and sorted_all when it is removed from the storage.
# For clarity, when a tx is marked as voided, it is not removed from all_tips and sorted_all.
self.all_tips.del_tx(tx, relax_assert=relax_assert)
# We only remove tx from sorted_all when it is removed from the storage.
# For clarity, when a tx is marked as voided, it is not removed from sorted_all.
self.sorted_all.del_tx(tx)
if self.addresses:
self.addresses.remove_tx(tx)
Expand All @@ -249,10 +233,8 @@ def del_tx(self, tx: BaseTransaction, *, remove_all: bool = False, relax_assert:
self.mempool_tips.update(tx, remove=True)

if tx.is_block:
self.block_tips.del_tx(tx, relax_assert=relax_assert)
self.sorted_blocks.del_tx(tx)
else:
self.tx_tips.del_tx(tx, relax_assert=relax_assert)
self.sorted_txs.del_tx(tx)

if self.tokens:
Expand All @@ -264,12 +246,8 @@ def __init__(self, *, settings: HathorSettings | None = None) -> None:
from hathor.indexes.memory_height_index import MemoryHeightIndex
from hathor.indexes.memory_info_index import MemoryInfoIndex
from hathor.indexes.memory_timestamp_index import MemoryTimestampIndex
from hathor.indexes.memory_tips_index import MemoryTipsIndex

self.info = MemoryInfoIndex()
self.all_tips = MemoryTipsIndex(scope_type=TipsScopeType.ALL)
self.block_tips = MemoryTipsIndex(scope_type=TipsScopeType.BLOCKS)
self.tx_tips = MemoryTipsIndex(scope_type=TipsScopeType.TXS)

self.sorted_all = MemoryTimestampIndex(scope_type=TimestampScopeType.ALL)
self.sorted_blocks = MemoryTimestampIndex(scope_type=TimestampScopeType.BLOCKS)
Expand Down Expand Up @@ -307,7 +285,6 @@ def enable_mempool_index(self) -> None:

class RocksDBIndexesManager(IndexesManager):
def __init__(self, rocksdb_storage: 'RocksDBStorage') -> None:
from hathor.indexes.partial_rocksdb_tips_index import PartialRocksDBTipsIndex
from hathor.indexes.rocksdb_height_index import RocksDBHeightIndex
from hathor.indexes.rocksdb_info_index import RocksDBInfoIndex
from hathor.indexes.rocksdb_timestamp_index import RocksDBTimestampIndex
Expand All @@ -316,9 +293,6 @@ def __init__(self, rocksdb_storage: 'RocksDBStorage') -> None:

self.info = RocksDBInfoIndex(self._db)
self.height = RocksDBHeightIndex(self._db)
self.all_tips = PartialRocksDBTipsIndex(self._db, scope_type=TipsScopeType.ALL)
self.block_tips = PartialRocksDBTipsIndex(self._db, scope_type=TipsScopeType.BLOCKS)
self.tx_tips = PartialRocksDBTipsIndex(self._db, scope_type=TipsScopeType.TXS)

self.sorted_all = RocksDBTimestampIndex(self._db, scope_type=TimestampScopeType.ALL)
self.sorted_blocks = RocksDBTimestampIndex(self._db, scope_type=TimestampScopeType.BLOCKS)
Expand Down
7 changes: 6 additions & 1 deletion hathor/indexes/memory_timestamp_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

from typing import Iterator, Optional

from sortedcontainers import SortedKeyList
Expand All @@ -33,7 +35,7 @@ class MemoryTimestampIndex(TimestampIndex):
""" Index of transactions sorted by their timestamps.
"""

_index: 'SortedKeyList[TransactionIndexElement]'
_index: SortedKeyList[TransactionIndexElement]

def __init__(self, *, scope_type: ScopeType):
super().__init__(scope_type=scope_type)
Expand Down Expand Up @@ -89,3 +91,6 @@ def get_hashes_and_next_idx(self, from_idx: RangeIdx, count: int) -> tuple[list[
def iter(self) -> Iterator[bytes]:
for element in self._index:
yield element.hash

def __contains__(self, elem: tuple[int, bytes]) -> bool:
return TransactionIndexElement(*elem) in self._index
156 changes: 0 additions & 156 deletions hathor/indexes/memory_tips_index.py

This file was deleted.

5 changes: 3 additions & 2 deletions hathor/indexes/mempool_tips_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ def iter_all(self, tx_storage: 'TransactionStorage') -> Iterator[Transaction]:
def get(self) -> set[bytes]:
"""
Get the set of mempool tips indexed.
What to do with `get_tx_tips()`? They kind of do the same thing and it might be really confusing in the future.
"""
raise NotImplementedError

Expand All @@ -98,6 +96,9 @@ def init_loop_step(self, tx: BaseTransaction) -> None:
assert tx.hash is not None
assert tx.storage is not None
tx_meta = tx.get_metadata()
# do not include voided transactions
if tx_meta.voided_by:
return
# do not include transactions that have been confirmed
if tx_meta.first_block:
return
Expand Down
Loading

0 comments on commit cbfd6f1

Please sign in to comment.