mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
refactored and moved all interfaces to abc.py (#504)
* refactored : host_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored : network_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored : notifee_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored : net_connection_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored: raw_connection_interface, secure_conn_interface and stream_muxer abc.py * refactored: addrbook_interface * refactored :peerdata_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :peermetadata_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :multiselect_client_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :multiselect_communicator_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :multiselect_muxer_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :interfaces Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :security_transport_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * refactored :listener_interface Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * moved all interfaces and typing files Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> * fixed documentation and moved pubsub abc.py Co-authored-by: Khwahish Patel <khwahish.p1@ahduni.edu.in> * added exclude-members in custom_types docs * added : newsfragment for moving all interfaces to libp2p.abc --------- Co-authored-by: mystical-prog <jdgt.vd.0405@gmail.com> Co-authored-by: Mystical <125946525+mystical-prog@users.noreply.github.com>
This commit is contained in:
@ -1,61 +0,0 @@
|
||||
from abc import (
|
||||
ABC,
|
||||
abstractmethod,
|
||||
)
|
||||
from collections.abc import (
|
||||
Sequence,
|
||||
)
|
||||
|
||||
from multiaddr import (
|
||||
Multiaddr,
|
||||
)
|
||||
|
||||
from .id import (
|
||||
ID,
|
||||
)
|
||||
|
||||
|
||||
class IAddrBook(ABC):
|
||||
@abstractmethod
|
||||
def add_addr(self, peer_id: ID, addr: Multiaddr, ttl: int) -> None:
|
||||
"""
|
||||
Calls add_addrs(peer_id, [addr], ttl)
|
||||
|
||||
:param peer_id: the peer to add address for
|
||||
:param addr: multiaddress of the peer
|
||||
:param ttl: time-to-live for the address (after this time, address is no longer valid)
|
||||
""" # noqa: E501
|
||||
|
||||
@abstractmethod
|
||||
def add_addrs(self, peer_id: ID, addrs: Sequence[Multiaddr], ttl: int) -> None:
|
||||
"""
|
||||
Adds addresses for a given peer all with the same time-to-live. If one
|
||||
of the addresses already exists for the peer and has a longer TTL, no
|
||||
operation should take place. If one of the addresses exists with a
|
||||
shorter TTL, extend the TTL to equal param ttl.
|
||||
|
||||
:param peer_id: the peer to add address for
|
||||
:param addr: multiaddresses of the peer
|
||||
:param ttl: time-to-live for the address (after this time, address is no longer valid
|
||||
""" # noqa: E501
|
||||
|
||||
@abstractmethod
|
||||
def addrs(self, peer_id: ID) -> list[Multiaddr]:
|
||||
"""
|
||||
:param peer_id: peer to get addresses of
|
||||
:return: all known (and valid) addresses for the given peer
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def clear_addrs(self, peer_id: ID) -> None:
|
||||
"""
|
||||
Removes all previously stored addresses.
|
||||
|
||||
:param peer_id: peer to remove addresses of
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def peers_with_addrs(self) -> list[ID]:
|
||||
"""
|
||||
:return: all of the peer IDs stored with addresses
|
||||
"""
|
||||
@ -9,15 +9,14 @@ from multiaddr import (
|
||||
Multiaddr,
|
||||
)
|
||||
|
||||
from libp2p.abc import (
|
||||
IPeerData,
|
||||
)
|
||||
from libp2p.crypto.keys import (
|
||||
PrivateKey,
|
||||
PublicKey,
|
||||
)
|
||||
|
||||
from .peerdata_interface import (
|
||||
IPeerData,
|
||||
)
|
||||
|
||||
|
||||
class PeerData(IPeerData):
|
||||
pubkey: PublicKey
|
||||
|
||||
@ -1,100 +0,0 @@
|
||||
from abc import (
|
||||
ABC,
|
||||
abstractmethod,
|
||||
)
|
||||
from collections.abc import (
|
||||
Sequence,
|
||||
)
|
||||
from typing import (
|
||||
Any,
|
||||
)
|
||||
|
||||
from multiaddr import (
|
||||
Multiaddr,
|
||||
)
|
||||
|
||||
from libp2p.crypto.keys import (
|
||||
PrivateKey,
|
||||
PublicKey,
|
||||
)
|
||||
|
||||
from .peermetadata_interface import (
|
||||
IPeerMetadata,
|
||||
)
|
||||
|
||||
|
||||
class IPeerData(ABC):
|
||||
@abstractmethod
|
||||
def get_protocols(self) -> list[str]:
|
||||
"""
|
||||
:return: all protocols associated with given peer
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def add_protocols(self, protocols: Sequence[str]) -> None:
|
||||
"""
|
||||
:param protocols: protocols to add
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def set_protocols(self, protocols: Sequence[str]) -> None:
|
||||
"""
|
||||
:param protocols: protocols to set
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def add_addrs(self, addrs: Sequence[Multiaddr]) -> None:
|
||||
"""
|
||||
:param addrs: multiaddresses to add
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_addrs(self) -> list[Multiaddr]:
|
||||
"""
|
||||
:return: all multiaddresses
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def clear_addrs(self) -> None:
|
||||
"""Clear all addresses."""
|
||||
|
||||
@abstractmethod
|
||||
def put_metadata(self, key: str, val: Any) -> None:
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:param val: val to associate with key
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_metadata(self, key: str) -> IPeerMetadata:
|
||||
"""
|
||||
:param key: key in KV pair
|
||||
:return: val for key
|
||||
: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
|
||||
"""
|
||||
@ -1,31 +0,0 @@
|
||||
from abc import (
|
||||
ABC,
|
||||
abstractmethod,
|
||||
)
|
||||
from typing import (
|
||||
Any,
|
||||
)
|
||||
|
||||
from .id import (
|
||||
ID,
|
||||
)
|
||||
|
||||
|
||||
class IPeerMetadata(ABC):
|
||||
@abstractmethod
|
||||
def get(self, peer_id: ID, key: str) -> Any:
|
||||
"""
|
||||
:param peer_id: peer ID to lookup key for
|
||||
:param key: key to look up
|
||||
:return: value at key for given peer
|
||||
:raise Exception: peer ID not found
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def put(self, peer_id: ID, key: str, val: Any) -> None:
|
||||
"""
|
||||
:param peer_id: peer ID to lookup key for
|
||||
:param key: key to associate with peer
|
||||
:param val: value to associated with key
|
||||
:raise Exception: unsuccessful put
|
||||
"""
|
||||
@ -12,6 +12,9 @@ from multiaddr import (
|
||||
Multiaddr,
|
||||
)
|
||||
|
||||
from libp2p.abc import (
|
||||
IPeerStore,
|
||||
)
|
||||
from libp2p.crypto.keys import (
|
||||
KeyPair,
|
||||
PrivateKey,
|
||||
@ -28,9 +31,6 @@ from .peerdata import (
|
||||
from .peerinfo import (
|
||||
PeerInfo,
|
||||
)
|
||||
from .peerstore_interface import (
|
||||
IPeerStore,
|
||||
)
|
||||
|
||||
|
||||
class PeerStore(IPeerStore):
|
||||
|
||||
@ -1,161 +0,0 @@
|
||||
from abc import (
|
||||
abstractmethod,
|
||||
)
|
||||
from collections.abc import (
|
||||
Sequence,
|
||||
)
|
||||
from typing import (
|
||||
Any,
|
||||
)
|
||||
|
||||
from multiaddr import (
|
||||
Multiaddr,
|
||||
)
|
||||
|
||||
from libp2p.crypto.keys import (
|
||||
KeyPair,
|
||||
PrivateKey,
|
||||
PublicKey,
|
||||
)
|
||||
|
||||
from .addrbook_interface import (
|
||||
IAddrBook,
|
||||
)
|
||||
from .id import (
|
||||
ID,
|
||||
)
|
||||
from .peerinfo import (
|
||||
PeerInfo,
|
||||
)
|
||||
from .peermetadata_interface import (
|
||||
IPeerMetadata,
|
||||
)
|
||||
|
||||
|
||||
class IPeerStore(IAddrBook, IPeerMetadata):
|
||||
@abstractmethod
|
||||
def peer_info(self, peer_id: ID) -> PeerInfo:
|
||||
"""
|
||||
:param peer_id: peer ID to get info for
|
||||
:return: peer info object
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def get_protocols(self, peer_id: ID) -> list[str]:
|
||||
"""
|
||||
:param peer_id: peer ID to get protocols for
|
||||
:return: protocols (as list of strings)
|
||||
:raise PeerStoreError: if peer ID not found
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def add_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None:
|
||||
"""
|
||||
:param peer_id: peer ID to add protocols for
|
||||
:param protocols: protocols to add
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def set_protocols(self, peer_id: ID, protocols: Sequence[str]) -> None:
|
||||
"""
|
||||
:param peer_id: peer ID to set protocols for
|
||||
:param protocols: protocols to set
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def peer_ids(self) -> list[ID]:
|
||||
"""
|
||||
: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
|
||||
"""
|
||||
Reference in New Issue
Block a user