Skip to content

Commit f587e50

Browse files
authored
Merge branch 'main' into todo/handletimeout
2 parents e132b15 + 3ca27c6 commit f587e50

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

libp2p/peer/id.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import hashlib
23

34
import base58
@@ -36,25 +37,23 @@ def digest(self) -> bytes:
3637

3738
class ID:
3839
_bytes: bytes
39-
_xor_id: int | None = None
40-
_b58_str: str | None = None
4140

4241
def __init__(self, peer_id_bytes: bytes) -> None:
4342
self._bytes = peer_id_bytes
4443

45-
@property
44+
@functools.cached_property
4645
def xor_id(self) -> int:
47-
if not self._xor_id:
48-
self._xor_id = int(sha256_digest(self._bytes).hex(), 16)
49-
return self._xor_id
46+
return int(sha256_digest(self._bytes).hex(), 16)
47+
48+
@functools.cached_property
49+
def base58(self) -> str:
50+
return base58.b58encode(self._bytes).decode()
5051

5152
def to_bytes(self) -> bytes:
5253
return self._bytes
5354

5455
def to_base58(self) -> str:
55-
if not self._b58_str:
56-
self._b58_str = base58.b58encode(self._bytes).decode()
57-
return self._b58_str
56+
return self.base58
5857

5958
def __repr__(self) -> str:
6059
return f"<libp2p.peer.id.ID ({self!s})>"

newsfragments/772.internal.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace the libp2p.peer.ID cache attributes with functools.cached_property functional decorator.

0 commit comments

Comments
 (0)