mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
Added tests for 'RoutedHost' and modified 'FindPeer'
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
from libp2p.host.basic_host import BasicHost
|
||||
from libp2p.host.exceptions import ConnectionFailure
|
||||
from libp2p.network.network_interface import INetwork
|
||||
from libp2p.peer.peerinfo import PeerInfo
|
||||
from libp2p.routing.interfaces import IPeerRouting
|
||||
@ -26,7 +27,10 @@ class RoutedHost(BasicHost):
|
||||
"""
|
||||
# check if we were given some addresses, otherwise, find some with the routing system.
|
||||
if not peer_info.addrs:
|
||||
peer_info.addrs = (await self._router.find_peer(peer_info.peer_id)).addrs
|
||||
found_peer_info = await self._router.find_peer(peer_info.peer_id)
|
||||
if not found_peer_info:
|
||||
raise ConnectionFailure("Unable to find Peer address")
|
||||
peer_info.addrs = found_peer_info.addrs
|
||||
self.peerstore.add_addrs(peer_info.peer_id, peer_info.addrs, 10)
|
||||
|
||||
# there is already a connection to this peer
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import json
|
||||
from typing import List, Sequence
|
||||
|
||||
import multiaddr
|
||||
@ -14,6 +15,17 @@ class PeerInfo:
|
||||
self.peer_id = peer_id
|
||||
self.addrs = list(addrs)
|
||||
|
||||
def to_string(self) -> str:
|
||||
return json.dumps([self.peer_id.to_string(), list(map(lambda a: str(a), self.addrs))])
|
||||
|
||||
def __eq__(self, other):
|
||||
return isinstance(other, PeerInfo) and self.peer_id == other.peer_id and self.addrs == other.addrs
|
||||
|
||||
@classmethod
|
||||
def info_from_string(cls, info: str) -> "PeerInfo":
|
||||
peer_id, raw_addrs = json.loads(info)
|
||||
return PeerInfo(ID.from_base58(peer_id), list(map(lambda a: multiaddr.Multiaddr(a), raw_addrs)))
|
||||
|
||||
|
||||
def info_from_p2p_addr(addr: multiaddr.Multiaddr) -> PeerInfo:
|
||||
if not addr:
|
||||
|
||||
@ -1,42 +1,23 @@
|
||||
import ast
|
||||
from typing import Union
|
||||
|
||||
from libp2p.kademlia.kad_peerinfo import KadPeerInfo, create_kad_peerinfo
|
||||
from libp2p.kademlia.network import KademliaServer
|
||||
from libp2p.peer.id import ID
|
||||
from libp2p.peer.peerinfo import PeerInfo
|
||||
from libp2p.routing.interfaces import IPeerRouting
|
||||
|
||||
|
||||
class KadmeliaPeerRouter(IPeerRouting):
|
||||
|
||||
server: KademliaServer
|
||||
|
||||
def __init__(self, dht_server: KademliaServer) -> None:
|
||||
self.server = dht_server
|
||||
|
||||
async def find_peer(self, peer_id: ID) -> KadPeerInfo:
|
||||
async def find_peer(self, peer_id: ID) -> PeerInfo:
|
||||
"""
|
||||
Find a specific peer
|
||||
:param peer_id: peer to search for
|
||||
:return: KadPeerInfo of specified peer
|
||||
:return: PeerInfo of specified peer
|
||||
"""
|
||||
# switching peer_id to xor_id used by kademlia as node_id
|
||||
xor_id = peer_id.xor_id
|
||||
# ignore type for kad
|
||||
value = await self.server.get(xor_id) # type: ignore
|
||||
return decode_peerinfo(value)
|
||||
|
||||
|
||||
def decode_peerinfo(encoded: Union[bytes, str]) -> KadPeerInfo:
|
||||
if isinstance(encoded, bytes):
|
||||
encoded = encoded.decode()
|
||||
try:
|
||||
lines = ast.literal_eval(encoded)
|
||||
except SyntaxError:
|
||||
return None
|
||||
ip = lines[1]
|
||||
port = lines[2]
|
||||
peer_id = lines[3]
|
||||
# ignore typing for kad
|
||||
peer_info = create_kad_peerinfo(peer_id, ip, port) # type: ignore
|
||||
return peer_info
|
||||
return PeerInfo.info_from_string(value) if value else None # TODO: should raise error if None?
|
||||
|
||||
Reference in New Issue
Block a user