Skip to content

Commit c32f145

Browse files
committed
refactor: remove VertexStorageProtocol
1 parent 25373c2 commit c32f145

13 files changed

+38
-79
lines changed

hathor/consensus/block_consensus.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def remove_first_block_markers(self, block: Block) -> None:
432432
storage = block.storage
433433

434434
from hathor.transaction.storage.traversal import BFSTimestampWalk
435-
bfs = BFSTimestampWalk(storage, is_dag_verifications=True, is_left_to_right=False)
435+
bfs = BFSTimestampWalk(storage.get_vertex, is_dag_verifications=True, is_left_to_right=False)
436436
for tx in bfs.run(block, skip_root=True):
437437
if tx.is_block:
438438
bfs.skip_neighbors(tx)
@@ -469,7 +469,7 @@ def _score_block_dfs(self, block: BaseTransaction, used: set[bytes],
469469

470470
else:
471471
from hathor.transaction.storage.traversal import BFSTimestampWalk
472-
bfs = BFSTimestampWalk(storage, is_dag_verifications=True, is_left_to_right=False)
472+
bfs = BFSTimestampWalk(storage.get_vertex, is_dag_verifications=True, is_left_to_right=False)
473473
for tx in bfs.run(parent, skip_root=False):
474474
assert not tx.is_block
475475

hathor/consensus/transaction_consensus.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,9 @@ def remove_voided_by(self, tx: Transaction, voided_hash: bytes) -> bool:
344344

345345
self.log.debug('remove_voided_by', tx=tx.hash_hex, voided_hash=voided_hash.hex())
346346

347-
bfs = BFSTimestampWalk(tx.storage, is_dag_funds=True, is_dag_verifications=True, is_left_to_right=True)
347+
bfs = BFSTimestampWalk(
348+
tx.storage.get_vertex, is_dag_funds=True, is_dag_verifications=True, is_left_to_right=True
349+
)
348350
check_list: list[BaseTransaction] = []
349351
for tx2 in bfs.run(tx, skip_root=False):
350352
assert tx2.storage is not None
@@ -400,7 +402,7 @@ def add_voided_by(self, tx: Transaction, voided_hash: bytes) -> bool:
400402
is_dag_verifications = False
401403

402404
from hathor.transaction.storage.traversal import BFSTimestampWalk
403-
bfs = BFSTimestampWalk(tx.storage, is_dag_funds=True, is_dag_verifications=is_dag_verifications,
405+
bfs = BFSTimestampWalk(tx.storage.get_vertex, is_dag_funds=True, is_dag_verifications=is_dag_verifications,
404406
is_left_to_right=True)
405407
check_list: list[Transaction] = []
406408
for tx2 in bfs.run(tx, skip_root=False):

hathor/indexes/mempool_tips_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def iter(self, tx_storage: 'TransactionStorage', max_timestamp: Optional[float]
209209

210210
def iter_all(self, tx_storage: 'TransactionStorage') -> Iterator[Transaction]:
211211
from hathor.transaction.storage.traversal import BFSTimestampWalk
212-
bfs = BFSTimestampWalk(tx_storage, is_dag_verifications=True, is_left_to_right=False)
212+
bfs = BFSTimestampWalk(tx_storage.get_vertex, is_dag_verifications=True, is_left_to_right=False)
213213
for tx in bfs.run(self.iter(tx_storage), skip_root=False):
214214
assert isinstance(tx, Transaction)
215215
if tx.get_metadata().first_block is not None:

hathor/p2p/sync_v2/streamers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ def __init__(self,
232232
assert tx.get_metadata().first_block == self.first_block.hash
233233

234234
self.current_block: Optional[Block] = self.first_block
235-
self.bfs = BFSOrderWalk(self.tx_storage, is_dag_verifications=True, is_dag_funds=True, is_left_to_right=False)
235+
self.bfs = BFSOrderWalk(
236+
self.tx_storage.get_vertex, is_dag_verifications=True, is_dag_funds=True, is_left_to_right=False
237+
)
236238
self.iter = self.get_iter()
237239

238240
def _stop_streaming_server(self, response_code: StreamEnd) -> None:

hathor/reward_lock/reward_lock.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
from typing import TYPE_CHECKING, Iterator, Optional
1618

1719
from hathor.conf.settings import HathorSettings
1820
from hathor.transaction import Block
1921
from hathor.util import not_none
2022

2123
if TYPE_CHECKING:
22-
from hathor.transaction.storage.vertex_storage_protocol import VertexStorageProtocol
24+
from hathor.transaction.storage import TransactionStorage
2325
from hathor.transaction.transaction import RewardLockedInfo, Transaction
2426

2527

26-
def iter_spent_rewards(tx: 'Transaction', storage: 'VertexStorageProtocol') -> Iterator[Block]:
28+
def iter_spent_rewards(tx: 'Transaction', storage: TransactionStorage) -> Iterator[Block]:
2729
"""Iterate over all the rewards being spent, assumes tx has been verified."""
2830
for input_tx in tx.inputs:
2931
spent_tx = storage.get_vertex(input_tx.tx_id)
@@ -41,7 +43,7 @@ def is_spent_reward_locked(settings: HathorSettings, tx: 'Transaction') -> bool:
4143
def get_spent_reward_locked_info(
4244
settings: HathorSettings,
4345
tx: 'Transaction',
44-
storage: 'VertexStorageProtocol',
46+
storage: TransactionStorage,
4547
) -> Optional['RewardLockedInfo']:
4648
"""Check if any input block reward is locked, returning the locked information if any, or None if they are all
4749
unlocked."""
@@ -54,7 +56,7 @@ def get_spent_reward_locked_info(
5456
return None
5557

5658

57-
def get_minimum_best_height(storage: 'VertexStorageProtocol') -> int:
59+
def get_minimum_best_height(storage: TransactionStorage) -> int:
5860
"""Return the height of the current best block that shall be used for `min_height` verification."""
5961
import math
6062

hathor/transaction/base_transaction.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,9 @@ def update_accumulated_weight(self, *, stop_value: float = inf, save_file: bool
672672
# directly verified by a block.
673673

674674
from hathor.transaction.storage.traversal import BFSTimestampWalk
675-
bfs_walk = BFSTimestampWalk(self.storage, is_dag_funds=True, is_dag_verifications=True, is_left_to_right=True)
675+
bfs_walk = BFSTimestampWalk(
676+
self.storage.get_vertex, is_dag_funds=True, is_dag_verifications=True, is_left_to_right=True
677+
)
676678
for tx in bfs_walk.run(self, skip_root=True):
677679
accumulated_weight += weight_to_work(tx.weight)
678680
if accumulated_weight > stop_value:

hathor/transaction/block.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ def iter_transactions_in_this_block(self) -> Iterator[BaseTransaction]:
360360
"""Return an iterator of the transactions that have this block as meta.first_block."""
361361
from hathor.transaction.storage.traversal import BFSOrderWalk
362362
assert self.storage is not None
363-
bfs = BFSOrderWalk(self.storage, is_dag_verifications=True, is_dag_funds=True, is_left_to_right=False)
363+
bfs = BFSOrderWalk(
364+
self.storage.get_vertex, is_dag_verifications=True, is_dag_funds=True, is_left_to_right=False
365+
)
364366
for tx in bfs.run(self, skip_root=True):
365367
tx_meta = tx.get_metadata()
366368
if tx_meta.first_block != self.hash:

hathor/transaction/storage/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from hathor.transaction.storage.cache_storage import TransactionCacheStorage
1616
from hathor.transaction.storage.memory_storage import TransactionMemoryStorage
1717
from hathor.transaction.storage.transaction_storage import TransactionStorage
18-
from hathor.transaction.storage.vertex_storage_protocol import VertexStorageProtocol
1918

2019
try:
2120
from hathor.transaction.storage.rocksdb_storage import TransactionRocksDBStorage
@@ -27,5 +26,4 @@
2726
'TransactionMemoryStorage',
2827
'TransactionCacheStorage',
2928
'TransactionRocksDBStorage',
30-
'VertexStorageProtocol'
3129
]

hathor/transaction/storage/transaction_storage.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ def iter_mempool_from_tx_tips(self) -> Iterator[Transaction]:
10121012
from hathor.transaction.storage.traversal import BFSTimestampWalk
10131013

10141014
root = self.iter_mempool_tips_from_tx_tips()
1015-
walk = BFSTimestampWalk(self, is_dag_funds=True, is_dag_verifications=True, is_left_to_right=False)
1015+
walk = BFSTimestampWalk(self.get_vertex, is_dag_funds=True, is_dag_verifications=True, is_left_to_right=False)
10161016
for tx in walk.run(root):
10171017
tx_meta = tx.get_metadata()
10181018
# XXX: skip blocks and tx-tips that have already been confirmed

hathor/transaction/storage/traversal.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
from abc import ABC, abstractmethod
1919
from collections import deque
2020
from itertools import chain
21-
from typing import TYPE_CHECKING, Iterable, Iterator, Optional, Union
21+
from typing import TYPE_CHECKING, Callable, Iterable, Iterator, Optional, Union
2222

2323
if TYPE_CHECKING:
24-
from hathor.transaction import BaseTransaction # noqa: F401
25-
from hathor.transaction.storage import VertexStorageProtocol
24+
from hathor.transaction import BaseTransaction, Vertex # noqa: F401
2625
from hathor.types import VertexId
2726

2827

@@ -50,7 +49,7 @@ class GenericWalk(ABC):
5049

5150
def __init__(
5251
self,
53-
storage: VertexStorageProtocol,
52+
vertex_getter: Callable[[VertexId], Vertex],
5453
*,
5554
is_dag_funds: bool = False,
5655
is_dag_verifications: bool = False,
@@ -64,7 +63,7 @@ def __init__(
6463
:param is_dag_verifications: Add neighbors from the DAG of verifications
6564
:param is_left_to_right: Decide which side of the DAG we will walk to
6665
"""
67-
self.storage = storage
66+
self.vertex_getter = vertex_getter
6867
self.seen = set()
6968

7069
self.is_dag_funds = is_dag_funds
@@ -119,7 +118,7 @@ def add_neighbors(self, tx: 'BaseTransaction') -> None:
119118
for _hash in it:
120119
if _hash not in self.seen:
121120
self.seen.add(_hash)
122-
neighbor = self.storage.get_vertex(_hash)
121+
neighbor = self.vertex_getter(_hash)
123122
self._push_visit(neighbor)
124123

125124
def skip_neighbors(self, tx: 'BaseTransaction') -> None:
@@ -164,14 +163,14 @@ class BFSTimestampWalk(GenericWalk):
164163

165164
def __init__(
166165
self,
167-
storage: VertexStorageProtocol,
166+
vertex_getter: Callable[[VertexId], Vertex],
168167
*,
169168
is_dag_funds: bool = False,
170169
is_dag_verifications: bool = False,
171170
is_left_to_right: bool = True,
172171
) -> None:
173172
super().__init__(
174-
storage,
173+
vertex_getter=vertex_getter,
175174
is_dag_funds=is_dag_funds,
176175
is_dag_verifications=is_dag_verifications,
177176
is_left_to_right=is_left_to_right
@@ -200,14 +199,14 @@ class BFSOrderWalk(GenericWalk):
200199

201200
def __init__(
202201
self,
203-
storage: VertexStorageProtocol,
202+
vertex_getter: Callable[[VertexId], Vertex],
204203
*,
205204
is_dag_funds: bool = False,
206205
is_dag_verifications: bool = False,
207206
is_left_to_right: bool = True,
208207
) -> None:
209208
super().__init__(
210-
storage,
209+
vertex_getter=vertex_getter,
211210
is_dag_funds=is_dag_funds,
212211
is_dag_verifications=is_dag_verifications,
213212
is_left_to_right=is_left_to_right
@@ -231,14 +230,14 @@ class DFSWalk(GenericWalk):
231230

232231
def __init__(
233232
self,
234-
storage: VertexStorageProtocol,
233+
vertex_getter: Callable[[VertexId], Vertex],
235234
*,
236235
is_dag_funds: bool = False,
237236
is_dag_verifications: bool = False,
238237
is_left_to_right: bool = True,
239238
) -> None:
240239
super().__init__(
241-
storage,
240+
vertex_getter=vertex_getter,
242241
is_dag_funds=is_dag_funds,
243242
is_dag_verifications=is_dag_verifications,
244243
is_left_to_right=is_left_to_right

hathor/transaction/storage/vertex_storage_protocol.py

-48
This file was deleted.

tests/p2p/test_sync_v2.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def test_exceeds_streaming_and_mempool_limits(self) -> None:
197197
blk = manager1.tx_storage.get_best_block()
198198
tx_parents = [manager1.tx_storage.get_transaction(x) for x in blk.parents[1:]]
199199
self.assertEqual(len(tx_parents), 2)
200-
dfs = DFSWalk(manager1.tx_storage, is_dag_verifications=True, is_left_to_right=False)
200+
dfs = DFSWalk(manager1.tx_storage.get_vertex, is_dag_verifications=True, is_left_to_right=False)
201201
cnt = 0
202202
for tx in dfs.run(tx_parents):
203203
if tx.get_metadata().first_block == blk.hash:

tests/tx/test_traversal.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_right_to_left(self):
8989

9090
class BaseBFSTimestampWalkTestCase(_TraversalTestCase):
9191
def gen_walk(self, **kwargs):
92-
return BFSTimestampWalk(self.manager.tx_storage, **kwargs)
92+
return BFSTimestampWalk(self.manager.tx_storage.get_vertex, **kwargs)
9393

9494
def _run_lr(self, walk, skip_root=True):
9595
seen = set()
@@ -120,7 +120,7 @@ class SyncV2BFSTimestampWalkTestCase(unittest.SyncV2Params, BaseBFSTimestampWalk
120120

121121
class BaseBFSOrderWalkTestCase(_TraversalTestCase):
122122
def gen_walk(self, **kwargs):
123-
return BFSOrderWalk(self.manager.tx_storage, **kwargs)
123+
return BFSOrderWalk(self.manager.tx_storage.get_vertex, **kwargs)
124124

125125
def _run_lr(self, walk, skip_root=True):
126126
seen = set()
@@ -168,7 +168,7 @@ class SyncBridgeBFSOrderWalkTestCase(unittest.SyncBridgeParams, SyncV2BFSOrderWa
168168

169169
class BaseDFSWalkTestCase(_TraversalTestCase):
170170
def gen_walk(self, **kwargs):
171-
return DFSWalk(self.manager.tx_storage, **kwargs)
171+
return DFSWalk(self.manager.tx_storage.get_vertex, **kwargs)
172172

173173
def _run_lr(self, walk, skip_root=True):
174174
seen = set()

0 commit comments

Comments
 (0)