Fix wrong peer id type used in KadPeerInfo

This commit is contained in:
NIC619 2019-07-31 21:35:50 +08:00
parent b928bdb356
commit f00e80bc25
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
4 changed files with 9 additions and 8 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
node_id = id_opt.to_bytes()
server = KademliaServer(ksize=ksize, alpha=alpha, node_id=node_id, storage=storage)
return KadmeliaPeerRouter(server)

View File

@ -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

View File

@ -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

View File

@ -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)