py-libp2p/libp2p/peer/peerdata.py

107 lines
2.7 KiB
Python
Raw Normal View History

2019-08-01 06:00:12 +08:00
from typing import Any, Dict, List, Sequence
2019-07-27 16:27:01 +08:00
from multiaddr import Multiaddr
2019-11-26 12:27:59 +08:00
from libp2p.crypto.keys import PrivateKey, PublicKey
2018-10-21 05:11:44 +08:00
from .peerdata_interface import IPeerData
2019-01-10 02:38:56 +08:00
2018-10-21 05:11:44 +08:00
class PeerData(IPeerData):
2019-11-26 12:27:59 +08:00
pubkey: PublicKey
privkey: PrivateKey
2019-07-27 16:27:01 +08:00
metadata: Dict[Any, Any]
protocols: List[str]
addrs: List[Multiaddr]
def __init__(self) -> None:
2018-10-22 03:15:53 +08:00
self.metadata = {}
2018-10-21 05:11:44 +08:00
self.protocols = []
self.addrs = []
2019-07-27 16:27:01 +08:00
def get_protocols(self) -> List[str]:
2019-11-26 12:27:59 +08:00
"""
:return: all protocols associated with given peer
"""
2018-10-22 03:15:53 +08:00
return self.protocols
2018-10-21 05:11:44 +08:00
2019-07-27 16:27:01 +08:00
def add_protocols(self, protocols: Sequence[str]) -> None:
2019-11-26 12:27:59 +08:00
"""
:param protocols: protocols to add
"""
2019-07-27 16:27:01 +08:00
self.protocols.extend(list(protocols))
2018-10-21 05:11:44 +08:00
2019-07-27 16:27:01 +08:00
def set_protocols(self, protocols: Sequence[str]) -> None:
2019-11-26 12:27:59 +08:00
"""
:param protocols: protocols to set
"""
2019-07-27 16:27:01 +08:00
self.protocols = list(protocols)
2019-07-27 16:27:01 +08:00
def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
2019-11-26 12:27:59 +08:00
"""
:param addrs: multiaddresses to add
"""
2019-07-30 23:41:28 +08:00
self.addrs.extend(addrs)
2018-10-21 05:11:44 +08:00
2019-07-27 16:27:01 +08:00
def get_addrs(self) -> List[Multiaddr]:
2019-11-26 12:27:59 +08:00
"""
:return: all multiaddresses
"""
2018-10-22 03:15:53 +08:00
return self.addrs
2018-10-21 05:11:44 +08:00
2019-07-27 16:27:01 +08:00
def clear_addrs(self) -> None:
2019-11-26 12:27:59 +08:00
"""Clear all addresses."""
2018-10-22 03:15:53 +08:00
self.addrs = []
2018-10-21 05:11:44 +08:00
2019-07-30 15:31:02 +08:00
def put_metadata(self, key: str, val: Any) -> None:
2019-11-26 12:27:59 +08:00
"""
:param key: key in KV pair
:param val: val to associate with key
"""
2018-10-22 03:15:53 +08:00
self.metadata[key] = val
2018-10-21 05:11:44 +08:00
2019-07-30 15:31:02 +08:00
def get_metadata(self, key: str) -> Any:
2019-11-26 12:27:59 +08:00
"""
:param key: key in KV pair
:return: val for key
:raise PeerDataError: key not found
"""
2018-10-22 03:15:53 +08:00
if key in self.metadata:
2018-10-30 07:03:19 +08:00
return self.metadata[key]
raise PeerDataError("key not found")
2018-10-30 07:03:19 +08:00
2019-11-26 12:27:59 +08:00
def add_pubkey(self, pubkey: PublicKey) -> None:
"""
:param pubkey:
"""
self.pubkey = pubkey
def get_pubkey(self) -> PublicKey:
"""
:return: public key of the peer
:raise PeerDataError: if public key not found
"""
if self.pubkey is None:
raise PeerDataError("public key not found")
return self.pubkey
def add_privkey(self, privkey: PrivateKey) -> None:
"""
:param privkey:
"""
self.privkey = privkey
def get_privkey(self) -> PrivateKey:
"""
:return: private key of the peer
:raise PeerDataError: if private key not found
"""
if self.privkey is None:
raise PeerDataError("private key not found")
return self.privkey
2019-01-10 02:38:56 +08:00
2018-10-30 07:03:19 +08:00
class PeerDataError(KeyError):
"""Raised when a key is not found in peer metadata."""