24
24
from twisted .python .failure import Failure
25
25
from twisted .web .client import Agent
26
26
27
- from hathor .conf .settings import HathorSettings
28
27
from hathor .p2p .entrypoint import Entrypoint
29
28
from hathor .p2p .netfilter .factory import NetfilterFactory
29
+ from hathor .p2p .p2p_dependencies import P2PDependencies
30
30
from hathor .p2p .peer import PrivatePeer , PublicPeer , UnverifiedPeer
31
31
from hathor .p2p .peer_discovery import PeerDiscovery
32
32
from hathor .p2p .peer_id import PeerId
37
37
from hathor .p2p .sync_factory import SyncAgentFactory
38
38
from hathor .p2p .sync_version import SyncVersion
39
39
from hathor .p2p .utils import parse_whitelist
40
- from hathor .pubsub import HathorEvents , PubSubManager
41
- from hathor .reactor import ReactorProtocol as Reactor
40
+ from hathor .pubsub import HathorEvents
42
41
from hathor .transaction import BaseTransaction
43
42
from hathor .util import Random
44
43
@@ -93,24 +92,23 @@ class GlobalRateLimiter:
93
92
94
93
def __init__ (
95
94
self ,
96
- settings : HathorSettings ,
97
- reactor : Reactor ,
95
+ dependencies : P2PDependencies ,
98
96
my_peer : PrivatePeer ,
99
- pubsub : PubSubManager ,
100
97
ssl : bool ,
101
98
rng : Random ,
102
99
whitelist_only : bool ,
103
100
) -> None :
104
101
self .log = logger .new ()
105
- self ._settings = settings
102
+ self .dependencies = dependencies
103
+ self ._settings = dependencies .settings
106
104
self .rng = rng
107
105
self .manager = None
108
106
109
- self .MAX_ENABLED_SYNC = settings .MAX_ENABLED_SYNC
110
- self .SYNC_UPDATE_INTERVAL = settings .SYNC_UPDATE_INTERVAL
111
- self .PEER_DISCOVERY_INTERVAL = settings .PEER_DISCOVERY_INTERVAL
107
+ self .MAX_ENABLED_SYNC = self . _settings .MAX_ENABLED_SYNC
108
+ self .SYNC_UPDATE_INTERVAL = self . _settings .SYNC_UPDATE_INTERVAL
109
+ self .PEER_DISCOVERY_INTERVAL = self . _settings .PEER_DISCOVERY_INTERVAL
112
110
113
- self .reactor = reactor
111
+ self .reactor = dependencies . reactor
114
112
self .my_peer = my_peer
115
113
116
114
# List of address descriptions to listen for new connections (eg: [tcp:8000])
@@ -129,10 +127,16 @@ def __init__(
129
127
from hathor .p2p .factory import HathorClientFactory , HathorServerFactory
130
128
self .use_ssl = ssl
131
129
self .server_factory = HathorServerFactory (
132
- self .my_peer , p2p_manager = self , use_ssl = self .use_ssl , settings = self ._settings
130
+ my_peer = self .my_peer ,
131
+ p2p_manager = self ,
132
+ dependencies = dependencies ,
133
+ use_ssl = self .use_ssl ,
133
134
)
134
135
self .client_factory = HathorClientFactory (
135
- self .my_peer , p2p_manager = self , use_ssl = self .use_ssl , settings = self ._settings
136
+ my_peer = self .my_peer ,
137
+ p2p_manager = self ,
138
+ dependencies = dependencies ,
139
+ use_ssl = self .use_ssl ,
136
140
)
137
141
138
142
# Global maximum number of connections.
@@ -184,9 +188,6 @@ def __init__(
184
188
self .wl_reconnect = LoopingCall (self .update_whitelist )
185
189
self .wl_reconnect .clock = self .reactor
186
190
187
- # Pubsub object to publish events
188
- self .pubsub = pubsub
189
-
190
191
# Parameter to explicitly enable whitelist-only mode, when False it will still check the whitelist for sync-v1
191
192
self .whitelist_only = whitelist_only
192
193
@@ -373,7 +374,7 @@ def on_connection_failure(self, failure: Failure, peer: Optional[UnverifiedPeer
373
374
self .log .warn ('connection failure' , entrypoint = entrypoint , failure = failure .getErrorMessage ())
374
375
self .connecting_peers .pop (endpoint )
375
376
376
- self .pubsub .publish (
377
+ self .dependencies .publish (
377
378
HathorEvents .NETWORK_PEER_CONNECTION_FAILED ,
378
379
peer = peer ,
379
380
peers_count = self ._get_peers_count ()
@@ -388,7 +389,7 @@ def on_peer_connect(self, protocol: HathorProtocol) -> None:
388
389
self .connections .add (protocol )
389
390
self .handshaking_peers .add (protocol )
390
391
391
- self .pubsub .publish (
392
+ self .dependencies .publish (
392
393
HathorEvents .NETWORK_PEER_CONNECTED ,
393
394
protocol = protocol ,
394
395
peers_count = self ._get_peers_count ()
@@ -405,7 +406,7 @@ def on_peer_ready(self, protocol: HathorProtocol) -> None:
405
406
406
407
# we emit the event even if it's a duplicate peer as a matching
407
408
# NETWORK_PEER_DISCONNECTED will be emitted regardless
408
- self .pubsub .publish (
409
+ self .dependencies .publish (
409
410
HathorEvents .NETWORK_PEER_READY ,
410
411
protocol = protocol ,
411
412
peers_count = self ._get_peers_count ()
@@ -459,7 +460,7 @@ def on_peer_disconnect(self, protocol: HathorProtocol) -> None:
459
460
# chance it can happen if both connections start at the same time and none of them has
460
461
# reached READY state while the other is on PEER_ID state
461
462
self .connected_peers [protocol .peer .id ] = existing_protocol
462
- self .pubsub .publish (
463
+ self .dependencies .publish (
463
464
HathorEvents .NETWORK_PEER_DISCONNECTED ,
464
465
protocol = protocol ,
465
466
peers_count = self ._get_peers_count ()
@@ -653,7 +654,7 @@ def connect_to(
653
654
deferred .addCallback (self ._connect_to_callback , peer , endpoint , entrypoint ) # type: ignore
654
655
deferred .addErrback (self .on_connection_failure , peer , endpoint ) # type: ignore
655
656
self .log .info ('connect to' , entrypoint = str (entrypoint ), peer = str (peer ))
656
- self .pubsub .publish (
657
+ self .dependencies .publish (
657
658
HathorEvents .NETWORK_PEER_CONNECTING ,
658
659
peer = peer ,
659
660
peers_count = self ._get_peers_count ()
0 commit comments