Skip to content

Commit 5de4584

Browse files
committed
refactor after rebase
1 parent f3d8cbf commit 5de4584

File tree

5 files changed

+56
-70
lines changed

5 files changed

+56
-70
lines changed

libp2p/abc.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -479,18 +479,6 @@ def peers_with_addrs(self) -> list[ID]:
479479
480480
"""
481481

482-
@abstractmethod
483-
def set_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
484-
"""Set addr"""
485-
486-
@abstractmethod
487-
def set_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
488-
"""Set addrs"""
489-
490-
@abstractmethod
491-
def update_addrs(self, peer_id: ID, oldTTL: int, newTTL: int) -> None:
492-
"""Update addrs"""
493-
494482
@abstractmethod
495483
def addr_stream(self, peer_id: ID) -> None:
496484
"""Addr stream"""
@@ -527,7 +515,7 @@ def peer_with_keys(self) -> list[ID]:
527515
"""peer_with_keys"""
528516

529517
@abstractmethod
530-
def clear_keydata(self, peer_id: ID) -> PublicKey:
518+
def clear_keydata(self, peer_id: ID) -> None:
531519
"""clear_keydata"""
532520

533521

@@ -671,18 +659,6 @@ def add_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
671659
672660
"""
673661

674-
@abstractmethod
675-
def set_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
676-
"""set_addr"""
677-
678-
@abstractmethod
679-
def set_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
680-
"""set_addrs"""
681-
682-
@abstractmethod
683-
def update_addrs(self, peer_id: ID, oldTTL: int, newTTL: int) -> None:
684-
"""update_addrs"""
685-
686662
@abstractmethod
687663
def addrs(self, peer_id: ID) -> list[Multiaddr]:
688664
"""
@@ -835,7 +811,7 @@ def peer_with_keys(self) -> list[ID]:
835811
"""peer_with_keys"""
836812

837813
@abstractmethod
838-
def clear_keydata(self, peer_id: ID) -> PublicKey:
814+
def clear_keydata(self, peer_id: ID) -> None:
839815
"""clear_keydata"""
840816

841817
##
@@ -1488,7 +1464,7 @@ def set_protocols(self, protocols: Sequence[str]) -> None:
14881464
"""
14891465

14901466
@abstractmethod
1491-
def add_addrs(self, addrs: Sequence[Multiaddr], ttl: int) -> None:
1467+
def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
14921468
"""
14931469
Add multiple multiaddresses to the peer's data.
14941470

libp2p/peer/peerdata.py

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class PeerData(IPeerData):
3434
addrs: list[Multiaddr]
3535
last_identified: int
3636
ttl: int # Keep ttl=0 by default for always valid
37+
latmap: float
3738

3839
def __init__(self) -> None:
3940
self.pubkey = None
@@ -43,6 +44,7 @@ def __init__(self) -> None:
4344
self.addrs = []
4445
self.last_identified = int(time.time())
4546
self.ttl = 0
47+
self.latmap = 0
4648

4749
def get_protocols(self) -> list[str]:
4850
"""
@@ -92,52 +94,13 @@ def clear_protocol_data(self) -> None:
9294
"""Clear all protocols"""
9395
self.protocols = []
9496

95-
def add_addrs(self, addrs: Sequence[Multiaddr], ttl: int) -> None:
97+
def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
9698
"""
9799
:param addrs: multiaddresses to add
98100
"""
99-
expiry = time.time() + ttl if ttl is not None else float("inf")
100101
for addr in addrs:
101102
if addr not in self.addrs:
102103
self.addrs.append(addr)
103-
current_expiry = self.addrs_ttl.get(addr, 0)
104-
if expiry > current_expiry:
105-
self.addrs_ttl[addr] = expiry
106-
107-
def set_addrs(self, addrs: Sequence[Multiaddr], ttl: int) -> None:
108-
"""
109-
:param addrs: multiaddresses to update
110-
:param ttl: new ttl
111-
"""
112-
now = time.time()
113-
114-
if ttl <= 0:
115-
# Put the TTL value to -1
116-
for addr in addrs:
117-
# TODO! if addr in self.addrs, remove them?
118-
if addr in self.addrs_ttl:
119-
del self.addrs_ttl[addr]
120-
return
121-
122-
expiry = now + ttl
123-
for addr in addrs:
124-
# TODO! if addr not in self.addrs, add them?
125-
self.addrs_ttl[addr] = expiry
126-
127-
def update_addrs(self, oldTTL: int, newTTL: int) -> None:
128-
"""
129-
:param oldTTL: old ttl
130-
:param newTTL: new ttl
131-
"""
132-
now = time.time()
133-
134-
new_expiry = now + newTTL
135-
old_expiry = now + oldTTL
136-
137-
for addr, expiry in list(self.addrs_ttl.items()):
138-
# Approximate match by expiry time
139-
if abs(expiry - old_expiry) < 1:
140-
self.addrs_ttl[addr] = new_expiry
141104

142105
def get_addrs(self) -> list[Multiaddr]:
143106
"""
@@ -200,6 +163,36 @@ def get_privkey(self) -> PrivateKey:
200163
raise PeerDataError("private key not found")
201164
return self.privkey
202165

166+
def clear_keydata(self) -> None:
167+
"""Clears keydata"""
168+
self.pubkey = None
169+
self.privkey = None
170+
171+
def record_latency(self, new_latency: float) -> None:
172+
"""
173+
Records a new latency measurement for the given peer
174+
using Exponentially Weighted Moving Average (EWMA)
175+
:param new_latency: the new latency value
176+
"""
177+
s = LATENCY_EWMA_SMOOTHING
178+
if s > 1 or s < 0:
179+
s = 0.1
180+
181+
if self.latmap is None:
182+
self.latmap = new_latency
183+
else:
184+
prev = self.latmap
185+
updated = ((1.0 - s) * prev) + (s * new_latency)
186+
self.latmap = updated
187+
188+
def latency_EWMA(self) -> float:
189+
"""Returns the latency EWMA value"""
190+
return self.latmap
191+
192+
def clear_metrics(self) -> None:
193+
"""Clear the latency metrics"""
194+
self.latmap = 0
195+
203196
def update_last_identified(self) -> None:
204197
self.last_identified = int(time.time())
205198

libp2p/peer/peerstore.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,17 @@ def supports_protocols(self, peer_id: ID, protocols: Sequence[str]) -> list[str]
100100
"""
101101
:return: all of the peer IDs stored in peer store
102102
"""
103-
return list(self.peer_data_map.keys())
103+
peer_data = self.peer_data_map[peer_id]
104+
return peer_data.supports_protocols(protocols)
105+
106+
def first_supported_protocol(self, peer_id: ID, protocols: Sequence[str]) -> str:
107+
peer_data = self.peer_data_map[peer_id]
108+
return peer_data.first_supported_protocol(protocols)
109+
110+
def clear_protocol_data(self, peer_id: ID) -> None:
111+
"""Clears prtocoldata"""
112+
peer_data = self.peer_data_map[peer_id]
113+
peer_data.clear_protocol_data()
104114

105115
def valid_peer_ids(self) -> list[ID]:
106116
"""
@@ -138,6 +148,11 @@ def put(self, peer_id: ID, key: str, val: Any) -> None:
138148
peer_data = self.peer_data_map[peer_id]
139149
peer_data.put_metadata(key, val)
140150

151+
def clear_metadata(self, peer_id: ID) -> None:
152+
"""Clears metadata"""
153+
peer_data = self.peer_data_map[peer_id]
154+
peer_data.clear_metadata()
155+
141156
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int = 0) -> None:
142157
"""
143158
:param peer_id: peer ID to add address for

libp2p/tools/async_service/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import (
1919
Any,
2020
TypeVar,
21+
cast,
2122
)
2223
import uuid
2324

@@ -360,7 +361,7 @@ async def _run_and_manage_task(self, task: TaskAPI) -> None:
360361
# Only show stacktrace if this is **not** a DaemonTaskExit error
361362
exc_info=not isinstance(err, DaemonTaskExit),
362363
)
363-
self._errors.append(sys.exc_info())
364+
self._errors.append(cast(EXC_INFO, sys.exc_info()))
364365
self.cancel()
365366
else:
366367
if task.parent is None:

libp2p/tools/async_service/trio_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
LifecycleError,
5353
)
5454
from .typing import (
55+
EXC_INFO,
5556
AsyncFn,
5657
)
5758

@@ -231,7 +232,7 @@ async def run(self) -> None:
231232
# Exceptions from any tasks spawned by our service will be
232233
# caught by trio and raised here, so we store them to report
233234
# together with any others we have already captured.
234-
self._errors.append(sys.exc_info())
235+
self._errors.append(cast(EXC_INFO, sys.exc_info()))
235236
finally:
236237
system_nursery.cancel_scope.cancel()
237238

0 commit comments

Comments
 (0)