File tree Expand file tree Collapse file tree 2 files changed +9
-9
lines changed Expand file tree Collapse file tree 2 files changed +9
-9
lines changed Original file line number Diff line number Diff line change
1
+ import functools
1
2
import hashlib
2
3
3
4
import base58
@@ -36,25 +37,23 @@ def digest(self) -> bytes:
36
37
37
38
class ID :
38
39
_bytes : bytes
39
- _xor_id : int | None = None
40
- _b58_str : str | None = None
41
40
42
41
def __init__ (self , peer_id_bytes : bytes ) -> None :
43
42
self ._bytes = peer_id_bytes
44
43
45
- @property
44
+ @functools . cached_property
46
45
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 ()
50
51
51
52
def to_bytes (self ) -> bytes :
52
53
return self ._bytes
53
54
54
55
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
58
57
59
58
def __repr__ (self ) -> str :
60
59
return f"<libp2p.peer.id.ID ({ self !s} )>"
Original file line number Diff line number Diff line change
1
+ Replace the libp2p.peer.ID cache attributes with functools.cached_property functional decorator.
You can’t perform that action at this time.
0 commit comments