py-libp2p/libp2p/peer/peerstore.py

198 lines
6.8 KiB
Python
Raw Normal View History

from collections import defaultdict
2019-11-26 12:27:59 +08:00
from typing import Any, Dict, List, Sequence
2019-07-27 16:27:01 +08:00
from multiaddr import Multiaddr
2019-11-26 11:52:31 +08:00
from libp2p.crypto.keys import KeyPair, PrivateKey, PublicKey
2019-11-24 18:04:00 +08:00
2019-07-27 16:27:01 +08:00
from .id import ID
from .peerdata import PeerData, PeerDataError
from .peerinfo import PeerInfo
2019-07-28 22:30:51 +08:00
from .peerstore_interface import IPeerStore
2019-01-10 02:38:56 +08:00
class PeerStore(IPeerStore):
2019-11-24 16:52:46 +08:00
peer_data_map: Dict[ID, PeerData]
2019-07-27 16:27:01 +08:00
def __init__(self) -> None:
self.peer_data_map = defaultdict(PeerData)
2019-11-26 12:27:59 +08:00
def peer_info(self, peer_id: ID) -> PeerInfo:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to get info for
:return: peer info object
"""
2019-11-24 16:52:46 +08:00
if peer_id in self.peer_data_map:
peer_data = self.peer_data_map[peer_id]
2019-11-26 12:27:59 +08:00
return PeerInfo(peer_id, peer_data.get_addrs())
raise PeerStoreError("peer ID not found")
2019-07-27 16:27:01 +08:00
def get_protocols(self, peer_id: ID) -> List[str]:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to get protocols for
:return: protocols (as list of strings)
:raise PeerStoreError: if peer ID not found
"""
2019-11-24 16:52:46 +08:00
if peer_id in self.peer_data_map:
return self.peer_data_map[peer_id].get_protocols()
raise PeerStoreError("peer ID not found")
2019-07-27 16:27:01 +08:00
def add_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to add protocols for
:param protocols: protocols to add
"""
peer_data = self.peer_data_map[peer_id]
2019-11-26 12:27:59 +08:00
peer_data.add_protocols(list(protocols))
2019-07-27 16:27:01 +08:00
def set_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to set protocols for
:param protocols: protocols to set
"""
peer_data = self.peer_data_map[peer_id]
2019-11-26 12:27:59 +08:00
peer_data.set_protocols(list(protocols))
2019-07-27 16:27:01 +08:00
def peer_ids(self) -> List[ID]:
2019-11-25 17:16:47 +08:00
"""
:return: all of the peer IDs stored in peer store
"""
2019-11-24 16:52:46 +08:00
return list(self.peer_data_map.keys())
2019-07-30 15:31:02 +08:00
def get(self, peer_id: ID, key: str) -> Any:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to get peer data for
:param key: the key to search value for
:return: value corresponding to the key
:raise PeerStoreError: if peer ID or value not found
"""
2019-11-24 16:52:46 +08:00
if peer_id in self.peer_data_map:
try:
2019-11-24 16:52:46 +08:00
val = self.peer_data_map[peer_id].get_metadata(key)
except PeerDataError as error:
raise PeerStoreError() from error
2018-10-29 20:13:39 +08:00
return val
raise PeerStoreError("peer ID not found")
2019-07-30 15:31:02 +08:00
def put(self, peer_id: ID, key: str, val: Any) -> None:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to put peer data for
:param key:
:param value:
"""
peer_data = self.peer_data_map[peer_id]
2019-11-26 12:27:59 +08:00
peer_data.put_metadata(key, val)
2019-07-27 16:27:01 +08:00
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to add address for
:param addr:
:param ttl: time-to-live for the this record
"""
self.add_addrs(peer_id, [addr], ttl)
2019-07-27 16:27:01 +08:00
def add_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to add address for
:param addrs:
:param ttl: time-to-live for the this record
"""
2018-10-22 03:15:53 +08:00
# Ignore ttl for now
peer_data = self.peer_data_map[peer_id]
2019-11-26 12:27:59 +08:00
peer_data.add_addrs(list(addrs))
2019-07-27 16:27:01 +08:00
def addrs(self, peer_id: ID) -> List[Multiaddr]:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to get addrs for
:return: list of addrs
:raise PeerStoreError: if peer ID not found
"""
2019-11-24 16:52:46 +08:00
if peer_id in self.peer_data_map:
return self.peer_data_map[peer_id].get_addrs()
raise PeerStoreError("peer ID not found")
2019-07-27 16:27:01 +08:00
def clear_addrs(self, peer_id: ID) -> None:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to clear addrs for
"""
2018-10-22 03:15:53 +08:00
# Only clear addresses if the peer is in peer map
2019-11-24 16:52:46 +08:00
if peer_id in self.peer_data_map:
self.peer_data_map[peer_id].clear_addrs()
2019-07-27 16:27:01 +08:00
def peers_with_addrs(self) -> List[ID]:
2019-11-25 17:16:47 +08:00
"""
:return: all of the peer IDs which has addrs stored in peer store
"""
2018-10-22 03:15:53 +08:00
# Add all peers with addrs at least 1 to output
2019-07-27 16:27:01 +08:00
output: List[ID] = []
2019-11-24 16:52:46 +08:00
for peer_id in self.peer_data_map:
if len(self.peer_data_map[peer_id].get_addrs()) >= 1:
2019-07-27 16:27:01 +08:00
output.append(peer_id)
2018-10-22 03:15:53 +08:00
return output
2018-10-30 07:03:19 +08:00
2019-11-24 18:04:00 +08:00
def add_pubkey(self, peer_id: ID, pubkey: PublicKey) -> None:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to add public key for
:param pubkey:
:raise PeerStoreError: if peer ID and pubkey does not match
2019-11-25 17:16:47 +08:00
"""
peer_data = self.peer_data_map[peer_id]
if ID.from_pubkey(pubkey) != peer_id:
raise PeerStoreError("peer ID and pubkey does not match")
2019-11-26 12:27:59 +08:00
peer_data.add_pubkey(pubkey)
2019-11-24 18:04:00 +08:00
def pubkey(self, peer_id: ID) -> PublicKey:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to get public key for
:return: public key of the peer
2019-11-26 12:27:59 +08:00
:raise PeerStoreError: if peer ID or peer pubkey not found
2019-11-25 17:16:47 +08:00
"""
2019-11-26 12:27:59 +08:00
if peer_id in self.peer_data_map:
peer_data = self.peer_data_map[peer_id]
try:
pubkey = peer_data.get_pubkey()
except PeerDataError as e:
raise PeerStoreError("peer pubkey not found") from e
2019-11-26 12:27:59 +08:00
return pubkey
2019-11-24 18:04:00 +08:00
raise PeerStoreError("peer ID not found")
def add_privkey(self, peer_id: ID, privkey: PrivateKey) -> None:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to add private key for
:param privkey:
:raise PeerStoreError: if peer ID or peer privkey not found
2019-11-25 17:16:47 +08:00
"""
peer_data = self.peer_data_map[peer_id]
if ID.from_pubkey(privkey.get_public_key()) != peer_id:
raise PeerStoreError("peer ID and privkey does not match")
2019-11-26 12:27:59 +08:00
peer_data.add_privkey(privkey)
2019-11-24 18:04:00 +08:00
def privkey(self, peer_id: ID) -> PrivateKey:
2019-11-25 17:16:47 +08:00
"""
:param peer_id: peer ID to get private key for
:return: private key of the peer
2019-11-26 12:27:59 +08:00
:raise PeerStoreError: if peer ID or peer privkey not found
2019-11-25 17:16:47 +08:00
"""
2019-11-26 12:27:59 +08:00
if peer_id in self.peer_data_map:
peer_data = self.peer_data_map[peer_id]
try:
privkey = peer_data.get_privkey()
except PeerDataError as e:
raise PeerStoreError("peer privkey not found") from e
2019-11-26 12:27:59 +08:00
return privkey
2019-11-24 18:04:00 +08:00
raise PeerStoreError("peer ID not found")
2019-11-26 11:52:31 +08:00
def add_key_pair(self, peer_id: ID, key_pair: KeyPair) -> None:
"""
:param peer_id: peer ID to add private key for
:param key_pair:
2019-11-25 17:16:47 +08:00
"""
2019-11-26 12:35:50 +08:00
self.add_pubkey(peer_id, key_pair.public_key)
self.add_privkey(peer_id, key_pair.private_key)
2019-11-24 18:04:00 +08:00
2019-01-10 02:38:56 +08:00
2018-10-30 07:03:19 +08:00
class PeerStoreError(KeyError):
"""Raised when peer ID is not found in peer store."""