2019-10-16 02:32:25 +08:00
|
|
|
import json
|
|
|
|
|
|
|
|
import multiaddr
|
|
|
|
|
2019-07-27 17:10:03 +08:00
|
|
|
from libp2p.kademlia.network import KademliaServer
|
|
|
|
from libp2p.peer.id import ID
|
2019-10-15 07:01:16 +08:00
|
|
|
from libp2p.peer.peerinfo import PeerInfo
|
2019-03-27 03:44:01 +08:00
|
|
|
from libp2p.routing.interfaces import IPeerRouting
|
|
|
|
|
|
|
|
|
|
|
|
class KadmeliaPeerRouter(IPeerRouting):
|
2019-07-27 17:10:03 +08:00
|
|
|
server: KademliaServer
|
|
|
|
|
|
|
|
def __init__(self, dht_server: KademliaServer) -> None:
|
2019-03-27 03:44:01 +08:00
|
|
|
self.server = dht_server
|
|
|
|
|
2019-10-15 07:01:16 +08:00
|
|
|
async def find_peer(self, peer_id: ID) -> PeerInfo:
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Find a specific peer.
|
|
|
|
|
2019-05-06 02:32:41 +08:00
|
|
|
:param peer_id: peer to search for
|
2019-10-15 07:01:16 +08:00
|
|
|
:return: PeerInfo of specified peer
|
2019-03-27 03:44:01 +08:00
|
|
|
"""
|
2019-04-25 10:11:54 +08:00
|
|
|
# switching peer_id to xor_id used by kademlia as node_id
|
2019-07-31 19:26:13 +08:00
|
|
|
xor_id = peer_id.xor_id
|
2019-08-11 16:47:54 +08:00
|
|
|
# ignore type for kad
|
|
|
|
value = await self.server.get(xor_id) # type: ignore
|
2019-10-16 01:02:03 +08:00
|
|
|
return (
|
2019-10-16 02:32:25 +08:00
|
|
|
peer_info_from_str(value) if value else None
|
2019-10-16 01:02:03 +08:00
|
|
|
) # TODO: should raise error if None?
|
2019-10-16 02:32:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
)
|