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,158 +0,0 @@
|
||||
from abc import (
|
||||
ABC,
|
||||
abstractmethod,
|
||||
)
|
||||
from collections.abc import (
|
||||
AsyncIterable,
|
||||
KeysView,
|
||||
)
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
AsyncContextManager,
|
||||
)
|
||||
|
||||
from libp2p.custom_types import (
|
||||
TProtocol,
|
||||
)
|
||||
from libp2p.peer.id import (
|
||||
ID,
|
||||
)
|
||||
from libp2p.tools.async_service import (
|
||||
ServiceAPI,
|
||||
)
|
||||
|
||||
from .pb import (
|
||||
rpc_pb2,
|
||||
)
|
||||
from .typing import (
|
||||
ValidatorFn,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .pubsub import Pubsub # noqa: F401
|
||||
|
||||
|
||||
class ISubscriptionAPI(
|
||||
AsyncContextManager["ISubscriptionAPI"], AsyncIterable[rpc_pb2.Message]
|
||||
):
|
||||
@abstractmethod
|
||||
async def unsubscribe(self) -> None:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def get(self) -> rpc_pb2.Message:
|
||||
...
|
||||
|
||||
|
||||
class IPubsubRouter(ABC):
|
||||
@abstractmethod
|
||||
def get_protocols(self) -> list[TProtocol]:
|
||||
"""
|
||||
:return: the list of protocols supported by the router
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def attach(self, pubsub: "Pubsub") -> None:
|
||||
"""
|
||||
Attach is invoked by the PubSub constructor to attach the router to a
|
||||
freshly initialized PubSub instance.
|
||||
|
||||
:param pubsub: pubsub instance to attach to
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def add_peer(self, peer_id: ID, protocol_id: TProtocol) -> None:
|
||||
"""
|
||||
Notifies the router that a new peer has been connected.
|
||||
|
||||
:param peer_id: id of peer to add
|
||||
:param protocol_id: router protocol the peer speaks, e.g., floodsub, gossipsub
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def remove_peer(self, peer_id: ID) -> None:
|
||||
"""
|
||||
Notifies the router that a peer has been disconnected.
|
||||
|
||||
:param peer_id: id of peer to remove
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def handle_rpc(self, rpc: rpc_pb2.RPC, sender_peer_id: ID) -> None:
|
||||
"""
|
||||
Invoked to process control messages in the RPC envelope.
|
||||
It is invoked after subscriptions and payload messages have been processed
|
||||
|
||||
:param rpc: RPC message
|
||||
:param sender_peer_id: id of the peer who sent the message
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def publish(self, msg_forwarder: ID, pubsub_msg: rpc_pb2.Message) -> None:
|
||||
"""
|
||||
Invoked to forward a new message that has been validated.
|
||||
|
||||
:param msg_forwarder: peer_id of message sender
|
||||
:param pubsub_msg: pubsub message to forward
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def join(self, topic: str) -> None:
|
||||
"""
|
||||
Join notifies the router that we want to receive and forward messages
|
||||
in a topic. It is invoked after the subscription announcement.
|
||||
|
||||
:param topic: topic to join
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
async def leave(self, topic: str) -> None:
|
||||
"""
|
||||
Leave notifies the router that we are no longer interested in a topic.
|
||||
It is invoked after the unsubscription announcement.
|
||||
|
||||
:param topic: topic to leave
|
||||
"""
|
||||
|
||||
|
||||
class IPubsub(ServiceAPI):
|
||||
@property
|
||||
@abstractmethod
|
||||
def my_id(self) -> ID:
|
||||
...
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def protocols(self) -> tuple[TProtocol, ...]:
|
||||
...
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def topic_ids(self) -> KeysView[str]:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def set_topic_validator(
|
||||
self, topic: str, validator: ValidatorFn, is_async_validator: bool
|
||||
) -> None:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
def remove_topic_validator(self, topic: str) -> None:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def wait_until_ready(self) -> None:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def subscribe(self, topic_id: str) -> ISubscriptionAPI:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def unsubscribe(self, topic_id: str) -> None:
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def publish(self, topic_id: str, data: bytes) -> None:
|
||||
...
|
||||
@ -6,6 +6,9 @@ import logging
|
||||
|
||||
import trio
|
||||
|
||||
from libp2p.abc import (
|
||||
IPubsubRouter,
|
||||
)
|
||||
from libp2p.custom_types import (
|
||||
TProtocol,
|
||||
)
|
||||
@ -19,9 +22,6 @@ from libp2p.utils import (
|
||||
encode_varint_prefixed,
|
||||
)
|
||||
|
||||
from .abc import (
|
||||
IPubsubRouter,
|
||||
)
|
||||
from .pb import (
|
||||
rpc_pb2,
|
||||
)
|
||||
|
||||
@ -17,6 +17,9 @@ from typing import (
|
||||
|
||||
import trio
|
||||
|
||||
from libp2p.abc import (
|
||||
IPubsubRouter,
|
||||
)
|
||||
from libp2p.custom_types import (
|
||||
TProtocol,
|
||||
)
|
||||
@ -36,9 +39,6 @@ from libp2p.utils import (
|
||||
encode_varint_prefixed,
|
||||
)
|
||||
|
||||
from .abc import (
|
||||
IPubsubRouter,
|
||||
)
|
||||
from .exceptions import (
|
||||
NoPubsubAttached,
|
||||
)
|
||||
|
||||
@ -23,19 +23,25 @@ from lru import (
|
||||
)
|
||||
import trio
|
||||
|
||||
from libp2p.abc import (
|
||||
IHost,
|
||||
INetStream,
|
||||
IPubsub,
|
||||
ISubscriptionAPI,
|
||||
)
|
||||
from libp2p.crypto.keys import (
|
||||
PrivateKey,
|
||||
)
|
||||
from libp2p.custom_types import (
|
||||
AsyncValidatorFn,
|
||||
SyncValidatorFn,
|
||||
TProtocol,
|
||||
ValidatorFn,
|
||||
)
|
||||
from libp2p.exceptions import (
|
||||
ParseError,
|
||||
ValidationError,
|
||||
)
|
||||
from libp2p.host.host_interface import (
|
||||
IHost,
|
||||
)
|
||||
from libp2p.io.exceptions import (
|
||||
IncompleteReadError,
|
||||
)
|
||||
@ -47,9 +53,6 @@ from libp2p.network.stream.exceptions import (
|
||||
StreamEOF,
|
||||
StreamReset,
|
||||
)
|
||||
from libp2p.network.stream.net_stream_interface import (
|
||||
INetStream,
|
||||
)
|
||||
from libp2p.peer.id import (
|
||||
ID,
|
||||
)
|
||||
@ -61,10 +64,6 @@ from libp2p.utils import (
|
||||
read_varint_prefixed_bytes,
|
||||
)
|
||||
|
||||
from .abc import (
|
||||
IPubsub,
|
||||
ISubscriptionAPI,
|
||||
)
|
||||
from .pb import (
|
||||
rpc_pb2,
|
||||
)
|
||||
@ -74,11 +73,6 @@ from .pubsub_notifee import (
|
||||
from .subscription import (
|
||||
TrioSubscriptionAPI,
|
||||
)
|
||||
from .typing import (
|
||||
AsyncValidatorFn,
|
||||
SyncValidatorFn,
|
||||
ValidatorFn,
|
||||
)
|
||||
from .validators import (
|
||||
PUBSUB_SIGNING_PREFIX,
|
||||
signature_validator,
|
||||
|
||||
@ -7,17 +7,11 @@ from multiaddr import (
|
||||
)
|
||||
import trio
|
||||
|
||||
from libp2p.network.connection.net_connection_interface import (
|
||||
from libp2p.abc import (
|
||||
INetConn,
|
||||
)
|
||||
from libp2p.network.network_interface import (
|
||||
INetwork,
|
||||
)
|
||||
from libp2p.network.notifee_interface import (
|
||||
INotifee,
|
||||
)
|
||||
from libp2p.network.stream.net_stream_interface import (
|
||||
INetStream,
|
||||
INetwork,
|
||||
INotifee,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
||||
@ -11,15 +11,16 @@ from typing import (
|
||||
|
||||
import trio
|
||||
|
||||
from .abc import (
|
||||
from libp2p.abc import (
|
||||
ISubscriptionAPI,
|
||||
)
|
||||
from libp2p.custom_types import (
|
||||
UnsubscribeFn,
|
||||
)
|
||||
|
||||
from .pb import (
|
||||
rpc_pb2,
|
||||
)
|
||||
from .typing import (
|
||||
UnsubscribeFn,
|
||||
)
|
||||
|
||||
|
||||
class BaseSubscriptionAPI(ISubscriptionAPI):
|
||||
|
||||
@ -1,21 +0,0 @@
|
||||
from collections.abc import (
|
||||
Awaitable,
|
||||
)
|
||||
from typing import (
|
||||
Callable,
|
||||
Union,
|
||||
)
|
||||
|
||||
from libp2p.peer.id import (
|
||||
ID,
|
||||
)
|
||||
|
||||
from .pb import (
|
||||
rpc_pb2,
|
||||
)
|
||||
|
||||
SyncValidatorFn = Callable[[ID, rpc_pb2.Message], bool]
|
||||
AsyncValidatorFn = Callable[[ID, rpc_pb2.Message], Awaitable[bool]]
|
||||
ValidatorFn = Union[SyncValidatorFn, AsyncValidatorFn]
|
||||
|
||||
UnsubscribeFn = Callable[[], Awaitable[None]]
|
||||
Reference in New Issue
Block a user