Skip to content

Commit 85bad2d

Browse files
LVivonaacul71
authored andcommitted
replace: attributes with cache cached_property
1 parent 62ea3bb commit 85bad2d

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

libp2p/peer/id.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import base58
44
import multihash
55

6+
from functools import (
7+
cached_property,
8+
)
9+
610
from libp2p.crypto.keys import (
711
PublicKey,
812
)
@@ -36,25 +40,23 @@ def digest(self) -> bytes:
3640

3741
class ID:
3842
_bytes: bytes
39-
_xor_id: int | None = None
40-
_b58_str: str | None = None
4143

4244
def __init__(self, peer_id_bytes: bytes) -> None:
4345
self._bytes = peer_id_bytes
4446

45-
@property
47+
@cached_property
4648
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
49+
return int(sha256_digest(self._bytes).hex(), 16)
50+
51+
@cached_property
52+
def base58(self) -> str:
53+
return base58.b58encode(self._bytes).decode()
5054

5155
def to_bytes(self) -> bytes:
5256
return self._bytes
5357

5458
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
59+
return self.base58
5860

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

0 commit comments

Comments
 (0)