Skip to content

Commit 8a1abfc

Browse files
committed
improvements 3
1 parent d593364 commit 8a1abfc

File tree

9 files changed

+17
-41
lines changed

9 files changed

+17
-41
lines changed

hathor/consensus/consensus.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,10 @@ def create_context(self) -> ConsensusAlgorithmContext:
7272
def update(self, base: BaseTransaction) -> None:
7373
assert base.storage is not None
7474
assert base.storage.is_only_valid_allowed()
75-
meta = base.get_metadata()
7675
try:
77-
if meta.voided_by and settings.CONSENSUS_FAIL_ID in meta.voided_by:
78-
self.log.warn('voided_by has previously failed, removing so we can revalidate', vertex=base.hash_hex)
79-
meta.del_voided_by(settings.CONSENSUS_FAIL_ID)
8076
self._unsafe_update(base)
8177
except Exception:
78+
meta = base.get_metadata()
8279
meta.add_voided_by(settings.CONSENSUS_FAIL_ID)
8380
assert base.storage is not None
8481
base.storage.save_transaction(base, only_metadata=True)

hathor/indexes/deps_index.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ def remove_from_needed_index(self, tx: bytes) -> None:
190190
raise NotImplementedError
191191

192192
@abstractmethod
193-
def get_next_needed_tx(self) -> bytes:
194-
"""Choose the start hash for downloading the needed txs"""
195-
raise NotImplementedError
196-
197-
# TODO: make this abstract
198-
def checked_next_needed_txs(self, tx_storage: 'TransactionStorage') -> Iterator[bytes]:
193+
def iter_next_needed_txs(self) -> Iterator[bytes]:
194+
"""Iterate over the next needed transactions."""
199195
raise NotImplementedError

hathor/indexes/memory_deps_index.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,9 @@ def is_tx_needed(self, tx: bytes) -> bool:
147147
def remove_from_needed_index(self, tx: bytes) -> None:
148148
self._needed_txs_index.pop(tx, None)
149149

150-
def get_next_needed_tx(self) -> bytes:
151-
# This strategy maximizes the chance to download multiple txs on the same stream
152-
# find the tx with highest "height"
153-
# XXX: we could cache this onto `needed_txs` so we don't have to fetch txs every time
154-
# TODO: improve this by using some sorted data structure to make this better than O(n)
155-
height, start_hash, tx = max((h, s, t) for t, (h, s) in self._needed_txs_index.items())
156-
self.log.debug('next needed tx start', needed=len(self._needed_txs_index), start=start_hash.hex(),
157-
height=height, needed_tx=tx.hex())
158-
return start_hash
159-
160-
def checked_next_needed_txs(self, tx_storage: 'TransactionStorage') -> Iterator[bytes]:
150+
def iter_next_needed_txs(self) -> Iterator[bytes]:
161151
for _, (__, tx_hash) in self._needed_txs_index.items():
162-
basic_tx_exists = False
163-
with tx_storage.allow_partially_validated_context():
164-
basic_tx_exists = tx_storage.transaction_exists(tx_hash)
165-
if not basic_tx_exists:
166-
yield tx_hash
152+
yield tx_hash
167153

168154
def _add_needed(self, tx: BaseTransaction) -> None:
169155
"""This method is idempotent, because self.update needs it to be indempotent."""

hathor/indexes/rocksdb_deps_index.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,6 @@ def remove_from_needed_index(self, tx: bytes) -> None:
351351
key_needed = self._to_key_needed(tx)
352352
self._db.delete((self._cf, key_needed))
353353

354-
def get_next_needed_tx(self) -> bytes:
355-
# This strategy maximizes the chance to download multiple txs on the same stream
356-
# Find the tx with highest "height"
357-
# XXX: we could cache this onto `needed_txs` so we don't have to fetch txs every time
358-
# TODO: improve this by using some sorted data structure to make this better than O(n)
359-
height, start_hash, tx = max((h, s, t) for t, h, s in self._iter_needed())
360-
self.log.debug('next needed tx start', start=start_hash.hex(), height=height, needed_tx=tx.hex())
361-
return start_hash
354+
def iter_next_needed_txs(self) -> Iterator[bytes]:
355+
for _, __, tx_hash in self._iter_needed():
356+
yield tx_hash

hathor/p2p/sync_v2/manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021 Hathor Labs
1+
# Copyright 2023 Hathor Labs
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -42,7 +42,7 @@
4242
settings = HathorSettings()
4343
logger = get_logger()
4444

45-
MAX_GET_TRANSACTIONS_BFS_LEN: int = 256
45+
MAX_GET_TRANSACTIONS_BFS_LEN: int = 8
4646

4747

4848
class PeerState(Enum):
@@ -312,7 +312,7 @@ def run_sync_transactions(self) -> None:
312312
assert self.tx_storage.indexes.deps is not None
313313

314314
# start_hash = self.tx_storage.indexes.deps.get_next_needed_tx()
315-
needed_txs, _ = collect_n(self.tx_storage.indexes.deps.checked_next_needed_txs(self.tx_storage),
315+
needed_txs, _ = collect_n(self.tx_storage.indexes.deps.iter_next_needed_txs(),
316316
MAX_GET_TRANSACTIONS_BFS_LEN)
317317

318318
# Start with the last received block and find the best block full validated in its chain

hathor/p2p/sync_v2/streamers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def __init__(self, node_sync: 'NodeBlockSync', start_from: list[BaseTransaction]
193193
self.last_block_height = 0
194194

195195
self.bfs = BFSOrderWalk(self.storage, is_dag_verifications=True, is_dag_funds=True, is_left_to_right=False)
196-
# XXX: maybe it should skip root
197196
self.iter = self.bfs.run(start_from, skip_root=False)
198197

199198
def start(self) -> None:

hathor/util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ def collect_n(it: Iterator[_T], n: int) -> tuple[list[_T], bool]:
363363
364364
>>> collect_n(iter(range(10)), 8)
365365
([0, 1, 2, 3, 4, 5, 6, 7], True)
366+
367+
# This also works for checking (albeit destructively, because it consumes from the itreator), if it is empty
368+
369+
>>> collect_n(iter(range(10)), 0)
370+
([], True)
366371
"""
367372
if n < 0:
368373
raise ValueError(f'n must be non-negative, got {n}')

tests/p2p/test_sync_v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _run_restart_test(self, *, full_verification: bool, use_tx_storage_cache: bo
107107

108108
manager3 = self.simulator.create_peer(builder3)
109109
self.assertEqual(partial_blocks, self._get_partial_blocks(manager3.tx_storage))
110-
self.assertTrue(manager3.tx_storage.indexes.deps.has_needed_tx())
110+
self.assertTrue(manager3.tx_storage.indexes.deps.has_needed_tx(manager3.tx_storage))
111111

112112
conn13 = FakeConnection(manager1, manager3, latency=0.05)
113113
self.simulator.add_connection(conn13)

tests/simulation/test_simulator.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ def test_new_syncing_peer(self):
139139
for miner in miners:
140140
miner.stop()
141141

142-
# self.simulator.run(1200)
143-
# self.simulator.run_until_complete(1200)
144142
self.assertTrue(self.simulator.run(3600, trigger=AllTriggers(stop_triggers)))
145143

146144
for idx, node in enumerate(nodes):

0 commit comments

Comments
 (0)