From 6aeb217349b1c196f36690c34b4600f0794d180e Mon Sep 17 00:00:00 2001 From: Luca Vivona Date: Tue, 15 Jul 2025 14:59:34 -0400 Subject: [PATCH 1/4] replace: attributes with cache cached_property --- libp2p/peer/id.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/libp2p/peer/id.py b/libp2p/peer/id.py index 0be51ea2..61e399cd 100644 --- a/libp2p/peer/id.py +++ b/libp2p/peer/id.py @@ -3,6 +3,10 @@ import hashlib import base58 import multihash +from functools import ( + cached_property, +) + from libp2p.crypto.keys import ( PublicKey, ) @@ -36,25 +40,23 @@ if ENABLE_INLINING: class ID: _bytes: bytes - _xor_id: int | None = None - _b58_str: str | None = None def __init__(self, peer_id_bytes: bytes) -> None: self._bytes = peer_id_bytes - @property + @cached_property def xor_id(self) -> int: - if not self._xor_id: - self._xor_id = int(sha256_digest(self._bytes).hex(), 16) - return self._xor_id + return int(sha256_digest(self._bytes).hex(), 16) + + @cached_property + def base58(self) -> str: + return base58.b58encode(self._bytes).decode() def to_bytes(self) -> bytes: return self._bytes def to_base58(self) -> str: - if not self._b58_str: - self._b58_str = base58.b58encode(self._bytes).decode() - return self._b58_str + return self.base58 def __repr__(self) -> str: return f"" From 23622ea1a088a39f3ba1fe5539eeb59afd205f5d Mon Sep 17 00:00:00 2001 From: Luca Vivona Date: Tue, 15 Jul 2025 15:28:03 -0400 Subject: [PATCH 2/4] style: enforce consistent import block --- libp2p/peer/id.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/libp2p/peer/id.py b/libp2p/peer/id.py index 61e399cd..28a9d75a 100644 --- a/libp2p/peer/id.py +++ b/libp2p/peer/id.py @@ -1,12 +1,9 @@ +import functools import hashlib import base58 import multihash -from functools import ( - cached_property, -) - from libp2p.crypto.keys import ( PublicKey, ) @@ -44,11 +41,11 @@ class ID: def __init__(self, peer_id_bytes: bytes) -> None: self._bytes = peer_id_bytes - @cached_property + @functools.cached_property def xor_id(self) -> int: return int(sha256_digest(self._bytes).hex(), 16) - @cached_property + @functools.cached_property def base58(self) -> str: return base58.b58encode(self._bytes).decode() From 9f40d97a056d1d493120be1afd204ec6b5f95615 Mon Sep 17 00:00:00 2001 From: Luca Vivona Date: Wed, 16 Jul 2025 22:08:25 -0400 Subject: [PATCH 3/4] chore(newsfragment): add entry to the release notes --- newsfragments/772.internal.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/772.internal.rst diff --git a/newsfragments/772.internal.rst b/newsfragments/772.internal.rst new file mode 100644 index 00000000..7079d6c4 --- /dev/null +++ b/newsfragments/772.internal.rst @@ -0,0 +1 @@ +Replace the libp2p.peer.ID cache attributes with functools.cached_property functional decorator. \ No newline at end of file From ae82895d86fd0992824fd93ea00fc4d8026587aa Mon Sep 17 00:00:00 2001 From: Luca Vivona Date: Wed, 16 Jul 2025 22:12:05 -0400 Subject: [PATCH 4/4] style: add new line within newsfragment --- newsfragments/772.internal.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/newsfragments/772.internal.rst b/newsfragments/772.internal.rst index 7079d6c4..2c84641c 100644 --- a/newsfragments/772.internal.rst +++ b/newsfragments/772.internal.rst @@ -1 +1 @@ -Replace the libp2p.peer.ID cache attributes with functools.cached_property functional decorator. \ No newline at end of file +Replace the libp2p.peer.ID cache attributes with functools.cached_property functional decorator.