Skip to content

Commit

Permalink
moved all interfaces and typing files
Browse files Browse the repository at this point in the history
Co-authored-by: mystical-prog <[email protected]>
  • Loading branch information
Khwahish29 and mystical-prog committed Feb 19, 2025
1 parent 2713f3d commit 866dec9
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 159 deletions.
8 changes: 0 additions & 8 deletions docs/libp2p.host.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ libp2p.host.exceptions module
:undoc-members:
:show-inheritance:

libp2p.host.host\_interface module
----------------------------------

.. automodule:: libp2p.host.host_interface
:members:
:undoc-members:
:show-inheritance:

libp2p.host.ping module
-----------------------

Expand Down
6 changes: 2 additions & 4 deletions libp2p/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
create_new_key_pair,
)
from libp2p.custom_types import (
TMuxerOptions,
TProtocol,
TSecurityOptions,
)
from libp2p.host.basic_host import (
BasicHost,
Expand Down Expand Up @@ -42,10 +44,6 @@
from libp2p.transport.tcp.tcp import (
TCP,
)
from libp2p.transport.typing import (
TMuxerOptions,
TSecurityOptions,
)
from libp2p.transport.upgrader import (
TransportUpgrader,
)
Expand Down
87 changes: 64 additions & 23 deletions libp2p/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
AsyncContextManager,
)

import multiaddr
from multiaddr import (
Multiaddr,
)
Expand All @@ -24,6 +23,7 @@
)
from libp2p.custom_types import (
StreamHandlerFn,
THandler,
TProtocol,
)
from libp2p.io.abc import (
Expand All @@ -40,7 +40,7 @@
ServiceAPI,
)

# raw_connection_interface
# -------------------------- raw_connection interface.py --------------------------


class IRawConnection(ReadWriteCloser):
Expand All @@ -49,7 +49,9 @@ class IRawConnection(ReadWriteCloser):
is_initiator: bool


# secure_conn_interface
# -------------------------- secure_conn interface.py --------------------------


"""
Represents a secured connection object, which includes a connection and details about
the security involved in the secured connection
Expand Down Expand Up @@ -80,7 +82,7 @@ class ISecureConn(AbstractSecureConn, IRawConnection):
pass


# stream_muxer abc.py
# -------------------------- stream_muxer abc.py --------------------------


class IMuxedConn(ABC):
Expand Down Expand Up @@ -152,7 +154,7 @@ def set_deadline(self, ttl: int) -> bool:
"""


# net_stream_interface
# -------------------------- net_stream interface.py --------------------------


class INetStream(ReadWriteCloser):
Expand All @@ -175,7 +177,9 @@ async def reset(self) -> None:
"""Close both ends of the stream."""


# net_connection_interface
# -------------------------- net_connection interface.py --------------------------


class INetConn(Closer):
muxed_conn: IMuxedConn
event_started: trio.Event
Expand All @@ -189,7 +193,9 @@ def get_streams(self) -> tuple[INetStream, ...]:
...


# peermetadata_interface
# -------------------------- peermetadata interface.py --------------------------


class IPeerMetadata(ABC):
@abstractmethod
def get(self, peer_id: ID, key: str) -> Any:
Expand All @@ -210,7 +216,7 @@ def put(self, peer_id: ID, key: str, val: Any) -> None:
"""


# addrbook_interface
# -------------------------- addrbook interface.py --------------------------


class IAddrBook(ABC):
Expand Down Expand Up @@ -259,7 +265,7 @@ def peers_with_addrs(self) -> list[ID]:
"""


# peerstore_interface
# -------------------------- peerstore interface.py --------------------------


class IPeerStore(IAddrBook, IPeerMetadata):
Expand Down Expand Up @@ -391,7 +397,7 @@ def add_key_pair(self, peer_id: ID, key_pair: KeyPair) -> None:
"""


# listener_interface
# -------------------------- listener interface.py --------------------------


class IListener(ABC):
Expand All @@ -417,7 +423,7 @@ async def close(self) -> None:
...


# network_interface
# -------------------------- network interface.py --------------------------


class INetwork(ABC):
Expand Down Expand Up @@ -480,7 +486,7 @@ class INetworkService(INetwork, ServiceAPI):
pass


# notifee_interface
# -------------------------- notifee interface.py --------------------------


class INotifee(ABC):
Expand Down Expand Up @@ -527,7 +533,9 @@ async def listen_close(self, network: "INetwork", multiaddr: Multiaddr) -> None:
"""


# host_interface
# -------------------------- host interface.py --------------------------


class IHost(ABC):
@abstractmethod
def get_id(self) -> ID:
Expand Down Expand Up @@ -561,7 +569,7 @@ def get_mux(self) -> Any:
"""

@abstractmethod
def get_addrs(self) -> list[multiaddr.Multiaddr]:
def get_addrs(self) -> list[Multiaddr]:
"""
:return: all the multiaddr addresses this host is listening to
"""
Expand All @@ -573,9 +581,7 @@ def get_connected_peers(self) -> list[ID]:
"""

@abstractmethod
def run(
self, listen_addrs: Sequence[multiaddr.Multiaddr]
) -> AsyncContextManager[None]:
def run(self, listen_addrs: Sequence[Multiaddr]) -> AsyncContextManager[None]:
"""
Run the host instance and listen to ``listen_addrs``.
Expand Down Expand Up @@ -627,7 +633,9 @@ async def close(self) -> None:
pass


# peerdata_interface
# -------------------------- peerdata interface.py --------------------------


class IPeerData(ABC):
@abstractmethod
def get_protocols(self) -> list[str]:
Expand Down Expand Up @@ -705,7 +713,9 @@ def get_privkey(self) -> PrivateKey:
"""


# multiselect_communicator_interface
# ------------------ multiselect_communicator interface.py ------------------


class IMultiselectCommunicator(ABC):
"""
Communicator helper class that ensures both the client and multistream
Expand All @@ -726,7 +736,9 @@ async def read(self) -> str:
"""Reads message from stream until EOF."""


# multiselect_client_interface
# -------------------------- multiselect_client interface.py --------------------------


class IMultiselectClient(ABC):
"""
Client for communicating with receiver's multiselect module in order to
Expand Down Expand Up @@ -771,7 +783,9 @@ async def try_select(
"""


# multiselect_muxer_interface
# -------------------------- multiselect_muxer interface.py --------------------------


class IMultiselectMuxer(ABC):
"""
Multiselect module that is responsible for responding to a multiselect
Expand Down Expand Up @@ -806,7 +820,7 @@ async def negotiate(
"""


# interfaces
# -------------------------- routing interface.py --------------------------


class IContentRouting(ABC):
Expand Down Expand Up @@ -837,7 +851,9 @@ async def find_peer(self, peer_id: ID) -> PeerInfo:
"""


# security_transport_interface
# -------------------------- security_transport interface.py --------------------------


"""
Transport that is used to secure a connection. This transport is
chosen by a security transport multistream module.
Expand Down Expand Up @@ -865,3 +881,28 @@ async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureCon
:return: secure connection object (that implements secure_conn_interface)
"""


# -------------------------- transport interface.py --------------------------


class ITransport(ABC):
@abstractmethod
async def dial(self, maddr: Multiaddr) -> IRawConnection:
"""
Dial a transport to peer listening on multiaddr.
:param multiaddr: multiaddr of peer
:param self_id: peer_id of the dialer (to send to receiver)
:return: list of multiaddrs
"""

@abstractmethod
def create_listener(self, handler_function: THandler) -> IListener:
"""
Create listener on transport.
:param handler_function: a function called when a new conntion is received
that takes a connection as argument which implements interface-connection
:return: a listener object that implements listener_interface.py
"""
38 changes: 36 additions & 2 deletions libp2p/custom_types.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
from collections.abc import (
Awaitable,
Mapping,
)
from typing import (
TYPE_CHECKING,
Callable,
NewType,
Union,
)

if TYPE_CHECKING:
from libp2p.abc import IMuxedStream # noqa: F401
from libp2p.abc import INetStream # noqa: F401
from libp2p.abc import (
IMuxedConn,
INetStream,
ISecureTransport,
)
else:

class INetStream:
pass

class IMuxedConn:
pass

class ISecureTransport:
pass


from libp2p.io.abc import (
ReadWriteCloser,
)
from libp2p.peer.id import (
ID,
)
from libp2p.pubsub.pb import (
rpc_pb2,
)

TProtocol = NewType("TProtocol", str)
StreamHandlerFn = Callable[["INetStream"], Awaitable[None]]
THandler = Callable[[ReadWriteCloser], Awaitable[None]]
TSecurityOptions = Mapping[TProtocol, "ISecureTransport"]
TMuxerClass = type["IMuxedConn"]
TMuxerOptions = Mapping[TProtocol, TMuxerClass]
SyncValidatorFn = Callable[[ID, rpc_pb2.Message], bool]
AsyncValidatorFn = Callable[[ID, rpc_pb2.Message], Awaitable[bool]]
ValidatorFn = Union[SyncValidatorFn, AsyncValidatorFn]
UnsubscribeFn = Callable[[], Awaitable[None]]
4 changes: 1 addition & 3 deletions libp2p/network/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
INetworkService,
INotifee,
IPeerStore,
ITransport,
)
from libp2p.custom_types import (
StreamHandlerFn,
Expand All @@ -37,9 +38,6 @@
OpenConnectionError,
SecurityUpgradeFailure,
)
from libp2p.transport.transport_interface import (
ITransport,
)
from libp2p.transport.upgrader import (
TransportUpgrader,
)
Expand Down
4 changes: 1 addition & 3 deletions libp2p/pubsub/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from libp2p.custom_types import (
TProtocol,
ValidatorFn,
)
from libp2p.peer.id import (
ID,
Expand All @@ -24,9 +25,6 @@
from .pb import (
rpc_pb2,
)
from .typing import (
ValidatorFn,
)

if TYPE_CHECKING:
from .pubsub import Pubsub # noqa: F401
Expand Down
8 changes: 3 additions & 5 deletions libp2p/pubsub/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
PrivateKey,
)
from libp2p.custom_types import (
AsyncValidatorFn,
SyncValidatorFn,
TProtocol,
ValidatorFn,
)
from libp2p.exceptions import (
ParseError,
Expand Down Expand Up @@ -72,11 +75,6 @@
from .subscription import (
TrioSubscriptionAPI,
)
from .typing import (
AsyncValidatorFn,
SyncValidatorFn,
ValidatorFn,
)
from .validators import (
PUBSUB_SIGNING_PREFIX,
signature_validator,
Expand Down
Loading

0 comments on commit 866dec9

Please sign in to comment.