diff --git a/libp2p/__init__.py b/libp2p/__init__.py index 9510e23..32fa008 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -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 + node_id = id_opt.to_bytes() server = KademliaServer(ksize=ksize, alpha=alpha, node_id=node_id, storage=storage) return KadmeliaPeerRouter(server) diff --git a/libp2p/kademlia/kad_peerinfo.py b/libp2p/kademlia/kad_peerinfo.py index 1468677..f1bffce 100644 --- a/libp2p/kademlia/kad_peerinfo.py +++ b/libp2p/kademlia/kad_peerinfo.py @@ -16,7 +16,7 @@ class KadPeerInfo(PeerInfo): def __init__(self, peer_id, peer_data=None): super(KadPeerInfo, self).__init__(peer_id, peer_data) - self.peer_id = peer_id + self.peer_id = peer_id.to_bytes() self.xor_id = peer_id.xor_id self.addrs = peer_data.get_addrs() if peer_data else None @@ -136,8 +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(node_id=None, sender_ip=None, sender_port=None): - node_id = node_id if node_id else ID(digest(random.getrandbits(255))) +def create_kad_peerinfo(node_id_bytes=None, sender_ip=None, sender_port=None): + node_id = ID(node_id_bytes) if node_id_bytes else ID(digest(random.getrandbits(255))) peer_data = None if sender_ip and sender_port: peer_data = PeerData() # pylint: disable=no-value-for-parameter diff --git a/libp2p/peer/id.py b/libp2p/peer/id.py index 62374d4..4918c2c 100644 --- a/libp2p/peer/id.py +++ b/libp2p/peer/id.py @@ -19,7 +19,7 @@ MAX_INLINE_KEY_LENGTH = 42 class ID: _bytes: bytes - _xor_id: int + _xor_id: int = None def __init__(self, peer_id_bytes: bytes) -> None: self._bytes = peer_id_bytes diff --git a/tests/routing/test_kad_peer_router.py b/tests/routing/test_kad_peer_router.py index 0581544..3e5c19d 100644 --- a/tests/routing/test_kad_peer_router.py +++ b/tests/routing/test_kad_peer_router.py @@ -1,5 +1,6 @@ import pytest +from libp2p.peer.id import ID from libp2p.kademlia.network import KademliaServer from libp2p.routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter @@ -17,7 +18,7 @@ async def test_simple_two_nodes(): await node_a.set(node_a_kad_peerinfo.xor_id, repr(node_a_kad_peerinfo)) router = KadmeliaPeerRouter(node_b) - returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj) + returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id)) print(repr(returned_info)) print(repr(node_a_kad_peerinfo)) assert repr(returned_info) == repr(node_a_kad_peerinfo) @@ -41,7 +42,7 @@ async def test_simple_three_nodes(): await node_a.set(node_a_kad_peerinfo.xor_id, repr(node_a_kad_peerinfo)) router = KadmeliaPeerRouter(node_c) - returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj) + returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id)) assert str(returned_info) == str(node_a_kad_peerinfo) @@ -69,5 +70,5 @@ async def test_simple_four_nodes(): await node_b.set(node_a_kad_peerinfo.xor_id, repr(node_a_kad_peerinfo)) router = KadmeliaPeerRouter(node_d) - returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj) + returned_info = await router.find_peer(ID(node_a_kad_peerinfo.peer_id)) assert str(returned_info) == str(node_a_kad_peerinfo)