Fix wrong peer id type used in KadPeerInfo
This commit is contained in:
parent
b928bdb356
commit
f00e80bc25
|
@ -47,7 +47,7 @@ def initialize_default_kademlia_router(ksize=20, alpha=3, id_opt=None, storage=N
|
||||||
if not id_opt:
|
if not id_opt:
|
||||||
id_opt = generate_id()
|
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)
|
server = KademliaServer(ksize=ksize, alpha=alpha, node_id=node_id, storage=storage)
|
||||||
return KadmeliaPeerRouter(server)
|
return KadmeliaPeerRouter(server)
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ class KadPeerInfo(PeerInfo):
|
||||||
def __init__(self, peer_id, peer_data=None):
|
def __init__(self, peer_id, peer_data=None):
|
||||||
super(KadPeerInfo, self).__init__(peer_id, peer_data)
|
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.xor_id = peer_id.xor_id
|
||||||
|
|
||||||
self.addrs = peer_data.get_addrs() if peer_data else None
|
self.addrs = peer_data.get_addrs() if peer_data else None
|
||||||
|
@ -136,8 +136,8 @@ class KadPeerHeap:
|
||||||
def get_uncontacted(self):
|
def get_uncontacted(self):
|
||||||
return [n for n in self if n.peer_id not in self.contacted]
|
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):
|
def create_kad_peerinfo(node_id_bytes=None, sender_ip=None, sender_port=None):
|
||||||
node_id = node_id if node_id else ID(digest(random.getrandbits(255)))
|
node_id = ID(node_id_bytes) if node_id_bytes else ID(digest(random.getrandbits(255)))
|
||||||
peer_data = None
|
peer_data = None
|
||||||
if sender_ip and sender_port:
|
if sender_ip and sender_port:
|
||||||
peer_data = PeerData() # pylint: disable=no-value-for-parameter
|
peer_data = PeerData() # pylint: disable=no-value-for-parameter
|
||||||
|
|
|
@ -19,7 +19,7 @@ MAX_INLINE_KEY_LENGTH = 42
|
||||||
class ID:
|
class ID:
|
||||||
|
|
||||||
_bytes: bytes
|
_bytes: bytes
|
||||||
_xor_id: int
|
_xor_id: int = None
|
||||||
|
|
||||||
def __init__(self, peer_id_bytes: bytes) -> None:
|
def __init__(self, peer_id_bytes: bytes) -> None:
|
||||||
self._bytes = peer_id_bytes
|
self._bytes = peer_id_bytes
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from libp2p.peer.id import ID
|
||||||
from libp2p.kademlia.network import KademliaServer
|
from libp2p.kademlia.network import KademliaServer
|
||||||
from libp2p.routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter
|
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))
|
await node_a.set(node_a_kad_peerinfo.xor_id, repr(node_a_kad_peerinfo))
|
||||||
|
|
||||||
router = KadmeliaPeerRouter(node_b)
|
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(returned_info))
|
||||||
print(repr(node_a_kad_peerinfo))
|
print(repr(node_a_kad_peerinfo))
|
||||||
assert repr(returned_info) == 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))
|
await node_a.set(node_a_kad_peerinfo.xor_id, repr(node_a_kad_peerinfo))
|
||||||
|
|
||||||
router = KadmeliaPeerRouter(node_c)
|
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)
|
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))
|
await node_b.set(node_a_kad_peerinfo.xor_id, repr(node_a_kad_peerinfo))
|
||||||
|
|
||||||
router = KadmeliaPeerRouter(node_d)
|
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)
|
assert str(returned_info) == str(node_a_kad_peerinfo)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user