mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-11 07:30:55 +00:00
Pull request feedback
This commit is contained in:
@ -30,7 +30,7 @@ class RoutedHost(BasicHost):
|
||||
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, found_peer_info.addrs, 10)
|
||||
self.peerstore.add_addrs(peer_info.peer_id, peer_info.addrs, 10)
|
||||
|
||||
# there is already a connection to this peer
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import json
|
||||
from typing import Any, List, Sequence
|
||||
|
||||
import multiaddr
|
||||
@ -14,11 +13,6 @@ 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: Any) -> bool:
|
||||
return (
|
||||
isinstance(other, PeerInfo)
|
||||
@ -26,14 +20,6 @@ class PeerInfo:
|
||||
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,3 +1,7 @@
|
||||
import json
|
||||
|
||||
import multiaddr
|
||||
|
||||
from libp2p.kademlia.network import KademliaServer
|
||||
from libp2p.peer.id import ID
|
||||
from libp2p.peer.peerinfo import PeerInfo
|
||||
@ -21,5 +25,18 @@ class KadmeliaPeerRouter(IPeerRouting):
|
||||
# ignore type for kad
|
||||
value = await self.server.get(xor_id) # type: ignore
|
||||
return (
|
||||
PeerInfo.info_from_string(value) if value else None
|
||||
peer_info_from_str(value) if value else None
|
||||
) # TODO: should raise error if None?
|
||||
|
||||
|
||||
def peer_info_to_str(peer_info: PeerInfo) -> str:
|
||||
return json.dumps(
|
||||
[peer_info.peer_id.to_string(), list(map(lambda a: str(a), peer_info.addrs))]
|
||||
)
|
||||
|
||||
|
||||
def peer_info_from_str(string: str) -> PeerInfo:
|
||||
peer_id, raw_addrs = json.loads(string)
|
||||
return PeerInfo(
|
||||
ID.from_base58(peer_id), list(map(lambda a: multiaddr.Multiaddr(a), raw_addrs))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user