Skip to content

Commit 81565b1

Browse files
committed
refactor(p2p): implement P2P dependencies
1 parent 5234061 commit 81565b1

30 files changed

+409
-252
lines changed

hathor/builder/builder.py

+15-17
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from hathor.indexes import IndexesManager, MemoryIndexesManager, RocksDBIndexesManager
3535
from hathor.manager import HathorManager
3636
from hathor.mining.cpu_mining_service import CpuMiningService
37+
from hathor.p2p import P2PDependencies
3738
from hathor.p2p.manager import ConnectionsManager
3839
from hathor.p2p.peer import PrivatePeer
3940
from hathor.pubsub import PubSubManager
@@ -64,12 +65,10 @@ class SyncSupportLevel(IntEnum):
6465
@classmethod
6566
def add_factories(
6667
cls,
67-
settings: HathorSettingsType,
6868
p2p_manager: ConnectionsManager,
69+
dependencies: P2PDependencies,
6970
sync_v1_support: 'SyncSupportLevel',
7071
sync_v2_support: 'SyncSupportLevel',
71-
vertex_parser: VertexParser,
72-
vertex_handler: VertexHandler,
7372
) -> None:
7473
"""Adds the sync factory to the manager according to the support level."""
7574
from hathor.p2p.sync_v1.factory import SyncV11Factory
@@ -78,18 +77,12 @@ def add_factories(
7877

7978
# sync-v1 support:
8079
if sync_v1_support > cls.UNAVAILABLE:
81-
p2p_manager.add_sync_factory(SyncVersion.V1_1, SyncV11Factory(p2p_manager, vertex_parser=vertex_parser))
80+
p2p_manager.add_sync_factory(SyncVersion.V1_1, SyncV11Factory(dependencies))
8281
if sync_v1_support is cls.ENABLED:
8382
p2p_manager.enable_sync_version(SyncVersion.V1_1)
8483
# sync-v2 support:
8584
if sync_v2_support > cls.UNAVAILABLE:
86-
sync_v2_factory = SyncV2Factory(
87-
settings,
88-
p2p_manager,
89-
vertex_parser=vertex_parser,
90-
vertex_handler=vertex_handler,
91-
)
92-
p2p_manager.add_sync_factory(SyncVersion.V2, sync_v2_factory)
85+
p2p_manager.add_sync_factory(SyncVersion.V2, SyncV2Factory(dependencies))
9386
if sync_v2_support is cls.ENABLED:
9487
p2p_manager.enable_sync_version(SyncVersion.V2)
9588

@@ -415,25 +408,30 @@ def _get_or_create_p2p_manager(self) -> ConnectionsManager:
415408
return self._p2p_manager
416409

417410
enable_ssl = True
418-
reactor = self._get_reactor()
419411
my_peer = self._get_peer()
420412

421-
self._p2p_manager = ConnectionsManager(
413+
dependencies = P2PDependencies(
414+
reactor=self._get_reactor(),
422415
settings=self._get_or_create_settings(),
423-
reactor=reactor,
416+
vertex_parser=self._get_or_create_vertex_parser(),
417+
tx_storage=self._get_or_create_tx_storage(),
418+
vertex_handler=self._get_or_create_vertex_handler(),
419+
verification_service=self._get_or_create_verification_service(),
420+
)
421+
422+
self._p2p_manager = ConnectionsManager(
423+
dependencies=dependencies,
424424
my_peer=my_peer,
425425
pubsub=self._get_or_create_pubsub(),
426426
ssl=enable_ssl,
427427
whitelist_only=False,
428428
rng=self._rng,
429429
)
430430
SyncSupportLevel.add_factories(
431-
self._get_or_create_settings(),
432431
self._p2p_manager,
432+
dependencies,
433433
self._sync_v1_support,
434434
self._sync_v2_support,
435-
self._get_or_create_vertex_parser(),
436-
self._get_or_create_vertex_handler(),
437435
)
438436
return self._p2p_manager
439437

hathor/builder/cli_builder.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from hathor.indexes import IndexesManager, MemoryIndexesManager, RocksDBIndexesManager
3535
from hathor.manager import HathorManager
3636
from hathor.mining.cpu_mining_service import CpuMiningService
37+
from hathor.p2p import P2PDependencies
3738
from hathor.p2p.entrypoint import Entrypoint
3839
from hathor.p2p.manager import ConnectionsManager
3940
from hathor.p2p.peer import PrivatePeer
@@ -318,16 +319,6 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
318319

319320
cpu_mining_service = CpuMiningService()
320321

321-
p2p_manager = ConnectionsManager(
322-
settings=settings,
323-
reactor=reactor,
324-
my_peer=peer,
325-
pubsub=pubsub,
326-
ssl=True,
327-
whitelist_only=False,
328-
rng=Random(),
329-
)
330-
331322
vertex_handler = VertexHandler(
332323
reactor=reactor,
333324
settings=settings,
@@ -340,13 +331,29 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
340331
log_vertex_bytes=self._args.log_vertex_bytes,
341332
)
342333

334+
p2p_dependencies = P2PDependencies(
335+
reactor=reactor,
336+
settings=settings,
337+
vertex_parser=vertex_parser,
338+
tx_storage=tx_storage,
339+
vertex_handler=vertex_handler,
340+
verification_service=verification_service,
341+
)
342+
343+
p2p_manager = ConnectionsManager(
344+
dependencies=p2p_dependencies,
345+
my_peer=peer,
346+
pubsub=pubsub,
347+
ssl=True,
348+
whitelist_only=False,
349+
rng=Random(),
350+
)
351+
343352
SyncSupportLevel.add_factories(
344-
settings,
345353
p2p_manager,
354+
p2p_dependencies,
346355
sync_v1_support,
347356
sync_v2_support,
348-
vertex_parser,
349-
vertex_handler,
350357
)
351358

352359
from hathor.consensus.poa import PoaBlockProducer, PoaSignerFile

hathor/cli/quick_test.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
16+
1517
import os
1618
from argparse import ArgumentParser
17-
from typing import Any
19+
from typing import TYPE_CHECKING
1820

1921
from structlog import get_logger
2022

2123
from hathor.cli.run_node import RunNode
2224

25+
if TYPE_CHECKING:
26+
from hathor.transaction import Vertex
27+
2328
logger = get_logger()
2429

2530

@@ -30,18 +35,17 @@ def __init__(self, vertex_handler, manager, n_blocks):
3035
self._manager = manager
3136
self._n_blocks = n_blocks
3237

33-
def on_new_vertex(self, *args: Any, **kwargs: Any) -> bool:
38+
def on_new_vertex(self, vertex: Vertex, *, fails_silently: bool) -> bool:
3439
from hathor.transaction import Block
3540
from hathor.transaction.base_transaction import GenericVertex
3641

3742
msg: str | None = None
38-
res = self._vertex_handler.on_new_vertex(*args, **kwargs)
43+
res = self._vertex_handler.on_new_vertex(vertex=vertex, fails_silently=fails_silently)
3944

4045
if self._n_blocks is None:
4146
should_quit = res
4247
msg = 'added a tx'
4348
else:
44-
vertex = args[0]
4549
should_quit = False
4650
assert isinstance(vertex, GenericVertex)
4751

@@ -77,7 +81,7 @@ def prepare(self, *, register_resources: bool = True) -> None:
7781
self.log.info('patching vertex_handler.on_new_vertex to quit on success')
7882
p2p_factory = self.manager.connections.get_sync_factory(SyncVersion.V2)
7983
assert isinstance(p2p_factory, SyncV2Factory)
80-
p2p_factory.vertex_handler = VertexHandlerWrapper(
84+
p2p_factory.dependencies.vertex_handler = VertexHandlerWrapper(
8185
self.manager.vertex_handler,
8286
self.manager,
8387
self._args.quit_after_n_blocks,

hathor/p2p/__init__.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2024 Hathor Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from hathor.p2p.dependencies.p2p_dependencies import P2PDependencies
16+
17+
__all__ = [
18+
'P2PDependencies',
19+
]

hathor/p2p/dependencies/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2024 Hathor Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from hathor.conf.settings import HathorSettings
16+
from hathor.p2p.dependencies.protocols import (
17+
P2PTransactionStorageProtocol,
18+
P2PVerificationServiceProtocol,
19+
P2PVertexHandlerProtocol,
20+
)
21+
from hathor.reactor import ReactorProtocol
22+
from hathor.transaction.vertex_parser import VertexParser
23+
24+
25+
class P2PDependencies:
26+
"""A simple class to unify all node dependencies that are required by P2P."""
27+
28+
__slots__ = ('reactor', 'settings', 'vertex_parser', 'vertex_handler', 'verification_service', 'tx_storage')
29+
30+
def __init__(
31+
self,
32+
*,
33+
reactor: ReactorProtocol,
34+
settings: HathorSettings,
35+
vertex_parser: VertexParser,
36+
vertex_handler: P2PVertexHandlerProtocol,
37+
verification_service: P2PVerificationServiceProtocol,
38+
tx_storage: P2PTransactionStorageProtocol,
39+
) -> None:
40+
self.reactor = reactor
41+
self.settings = settings
42+
self.vertex_parser = vertex_parser
43+
self.vertex_handler = vertex_handler
44+
self.verification_service = verification_service
45+
self.tx_storage = tx_storage

hathor/p2p/dependencies/protocols.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2024 Hathor Labs
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Protocol
16+
17+
from hathor.indexes.height_index import HeightInfo
18+
from hathor.transaction import Block, Vertex
19+
from hathor.types import VertexId
20+
21+
22+
class P2PVertexHandlerProtocol(Protocol):
23+
"""Abstract the VertexHandler as a Python protocol to be used in P2P."""
24+
25+
def on_new_vertex(self, vertex: Vertex, *, fails_silently: bool = True) -> bool: ...
26+
27+
28+
class P2PVerificationServiceProtocol(Protocol):
29+
"""Abstract the VerificationService as a Python protocol to be used in P2P."""
30+
31+
def verify_basic(self, vertex: Vertex) -> None: ...
32+
33+
34+
class P2PTransactionStorageProtocol(Protocol):
35+
"""Abstract the TransactionStorage as a Python protocol to be used in P2P."""
36+
37+
def get_genesis(self, vertex_id: VertexId) -> Vertex | None: ...
38+
def get_vertex(self, vertex_id: VertexId) -> Vertex: ...
39+
def get_block(self, block_id: VertexId) -> Block: ...
40+
def transaction_exists(self, vertex_id: VertexId) -> bool: ...
41+
def can_validate_full(self, vertex: Vertex) -> bool: ...
42+
def compare_bytes_with_local_tx(self, vertex: Vertex) -> bool: ...
43+
def get_best_block(self) -> Block: ...
44+
def get_n_height_tips(self, n_blocks: int) -> list[HeightInfo]: ...
45+
def get_mempool_tips(self) -> set[VertexId]: ...
46+
def height_index_get(self, height: int) -> VertexId | None: ...
47+
def partial_vertex_exists(self, vertex_id: VertexId) -> bool: ...

hathor/p2p/factory.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from twisted.internet import protocol
1818
from twisted.internet.interfaces import IAddress
1919

20-
from hathor.conf.settings import HathorSettings
20+
from hathor.p2p import P2PDependencies
2121
from hathor.p2p.manager import ConnectionsManager
2222
from hathor.p2p.peer import PrivatePeer
2323
from hathor.p2p.protocol import HathorLineReceiver
@@ -31,22 +31,22 @@ def __init__(
3131
my_peer: PrivatePeer,
3232
p2p_manager: ConnectionsManager,
3333
*,
34-
settings: HathorSettings,
34+
dependencies: P2PDependencies,
3535
use_ssl: bool,
3636
):
3737
super().__init__()
38-
self._settings = settings
3938
self.my_peer = my_peer
4039
self.p2p_manager = p2p_manager
40+
self.dependencies = dependencies
4141
self.use_ssl = use_ssl
4242

4343
def buildProtocol(self, addr: IAddress) -> HathorLineReceiver:
4444
p = HathorLineReceiver(
4545
my_peer=self.my_peer,
4646
p2p_manager=self.p2p_manager,
47+
dependencies=self.dependencies,
4748
use_ssl=self.use_ssl,
4849
inbound=self.inbound,
49-
settings=self._settings
5050
)
5151
p.factory = self
5252
return p

0 commit comments

Comments
 (0)