29
29
from hathor .p2p .netfilter .factory import NetfilterFactory
30
30
from hathor .p2p .peer import Peer
31
31
from hathor .p2p .peer_discovery import PeerDiscovery
32
+ from hathor .p2p .peer_id import PeerId
32
33
from hathor .p2p .peer_storage import PeerStorage
33
34
from hathor .p2p .protocol import HathorProtocol
34
35
from hathor .p2p .rate_limiter import RateLimiter
51
52
52
53
53
54
class _SyncRotateInfo (NamedTuple ):
54
- candidates : list [str ]
55
- old : set [str ]
56
- new : set [str ]
57
- to_disable : set [str ]
58
- to_enable : set [str ]
55
+ candidates : list [PeerId ]
56
+ old : set [PeerId ]
57
+ new : set [PeerId ]
58
+ to_disable : set [PeerId ]
59
+ to_enable : set [PeerId ]
59
60
60
61
61
62
class _ConnectingPeer (NamedTuple ):
@@ -79,7 +80,7 @@ class GlobalRateLimiter:
79
80
80
81
manager : Optional ['HathorManager' ]
81
82
connections : set [HathorProtocol ]
82
- connected_peers : dict [str , HathorProtocol ]
83
+ connected_peers : dict [PeerId , HathorProtocol ]
83
84
connecting_peers : dict [IStreamClientEndpoint , _ConnectingPeer ]
84
85
handshaking_peers : set [HathorProtocol ]
85
86
whitelist_only : bool
@@ -174,7 +175,7 @@ def __init__(
174
175
self .lc_sync_update_interval : float = 5 # seconds
175
176
176
177
# Peers that always have sync enabled.
177
- self .always_enable_sync : set [str ] = set ()
178
+ self .always_enable_sync : set [PeerId ] = set ()
178
179
179
180
# Timestamp of the last time sync was updated.
180
181
self ._last_sync_rotate : float = 0.
@@ -485,7 +486,7 @@ def iter_not_ready_endpoints(self) -> Iterable[Entrypoint]:
485
486
else :
486
487
self .log .warn ('handshaking protocol has empty connection string' , protocol = protocol )
487
488
488
- def is_peer_connected (self , peer_id : str ) -> bool :
489
+ def is_peer_connected (self , peer_id : PeerId ) -> bool :
489
490
"""
490
491
:type peer_id: string (peer.id)
491
492
"""
@@ -729,7 +730,7 @@ def get_connection_to_drop(self, protocol: HathorProtocol) -> HathorProtocol:
729
730
assert protocol .peer .id is not None
730
731
assert protocol .my_peer .id is not None
731
732
other_connection = self .connected_peers [protocol .peer .id ]
732
- if protocol .my_peer .id > protocol .peer .id :
733
+ if bytes ( protocol .my_peer .id ) > bytes ( protocol .peer .id ) :
733
734
# connection started by me is kept
734
735
if not protocol .inbound :
735
736
# other connection is dropped
@@ -751,7 +752,7 @@ def drop_connection(self, protocol: HathorProtocol) -> None:
751
752
self .log .debug ('dropping connection' , peer_id = protocol .peer .id , protocol = type (protocol ).__name__ )
752
753
protocol .send_error_and_close_connection ('Connection droped' )
753
754
754
- def drop_connection_by_peer_id (self , peer_id : str ) -> None :
755
+ def drop_connection_by_peer_id (self , peer_id : PeerId ) -> None :
755
756
""" Drop a connection by peer id
756
757
"""
757
758
protocol = self .connected_peers .get (peer_id )
@@ -765,9 +766,9 @@ def sync_update(self) -> None:
765
766
except Exception :
766
767
self .log .error ('_sync_rotate_if_needed failed' , exc_info = True )
767
768
768
- def set_always_enable_sync (self , values : list [str ]) -> None :
769
+ def set_always_enable_sync (self , values : list [PeerId ]) -> None :
769
770
"""Set a new list of peers to always enable sync. This operation completely replaces the previous list."""
770
- new : set [str ] = set (values )
771
+ new : set [PeerId ] = set (values )
771
772
772
773
old = self .always_enable_sync
773
774
if new == old :
@@ -792,14 +793,14 @@ def set_always_enable_sync(self, values: list[str]) -> None:
792
793
793
794
def _calculate_sync_rotate (self ) -> _SyncRotateInfo :
794
795
"""Calculate new sync rotation."""
795
- current_enabled : set [str ] = set ()
796
+ current_enabled : set [PeerId ] = set ()
796
797
for peer_id , conn in self .connected_peers .items ():
797
798
if conn .is_sync_enabled ():
798
799
current_enabled .add (peer_id )
799
800
800
801
candidates = list (self .connected_peers .keys ())
801
802
self .rng .shuffle (candidates )
802
- selected_peers : set [str ] = set (candidates [:self .MAX_ENABLED_SYNC ])
803
+ selected_peers : set [PeerId ] = set (candidates [:self .MAX_ENABLED_SYNC ])
803
804
804
805
to_disable = current_enabled - selected_peers
805
806
to_enable = selected_peers - current_enabled
0 commit comments