Merge pull request #360 from NIC619/store_our_keypair_in_peerstore
Store our keypair in peerstore
This commit is contained in:
commit
da10fc8531
|
@ -105,6 +105,9 @@ def initialize_default_swarm(
|
||||||
)
|
)
|
||||||
|
|
||||||
peerstore = peerstore_opt or PeerStore()
|
peerstore = peerstore_opt or PeerStore()
|
||||||
|
# Store our key pair in peerstore
|
||||||
|
peerstore.add_key_pair(id_opt, key_pair)
|
||||||
|
|
||||||
# TODO: Initialize discovery if not presented
|
# TODO: Initialize discovery if not presented
|
||||||
return Swarm(id_opt, peerstore, upgrader, transport)
|
return Swarm(id_opt, peerstore, upgrader, transport)
|
||||||
|
|
||||||
|
@ -151,9 +154,9 @@ async def new_node(
|
||||||
# TODO routing unimplemented
|
# TODO routing unimplemented
|
||||||
host: IHost # If not explicitly typed, MyPy raises error
|
host: IHost # If not explicitly typed, MyPy raises error
|
||||||
if disc_opt:
|
if disc_opt:
|
||||||
host = RoutedHost(key_pair.public_key, swarm_opt, disc_opt)
|
host = RoutedHost(swarm_opt, disc_opt)
|
||||||
else:
|
else:
|
||||||
host = BasicHost(key_pair.public_key, swarm_opt)
|
host = BasicHost(swarm_opt)
|
||||||
|
|
||||||
# Kick off cleanup job
|
# Kick off cleanup job
|
||||||
asyncio.ensure_future(cleanup_done_tasks())
|
asyncio.ensure_future(cleanup_done_tasks())
|
||||||
|
|
|
@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, List, Sequence
|
||||||
|
|
||||||
import multiaddr
|
import multiaddr
|
||||||
|
|
||||||
from libp2p.crypto.keys import PublicKey
|
from libp2p.crypto.keys import PrivateKey, PublicKey
|
||||||
from libp2p.host.defaults import get_default_protocols
|
from libp2p.host.defaults import get_default_protocols
|
||||||
from libp2p.host.exceptions import StreamFailure
|
from libp2p.host.exceptions import StreamFailure
|
||||||
from libp2p.network.network_interface import INetwork
|
from libp2p.network.network_interface import INetwork
|
||||||
|
@ -39,7 +39,6 @@ class BasicHost(IHost):
|
||||||
right after a stream is initialized.
|
right after a stream is initialized.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_public_key: PublicKey
|
|
||||||
_network: INetwork
|
_network: INetwork
|
||||||
peerstore: IPeerStore
|
peerstore: IPeerStore
|
||||||
|
|
||||||
|
@ -48,11 +47,9 @@ class BasicHost(IHost):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
public_key: PublicKey,
|
|
||||||
network: INetwork,
|
network: INetwork,
|
||||||
default_protocols: "OrderedDict[TProtocol, StreamHandlerFn]" = None,
|
default_protocols: "OrderedDict[TProtocol, StreamHandlerFn]" = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
self._public_key = public_key
|
|
||||||
self._network = network
|
self._network = network
|
||||||
self._network.set_stream_handler(self._swarm_stream_handler)
|
self._network.set_stream_handler(self._swarm_stream_handler)
|
||||||
self.peerstore = self._network.peerstore
|
self.peerstore = self._network.peerstore
|
||||||
|
@ -68,7 +65,10 @@ class BasicHost(IHost):
|
||||||
return self._network.get_peer_id()
|
return self._network.get_peer_id()
|
||||||
|
|
||||||
def get_public_key(self) -> PublicKey:
|
def get_public_key(self) -> PublicKey:
|
||||||
return self._public_key
|
return self.peerstore.pubkey(self.get_id())
|
||||||
|
|
||||||
|
def get_private_key(self) -> PrivateKey:
|
||||||
|
return self.peerstore.privkey(self.get_id())
|
||||||
|
|
||||||
def get_network(self) -> INetwork:
|
def get_network(self) -> INetwork:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -3,7 +3,7 @@ from typing import Any, List, Sequence
|
||||||
|
|
||||||
import multiaddr
|
import multiaddr
|
||||||
|
|
||||||
from libp2p.crypto.keys import PublicKey
|
from libp2p.crypto.keys import PrivateKey, PublicKey
|
||||||
from libp2p.network.network_interface import INetwork
|
from libp2p.network.network_interface import INetwork
|
||||||
from libp2p.network.stream.net_stream_interface import INetStream
|
from libp2p.network.stream.net_stream_interface import INetStream
|
||||||
from libp2p.peer.id import ID
|
from libp2p.peer.id import ID
|
||||||
|
@ -24,6 +24,12 @@ class IHost(ABC):
|
||||||
:return: the public key belonging to the peer
|
:return: the public key belonging to the peer
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_private_key(self) -> PrivateKey:
|
||||||
|
"""
|
||||||
|
:return: the private key belonging to the peer
|
||||||
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_network(self) -> INetwork:
|
def get_network(self) -> INetwork:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
from libp2p.crypto.keys import PublicKey
|
|
||||||
from libp2p.host.basic_host import BasicHost
|
from libp2p.host.basic_host import BasicHost
|
||||||
from libp2p.host.exceptions import ConnectionFailure
|
from libp2p.host.exceptions import ConnectionFailure
|
||||||
from libp2p.network.network_interface import INetwork
|
from libp2p.network.network_interface import INetwork
|
||||||
|
@ -11,8 +10,8 @@ from libp2p.routing.interfaces import IPeerRouting
|
||||||
class RoutedHost(BasicHost):
|
class RoutedHost(BasicHost):
|
||||||
_router: IPeerRouting
|
_router: IPeerRouting
|
||||||
|
|
||||||
def __init__(self, public_key: PublicKey, network: INetwork, router: IPeerRouting):
|
def __init__(self, network: INetwork, router: IPeerRouting):
|
||||||
super().__init__(public_key, network)
|
super().__init__(network)
|
||||||
self._router = router
|
self._router = router
|
||||||
|
|
||||||
async def connect(self, peer_info: PeerInfo) -> None:
|
async def connect(self, peer_info: PeerInfo) -> None:
|
||||||
|
|
|
@ -7,9 +7,6 @@ from .id import ID
|
||||||
|
|
||||||
|
|
||||||
class IAddrBook(ABC):
|
class IAddrBook(ABC):
|
||||||
def __init__(self) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
|
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -2,46 +2,107 @@ from typing import Any, Dict, List, Sequence
|
||||||
|
|
||||||
from multiaddr import Multiaddr
|
from multiaddr import Multiaddr
|
||||||
|
|
||||||
|
from libp2p.crypto.keys import PrivateKey, PublicKey
|
||||||
|
|
||||||
from .peerdata_interface import IPeerData
|
from .peerdata_interface import IPeerData
|
||||||
|
|
||||||
|
|
||||||
class PeerData(IPeerData):
|
class PeerData(IPeerData):
|
||||||
|
|
||||||
|
pubkey: PublicKey
|
||||||
|
privkey: PrivateKey
|
||||||
metadata: Dict[Any, Any]
|
metadata: Dict[Any, Any]
|
||||||
protocols: List[str]
|
protocols: List[str]
|
||||||
addrs: List[Multiaddr]
|
addrs: List[Multiaddr]
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
self.pubkey = None
|
||||||
|
self.privkey = None
|
||||||
self.metadata = {}
|
self.metadata = {}
|
||||||
self.protocols = []
|
self.protocols = []
|
||||||
self.addrs = []
|
self.addrs = []
|
||||||
|
|
||||||
def get_protocols(self) -> List[str]:
|
def get_protocols(self) -> List[str]:
|
||||||
|
"""
|
||||||
|
:return: all protocols associated with given peer
|
||||||
|
"""
|
||||||
return self.protocols
|
return self.protocols
|
||||||
|
|
||||||
def add_protocols(self, protocols: Sequence[str]) -> None:
|
def add_protocols(self, protocols: Sequence[str]) -> None:
|
||||||
|
"""
|
||||||
|
:param protocols: protocols to add
|
||||||
|
"""
|
||||||
self.protocols.extend(list(protocols))
|
self.protocols.extend(list(protocols))
|
||||||
|
|
||||||
def set_protocols(self, protocols: Sequence[str]) -> None:
|
def set_protocols(self, protocols: Sequence[str]) -> None:
|
||||||
|
"""
|
||||||
|
:param protocols: protocols to set
|
||||||
|
"""
|
||||||
self.protocols = list(protocols)
|
self.protocols = list(protocols)
|
||||||
|
|
||||||
def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
|
def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
|
||||||
|
"""
|
||||||
|
:param addrs: multiaddresses to add
|
||||||
|
"""
|
||||||
self.addrs.extend(addrs)
|
self.addrs.extend(addrs)
|
||||||
|
|
||||||
def get_addrs(self) -> List[Multiaddr]:
|
def get_addrs(self) -> List[Multiaddr]:
|
||||||
|
"""
|
||||||
|
:return: all multiaddresses
|
||||||
|
"""
|
||||||
return self.addrs
|
return self.addrs
|
||||||
|
|
||||||
def clear_addrs(self) -> None:
|
def clear_addrs(self) -> None:
|
||||||
|
"""Clear all addresses."""
|
||||||
self.addrs = []
|
self.addrs = []
|
||||||
|
|
||||||
def put_metadata(self, key: str, val: Any) -> None:
|
def put_metadata(self, key: str, val: Any) -> None:
|
||||||
|
"""
|
||||||
|
:param key: key in KV pair
|
||||||
|
:param val: val to associate with key
|
||||||
|
"""
|
||||||
self.metadata[key] = val
|
self.metadata[key] = val
|
||||||
|
|
||||||
def get_metadata(self, key: str) -> Any:
|
def get_metadata(self, key: str) -> Any:
|
||||||
|
"""
|
||||||
|
:param key: key in KV pair
|
||||||
|
:return: val for key
|
||||||
|
:raise PeerDataError: key not found
|
||||||
|
"""
|
||||||
if key in self.metadata:
|
if key in self.metadata:
|
||||||
return self.metadata[key]
|
return self.metadata[key]
|
||||||
raise PeerDataError("key not found")
|
raise PeerDataError("key not found")
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
class PeerDataError(KeyError):
|
class PeerDataError(KeyError):
|
||||||
"""Raised when a key is not found in peer metadata."""
|
"""Raised when a key is not found in peer metadata."""
|
||||||
|
|
|
@ -3,6 +3,8 @@ from typing import Any, List, Sequence
|
||||||
|
|
||||||
from multiaddr import Multiaddr
|
from multiaddr import Multiaddr
|
||||||
|
|
||||||
|
from libp2p.crypto.keys import PrivateKey, PublicKey
|
||||||
|
|
||||||
from .peermetadata_interface import IPeerMetadata
|
from .peermetadata_interface import IPeerMetadata
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,7 +24,7 @@ class IPeerData(ABC):
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def set_protocols(self, protocols: Sequence[str]) -> None:
|
def set_protocols(self, protocols: Sequence[str]) -> None:
|
||||||
"""
|
"""
|
||||||
:param protocols: protocols to add
|
:param protocols: protocols to set
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@ -46,7 +48,6 @@ class IPeerData(ABC):
|
||||||
"""
|
"""
|
||||||
:param key: key in KV pair
|
:param key: key in KV pair
|
||||||
:param val: val to associate with key
|
:param val: val to associate with key
|
||||||
:raise Exception: unsuccesful put
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@ -54,5 +55,31 @@ class IPeerData(ABC):
|
||||||
"""
|
"""
|
||||||
:param key: key in KV pair
|
:param key: key in KV pair
|
||||||
:return: val for key
|
:return: val for key
|
||||||
:raise Exception: key not found
|
:raise PeerDataError: key not found
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def add_pubkey(self, pubkey: PublicKey) -> None:
|
||||||
|
"""
|
||||||
|
:param pubkey:
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_pubkey(self) -> PublicKey:
|
||||||
|
"""
|
||||||
|
:return: public key of the peer
|
||||||
|
:raise PeerDataError: if public key not found
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def add_privkey(self, privkey: PrivateKey) -> None:
|
||||||
|
"""
|
||||||
|
:param privkey:
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_privkey(self) -> PrivateKey:
|
||||||
|
"""
|
||||||
|
:return: private key of the peer
|
||||||
|
:raise PeerDataError: if private key not found
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -5,9 +5,6 @@ from .id import ID
|
||||||
|
|
||||||
|
|
||||||
class IPeerMetadata(ABC):
|
class IPeerMetadata(ABC):
|
||||||
def __init__(self) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get(self, peer_id: ID, key: str) -> Any:
|
def get(self, peer_id: ID, key: str) -> Any:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
from typing import Any, Dict, List, Optional, Sequence
|
from collections import defaultdict
|
||||||
|
from typing import Any, Dict, List, Sequence
|
||||||
|
|
||||||
from multiaddr import Multiaddr
|
from multiaddr import Multiaddr
|
||||||
|
|
||||||
|
from libp2p.crypto.keys import KeyPair, PrivateKey, PublicKey
|
||||||
|
|
||||||
from .id import ID
|
from .id import ID
|
||||||
from .peerdata import PeerData, PeerDataError
|
from .peerdata import PeerData, PeerDataError
|
||||||
from .peerinfo import PeerInfo
|
from .peerinfo import PeerInfo
|
||||||
|
@ -10,90 +13,185 @@ from .peerstore_interface import IPeerStore
|
||||||
|
|
||||||
class PeerStore(IPeerStore):
|
class PeerStore(IPeerStore):
|
||||||
|
|
||||||
peer_map: Dict[ID, PeerData]
|
peer_data_map: Dict[ID, PeerData]
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
IPeerStore.__init__(self)
|
self.peer_data_map = defaultdict(PeerData)
|
||||||
self.peer_map = {}
|
|
||||||
|
|
||||||
def __create_or_get_peer(self, peer_id: ID) -> PeerData:
|
def peer_info(self, peer_id: ID) -> PeerInfo:
|
||||||
"""
|
"""
|
||||||
Returns the peer data for peer_id or creates a new peer data (and
|
:param peer_id: peer ID to get info for
|
||||||
stores it in peer_map) if peer data for peer_id does not yet exist.
|
:return: peer info object
|
||||||
|
|
||||||
:param peer_id: peer ID
|
|
||||||
:return: peer data
|
|
||||||
"""
|
"""
|
||||||
if peer_id in self.peer_map:
|
if peer_id in self.peer_data_map:
|
||||||
return self.peer_map[peer_id]
|
peer_data = self.peer_data_map[peer_id]
|
||||||
data = PeerData()
|
return PeerInfo(peer_id, peer_data.get_addrs())
|
||||||
self.peer_map[peer_id] = data
|
raise PeerStoreError("peer ID not found")
|
||||||
return self.peer_map[peer_id]
|
|
||||||
|
|
||||||
def peer_info(self, peer_id: ID) -> Optional[PeerInfo]:
|
|
||||||
if peer_id in self.peer_map:
|
|
||||||
peer_data = self.peer_map[peer_id]
|
|
||||||
return PeerInfo(peer_id, peer_data.addrs)
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_protocols(self, peer_id: ID) -> List[str]:
|
def get_protocols(self, peer_id: ID) -> List[str]:
|
||||||
if peer_id in self.peer_map:
|
"""
|
||||||
return self.peer_map[peer_id].get_protocols()
|
:param peer_id: peer ID to get protocols for
|
||||||
|
:return: protocols (as list of strings)
|
||||||
|
:raise PeerStoreError: if peer ID not found
|
||||||
|
"""
|
||||||
|
if peer_id in self.peer_data_map:
|
||||||
|
return self.peer_data_map[peer_id].get_protocols()
|
||||||
raise PeerStoreError("peer ID not found")
|
raise PeerStoreError("peer ID not found")
|
||||||
|
|
||||||
def add_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None:
|
def add_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None:
|
||||||
peer = self.__create_or_get_peer(peer_id)
|
"""
|
||||||
peer.add_protocols(list(protocols))
|
:param peer_id: peer ID to add protocols for
|
||||||
|
:param protocols: protocols to add
|
||||||
|
"""
|
||||||
|
peer_data = self.peer_data_map[peer_id]
|
||||||
|
peer_data.add_protocols(list(protocols))
|
||||||
|
|
||||||
def set_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None:
|
def set_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None:
|
||||||
peer = self.__create_or_get_peer(peer_id)
|
"""
|
||||||
peer.set_protocols(list(protocols))
|
:param peer_id: peer ID to set protocols for
|
||||||
|
:param protocols: protocols to set
|
||||||
|
"""
|
||||||
|
peer_data = self.peer_data_map[peer_id]
|
||||||
|
peer_data.set_protocols(list(protocols))
|
||||||
|
|
||||||
def peer_ids(self) -> List[ID]:
|
def peer_ids(self) -> List[ID]:
|
||||||
return list(self.peer_map.keys())
|
"""
|
||||||
|
:return: all of the peer IDs stored in peer store
|
||||||
|
"""
|
||||||
|
return list(self.peer_data_map.keys())
|
||||||
|
|
||||||
def get(self, peer_id: ID, key: str) -> Any:
|
def get(self, peer_id: ID, key: str) -> Any:
|
||||||
if peer_id in self.peer_map:
|
"""
|
||||||
|
: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
|
||||||
|
"""
|
||||||
|
if peer_id in self.peer_data_map:
|
||||||
try:
|
try:
|
||||||
val = self.peer_map[peer_id].get_metadata(key)
|
val = self.peer_data_map[peer_id].get_metadata(key)
|
||||||
except PeerDataError as error:
|
except PeerDataError as error:
|
||||||
raise PeerStoreError(error)
|
raise PeerStoreError(error)
|
||||||
return val
|
return val
|
||||||
raise PeerStoreError("peer ID not found")
|
raise PeerStoreError("peer ID not found")
|
||||||
|
|
||||||
def put(self, peer_id: ID, key: str, val: Any) -> None:
|
def put(self, peer_id: ID, key: str, val: Any) -> None:
|
||||||
# <<?>>
|
"""
|
||||||
# This can output an error, not sure what the possible errors are
|
:param peer_id: peer ID to put peer data for
|
||||||
peer = self.__create_or_get_peer(peer_id)
|
:param key:
|
||||||
peer.put_metadata(key, val)
|
:param value:
|
||||||
|
"""
|
||||||
|
peer_data = self.peer_data_map[peer_id]
|
||||||
|
peer_data.put_metadata(key, val)
|
||||||
|
|
||||||
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
|
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
|
||||||
|
"""
|
||||||
|
: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)
|
self.add_addrs(peer_id, [addr], ttl)
|
||||||
|
|
||||||
def add_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
|
def add_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to add address for
|
||||||
|
:param addrs:
|
||||||
|
:param ttl: time-to-live for the this record
|
||||||
|
"""
|
||||||
# Ignore ttl for now
|
# Ignore ttl for now
|
||||||
peer = self.__create_or_get_peer(peer_id)
|
peer_data = self.peer_data_map[peer_id]
|
||||||
peer.add_addrs(list(addrs))
|
peer_data.add_addrs(list(addrs))
|
||||||
|
|
||||||
def addrs(self, peer_id: ID) -> List[Multiaddr]:
|
def addrs(self, peer_id: ID) -> List[Multiaddr]:
|
||||||
if peer_id in self.peer_map:
|
"""
|
||||||
return self.peer_map[peer_id].get_addrs()
|
:param peer_id: peer ID to get addrs for
|
||||||
|
:return: list of addrs
|
||||||
|
:raise PeerStoreError: if peer ID not found
|
||||||
|
"""
|
||||||
|
if peer_id in self.peer_data_map:
|
||||||
|
return self.peer_data_map[peer_id].get_addrs()
|
||||||
raise PeerStoreError("peer ID not found")
|
raise PeerStoreError("peer ID not found")
|
||||||
|
|
||||||
def clear_addrs(self, peer_id: ID) -> None:
|
def clear_addrs(self, peer_id: ID) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to clear addrs for
|
||||||
|
"""
|
||||||
# Only clear addresses if the peer is in peer map
|
# Only clear addresses if the peer is in peer map
|
||||||
if peer_id in self.peer_map:
|
if peer_id in self.peer_data_map:
|
||||||
self.peer_map[peer_id].clear_addrs()
|
self.peer_data_map[peer_id].clear_addrs()
|
||||||
|
|
||||||
def peers_with_addrs(self) -> List[ID]:
|
def peers_with_addrs(self) -> List[ID]:
|
||||||
|
"""
|
||||||
|
:return: all of the peer IDs which has addrs stored in peer store
|
||||||
|
"""
|
||||||
# Add all peers with addrs at least 1 to output
|
# Add all peers with addrs at least 1 to output
|
||||||
output: List[ID] = []
|
output: List[ID] = []
|
||||||
|
|
||||||
for peer_id in self.peer_map:
|
for peer_id in self.peer_data_map:
|
||||||
if len(self.peer_map[peer_id].get_addrs()) >= 1:
|
if len(self.peer_data_map[peer_id].get_addrs()) >= 1:
|
||||||
output.append(peer_id)
|
output.append(peer_id)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
def add_pubkey(self, peer_id: ID, pubkey: PublicKey) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to add public key for
|
||||||
|
:param pubkey:
|
||||||
|
:raise PeerStoreError: if peer ID and pubkey does not match
|
||||||
|
"""
|
||||||
|
peer_data = self.peer_data_map[peer_id]
|
||||||
|
if ID.from_pubkey(pubkey) != peer_id:
|
||||||
|
raise PeerStoreError("peer ID and pubkey does not match")
|
||||||
|
peer_data.add_pubkey(pubkey)
|
||||||
|
|
||||||
|
def pubkey(self, peer_id: ID) -> PublicKey:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to get public key for
|
||||||
|
:return: public key of the peer
|
||||||
|
:raise PeerStoreError: if peer ID or peer pubkey not found
|
||||||
|
"""
|
||||||
|
if peer_id in self.peer_data_map:
|
||||||
|
peer_data = self.peer_data_map[peer_id]
|
||||||
|
try:
|
||||||
|
pubkey = peer_data.get_pubkey()
|
||||||
|
except PeerDataError:
|
||||||
|
raise PeerStoreError("peer pubkey not found")
|
||||||
|
return pubkey
|
||||||
|
raise PeerStoreError("peer ID not found")
|
||||||
|
|
||||||
|
def add_privkey(self, peer_id: ID, privkey: PrivateKey) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to add private key for
|
||||||
|
:param privkey:
|
||||||
|
:raise PeerStoreError: if peer ID or peer privkey not found
|
||||||
|
"""
|
||||||
|
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")
|
||||||
|
peer_data.add_privkey(privkey)
|
||||||
|
|
||||||
|
def privkey(self, peer_id: ID) -> PrivateKey:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to get private key for
|
||||||
|
:return: private key of the peer
|
||||||
|
:raise PeerStoreError: if peer ID or peer privkey not found
|
||||||
|
"""
|
||||||
|
if peer_id in self.peer_data_map:
|
||||||
|
peer_data = self.peer_data_map[peer_id]
|
||||||
|
try:
|
||||||
|
privkey = peer_data.get_privkey()
|
||||||
|
except PeerDataError:
|
||||||
|
raise PeerStoreError("peer privkey not found")
|
||||||
|
return privkey
|
||||||
|
raise PeerStoreError("peer ID not found")
|
||||||
|
|
||||||
|
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:
|
||||||
|
"""
|
||||||
|
self.add_pubkey(peer_id, key_pair.public_key)
|
||||||
|
self.add_privkey(peer_id, key_pair.private_key)
|
||||||
|
|
||||||
|
|
||||||
class PeerStoreError(KeyError):
|
class PeerStoreError(KeyError):
|
||||||
"""Raised when peer ID is not found in peer store."""
|
"""Raised when peer ID is not found in peer store."""
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from typing import List, Sequence
|
from typing import Any, List, Sequence
|
||||||
|
|
||||||
|
from multiaddr import Multiaddr
|
||||||
|
|
||||||
|
from libp2p.crypto.keys import KeyPair, PrivateKey, PublicKey
|
||||||
|
|
||||||
from .addrbook_interface import IAddrBook
|
from .addrbook_interface import IAddrBook
|
||||||
from .id import ID
|
from .id import ID
|
||||||
|
@ -8,10 +12,6 @@ from .peermetadata_interface import IPeerMetadata
|
||||||
|
|
||||||
|
|
||||||
class IPeerStore(IAddrBook, IPeerMetadata):
|
class IPeerStore(IAddrBook, IPeerMetadata):
|
||||||
def __init__(self) -> None:
|
|
||||||
IPeerMetadata.__init__(self)
|
|
||||||
IAddrBook.__init__(self)
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def peer_info(self, peer_id: ID) -> PeerInfo:
|
def peer_info(self, peer_id: ID) -> PeerInfo:
|
||||||
"""
|
"""
|
||||||
|
@ -23,8 +23,8 @@ class IPeerStore(IAddrBook, IPeerMetadata):
|
||||||
def get_protocols(self, peer_id: ID) -> List[str]:
|
def get_protocols(self, peer_id: ID) -> List[str]:
|
||||||
"""
|
"""
|
||||||
:param peer_id: peer ID to get protocols for
|
:param peer_id: peer ID to get protocols for
|
||||||
:return: protocols (as strings)
|
:return: protocols (as list of strings)
|
||||||
:raise Exception: peer ID not found exception
|
:raise PeerStoreError: if peer ID not found
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@ -32,7 +32,6 @@ class IPeerStore(IAddrBook, IPeerMetadata):
|
||||||
"""
|
"""
|
||||||
:param peer_id: peer ID to add protocols for
|
:param peer_id: peer ID to add protocols for
|
||||||
:param protocols: protocols to add
|
:param protocols: protocols to add
|
||||||
:raise Exception: peer ID not found
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@ -40,7 +39,6 @@ class IPeerStore(IAddrBook, IPeerMetadata):
|
||||||
"""
|
"""
|
||||||
:param peer_id: peer ID to set protocols for
|
:param peer_id: peer ID to set protocols for
|
||||||
:param protocols: protocols to set
|
:param protocols: protocols to set
|
||||||
:raise Exception: peer ID not found
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
@ -48,3 +46,95 @@ class IPeerStore(IAddrBook, IPeerMetadata):
|
||||||
"""
|
"""
|
||||||
:return: all of the peer IDs stored in peer store
|
:return: all of the peer IDs stored in peer store
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get(self, peer_id: ID, key: str) -> Any:
|
||||||
|
"""
|
||||||
|
: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
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def put(self, peer_id: ID, key: str, val: Any) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to put peer data for
|
||||||
|
:param key:
|
||||||
|
:param value:
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to add address for
|
||||||
|
:param addr:
|
||||||
|
:param ttl: time-to-live for the this record
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def add_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to add address for
|
||||||
|
:param addrs:
|
||||||
|
:param ttl: time-to-live for the this record
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def addrs(self, peer_id: ID) -> List[Multiaddr]:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to get addrs for
|
||||||
|
:return: list of addrs
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def clear_addrs(self, peer_id: ID) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to clear addrs for
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def peers_with_addrs(self) -> List[ID]:
|
||||||
|
"""
|
||||||
|
:return: all of the peer IDs which has addrs stored in peer store
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def add_pubkey(self, peer_id: ID, pubkey: PublicKey) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to add public key for
|
||||||
|
:param pubkey:
|
||||||
|
:raise PeerStoreError: if peer ID already has pubkey set
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def pubkey(self, peer_id: ID) -> PublicKey:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to get public key for
|
||||||
|
:return: public key of the peer
|
||||||
|
:raise PeerStoreError: if peer ID not found
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def add_privkey(self, peer_id: ID, privkey: PrivateKey) -> None:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to add private key for
|
||||||
|
:param privkey:
|
||||||
|
:raise PeerStoreError: if peer ID already has privkey set
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def privkey(self, peer_id: ID) -> PrivateKey:
|
||||||
|
"""
|
||||||
|
:param peer_id: peer ID to get private key for
|
||||||
|
:return: private key of the peer
|
||||||
|
:raise PeerStoreError: if peer ID not found
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
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:
|
||||||
|
:raise PeerStoreError: if peer ID already has pubkey or privkey set
|
||||||
|
"""
|
||||||
|
|
|
@ -10,6 +10,7 @@ from libp2p.host.basic_host import BasicHost
|
||||||
from libp2p.network.connection.swarm_connection import SwarmConn
|
from libp2p.network.connection.swarm_connection import SwarmConn
|
||||||
from libp2p.network.stream.net_stream_interface import INetStream
|
from libp2p.network.stream.net_stream_interface import INetStream
|
||||||
from libp2p.network.swarm import Swarm
|
from libp2p.network.swarm import Swarm
|
||||||
|
from libp2p.peer.id import ID
|
||||||
from libp2p.peer.peerstore import PeerStore
|
from libp2p.peer.peerstore import PeerStore
|
||||||
from libp2p.pubsub.floodsub import FloodSub
|
from libp2p.pubsub.floodsub import FloodSub
|
||||||
from libp2p.pubsub.gossipsub import GossipSub
|
from libp2p.pubsub.gossipsub import GossipSub
|
||||||
|
@ -33,6 +34,12 @@ from .constants import (
|
||||||
from .utils import connect, connect_swarm
|
from .utils import connect, connect_swarm
|
||||||
|
|
||||||
|
|
||||||
|
def initialize_peerstore_with_our_keypair(self_id: ID, key_pair: KeyPair) -> PeerStore:
|
||||||
|
peer_store = PeerStore()
|
||||||
|
peer_store.add_key_pair(self_id, key_pair)
|
||||||
|
return peer_store
|
||||||
|
|
||||||
|
|
||||||
def security_transport_factory(
|
def security_transport_factory(
|
||||||
is_secure: bool, key_pair: KeyPair
|
is_secure: bool, key_pair: KeyPair
|
||||||
) -> Dict[TProtocol, BaseSecureTransport]:
|
) -> Dict[TProtocol, BaseSecureTransport]:
|
||||||
|
@ -52,7 +59,9 @@ class SwarmFactory(factory.Factory):
|
||||||
muxer_opt = {MPLEX_PROTOCOL_ID: Mplex}
|
muxer_opt = {MPLEX_PROTOCOL_ID: Mplex}
|
||||||
|
|
||||||
peer_id = factory.LazyAttribute(lambda o: generate_peer_id_from(o.key_pair))
|
peer_id = factory.LazyAttribute(lambda o: generate_peer_id_from(o.key_pair))
|
||||||
peerstore = factory.LazyFunction(PeerStore)
|
peerstore = factory.LazyAttribute(
|
||||||
|
lambda o: initialize_peerstore_with_our_keypair(o.peer_id, o.key_pair)
|
||||||
|
)
|
||||||
upgrader = factory.LazyAttribute(
|
upgrader = factory.LazyAttribute(
|
||||||
lambda o: TransportUpgrader(
|
lambda o: TransportUpgrader(
|
||||||
security_transport_factory(o.is_secure, o.key_pair), o.muxer_opt
|
security_transport_factory(o.is_secure, o.key_pair), o.muxer_opt
|
||||||
|
@ -97,7 +106,6 @@ class HostFactory(factory.Factory):
|
||||||
is_secure = False
|
is_secure = False
|
||||||
key_pair = factory.LazyFunction(generate_new_rsa_identity)
|
key_pair = factory.LazyFunction(generate_new_rsa_identity)
|
||||||
|
|
||||||
public_key = factory.LazyAttribute(lambda o: o.key_pair.public_key)
|
|
||||||
network = factory.LazyAttribute(
|
network = factory.LazyAttribute(
|
||||||
lambda o: SwarmFactory(is_secure=o.is_secure, key_pair=o.key_pair)
|
lambda o: SwarmFactory(is_secure=o.is_secure, key_pair=o.key_pair)
|
||||||
)
|
)
|
||||||
|
@ -113,10 +121,7 @@ class HostFactory(factory.Factory):
|
||||||
for key_pair in key_pairs
|
for key_pair in key_pairs
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
return tuple(
|
return tuple(BasicHost(swarm) for swarm in swarms)
|
||||||
BasicHost(key_pair.public_key, swarm)
|
|
||||||
for key_pair, swarm in zip(key_pairs, swarms)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class FloodsubFactory(factory.Factory):
|
class FloodsubFactory(factory.Factory):
|
||||||
|
|
|
@ -7,7 +7,7 @@ from libp2p.host.defaults import get_default_protocols
|
||||||
def test_default_protocols():
|
def test_default_protocols():
|
||||||
key_pair = create_new_key_pair()
|
key_pair = create_new_key_pair()
|
||||||
swarm = initialize_default_swarm(key_pair)
|
swarm = initialize_default_swarm(key_pair)
|
||||||
host = BasicHost(key_pair.public_key, swarm)
|
host = BasicHost(swarm)
|
||||||
|
|
||||||
mux = host.get_mux()
|
mux = host.get_mux()
|
||||||
handlers = mux.handlers
|
handlers = mux.handlers
|
||||||
|
|
|
@ -328,18 +328,19 @@ async def test_host_connect():
|
||||||
transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
|
transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
|
||||||
(node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
|
(node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
|
||||||
|
|
||||||
assert not node_a.get_peerstore().peer_ids()
|
# Only our peer ID is stored in peer store
|
||||||
|
assert len(node_a.get_peerstore().peer_ids()) == 1
|
||||||
|
|
||||||
addr = node_b.get_addrs()[0]
|
addr = node_b.get_addrs()[0]
|
||||||
info = info_from_p2p_addr(addr)
|
info = info_from_p2p_addr(addr)
|
||||||
await node_a.connect(info)
|
await node_a.connect(info)
|
||||||
|
|
||||||
assert len(node_a.get_peerstore().peer_ids()) == 1
|
assert len(node_a.get_peerstore().peer_ids()) == 2
|
||||||
|
|
||||||
await node_a.connect(info)
|
await node_a.connect(info)
|
||||||
|
|
||||||
# make sure we don't do double connection
|
# make sure we don't do double connection
|
||||||
assert len(node_a.get_peerstore().peer_ids()) == 1
|
assert len(node_a.get_peerstore().peer_ids()) == 2
|
||||||
|
|
||||||
assert node_b.get_id() in node_a.get_peerstore().peer_ids()
|
assert node_b.get_id() in node_a.get_peerstore().peer_ids()
|
||||||
ma_node_b = multiaddr.Multiaddr("/p2p/%s" % node_b.get_id().pretty())
|
ma_node_b = multiaddr.Multiaddr("/p2p/%s" % node_b.get_id().pretty())
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
from libp2p.peer.peerstore import PeerStore
|
import pytest
|
||||||
|
|
||||||
|
from libp2p.peer.peerstore import PeerStore, PeerStoreError
|
||||||
|
|
||||||
# Testing methods from IPeerStore base class.
|
# Testing methods from IPeerStore base class.
|
||||||
|
|
||||||
|
|
||||||
def test_peer_info_empty():
|
def test_peer_info_empty():
|
||||||
store = PeerStore()
|
store = PeerStore()
|
||||||
info = store.peer_info("peer")
|
with pytest.raises(PeerStoreError):
|
||||||
|
store.peer_info("peer")
|
||||||
assert not info
|
|
||||||
|
|
||||||
|
|
||||||
def test_peer_info_basic():
|
def test_peer_info_basic():
|
||||||
|
|
Loading…
Reference in New Issue
Block a user