Refactor ID to take in type bytes only

This commit is contained in:
NIC619 2019-07-31 19:26:13 +08:00
parent f6e456c96e
commit 80481252ca
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
6 changed files with 56 additions and 58 deletions

View File

@ -47,7 +47,7 @@ def initialize_default_kademlia_router(ksize=20, alpha=3, id_opt=None, storage=N
if not id_opt:
id_opt = generate_id()
node_id = id_opt.get_raw_id()
node_id = id_opt
server = KademliaServer(ksize=ksize, alpha=alpha, node_id=node_id, storage=storage)
return KadmeliaPeerRouter(server)

View File

@ -16,9 +16,8 @@ class KadPeerInfo(PeerInfo):
def __init__(self, peer_id, peer_data=None):
super(KadPeerInfo, self).__init__(peer_id, peer_data)
self.peer_id_obj = peer_id
self.peer_id = peer_id.get_raw_id()
self.xor_id = peer_id.get_xor_id()
self.peer_id = peer_id
self.xor_id = peer_id.xor_id
self.addrs = peer_data.get_addrs() if peer_data else None
@ -137,9 +136,8 @@ class KadPeerHeap:
def get_uncontacted(self):
return [n for n in self if n.peer_id not in self.contacted]
def create_kad_peerinfo(raw_node_id=None, sender_ip=None, sender_port=None):
node_id = ID(raw_node_id) if raw_node_id else ID(digest(random.getrandbits(255)))
def create_kad_peerinfo(node_id=None, sender_ip=None, sender_port=None):
node_id = node_id if node_id else ID(digest(random.getrandbits(255)))
peer_data = None
if sender_ip and sender_port:
peer_data = PeerData() # pylint: disable=no-value-for-parameter

View File

@ -18,52 +18,55 @@ MAX_INLINE_KEY_LENGTH = 42
class ID:
_id_str: bytes
_bytes: bytes
_xor_id: int
def __init__(self, id_str: bytes) -> None:
self._id_str = id_str
def __init__(self, peer_id_bytes: bytes) -> None:
self._bytes = peer_id_bytes
@property
def xor_id(self) -> int:
if not self._xor_id:
self._xor_id = int(digest(self._bytes).hex(), 16)
return self._xor_id
def to_bytes(self) -> bytes:
return self._id_str
def get_raw_id(self) -> bytes:
return self._id_str
return self._bytes
def pretty(self) -> str:
return base58.b58encode(self._id_str).decode()
def get_xor_id(self) -> int:
return int(digest(self.get_raw_id()).hex(), 16)
return base58.b58encode(self._bytes).decode()
def __str__(self) -> str:
pid = self.pretty()
return pid
return self.pretty()
__repr__ = __str__
def __eq__(self, other: object) -> bool:
# pylint: disable=protected-access
if not isinstance(other, ID):
#pylint: disable=protected-access, no-else-return
if isinstance(other, bytes):
return self._bytes == other
elif isinstance(other, ID):
return self._bytes == other._bytes
else:
return NotImplemented
return self._id_str == other._id_str
def __hash__(self) -> int:
return hash(self._id_str)
return hash(self._bytes)
def id_b58_encode(peer_id: ID) -> str:
"""
return a b58-encoded string
"""
# pylint: disable=protected-access
return base58.b58encode(peer_id.get_raw_id()).decode()
#pylint: disable=protected-access
return base58.b58encode(peer_id.to_bytes()).decode()
def id_b58_decode(peer_id_str: str) -> ID:
def id_b58_decode(b58_encoded_peer_id_str: str) -> ID:
"""
return a base58-decoded peer ID
"""
return ID(base58.b58decode(peer_id_str))
return ID(base58.b58decode(b58_encoded_peer_id_str))
def id_from_public_key(key: RsaKey) -> ID:

View File

@ -22,7 +22,7 @@ class KadmeliaPeerRouter(IPeerRouting):
:return: KadPeerInfo of specified peer
"""
# switching peer_id to xor_id used by kademlia as node_id
xor_id = peer_id.get_xor_id()
xor_id = peer_id.xor_id
value = await self.server.get(xor_id)
return decode_peerinfo(value)

View File

@ -15,13 +15,14 @@ from libp2p.peer.id import (
ALPHABETS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def test_init_():
def test_init():
random_id_string = ""
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
peer_id = ID(random_id_string)
# pylint: disable=protected-access
assert peer_id._id_str == random_id_string
peer_id = ID(random_id_string.encode())
#pylint: disable=protected-access
assert peer_id == random_id_string.encode()
def test_no_init_value():
@ -34,9 +35,9 @@ def test_pretty():
random_id_string = ""
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
peer_id = ID(random_id_string)
peer_id = ID(random_id_string.encode())
actual = peer_id.pretty()
expected = base58.b58encode(random_id_string).decode()
expected = base58.b58encode(random_id_string.encode()).decode()
assert actual == expected
@ -45,9 +46,9 @@ def test_str_less_than_10():
random_id_string = ""
for _ in range(5):
random_id_string += random.SystemRandom().choice(ALPHABETS)
pid = base58.b58encode(random_id_string).decode()
expected = pid
actual = ID(random_id_string).__str__()
peer_id = base58.b58encode(random_id_string.encode()).decode()
expected = peer_id
actual = ID(random_id_string.encode()).__str__()
assert actual == expected
@ -56,9 +57,9 @@ def test_str_more_than_10():
random_id_string = ""
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
pid = base58.b58encode(random_id_string).decode()
expected = pid
actual = ID(random_id_string).__str__()
peer_id = base58.b58encode(random_id_string.encode()).decode()
expected = peer_id
actual = ID(random_id_string.encode()).__str__()
assert actual == expected
@ -67,21 +68,17 @@ def test_eq_true():
random_id_string = ""
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
other = ID(random_id_string)
peer_id = ID(random_id_string.encode())
expected = True
actual = ID(random_id_string).__eq__(other)
assert actual == expected
assert peer_id == ID(random_id_string.encode())
assert peer_id.to_bytes() == random_id_string.encode()
def test_eq_false():
other = ID("efgh")
peer_id = ID("efgh")
other = ID("abcd")
expected = False
actual = ID("abcd").__eq__(other)
assert actual == expected
assert peer_id != other
def test_hash():
@ -89,8 +86,8 @@ def test_hash():
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
expected = hash(random_id_string)
actual = ID(random_id_string).__hash__()
expected = hash(random_id_string.encode())
actual = ID(random_id_string.encode()).__hash__()
assert actual == expected
@ -99,8 +96,8 @@ def test_id_b58_encode():
random_id_string = ""
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
expected = base58.b58encode(random_id_string).decode()
actual = id_b58_encode(ID(random_id_string))
expected = base58.b58encode(random_id_string.encode()).decode()
actual = id_b58_encode(ID(random_id_string.encode()))
assert actual == expected
@ -109,8 +106,8 @@ def test_id_b58_decode():
random_id_string = ""
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
expected = ID(base58.b58decode(random_id_string))
actual = id_b58_decode(random_id_string)
expected = ID(base58.b58decode(random_id_string.encode()))
actual = id_b58_decode(random_id_string.encode())
assert actual == expected

View File

@ -19,7 +19,7 @@ def test_init_():
random_id_string = ""
for _ in range(10):
random_id_string += random.SystemRandom().choice(ALPHABETS)
peer_id = ID(random_id_string)
peer_id = ID(random_id_string.encode())
peer_info = PeerInfo(peer_id, peer_data)
assert peer_info.peer_id == peer_id