From 25c610dba403464c59979931253b8285547e9605 Mon Sep 17 00:00:00 2001 From: Khwahish Patel Date: Wed, 5 Feb 2025 00:53:42 +0530 Subject: [PATCH 1/2] refactored exports to abc.py Co-authored-by: mystical-prog --- README.md | 174 ++-- examples/chat/chat.py | 6 +- examples/echo/echo.py | 6 +- examples/ping/ping.py | 6 +- libp2p/__init__.py | 18 +- libp2p/abc.py | 968 ++++++++++++++++++ libp2p/custom_types.py | 6 +- libp2p/host/basic_host.py | 19 +- libp2p/host/defaults.py | 2 +- libp2p/host/host_interface.py | 133 --- libp2p/host/ping.py | 10 +- libp2p/host/routed_host.py | 10 +- libp2p/identity/identify/protocol.py | 10 +- libp2p/io/abc.py | 68 -- libp2p/io/msgio.py | 3 +- libp2p/io/trio.py | 2 +- libp2p/io/utils.py | 2 +- .../connection/net_connection_interface.py | 28 - libp2p/network/connection/raw_connection.py | 6 +- .../connection/raw_connection_interface.py | 9 - libp2p/network/connection/swarm_connection.py | 8 +- libp2p/network/network_interface.py | 100 -- libp2p/network/notifee_interface.py | 65 -- libp2p/network/stream/net_stream.py | 10 +- libp2p/network/stream/net_stream_interface.py | 33 - libp2p/network/swarm.py | 40 +- libp2p/peer/addrbook_interface.py | 61 -- libp2p/peer/peerdata.py | 7 +- libp2p/peer/peerdata_interface.py | 100 -- libp2p/peer/peermetadata_interface.py | 31 - libp2p/peer/peerstore.py | 6 +- libp2p/peer/peerstore_interface.py | 161 --- libp2p/protocol_muxer/multiselect.py | 10 +- libp2p/protocol_muxer/multiselect_client.py | 10 +- .../multiselect_client_interface.py | 58 -- .../multiselect_communicator.py | 10 +- .../multiselect_communicator_interface.py | 24 - .../multiselect_muxer_interface.py | 47 - libp2p/pubsub/pubsub.py | 10 +- libp2p/pubsub/pubsub_notifee.py | 10 +- libp2p/routing/__init__.py | 0 libp2p/routing/interfaces.py | 42 - libp2p/security/base_session.py | 6 +- libp2p/security/base_transport.py | 6 +- libp2p/security/insecure/transport.py | 20 +- libp2p/security/noise/io.py | 6 +- libp2p/security/noise/patterns.py | 10 +- libp2p/security/noise/transport.py | 14 +- libp2p/security/secio/transport.py | 20 +- libp2p/security/secure_conn_interface.py | 44 - libp2p/security/secure_session.py | 6 +- libp2p/security/secure_transport_interface.py | 42 - libp2p/security/security_multistream.py | 20 +- libp2p/stream_muxer/abc.py | 85 -- libp2p/stream_muxer/mplex/mplex.py | 12 +- libp2p/stream_muxer/mplex/mplex_stream.py | 2 +- libp2p/stream_muxer/muxer_multistream.py | 15 +- libp2p/tools/factories.py | 30 +- libp2p/tools/pubsub/dummy_account_node.py | 2 +- libp2p/tools/pubsub/utils.py | 2 +- libp2p/tools/utils.py | 6 +- libp2p/transport/listener_interface.py | 32 - libp2p/transport/tcp/tcp.py | 17 +- libp2p/transport/transport_interface.py | 41 - libp2p/transport/typing.py | 14 +- libp2p/transport/upgrader.py | 19 +- libp2p/utils.py | 6 +- tests/core/network/test_notify.py | 2 +- tests/utils/interop/utils.py | 2 +- 69 files changed, 1235 insertions(+), 1575 deletions(-) create mode 100644 libp2p/abc.py delete mode 100644 libp2p/host/host_interface.py delete mode 100644 libp2p/io/abc.py delete mode 100644 libp2p/network/connection/net_connection_interface.py delete mode 100644 libp2p/network/connection/raw_connection_interface.py delete mode 100644 libp2p/network/network_interface.py delete mode 100644 libp2p/network/notifee_interface.py delete mode 100644 libp2p/network/stream/net_stream_interface.py delete mode 100644 libp2p/peer/addrbook_interface.py delete mode 100644 libp2p/peer/peerdata_interface.py delete mode 100644 libp2p/peer/peermetadata_interface.py delete mode 100644 libp2p/peer/peerstore_interface.py delete mode 100644 libp2p/protocol_muxer/multiselect_client_interface.py delete mode 100644 libp2p/protocol_muxer/multiselect_communicator_interface.py delete mode 100644 libp2p/protocol_muxer/multiselect_muxer_interface.py delete mode 100644 libp2p/routing/__init__.py delete mode 100644 libp2p/routing/interfaces.py delete mode 100644 libp2p/security/secure_conn_interface.py delete mode 100644 libp2p/security/secure_transport_interface.py delete mode 100644 libp2p/stream_muxer/abc.py delete mode 100644 libp2p/transport/listener_interface.py delete mode 100644 libp2p/transport/transport_interface.py diff --git a/README.md b/README.md index cf8fe5f17..95096b972 100644 --- a/README.md +++ b/README.md @@ -29,94 +29,94 @@ py-libp2p aims for conformity with [the standard libp2p modules](https://libp2p. > Legend: ✅: Done   🛠️: In Progress   🚫: Missing   ❌: Not planned -| libp2p Node | Status | -| ------------ | :-----------: | -| **`libp2p`** | ✅ | - -| Core Protocols | Status | -| -------------- | :-----------: | -| **`Ping`** | ✅ | -| **`Identify`** | ✅ | - -| Transport Protocols | Status | -| ------------------- | :-----------: | -| **`TCP`** | ✅| -| **`QUIC`** | 🛠️ | -| **`UDP`** | 🚫 | -| **`WebSockets`** | ❌ | -| **`UTP`** | ❌ | -| **`WebRTC`** | ❌ | -| **`SCTP`** | ❌ | -| **`Tor`** | ❌ | -| **`i2p`** | ❌ | -| **`cjdns`** | ❌ | -| **`Bluetooth LE`** | ❌ | -| **`Audio TP`** | ❌ | -| **`Zerotier`** | ❌ | - -| Stream Muxers | Status | -| ---------------- | :-----------: | -| **`multiplex`** | ✅ | +| libp2p Node | Status | +| ------------ | :----: | +| **`libp2p`** | ✅ | + +| Core Protocols | Status | +| -------------- | :----: | +| **`Ping`** | ✅ | +| **`Identify`** | ✅ | + +| Transport Protocols | Status | +| ------------------- | :----: | +| **`TCP`** | ✅ | +| **`QUIC`** | 🛠️ | +| **`UDP`** | 🚫 | +| **`WebSockets`** | ❌ | +| **`UTP`** | ❌ | +| **`WebRTC`** | ❌ | +| **`SCTP`** | ❌ | +| **`Tor`** | ❌ | +| **`i2p`** | ❌ | +| **`cjdns`** | ❌ | +| **`Bluetooth LE`** | ❌ | +| **`Audio TP`** | ❌ | +| **`Zerotier`** | ❌ | + +| Stream Muxers | Status | +| ---------------- | :----: | +| **`multiplex`** | ✅ | | **`yamux`** | 🚫 | -| **`benchmarks`** | ❌ | -| **`muxado`** | ❌ | -| **`spdystream`** | ❌ | -| **`spdy`** | ❌ | -| **`http2`** | ❌ | -| **`QUIC`** | ❌ | - -| Protocol Muxers | Status | -| ----------------- | :-----------: | -| **`multiselect`** | ✅ | - -| Switch (Swarm) | Status | -| ------------------ | :-----------: | -| **`Switch`** | ✅ | -| **`Dialer stack`** | ✅ | - -| Peer Discovery | Status | -| -------------------- | :--------: | -| **`bootstrap list`** | 🚫 | -| **`Kademlia DHT`** | ❌ | -| **`mDNS`** | ❌ | -| **`PEX`** | ❌ | -| **`DNS`** | ❌ | - -| Content Routing | Status | -| ------------------ | :-----------: | -| **`Kademlia DHT`** | ❌ | -| **`floodsub`** | ✅ | -| **`gossipsub`** | ✅ | -| **`PHT`** | ❌ | - -| Peer Routing | Status | -| ------------------ | :-----------: | -| **`Kademlia DHT`** | ❌ | -| **`floodsub`** | ✅| -| **`gossipsub`** | ✅ | -| **`PHT`** | ❌ | - -| NAT Traversal | Status | -| ------------------------ | :--------: | -| **`nat-pmp`** | ❌ | -| **`upnp`** | ❌ | -| **`ext addr discovery`** | ❌ | -| **`STUN-like`** | ❌ | -| **`line-switch relay`** | ❌ | -| **`pkt-switch relay`** | ❌ | - -| Exchange | Status | -| ---------------- | :--------: | -| **`HTTP`** | ❌ | -| **`Bitswap`** | ❌ | -| **`Bittorrent`** | ❌ | - -| Consensus | Status | -| -------------- | :--------: | -| **`Paxos`** | ❌ | -| **`Raft`** | ❌ | -| **`PBTF`** | ❌ | -| **`Nakamoto`** | ❌ | +| **`benchmarks`** | ❌ | +| **`muxado`** | ❌ | +| **`spdystream`** | ❌ | +| **`spdy`** | ❌ | +| **`http2`** | ❌ | +| **`QUIC`** | ❌ | + +| Protocol Muxers | Status | +| ----------------- | :----: | +| **`multiselect`** | ✅ | + +| Switch (Swarm) | Status | +| ------------------ | :----: | +| **`Switch`** | ✅ | +| **`Dialer stack`** | ✅ | + +| Peer Discovery | Status | +| -------------------- | :----: | +| **`bootstrap list`** | 🚫 | +| **`Kademlia DHT`** | ❌ | +| **`mDNS`** | ❌ | +| **`PEX`** | ❌ | +| **`DNS`** | ❌ | + +| Content Routing | Status | +| ------------------ | :----: | +| **`Kademlia DHT`** | ❌ | +| **`floodsub`** | ✅ | +| **`gossipsub`** | ✅ | +| **`PHT`** | ❌ | + +| Peer Routing | Status | +| ------------------ | :----: | +| **`Kademlia DHT`** | ❌ | +| **`floodsub`** | ✅ | +| **`gossipsub`** | ✅ | +| **`PHT`** | ❌ | + +| NAT Traversal | Status | +| ------------------------ | :----: | +| **`nat-pmp`** | ❌ | +| **`upnp`** | ❌ | +| **`ext addr discovery`** | ❌ | +| **`STUN-like`** | ❌ | +| **`line-switch relay`** | ❌ | +| **`pkt-switch relay`** | ❌ | + +| Exchange | Status | +| ---------------- | :----: | +| **`HTTP`** | ❌ | +| **`Bitswap`** | ❌ | +| **`Bittorrent`** | ❌ | + +| Consensus | Status | +| -------------- | :----: | +| **`Paxos`** | ❌ | +| **`Raft`** | ❌ | +| **`PBTF`** | ❌ | +| **`Nakamoto`** | ❌ | ## Explanation of Basic Two Node Communication diff --git a/examples/chat/chat.py b/examples/chat/chat.py index 048d1e3de..de915ae55 100755 --- a/examples/chat/chat.py +++ b/examples/chat/chat.py @@ -7,12 +7,12 @@ from libp2p import ( new_host, ) +from libp2p.abc import ( + INetStream, +) from libp2p.custom_types import ( TProtocol, ) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) from libp2p.peer.peerinfo import ( info_from_p2p_addr, ) diff --git a/examples/echo/echo.py b/examples/echo/echo.py index 331a8309c..b86418a5c 100644 --- a/examples/echo/echo.py +++ b/examples/echo/echo.py @@ -6,15 +6,15 @@ from libp2p import ( new_host, ) +from libp2p.abc import ( + INetStream, +) from libp2p.crypto.secp256k1 import ( create_new_key_pair, ) from libp2p.custom_types import ( TProtocol, ) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) from libp2p.peer.peerinfo import ( info_from_p2p_addr, ) diff --git a/examples/ping/ping.py b/examples/ping/ping.py index a6690dbd2..642839ba6 100644 --- a/examples/ping/ping.py +++ b/examples/ping/ping.py @@ -6,12 +6,12 @@ from libp2p import ( new_host, ) +from libp2p.abc import ( + INetStream, +) from libp2p.custom_types import ( TProtocol, ) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) from libp2p.peer.peerinfo import ( info_from_p2p_addr, ) diff --git a/libp2p/__init__.py b/libp2p/__init__.py index 0bd528baf..194491846 100644 --- a/libp2p/__init__.py +++ b/libp2p/__init__.py @@ -1,5 +1,11 @@ from importlib.metadata import version as __version +from libp2p.abc import ( + IHost, + INetworkService, + IPeerRouting, + IPeerStore, +) from libp2p.crypto.keys import ( KeyPair, ) @@ -12,15 +18,9 @@ from libp2p.host.basic_host import ( BasicHost, ) -from libp2p.host.host_interface import ( - IHost, -) from libp2p.host.routed_host import ( RoutedHost, ) -from libp2p.network.network_interface import ( - INetworkService, -) from libp2p.network.swarm import ( Swarm, ) @@ -30,12 +30,6 @@ from libp2p.peer.peerstore import ( PeerStore, ) -from libp2p.peer.peerstore_interface import ( - IPeerStore, -) -from libp2p.routing.interfaces import ( - IPeerRouting, -) from libp2p.security.insecure.transport import ( PLAINTEXT_PROTOCOL_ID, InsecureTransport, diff --git a/libp2p/abc.py b/libp2p/abc.py new file mode 100644 index 000000000..e7650d9a4 --- /dev/null +++ b/libp2p/abc.py @@ -0,0 +1,968 @@ +from abc import ( + ABC, + abstractmethod, +) +from collections.abc import ( + Iterable, + Sequence, +) +from typing import ( + Any, + AsyncContextManager, +) + +from multiaddr import ( + Multiaddr, +) +import trio + +from libp2p.crypto.keys import ( + KeyPair, + PrivateKey, + PublicKey, +) +from libp2p.custom_types import ( + StreamHandlerFn, + TProtocol, +) +from libp2p.peer.id import ( + ID, +) +from libp2p.peer.peerinfo import ( + PeerInfo, +) +from libp2p.tools.async_service import ( + ServiceAPI, +) +from libp2p.transport.typing import ( + THandler, +) + +# io_interface + + +class Closer(ABC): + @abstractmethod + async def close(self) -> None: + ... + + +class Reader(ABC): + @abstractmethod + async def read(self, n: int = None) -> bytes: + ... + + +class Writer(ABC): + @abstractmethod + async def write(self, data: bytes) -> None: + ... + + +class WriteCloser(Writer, Closer): + pass + + +class ReadCloser(Reader, Closer): + pass + + +class ReadWriter(Reader, Writer): + pass + + +class ReadWriteCloser(Reader, Writer, Closer): + pass + + +class MsgReader(ABC): + @abstractmethod + async def read_msg(self) -> bytes: + ... + + +class MsgWriter(ABC): + @abstractmethod + async def write_msg(self, msg: bytes) -> None: + ... + + +class MsgReadWriteCloser(MsgReader, MsgWriter, Closer): + pass + + +class Encrypter(ABC): + @abstractmethod + def encrypt(self, data: bytes) -> bytes: + ... + + @abstractmethod + def decrypt(self, data: bytes) -> bytes: + ... + + +class EncryptedMsgReadWriter(MsgReadWriteCloser, Encrypter): + """Read/write message with encryption/decryption.""" + + +# raw_connection_interface + + +class IRawConnection(ReadWriteCloser): + """A Raw Connection provides a Reader and a Writer.""" + + is_initiator: bool + + +# listener_interface + + +class IListener(ABC): + @abstractmethod + async def listen(self, maddr: Multiaddr, nursery: trio.Nursery) -> bool: + """ + Put listener in listening mode and wait for incoming connections. + + :param maddr: multiaddr of peer + :return: return True if successful + """ + + @abstractmethod + def get_addrs(self) -> tuple[Multiaddr, ...]: + """ + Retrieve list of addresses the listener is listening on. + + :return: return list of addrs + """ + + @abstractmethod + async def close(self) -> None: + ... + + +# transport_interface + + +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 + """ + + +# multiselect_communicator_interface + + +class IMultiselectCommunicator(ABC): + """ + Communicator helper class that ensures both the client and multistream + module will follow the same multistream protocol, which is necessary for + them to work. + """ + + @abstractmethod + async def write(self, msg_str: str) -> None: + """ + Write message to stream. + + :param msg_str: message to write + """ + + @abstractmethod + async def read(self) -> str: + """Reads message from stream until EOF.""" + + +# multiselect_muxer_interface + + +class IMultiselectMuxer(ABC): + """ + Multiselect module that is responsible for responding to a multiselect + client and deciding on a specific protocol and handler pair to use for + communication. + """ + + handlers: dict[TProtocol, StreamHandlerFn] + + @abstractmethod + def add_handler(self, protocol: TProtocol, handler: StreamHandlerFn) -> None: + """ + Store the handler with the given protocol. + + :param protocol: protocol name + :param handler: handler function + """ + + def get_protocols(self) -> tuple[TProtocol, ...]: + return tuple(self.handlers.keys()) + + @abstractmethod + async def negotiate( + self, communicator: IMultiselectCommunicator + ) -> tuple[TProtocol, StreamHandlerFn]: + """ + Negotiate performs protocol selection. + + :param stream: stream to negotiate on + :return: selected protocol name, handler function + :raise Exception: negotiation failed exception + """ + + +# multiselect_client_interface + + +class IMultiselectClient(ABC): + """ + Client for communicating with receiver's multiselect module in order to + select a protocol id to communicate over. + """ + + @abstractmethod + async def handshake(self, communicator: IMultiselectCommunicator) -> None: + """ + Ensure that the client and multiselect are both using the same + multiselect protocol. + + :param stream: stream to communicate with multiselect over + :raise Exception: multiselect protocol ID mismatch + """ + + @abstractmethod + async def select_one_of( + self, protocols: Sequence[TProtocol], communicator: IMultiselectCommunicator + ) -> TProtocol: + """ + For each protocol, send message to multiselect selecting protocol and + fail if multiselect does not return same protocol. Returns first + protocol that multiselect agrees on (i.e. that multiselect selects) + + :param protocol: protocol to select + :param stream: stream to communicate with multiselect over + :return: selected protocol + """ + + @abstractmethod + async def try_select( + self, communicator: IMultiselectCommunicator, protocol: TProtocol + ) -> TProtocol: + """ + Try to select the given protocol or raise exception if fails. + + :param communicator: communicator to use to communicate with counterparty + :param protocol: protocol to select + :raise Exception: error in protocol selection + :return: selected protocol + """ + + +# peermetadata_interface + + +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 + """ + + +# addrbook_interface + + +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 + """ + + +# peerstore_interface + + +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 + """ + + +# secure_conn_interface + +""" +Represents a secured connection object, which includes a connection and details about +the security involved in the secured connection + +Relevant go repo: https://github.com/libp2p/go-conn-security/blob/master/interface.go +""" + + +class AbstractSecureConn(ABC): + @abstractmethod + def get_local_peer(self) -> ID: + pass + + @abstractmethod + def get_local_private_key(self) -> PrivateKey: + pass + + @abstractmethod + def get_remote_peer(self) -> ID: + pass + + @abstractmethod + def get_remote_public_key(self) -> PublicKey: + pass + + +class ISecureConn(AbstractSecureConn, IRawConnection): + pass + + +class ISecureTransport(ABC): + @abstractmethod + async def secure_inbound(self, conn: IRawConnection) -> ISecureConn: + """ + Secure the connection, either locally or by communicating with opposing + node via conn, for an inbound connection (i.e. we are not the + initiator) + + :return: secure connection object (that implements secure_conn_interface) + """ + + @abstractmethod + async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureConn: + """ + Secure the connection, either locally or by communicating with opposing + node via conn, for an inbound connection (i.e. we are the initiator) + + :return: secure connection object (that implements secure_conn_interface) + """ + + +# stream_muxer_interface + + +class IMuxedConn(ABC): + """ + reference: https://github.com/libp2p/go-stream-muxer/blob/master/muxer.go + """ + + peer_id: ID + event_started: trio.Event + + @abstractmethod + def __init__(self, conn: ISecureConn, peer_id: ID) -> None: + """ + Create a new muxed connection. + + :param conn: an instance of secured connection + for new muxed streams + :param peer_id: peer_id of peer the connection is to + """ + + @property + @abstractmethod + def is_initiator(self) -> bool: + """If this connection is the initiator.""" + + @abstractmethod + async def start(self) -> None: + """Start the multiplexer.""" + + @abstractmethod + async def close(self) -> None: + """Close connection.""" + + @property + @abstractmethod + def is_closed(self) -> bool: + """ + Check connection is fully closed. + + :return: true if successful + """ + + @abstractmethod + async def open_stream(self) -> "IMuxedStream": + """ + Create a new muxed_stream. + + :return: a new ``IMuxedStream`` stream + """ + + @abstractmethod + async def accept_stream(self) -> "IMuxedStream": + """Accept a muxed stream opened by the other end.""" + + +class IMuxedStream(ReadWriteCloser): + muxed_conn: IMuxedConn + + @abstractmethod + async def reset(self) -> None: + """Close both ends of the stream tells this remote side to hang up.""" + + @abstractmethod + def set_deadline(self, ttl: int) -> bool: + """ + Set deadline for muxed stream. + + :return: a new stream + """ + + +# net_stream_interface + + +class INetStream(ReadWriteCloser): + muxed_conn: IMuxedConn + + @abstractmethod + def get_protocol(self) -> TProtocol: + """ + :return: protocol id that stream runs on + """ + + @abstractmethod + def set_protocol(self, protocol_id: TProtocol) -> None: + """ + :param protocol_id: protocol id that stream runs on + """ + + @abstractmethod + async def reset(self) -> None: + """Close both ends of the stream.""" + + +# net_connection_interface + + +class INetConn(Closer): + muxed_conn: IMuxedConn + event_started: trio.Event + + @abstractmethod + async def new_stream(self) -> INetStream: + ... + + @abstractmethod + def get_streams(self) -> tuple[INetStream, ...]: + ... + + +# network_interface + + +class INetwork(ABC): + peerstore: IPeerStore + connections: dict[ID, INetConn] + listeners: dict[str, IListener] + + @abstractmethod + def get_peer_id(self) -> ID: + """ + :return: the peer id + """ + + @abstractmethod + async def dial_peer(self, peer_id: ID) -> INetConn: + """ + dial_peer try to create a connection to peer_id. + + :param peer_id: peer if we want to dial + :raises SwarmException: raised when an error occurs + :return: muxed connection + """ + + @abstractmethod + async def new_stream(self, peer_id: ID) -> INetStream: + """ + :param peer_id: peer_id of destination + :param protocol_ids: available protocol ids to use for stream + :return: net stream instance + """ + + @abstractmethod + def set_stream_handler(self, stream_handler: StreamHandlerFn) -> None: + """Set the stream handler for all incoming streams.""" + + @abstractmethod + async def listen(self, *multiaddrs: Sequence[Multiaddr]) -> bool: + """ + :param multiaddrs: one or many multiaddrs to start listening on + :return: True if at least one success + """ + + @abstractmethod + def register_notifee(self, notifee: "INotifee") -> None: + """ + :param notifee: object implementing Notifee interface + :return: true if notifee registered successfully, false otherwise + """ + + @abstractmethod + async def close(self) -> None: + pass + + @abstractmethod + async def close_peer(self, peer_id: ID) -> None: + pass + + +class INetworkService(INetwork, ServiceAPI): + pass + + +# host_interface + + +class IHost(ABC): + @abstractmethod + def get_id(self) -> ID: + """ + :return: peer_id of host + """ + + @abstractmethod + def get_public_key(self) -> PublicKey: + """ + :return: the public key belonging to the peer + """ + + @abstractmethod + def get_private_key(self) -> PrivateKey: + """ + :return: the private key belonging to the peer + """ + + @abstractmethod + def get_network(self) -> INetworkService: + """ + :return: network instance of host + """ + + # FIXME: Replace with correct return type + @abstractmethod + def get_mux(self) -> Any: + """ + :return: mux instance of host + """ + + @abstractmethod + def get_addrs(self) -> list[Multiaddr]: + """ + :return: all the multiaddr addresses this host is listening to + """ + + @abstractmethod + def get_connected_peers(self) -> list[ID]: + """ + :return: all the ids of peers this host is currently connected to + """ + + @abstractmethod + def run(self, listen_addrs: Sequence[Multiaddr]) -> AsyncContextManager[None]: + """ + Run the host instance and listen to ``listen_addrs``. + + :param listen_addrs: a sequence of multiaddrs that we want to listen to + """ + + @abstractmethod + def set_stream_handler( + self, protocol_id: TProtocol, stream_handler: StreamHandlerFn + ) -> None: + """ + Set stream handler for host. + + :param protocol_id: protocol id used on stream + :param stream_handler: a stream handler function + """ + + # protocol_id can be a list of protocol_ids + # stream will decide which protocol_id to run on + @abstractmethod + async def new_stream( + self, peer_id: ID, protocol_ids: Sequence[TProtocol] + ) -> INetStream: + """ + :param peer_id: peer_id that host is connecting + :param protocol_ids: available protocol ids to use for stream + :return: stream: new stream created + """ + + @abstractmethod + async def connect(self, peer_info: PeerInfo) -> None: + """ + Ensure there is a connection between this host and the peer + with given peer_info.peer_id. connect will absorb the addresses in + peer_info into its internal peerstore. If there is not an active + connection, connect will issue a dial, and block until a connection is + opened, or an error is returned. + + :param peer_info: peer_info of the peer we want to connect to + :type peer_info: peer.peerinfo.PeerInfo + """ + + @abstractmethod + async def disconnect(self, peer_id: ID) -> None: + pass + + @abstractmethod + async def close(self) -> None: + pass + + +# notifee_interface + + +class INotifee(ABC): + @abstractmethod + async def opened_stream(self, network: "INetwork", stream: INetStream) -> None: + """ + :param network: network the stream was opened on + :param stream: stream that was opened + """ + + @abstractmethod + async def closed_stream(self, network: "INetwork", stream: INetStream) -> None: + """ + :param network: network the stream was closed on + :param stream: stream that was closed + """ + + @abstractmethod + async def connected(self, network: "INetwork", conn: INetConn) -> None: + """ + :param network: network the connection was opened on + :param conn: connection that was opened + """ + + @abstractmethod + async def disconnected(self, network: "INetwork", conn: INetConn) -> None: + """ + :param network: network the connection was closed on + :param conn: connection that was closed + """ + + @abstractmethod + async def listen(self, network: "INetwork", multiaddr: Multiaddr) -> None: + """ + :param network: network the listener is listening on + :param multiaddr: multiaddress listener is listening on + """ + + @abstractmethod + async def listen_close(self, network: "INetwork", multiaddr: Multiaddr) -> None: + """ + :param network: network the connection was opened on + :param multiaddr: multiaddress listener is no longer listening on + """ + + +# secure_transport_interface + +""" +Transport that is used to secure a connection. This transport is +chosen by a security transport multistream module. + +Relevant go repo: https://github.com/libp2p/go-conn-security/blob/master/interface.go +""" + +# peerdata_interface + + +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 + """ + + +# routing_interface +class IContentRouting(ABC): + @abstractmethod + def provide(self, cid: bytes, announce: bool = True) -> None: + """ + Provide adds the given cid to the content routing system. + + If announce is True, it also announces it, otherwise it is just + kept in the local accounting of which objects are being + provided. + """ + + @abstractmethod + def find_provider_iter(self, cid: bytes, count: int) -> Iterable[PeerInfo]: + """ + Search for peers who are able to provide a given key returns an + iterator of peer.PeerInfo. + """ + + +class IPeerRouting(ABC): + @abstractmethod + async def find_peer(self, peer_id: ID) -> PeerInfo: + """ + Find specific Peer FindPeer searches for a peer with given peer_id, + returns a peer.PeerInfo with relevant addresses. + """ diff --git a/libp2p/custom_types.py b/libp2p/custom_types.py index 103c65205..79c8356e9 100644 --- a/libp2p/custom_types.py +++ b/libp2p/custom_types.py @@ -8,8 +8,10 @@ ) if TYPE_CHECKING: - from libp2p.network.stream.net_stream_interface import INetStream # noqa: F401 - from libp2p.stream_muxer.abc import IMuxedStream # noqa: F401 + from libp2p.abc import ( # noqa: F401 + IMuxedStream, + INetStream, + ) TProtocol = NewType("TProtocol", str) StreamHandlerFn = Callable[["INetStream"], Awaitable[None]] diff --git a/libp2p/host/basic_host.py b/libp2p/host/basic_host.py index fff4124ca..fbe2e667c 100644 --- a/libp2p/host/basic_host.py +++ b/libp2p/host/basic_host.py @@ -12,6 +12,12 @@ import multiaddr +from libp2p.abc import ( + IHost, + INetStream, + INetworkService, + IPeerStore, +) from libp2p.crypto.keys import ( PrivateKey, PublicKey, @@ -26,21 +32,12 @@ from libp2p.host.exceptions import ( StreamFailure, ) -from libp2p.network.network_interface import ( - INetworkService, -) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) from libp2p.peer.id import ( ID, ) from libp2p.peer.peerinfo import ( PeerInfo, ) -from libp2p.peer.peerstore_interface import ( - IPeerStore, -) from libp2p.protocol_muxer.exceptions import ( MultiselectClientError, MultiselectError, @@ -58,10 +55,6 @@ background_trio_service, ) -from .host_interface import ( - IHost, -) - if TYPE_CHECKING: from collections import ( OrderedDict, diff --git a/libp2p/host/defaults.py b/libp2p/host/defaults.py index 624c71f8c..93634233d 100644 --- a/libp2p/host/defaults.py +++ b/libp2p/host/defaults.py @@ -5,7 +5,7 @@ TYPE_CHECKING, ) -from libp2p.host.host_interface import ( +from libp2p.abc import ( IHost, ) from libp2p.host.ping import ( diff --git a/libp2p/host/host_interface.py b/libp2p/host/host_interface.py deleted file mode 100644 index 00b83303b..000000000 --- a/libp2p/host/host_interface.py +++ /dev/null @@ -1,133 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) -from collections.abc import ( - Sequence, -) -from typing import ( - Any, - AsyncContextManager, -) - -import multiaddr - -from libp2p.crypto.keys import ( - PrivateKey, - PublicKey, -) -from libp2p.custom_types import ( - StreamHandlerFn, - TProtocol, -) -from libp2p.network.network_interface import ( - INetworkService, -) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) -from libp2p.peer.id import ( - ID, -) -from libp2p.peer.peerinfo import ( - PeerInfo, -) - - -class IHost(ABC): - @abstractmethod - def get_id(self) -> ID: - """ - :return: peer_id of host - """ - - @abstractmethod - def get_public_key(self) -> PublicKey: - """ - :return: the public key belonging to the peer - """ - - @abstractmethod - def get_private_key(self) -> PrivateKey: - """ - :return: the private key belonging to the peer - """ - - @abstractmethod - def get_network(self) -> INetworkService: - """ - :return: network instance of host - """ - - # FIXME: Replace with correct return type - @abstractmethod - def get_mux(self) -> Any: - """ - :return: mux instance of host - """ - - @abstractmethod - def get_addrs(self) -> list[multiaddr.Multiaddr]: - """ - :return: all the multiaddr addresses this host is listening to - """ - - @abstractmethod - def get_connected_peers(self) -> list[ID]: - """ - :return: all the ids of peers this host is currently connected to - """ - - @abstractmethod - def run( - self, listen_addrs: Sequence[multiaddr.Multiaddr] - ) -> AsyncContextManager[None]: - """ - Run the host instance and listen to ``listen_addrs``. - - :param listen_addrs: a sequence of multiaddrs that we want to listen to - """ - - @abstractmethod - def set_stream_handler( - self, protocol_id: TProtocol, stream_handler: StreamHandlerFn - ) -> None: - """ - Set stream handler for host. - - :param protocol_id: protocol id used on stream - :param stream_handler: a stream handler function - """ - - # protocol_id can be a list of protocol_ids - # stream will decide which protocol_id to run on - @abstractmethod - async def new_stream( - self, peer_id: ID, protocol_ids: Sequence[TProtocol] - ) -> INetStream: - """ - :param peer_id: peer_id that host is connecting - :param protocol_ids: available protocol ids to use for stream - :return: stream: new stream created - """ - - @abstractmethod - async def connect(self, peer_info: PeerInfo) -> None: - """ - Ensure there is a connection between this host and the peer - with given peer_info.peer_id. connect will absorb the addresses in - peer_info into its internal peerstore. If there is not an active - connection, connect will issue a dial, and block until a connection is - opened, or an error is returned. - - :param peer_info: peer_info of the peer we want to connect to - :type peer_info: peer.peerinfo.PeerInfo - """ - - @abstractmethod - async def disconnect(self, peer_id: ID) -> None: - pass - - @abstractmethod - async def close(self) -> None: - pass diff --git a/libp2p/host/ping.py b/libp2p/host/ping.py index e19061462..4cc16bc4a 100644 --- a/libp2p/host/ping.py +++ b/libp2p/host/ping.py @@ -4,20 +4,18 @@ import trio +from libp2p.abc import ( + IHost, + INetStream, +) from libp2p.custom_types import ( TProtocol, ) -from libp2p.host.host_interface import ( - IHost, -) from libp2p.network.stream.exceptions import ( StreamClosed, StreamEOF, StreamReset, ) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) from libp2p.peer.id import ID as PeerID ID = TProtocol("/ipfs/ping/1.0.0") diff --git a/libp2p/host/routed_host.py b/libp2p/host/routed_host.py index 3a4b1a286..7cbe81d95 100644 --- a/libp2p/host/routed_host.py +++ b/libp2p/host/routed_host.py @@ -1,18 +1,16 @@ +from libp2p.abc import ( + INetworkService, + IPeerRouting, +) from libp2p.host.basic_host import ( BasicHost, ) from libp2p.host.exceptions import ( ConnectionFailure, ) -from libp2p.network.network_interface import ( - INetworkService, -) from libp2p.peer.peerinfo import ( PeerInfo, ) -from libp2p.routing.interfaces import ( - IPeerRouting, -) # RoutedHost is a p2p Host that includes a routing system. diff --git a/libp2p/identity/identify/protocol.py b/libp2p/identity/identify/protocol.py index 3bf6ae1f9..bb3bd9d66 100644 --- a/libp2p/identity/identify/protocol.py +++ b/libp2p/identity/identify/protocol.py @@ -4,19 +4,17 @@ Multiaddr, ) +from libp2p.abc import ( + IHost, + INetStream, +) from libp2p.custom_types import ( StreamHandlerFn, TProtocol, ) -from libp2p.host.host_interface import ( - IHost, -) from libp2p.network.stream.exceptions import ( StreamClosed, ) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) from .pb.identify_pb2 import ( Identify, diff --git a/libp2p/io/abc.py b/libp2p/io/abc.py deleted file mode 100644 index d87da7af1..000000000 --- a/libp2p/io/abc.py +++ /dev/null @@ -1,68 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) - - -class Closer(ABC): - @abstractmethod - async def close(self) -> None: - ... - - -class Reader(ABC): - @abstractmethod - async def read(self, n: int = None) -> bytes: - ... - - -class Writer(ABC): - @abstractmethod - async def write(self, data: bytes) -> None: - ... - - -class WriteCloser(Writer, Closer): - pass - - -class ReadCloser(Reader, Closer): - pass - - -class ReadWriter(Reader, Writer): - pass - - -class ReadWriteCloser(Reader, Writer, Closer): - pass - - -class MsgReader(ABC): - @abstractmethod - async def read_msg(self) -> bytes: - ... - - -class MsgWriter(ABC): - @abstractmethod - async def write_msg(self, msg: bytes) -> None: - ... - - -class MsgReadWriteCloser(MsgReader, MsgWriter, Closer): - pass - - -class Encrypter(ABC): - @abstractmethod - def encrypt(self, data: bytes) -> bytes: - ... - - @abstractmethod - def decrypt(self, data: bytes) -> bytes: - ... - - -class EncryptedMsgReadWriter(MsgReadWriteCloser, Encrypter): - """Read/write message with encryption/decryption.""" diff --git a/libp2p/io/msgio.py b/libp2p/io/msgio.py index fa049cbdc..7a6c67500 100644 --- a/libp2p/io/msgio.py +++ b/libp2p/io/msgio.py @@ -5,6 +5,7 @@ NOTE: currently missing the capability to indicate lengths by "varint" method. """ + from abc import ( abstractmethod, ) @@ -12,7 +13,7 @@ Literal, ) -from libp2p.io.abc import ( +from libp2p.abc import ( MsgReadWriteCloser, Reader, ReadWriteCloser, diff --git a/libp2p/io/trio.py b/libp2p/io/trio.py index 3998dbef6..9d137141c 100644 --- a/libp2p/io/trio.py +++ b/libp2p/io/trio.py @@ -2,7 +2,7 @@ import trio -from libp2p.io.abc import ( +from libp2p.abc import ( ReadWriteCloser, ) from libp2p.io.exceptions import ( diff --git a/libp2p/io/utils.py b/libp2p/io/utils.py index 8f873ea08..b00f1083f 100644 --- a/libp2p/io/utils.py +++ b/libp2p/io/utils.py @@ -1,4 +1,4 @@ -from libp2p.io.abc import ( +from libp2p.abc import ( Reader, ) from libp2p.io.exceptions import ( diff --git a/libp2p/network/connection/net_connection_interface.py b/libp2p/network/connection/net_connection_interface.py deleted file mode 100644 index 4e5930d6c..000000000 --- a/libp2p/network/connection/net_connection_interface.py +++ /dev/null @@ -1,28 +0,0 @@ -from abc import ( - abstractmethod, -) - -import trio - -from libp2p.io.abc import ( - Closer, -) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) -from libp2p.stream_muxer.abc import ( - IMuxedConn, -) - - -class INetConn(Closer): - muxed_conn: IMuxedConn - event_started: trio.Event - - @abstractmethod - async def new_stream(self) -> INetStream: - ... - - @abstractmethod - def get_streams(self) -> tuple[INetStream, ...]: - ... diff --git a/libp2p/network/connection/raw_connection.py b/libp2p/network/connection/raw_connection.py index 98961296d..d087f0576 100644 --- a/libp2p/network/connection/raw_connection.py +++ b/libp2p/network/connection/raw_connection.py @@ -1,4 +1,5 @@ -from libp2p.io.abc import ( +from libp2p.abc import ( + IRawConnection, ReadWriteCloser, ) from libp2p.io.exceptions import ( @@ -8,9 +9,6 @@ from .exceptions import ( RawConnError, ) -from .raw_connection_interface import ( - IRawConnection, -) class RawConnection(IRawConnection): diff --git a/libp2p/network/connection/raw_connection_interface.py b/libp2p/network/connection/raw_connection_interface.py deleted file mode 100644 index 6f2bab2bd..000000000 --- a/libp2p/network/connection/raw_connection_interface.py +++ /dev/null @@ -1,9 +0,0 @@ -from libp2p.io.abc import ( - ReadWriteCloser, -) - - -class IRawConnection(ReadWriteCloser): - """A Raw Connection provides a Reader and a Writer.""" - - is_initiator: bool diff --git a/libp2p/network/connection/swarm_connection.py b/libp2p/network/connection/swarm_connection.py index 1490ab60f..0470d3bb2 100644 --- a/libp2p/network/connection/swarm_connection.py +++ b/libp2p/network/connection/swarm_connection.py @@ -4,16 +4,14 @@ import trio -from libp2p.network.connection.net_connection_interface import ( +from libp2p.abc import ( + IMuxedConn, + IMuxedStream, INetConn, ) from libp2p.network.stream.net_stream import ( NetStream, ) -from libp2p.stream_muxer.abc import ( - IMuxedConn, - IMuxedStream, -) from libp2p.stream_muxer.exceptions import ( MuxedConnUnavailable, ) diff --git a/libp2p/network/network_interface.py b/libp2p/network/network_interface.py deleted file mode 100644 index 6f97f71ae..000000000 --- a/libp2p/network/network_interface.py +++ /dev/null @@ -1,100 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) -from collections.abc import ( - Sequence, -) -from typing import ( - TYPE_CHECKING, -) - -from multiaddr import ( - Multiaddr, -) - -from libp2p.custom_types import ( - StreamHandlerFn, -) -from libp2p.network.connection.net_connection_interface import ( - INetConn, -) -from libp2p.peer.id import ( - ID, -) -from libp2p.peer.peerstore_interface import ( - IPeerStore, -) -from libp2p.tools.async_service import ( - ServiceAPI, -) -from libp2p.transport.listener_interface import ( - IListener, -) - -from .stream.net_stream_interface import ( - INetStream, -) - -if TYPE_CHECKING: - from .notifee_interface import INotifee # noqa: F401 - - -class INetwork(ABC): - peerstore: IPeerStore - connections: dict[ID, INetConn] - listeners: dict[str, IListener] - - @abstractmethod - def get_peer_id(self) -> ID: - """ - :return: the peer id - """ - - @abstractmethod - async def dial_peer(self, peer_id: ID) -> INetConn: - """ - dial_peer try to create a connection to peer_id. - - :param peer_id: peer if we want to dial - :raises SwarmException: raised when an error occurs - :return: muxed connection - """ - - @abstractmethod - async def new_stream(self, peer_id: ID) -> INetStream: - """ - :param peer_id: peer_id of destination - :param protocol_ids: available protocol ids to use for stream - :return: net stream instance - """ - - @abstractmethod - def set_stream_handler(self, stream_handler: StreamHandlerFn) -> None: - """Set the stream handler for all incoming streams.""" - - @abstractmethod - async def listen(self, *multiaddrs: Sequence[Multiaddr]) -> bool: - """ - :param multiaddrs: one or many multiaddrs to start listening on - :return: True if at least one success - """ - - @abstractmethod - def register_notifee(self, notifee: "INotifee") -> None: - """ - :param notifee: object implementing Notifee interface - :return: true if notifee registered successfully, false otherwise - """ - - @abstractmethod - async def close(self) -> None: - pass - - @abstractmethod - async def close_peer(self, peer_id: ID) -> None: - pass - - -class INetworkService(INetwork, ServiceAPI): - pass diff --git a/libp2p/network/notifee_interface.py b/libp2p/network/notifee_interface.py deleted file mode 100644 index 22deb5759..000000000 --- a/libp2p/network/notifee_interface.py +++ /dev/null @@ -1,65 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) -from typing import ( - TYPE_CHECKING, -) - -from multiaddr import ( - Multiaddr, -) - -from libp2p.network.connection.net_connection_interface import ( - INetConn, -) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) - -if TYPE_CHECKING: - from .network_interface import INetwork # noqa: F401 - - -class INotifee(ABC): - @abstractmethod - async def opened_stream(self, network: "INetwork", stream: INetStream) -> None: - """ - :param network: network the stream was opened on - :param stream: stream that was opened - """ - - @abstractmethod - async def closed_stream(self, network: "INetwork", stream: INetStream) -> None: - """ - :param network: network the stream was closed on - :param stream: stream that was closed - """ - - @abstractmethod - async def connected(self, network: "INetwork", conn: INetConn) -> None: - """ - :param network: network the connection was opened on - :param conn: connection that was opened - """ - - @abstractmethod - async def disconnected(self, network: "INetwork", conn: INetConn) -> None: - """ - :param network: network the connection was closed on - :param conn: connection that was closed - """ - - @abstractmethod - async def listen(self, network: "INetwork", multiaddr: Multiaddr) -> None: - """ - :param network: network the listener is listening on - :param multiaddr: multiaddress listener is listening on - """ - - @abstractmethod - async def listen_close(self, network: "INetwork", multiaddr: Multiaddr) -> None: - """ - :param network: network the connection was opened on - :param multiaddr: multiaddress listener is no longer listening on - """ diff --git a/libp2p/network/stream/net_stream.py b/libp2p/network/stream/net_stream.py index 90d4c55cb..5dc053c4e 100644 --- a/libp2p/network/stream/net_stream.py +++ b/libp2p/network/stream/net_stream.py @@ -2,12 +2,13 @@ Optional, ) +from libp2p.abc import ( + IMuxedStream, + INetStream, +) from libp2p.custom_types import ( TProtocol, ) -from libp2p.stream_muxer.abc import ( - IMuxedStream, -) from libp2p.stream_muxer.exceptions import ( MuxedStreamClosed, MuxedStreamEOF, @@ -19,9 +20,6 @@ StreamEOF, StreamReset, ) -from .net_stream_interface import ( - INetStream, -) # TODO: Handle exceptions from `muxed_stream` diff --git a/libp2p/network/stream/net_stream_interface.py b/libp2p/network/stream/net_stream_interface.py deleted file mode 100644 index ceaede3a7..000000000 --- a/libp2p/network/stream/net_stream_interface.py +++ /dev/null @@ -1,33 +0,0 @@ -from abc import ( - abstractmethod, -) - -from libp2p.custom_types import ( - TProtocol, -) -from libp2p.io.abc import ( - ReadWriteCloser, -) -from libp2p.stream_muxer.abc import ( - IMuxedConn, -) - - -class INetStream(ReadWriteCloser): - muxed_conn: IMuxedConn - - @abstractmethod - def get_protocol(self) -> TProtocol: - """ - :return: protocol id that stream runs on - """ - - @abstractmethod - def set_protocol(self, protocol_id: TProtocol) -> None: - """ - :param protocol_id: protocol id that stream runs on - """ - - @abstractmethod - async def reset(self) -> None: - """Close both ends of the stream.""" diff --git a/libp2p/network/swarm.py b/libp2p/network/swarm.py index 647a073a4..8733cab64 100644 --- a/libp2p/network/swarm.py +++ b/libp2p/network/swarm.py @@ -8,14 +8,19 @@ ) import trio -from libp2p.custom_types import ( - StreamHandlerFn, -) -from libp2p.io.abc import ( +from libp2p.abc import ( + IListener, + IMuxedConn, + INetConn, + INetStream, + INetworkService, + INotifee, + IPeerStore, + ITransport, ReadWriteCloser, ) -from libp2p.network.connection.net_connection_interface import ( - INetConn, +from libp2p.custom_types import ( + StreamHandlerFn, ) from libp2p.peer.id import ( ID, @@ -23,12 +28,6 @@ from libp2p.peer.peerstore import ( PeerStoreError, ) -from libp2p.peer.peerstore_interface import ( - IPeerStore, -) -from libp2p.stream_muxer.abc import ( - IMuxedConn, -) from libp2p.tools.async_service import ( Service, ) @@ -37,12 +36,6 @@ OpenConnectionError, SecurityUpgradeFailure, ) -from libp2p.transport.listener_interface import ( - IListener, -) -from libp2p.transport.transport_interface import ( - ITransport, -) from libp2p.transport.upgrader import ( TransportUpgrader, ) @@ -59,15 +52,6 @@ from .exceptions import ( SwarmException, ) -from .network_interface import ( - INetworkService, -) -from .notifee_interface import ( - INotifee, -) -from .stream.net_stream_interface import ( - INetStream, -) logger = logging.getLogger("libp2p.network.swarm") @@ -371,7 +355,7 @@ def remove_conn(self, swarm_conn: SwarmConn) -> None: def register_notifee(self, notifee: INotifee) -> None: """ - :param notifee: object implementing Notifee interface + ::param notifee: object implementing INotifee (from libp2p.abc) :return: true if notifee registered successfully, false otherwise """ self.notifees.append(notifee) diff --git a/libp2p/peer/addrbook_interface.py b/libp2p/peer/addrbook_interface.py deleted file mode 100644 index 35dcbd8c5..000000000 --- a/libp2p/peer/addrbook_interface.py +++ /dev/null @@ -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 - """ diff --git a/libp2p/peer/peerdata.py b/libp2p/peer/peerdata.py index bd22d7299..f0e524632 100644 --- a/libp2p/peer/peerdata.py +++ b/libp2p/peer/peerdata.py @@ -9,15 +9,14 @@ Multiaddr, ) +from libp2p.abc import ( + IPeerData, +) from libp2p.crypto.keys import ( PrivateKey, PublicKey, ) -from .peerdata_interface import ( - IPeerData, -) - class PeerData(IPeerData): pubkey: PublicKey diff --git a/libp2p/peer/peerdata_interface.py b/libp2p/peer/peerdata_interface.py deleted file mode 100644 index a2c61a5c8..000000000 --- a/libp2p/peer/peerdata_interface.py +++ /dev/null @@ -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 - """ diff --git a/libp2p/peer/peermetadata_interface.py b/libp2p/peer/peermetadata_interface.py deleted file mode 100644 index 39d8967da..000000000 --- a/libp2p/peer/peermetadata_interface.py +++ /dev/null @@ -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 - """ diff --git a/libp2p/peer/peerstore.py b/libp2p/peer/peerstore.py index f418dad69..f49739ba5 100644 --- a/libp2p/peer/peerstore.py +++ b/libp2p/peer/peerstore.py @@ -12,6 +12,9 @@ Multiaddr, ) +from libp2p.abc import ( + IPeerStore, +) from libp2p.crypto.keys import ( KeyPair, PrivateKey, @@ -28,9 +31,6 @@ from .peerinfo import ( PeerInfo, ) -from .peerstore_interface import ( - IPeerStore, -) class PeerStore(IPeerStore): diff --git a/libp2p/peer/peerstore_interface.py b/libp2p/peer/peerstore_interface.py deleted file mode 100644 index 1b5db0693..000000000 --- a/libp2p/peer/peerstore_interface.py +++ /dev/null @@ -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 - """ diff --git a/libp2p/protocol_muxer/multiselect.py b/libp2p/protocol_muxer/multiselect.py index 69ea27542..ed9bccca2 100644 --- a/libp2p/protocol_muxer/multiselect.py +++ b/libp2p/protocol_muxer/multiselect.py @@ -1,3 +1,7 @@ +from libp2p.abc import ( + IMultiselectCommunicator, + IMultiselectMuxer, +) from libp2p.custom_types import ( StreamHandlerFn, TProtocol, @@ -7,12 +11,6 @@ MultiselectCommunicatorError, MultiselectError, ) -from .multiselect_communicator_interface import ( - IMultiselectCommunicator, -) -from .multiselect_muxer_interface import ( - IMultiselectMuxer, -) MULTISELECT_PROTOCOL_ID = "/multistream/1.0.0" PROTOCOL_NOT_FOUND_MSG = "na" diff --git a/libp2p/protocol_muxer/multiselect_client.py b/libp2p/protocol_muxer/multiselect_client.py index d88065216..abbab54cb 100644 --- a/libp2p/protocol_muxer/multiselect_client.py +++ b/libp2p/protocol_muxer/multiselect_client.py @@ -2,6 +2,10 @@ Sequence, ) +from libp2p.abc import ( + IMultiselectClient, + IMultiselectCommunicator, +) from libp2p.custom_types import ( TProtocol, ) @@ -10,12 +14,6 @@ MultiselectClientError, MultiselectCommunicatorError, ) -from .multiselect_client_interface import ( - IMultiselectClient, -) -from .multiselect_communicator_interface import ( - IMultiselectCommunicator, -) MULTISELECT_PROTOCOL_ID = "/multistream/1.0.0" PROTOCOL_NOT_FOUND_MSG = "na" diff --git a/libp2p/protocol_muxer/multiselect_client_interface.py b/libp2p/protocol_muxer/multiselect_client_interface.py deleted file mode 100644 index 180aa4f64..000000000 --- a/libp2p/protocol_muxer/multiselect_client_interface.py +++ /dev/null @@ -1,58 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) -from collections.abc import ( - Sequence, -) - -from libp2p.custom_types import ( - TProtocol, -) -from libp2p.protocol_muxer.multiselect_communicator_interface import ( - IMultiselectCommunicator, -) - - -class IMultiselectClient(ABC): - """ - Client for communicating with receiver's multiselect module in order to - select a protocol id to communicate over. - """ - - @abstractmethod - async def handshake(self, communicator: IMultiselectCommunicator) -> None: - """ - Ensure that the client and multiselect are both using the same - multiselect protocol. - - :param stream: stream to communicate with multiselect over - :raise Exception: multiselect protocol ID mismatch - """ - - @abstractmethod - async def select_one_of( - self, protocols: Sequence[TProtocol], communicator: IMultiselectCommunicator - ) -> TProtocol: - """ - For each protocol, send message to multiselect selecting protocol and - fail if multiselect does not return same protocol. Returns first - protocol that multiselect agrees on (i.e. that multiselect selects) - - :param protocol: protocol to select - :param stream: stream to communicate with multiselect over - :return: selected protocol - """ - - @abstractmethod - async def try_select( - self, communicator: IMultiselectCommunicator, protocol: TProtocol - ) -> TProtocol: - """ - Try to select the given protocol or raise exception if fails. - - :param communicator: communicator to use to communicate with counterparty - :param protocol: protocol to select - :raise Exception: error in protocol selection - :return: selected protocol - """ diff --git a/libp2p/protocol_muxer/multiselect_communicator.py b/libp2p/protocol_muxer/multiselect_communicator.py index f4ecc369a..b309035df 100644 --- a/libp2p/protocol_muxer/multiselect_communicator.py +++ b/libp2p/protocol_muxer/multiselect_communicator.py @@ -1,9 +1,10 @@ +from libp2p.abc import ( + IMultiselectCommunicator, + ReadWriteCloser, +) from libp2p.exceptions import ( ParseError, ) -from libp2p.io.abc import ( - ReadWriteCloser, -) from libp2p.io.exceptions import ( IOException, ) @@ -15,9 +16,6 @@ from .exceptions import ( MultiselectCommunicatorError, ) -from .multiselect_communicator_interface import ( - IMultiselectCommunicator, -) class MultiselectCommunicator(IMultiselectCommunicator): diff --git a/libp2p/protocol_muxer/multiselect_communicator_interface.py b/libp2p/protocol_muxer/multiselect_communicator_interface.py deleted file mode 100644 index c6e097c0f..000000000 --- a/libp2p/protocol_muxer/multiselect_communicator_interface.py +++ /dev/null @@ -1,24 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) - - -class IMultiselectCommunicator(ABC): - """ - Communicator helper class that ensures both the client and multistream - module will follow the same multistream protocol, which is necessary for - them to work. - """ - - @abstractmethod - async def write(self, msg_str: str) -> None: - """ - Write message to stream. - - :param msg_str: message to write - """ - - @abstractmethod - async def read(self) -> str: - """Reads message from stream until EOF.""" diff --git a/libp2p/protocol_muxer/multiselect_muxer_interface.py b/libp2p/protocol_muxer/multiselect_muxer_interface.py deleted file mode 100644 index f0ecf063a..000000000 --- a/libp2p/protocol_muxer/multiselect_muxer_interface.py +++ /dev/null @@ -1,47 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) - -from libp2p.custom_types import ( - StreamHandlerFn, - TProtocol, -) - -from .multiselect_communicator_interface import ( - IMultiselectCommunicator, -) - - -class IMultiselectMuxer(ABC): - """ - Multiselect module that is responsible for responding to a multiselect - client and deciding on a specific protocol and handler pair to use for - communication. - """ - - handlers: dict[TProtocol, StreamHandlerFn] - - @abstractmethod - def add_handler(self, protocol: TProtocol, handler: StreamHandlerFn) -> None: - """ - Store the handler with the given protocol. - - :param protocol: protocol name - :param handler: handler function - """ - - def get_protocols(self) -> tuple[TProtocol, ...]: - return tuple(self.handlers.keys()) - - @abstractmethod - async def negotiate( - self, communicator: IMultiselectCommunicator - ) -> tuple[TProtocol, StreamHandlerFn]: - """ - Negotiate performs protocol selection. - - :param stream: stream to negotiate on - :return: selected protocol name, handler function - :raise Exception: negotiation failed exception - """ diff --git a/libp2p/pubsub/pubsub.py b/libp2p/pubsub/pubsub.py index 1b1fe19f3..1d2ecc992 100644 --- a/libp2p/pubsub/pubsub.py +++ b/libp2p/pubsub/pubsub.py @@ -23,6 +23,10 @@ ) import trio +from libp2p.abc import ( + IHost, + INetStream, +) from libp2p.crypto.keys import ( PrivateKey, ) @@ -33,9 +37,6 @@ ParseError, ValidationError, ) -from libp2p.host.host_interface import ( - IHost, -) from libp2p.io.exceptions import ( IncompleteReadError, ) @@ -47,9 +48,6 @@ StreamEOF, StreamReset, ) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) from libp2p.peer.id import ( ID, ) diff --git a/libp2p/pubsub/pubsub_notifee.py b/libp2p/pubsub/pubsub_notifee.py index ae54d1374..624aab6df 100644 --- a/libp2p/pubsub/pubsub_notifee.py +++ b/libp2p/pubsub/pubsub_notifee.py @@ -7,18 +7,12 @@ ) import trio -from libp2p.network.connection.net_connection_interface import ( +from libp2p.abc import ( INetConn, -) -from libp2p.network.network_interface import ( + INetStream, INetwork, -) -from libp2p.network.notifee_interface import ( INotifee, ) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) if TYPE_CHECKING: from libp2p.peer.id import ID # noqa: F401 diff --git a/libp2p/routing/__init__.py b/libp2p/routing/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/libp2p/routing/interfaces.py b/libp2p/routing/interfaces.py deleted file mode 100644 index 29f63af39..000000000 --- a/libp2p/routing/interfaces.py +++ /dev/null @@ -1,42 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) -from collections.abc import ( - Iterable, -) - -from libp2p.peer.id import ( - ID, -) -from libp2p.peer.peerinfo import ( - PeerInfo, -) - - -class IContentRouting(ABC): - @abstractmethod - def provide(self, cid: bytes, announce: bool = True) -> None: - """ - Provide adds the given cid to the content routing system. - - If announce is True, it also announces it, otherwise it is just - kept in the local accounting of which objects are being - provided. - """ - - @abstractmethod - def find_provider_iter(self, cid: bytes, count: int) -> Iterable[PeerInfo]: - """ - Search for peers who are able to provide a given key returns an - iterator of peer.PeerInfo. - """ - - -class IPeerRouting(ABC): - @abstractmethod - async def find_peer(self, peer_id: ID) -> PeerInfo: - """ - Find specific Peer FindPeer searches for a peer with given peer_id, - returns a peer.PeerInfo with relevant addresses. - """ diff --git a/libp2p/security/base_session.py b/libp2p/security/base_session.py index 07ed74fc2..fa99a62ab 100644 --- a/libp2p/security/base_session.py +++ b/libp2p/security/base_session.py @@ -2,6 +2,9 @@ Optional, ) +from libp2p.abc import ( + ISecureConn, +) from libp2p.crypto.keys import ( PrivateKey, PublicKey, @@ -9,9 +12,6 @@ from libp2p.peer.id import ( ID, ) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) class BaseSession(ISecureConn): diff --git a/libp2p/security/base_transport.py b/libp2p/security/base_transport.py index 5d055c2f1..108ded01d 100644 --- a/libp2p/security/base_transport.py +++ b/libp2p/security/base_transport.py @@ -3,15 +3,15 @@ Callable, ) +from libp2p.abc import ( + ISecureTransport, +) from libp2p.crypto.keys import ( KeyPair, ) from libp2p.peer.id import ( ID, ) -from libp2p.security.secure_transport_interface import ( - ISecureTransport, -) def default_secure_bytes_provider(n: int) -> bytes: diff --git a/libp2p/security/insecure/transport.py b/libp2p/security/insecure/transport.py index a60b3a619..406fc3c6e 100644 --- a/libp2p/security/insecure/transport.py +++ b/libp2p/security/insecure/transport.py @@ -1,3 +1,8 @@ +from libp2p.abc import ( + IRawConnection, + ISecureConn, + ReadWriteCloser, +) from libp2p.crypto.exceptions import ( MissingDeserializerError, ) @@ -14,18 +19,12 @@ from libp2p.custom_types import ( TProtocol, ) -from libp2p.io.abc import ( - ReadWriteCloser, -) from libp2p.io.msgio import ( VarIntLengthMsgReadWriter, ) from libp2p.network.connection.exceptions import ( RawConnError, ) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) from libp2p.peer.id import ( ID, ) @@ -38,9 +37,6 @@ from libp2p.security.exceptions import ( HandshakeFailure, ) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) from .pb import ( plaintext_pb2, @@ -162,7 +158,8 @@ async def secure_inbound(self, conn: IRawConnection) -> ISecureConn: node via conn, for an inbound connection (i.e. we are not the initiator) - :return: secure connection object (that implements secure_conn_interface) + :return: secure connection object that implements ISecureConn (from libp2p.abc) + """ return await run_handshake( self.local_peer, self.local_private_key, conn, False, None @@ -173,7 +170,8 @@ async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureCon Secure the connection, either locally or by communicating with opposing node via conn, for an inbound connection (i.e. we are the initiator) - :return: secure connection object (that implements secure_conn_interface) + :return: secure connection object that implements ISecureConn (from libp2p.abc) + """ return await run_handshake( self.local_peer, self.local_private_key, conn, True, peer_id diff --git a/libp2p/security/noise/io.py b/libp2p/security/noise/io.py index 725c809ba..593b5824d 100644 --- a/libp2p/security/noise/io.py +++ b/libp2p/security/noise/io.py @@ -4,17 +4,15 @@ from noise.connection import NoiseConnection as NoiseState -from libp2p.io.abc import ( +from libp2p.abc import ( EncryptedMsgReadWriter, + IRawConnection, MsgReadWriteCloser, ReadWriteCloser, ) from libp2p.io.msgio import ( FixedSizeLenMsgReadWriter, ) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) SIZE_NOISE_MESSAGE_LEN = 2 MAX_NOISE_MESSAGE_LEN = 2 ** (8 * SIZE_NOISE_MESSAGE_LEN) - 1 diff --git a/libp2p/security/noise/patterns.py b/libp2p/security/noise/patterns.py index 0803060e9..27b8d63b6 100644 --- a/libp2p/security/noise/patterns.py +++ b/libp2p/security/noise/patterns.py @@ -10,6 +10,10 @@ from noise.connection import Keypair as NoiseKeypairEnum from noise.connection import NoiseConnection as NoiseState +from libp2p.abc import ( + IRawConnection, + ISecureConn, +) from libp2p.crypto.ed25519 import ( Ed25519PublicKey, ) @@ -17,15 +21,9 @@ PrivateKey, PublicKey, ) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) from libp2p.peer.id import ( ID, ) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) from libp2p.security.secure_session import ( SecureSession, ) diff --git a/libp2p/security/noise/transport.py b/libp2p/security/noise/transport.py index d335d4389..e90dcc649 100644 --- a/libp2p/security/noise/transport.py +++ b/libp2p/security/noise/transport.py @@ -1,3 +1,8 @@ +from libp2p.abc import ( + IRawConnection, + ISecureConn, + ISecureTransport, +) from libp2p.crypto.keys import ( KeyPair, PrivateKey, @@ -5,18 +10,9 @@ from libp2p.custom_types import ( TProtocol, ) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) from libp2p.peer.id import ( ID, ) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) -from libp2p.security.secure_transport_interface import ( - ISecureTransport, -) from .patterns import ( IPattern, diff --git a/libp2p/security/secio/transport.py b/libp2p/security/secio/transport.py index ac3286980..0a85804b5 100644 --- a/libp2p/security/secio/transport.py +++ b/libp2p/security/secio/transport.py @@ -8,6 +8,11 @@ import multihash +from libp2p.abc import ( + EncryptedMsgReadWriter, + IRawConnection, + ISecureConn, +) from libp2p.crypto.authenticated_encryption import ( EncryptionParameters as AuthenticatedEncryptionParameters, ) @@ -37,9 +42,6 @@ from libp2p.custom_types import ( TProtocol, ) -from libp2p.io.abc import ( - EncryptedMsgReadWriter, -) from libp2p.io.exceptions import ( DecryptionFailedException, IOException, @@ -47,16 +49,10 @@ from libp2p.io.msgio import ( FixedSizeLenMsgReadWriter, ) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) from libp2p.peer.id import ID as PeerID from libp2p.security.base_transport import ( BaseSecureTransport, ) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) from libp2p.security.secure_session import ( SecureSession, ) @@ -454,7 +450,8 @@ async def secure_inbound(self, conn: IRawConnection) -> ISecureConn: node via conn, for an inbound connection (i.e. we are not the initiator) - :return: secure connection object (that implements secure_conn_interface) + :return: secure connection object that implements ISecureConn (from libp2p.abc) + """ local_nonce = self.get_nonce() local_peer = self.local_peer @@ -471,7 +468,8 @@ async def secure_outbound( Secure the connection, either locally or by communicating with opposing node via conn, for an inbound connection (i.e. we are the initiator) - :return: secure connection object (that implements secure_conn_interface) + :return: secure connection object that implements ISecureConn (from libp2p.abc) + """ local_nonce = self.get_nonce() local_peer = self.local_peer diff --git a/libp2p/security/secure_conn_interface.py b/libp2p/security/secure_conn_interface.py deleted file mode 100644 index 245b5f589..000000000 --- a/libp2p/security/secure_conn_interface.py +++ /dev/null @@ -1,44 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) - -from libp2p.crypto.keys import ( - PrivateKey, - PublicKey, -) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) -from libp2p.peer.id import ( - ID, -) - -""" -Represents a secured connection object, which includes a connection and details about -the security involved in the secured connection - -Relevant go repo: https://github.com/libp2p/go-conn-security/blob/master/interface.go -""" - - -class AbstractSecureConn(ABC): - @abstractmethod - def get_local_peer(self) -> ID: - pass - - @abstractmethod - def get_local_private_key(self) -> PrivateKey: - pass - - @abstractmethod - def get_remote_peer(self) -> ID: - pass - - @abstractmethod - def get_remote_public_key(self) -> PublicKey: - pass - - -class ISecureConn(AbstractSecureConn, IRawConnection): - pass diff --git a/libp2p/security/secure_session.py b/libp2p/security/secure_session.py index 7c7276195..8903057f6 100644 --- a/libp2p/security/secure_session.py +++ b/libp2p/security/secure_session.py @@ -1,12 +1,12 @@ import io +from libp2p.abc import ( + EncryptedMsgReadWriter, +) from libp2p.crypto.keys import ( PrivateKey, PublicKey, ) -from libp2p.io.abc import ( - EncryptedMsgReadWriter, -) from libp2p.peer.id import ( ID, ) diff --git a/libp2p/security/secure_transport_interface.py b/libp2p/security/secure_transport_interface.py deleted file mode 100644 index 5fcf27329..000000000 --- a/libp2p/security/secure_transport_interface.py +++ /dev/null @@ -1,42 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) - -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) -from libp2p.peer.id import ( - ID, -) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) - -""" -Transport that is used to secure a connection. This transport is -chosen by a security transport multistream module. - -Relevant go repo: https://github.com/libp2p/go-conn-security/blob/master/interface.go -""" - - -class ISecureTransport(ABC): - @abstractmethod - async def secure_inbound(self, conn: IRawConnection) -> ISecureConn: - """ - Secure the connection, either locally or by communicating with opposing - node via conn, for an inbound connection (i.e. we are not the - initiator) - - :return: secure connection object (that implements secure_conn_interface) - """ - - @abstractmethod - async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureConn: - """ - Secure the connection, either locally or by communicating with opposing - node via conn, for an inbound connection (i.e. we are the initiator) - - :return: secure connection object (that implements secure_conn_interface) - """ diff --git a/libp2p/security/security_multistream.py b/libp2p/security/security_multistream.py index 3f708b995..924f45b08 100644 --- a/libp2p/security/security_multistream.py +++ b/libp2p/security/security_multistream.py @@ -5,12 +5,14 @@ OrderedDict, ) +from libp2p.abc import ( + IRawConnection, + ISecureConn, + ISecureTransport, +) from libp2p.custom_types import ( TProtocol, ) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) from libp2p.peer.id import ( ID, ) @@ -23,12 +25,6 @@ from libp2p.protocol_muxer.multiselect_communicator import ( MultiselectCommunicator, ) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) -from libp2p.security.secure_transport_interface import ( - ISecureTransport, -) from libp2p.transport.typing import ( TSecurityOptions, ) @@ -82,7 +78,8 @@ async def secure_inbound(self, conn: IRawConnection) -> ISecureConn: node via conn, for an inbound connection (i.e. we are not the initiator) - :return: secure connection object (that implements secure_conn_interface) + :return: secure connection object that implements ISecureConn (from libp2p.abc) + """ transport = await self.select_transport(conn, False) secure_conn = await transport.secure_inbound(conn) @@ -93,7 +90,8 @@ async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureCon Secure the connection, either locally or by communicating with opposing node via conn, for an inbound connection (i.e. we are the initiator) - :return: secure connection object (that implements secure_conn_interface) + :return: secure connection object that implements ISecureConn (from libp2p.abc) + """ transport = await self.select_transport(conn, True) secure_conn = await transport.secure_outbound(conn, peer_id) diff --git a/libp2p/stream_muxer/abc.py b/libp2p/stream_muxer/abc.py deleted file mode 100644 index a054bef66..000000000 --- a/libp2p/stream_muxer/abc.py +++ /dev/null @@ -1,85 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) - -import trio - -from libp2p.io.abc import ( - ReadWriteCloser, -) -from libp2p.peer.id import ( - ID, -) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) - - -class IMuxedConn(ABC): - """ - reference: https://github.com/libp2p/go-stream-muxer/blob/master/muxer.go - """ - - peer_id: ID - event_started: trio.Event - - @abstractmethod - def __init__(self, conn: ISecureConn, peer_id: ID) -> None: - """ - Create a new muxed connection. - - :param conn: an instance of secured connection - for new muxed streams - :param peer_id: peer_id of peer the connection is to - """ - - @property - @abstractmethod - def is_initiator(self) -> bool: - """If this connection is the initiator.""" - - @abstractmethod - async def start(self) -> None: - """Start the multiplexer.""" - - @abstractmethod - async def close(self) -> None: - """Close connection.""" - - @property - @abstractmethod - def is_closed(self) -> bool: - """ - Check connection is fully closed. - - :return: true if successful - """ - - @abstractmethod - async def open_stream(self) -> "IMuxedStream": - """ - Create a new muxed_stream. - - :return: a new ``IMuxedStream`` stream - """ - - @abstractmethod - async def accept_stream(self) -> "IMuxedStream": - """Accept a muxed stream opened by the other end.""" - - -class IMuxedStream(ReadWriteCloser): - muxed_conn: IMuxedConn - - @abstractmethod - async def reset(self) -> None: - """Close both ends of the stream tells this remote side to hang up.""" - - @abstractmethod - def set_deadline(self, ttl: int) -> bool: - """ - Set deadline for muxed stream. - - :return: a new stream - """ diff --git a/libp2p/stream_muxer/mplex/mplex.py b/libp2p/stream_muxer/mplex/mplex.py index b63b0dc7d..332a84ae8 100644 --- a/libp2p/stream_muxer/mplex/mplex.py +++ b/libp2p/stream_muxer/mplex/mplex.py @@ -5,6 +5,11 @@ import trio +from libp2p.abc import ( + IMuxedConn, + IMuxedStream, + ISecureConn, +) from libp2p.custom_types import ( TProtocol, ) @@ -20,13 +25,6 @@ from libp2p.peer.id import ( ID, ) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) -from libp2p.stream_muxer.abc import ( - IMuxedConn, - IMuxedStream, -) from libp2p.utils import ( decode_uvarint_from_stream, encode_uvarint, diff --git a/libp2p/stream_muxer/mplex/mplex_stream.py b/libp2p/stream_muxer/mplex/mplex_stream.py index 6714604c3..3026b824d 100644 --- a/libp2p/stream_muxer/mplex/mplex_stream.py +++ b/libp2p/stream_muxer/mplex/mplex_stream.py @@ -4,7 +4,7 @@ import trio -from libp2p.stream_muxer.abc import ( +from libp2p.abc import ( IMuxedStream, ) from libp2p.stream_muxer.exceptions import ( diff --git a/libp2p/stream_muxer/muxer_multistream.py b/libp2p/stream_muxer/muxer_multistream.py index 13e7e3015..f579dc538 100644 --- a/libp2p/stream_muxer/muxer_multistream.py +++ b/libp2p/stream_muxer/muxer_multistream.py @@ -2,12 +2,14 @@ OrderedDict, ) +from libp2p.abc import ( + IMuxedConn, + IRawConnection, + ISecureConn, +) from libp2p.custom_types import ( TProtocol, ) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) from libp2p.peer.id import ( ID, ) @@ -20,18 +22,11 @@ from libp2p.protocol_muxer.multiselect_communicator import ( MultiselectCommunicator, ) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) from libp2p.transport.typing import ( TMuxerClass, TMuxerOptions, ) -from .abc import ( - IMuxedConn, -) - # FIXME: add negotiate timeout to `MuxerMultistream` DEFAULT_NEGOTIATE_TIMEOUT = 60 diff --git a/libp2p/tools/factories.py b/libp2p/tools/factories.py index 795870a87..7bbdd6639 100644 --- a/libp2p/tools/factories.py +++ b/libp2p/tools/factories.py @@ -22,6 +22,15 @@ generate_new_rsa_identity, generate_peer_id_from, ) +from libp2p.abc import ( + IHost, + INetStream, + IPeerRouting, + IRawConnection, + ISecureConn, + ISecureTransport, + ReadWriteCloser, +) from libp2p.crypto.ed25519 import create_new_key_pair as create_ed25519_key_pair from libp2p.crypto.keys import ( KeyPair, @@ -34,27 +43,15 @@ from libp2p.host.basic_host import ( BasicHost, ) -from libp2p.host.host_interface import ( - IHost, -) from libp2p.host.routed_host import ( RoutedHost, ) -from libp2p.io.abc import ( - ReadWriteCloser, -) from libp2p.network.connection.raw_connection import ( RawConnection, ) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) from libp2p.network.connection.swarm_connection import ( SwarmConn, ) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) from libp2p.network.swarm import ( Swarm, ) @@ -81,9 +78,6 @@ Pubsub, get_peer_and_seqno_msg_id, ) -from libp2p.routing.interfaces import ( - IPeerRouting, -) from libp2p.security.insecure.transport import ( PLAINTEXT_PROTOCOL_ID, InsecureTransport, @@ -95,12 +89,6 @@ from libp2p.security.noise.transport import PROTOCOL_ID as NOISE_PROTOCOL_ID from libp2p.security.noise.transport import Transport as NoiseTransport import libp2p.security.secio.transport as secio -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) -from libp2p.security.secure_transport_interface import ( - ISecureTransport, -) from libp2p.stream_muxer.mplex.mplex import ( MPLEX_PROTOCOL_ID, Mplex, diff --git a/libp2p/tools/pubsub/dummy_account_node.py b/libp2p/tools/pubsub/dummy_account_node.py index 8c24e6ea6..17dab92d4 100644 --- a/libp2p/tools/pubsub/dummy_account_node.py +++ b/libp2p/tools/pubsub/dummy_account_node.py @@ -6,7 +6,7 @@ asynccontextmanager, ) -from libp2p.host.host_interface import ( +from libp2p.abc import ( IHost, ) from libp2p.pubsub.pubsub import ( diff --git a/libp2p/tools/pubsub/utils.py b/libp2p/tools/pubsub/utils.py index 6b1482d65..3437916a0 100644 --- a/libp2p/tools/pubsub/utils.py +++ b/libp2p/tools/pubsub/utils.py @@ -2,7 +2,7 @@ Sequence, ) -from libp2p.host.host_interface import ( +from libp2p.abc import ( IHost, ) from libp2p.peer.id import ( diff --git a/libp2p/tools/utils.py b/libp2p/tools/utils.py index ba9af8f9d..da3f66c10 100644 --- a/libp2p/tools/utils.py +++ b/libp2p/tools/utils.py @@ -5,15 +5,13 @@ Callable, ) -from libp2p.host.host_interface import ( +from libp2p.abc import ( IHost, + INetStream, ) from libp2p.network.stream.exceptions import ( StreamError, ) -from libp2p.network.stream.net_stream_interface import ( - INetStream, -) from libp2p.network.swarm import ( Swarm, ) diff --git a/libp2p/transport/listener_interface.py b/libp2p/transport/listener_interface.py deleted file mode 100644 index 8ca75f8e2..000000000 --- a/libp2p/transport/listener_interface.py +++ /dev/null @@ -1,32 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) - -from multiaddr import ( - Multiaddr, -) -import trio - - -class IListener(ABC): - @abstractmethod - async def listen(self, maddr: Multiaddr, nursery: trio.Nursery) -> bool: - """ - Put listener in listening mode and wait for incoming connections. - - :param maddr: multiaddr of peer - :return: return True if successful - """ - - @abstractmethod - def get_addrs(self) -> tuple[Multiaddr, ...]: - """ - Retrieve list of addresses the listener is listening on. - - :return: return list of addrs - """ - - @abstractmethod - async def close(self) -> None: - ... diff --git a/libp2p/transport/tcp/tcp.py b/libp2p/transport/tcp/tcp.py index aacaad550..ab421ea98 100644 --- a/libp2p/transport/tcp/tcp.py +++ b/libp2p/transport/tcp/tcp.py @@ -15,24 +15,20 @@ TaskStatus, ) +from libp2p.abc import ( + IListener, + IRawConnection, + ITransport, +) from libp2p.io.trio import ( TrioTCPStream, ) from libp2p.network.connection.raw_connection import ( RawConnection, ) -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) from libp2p.transport.exceptions import ( OpenConnectionError, ) -from libp2p.transport.listener_interface import ( - IListener, -) -from libp2p.transport.transport_interface import ( - ITransport, -) from libp2p.transport.typing import ( THandler, ) @@ -120,7 +116,8 @@ def create_listener(self, handler_function: THandler) -> TCPListener: :param handler_function: a function called when a new connection is received that takes a connection as argument which implements interface-connection - :return: a listener object that implements listener_interface.py + ::return: a listener object that implements IListener (from libp2p.abc) + """ return TCPListener(handler_function) diff --git a/libp2p/transport/transport_interface.py b/libp2p/transport/transport_interface.py deleted file mode 100644 index 32feab4a5..000000000 --- a/libp2p/transport/transport_interface.py +++ /dev/null @@ -1,41 +0,0 @@ -from abc import ( - ABC, - abstractmethod, -) - -from multiaddr import ( - Multiaddr, -) - -from libp2p.network.connection.raw_connection_interface import ( - IRawConnection, -) - -from .listener_interface import ( - IListener, -) -from .typing import ( - THandler, -) - - -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 - """ diff --git a/libp2p/transport/typing.py b/libp2p/transport/typing.py index 71d8c256f..b6e449e1e 100644 --- a/libp2p/transport/typing.py +++ b/libp2p/transport/typing.py @@ -6,17 +6,13 @@ Callable, ) -from libp2p.custom_types import ( - TProtocol, -) -from libp2p.io.abc import ( - ReadWriteCloser, -) -from libp2p.security.secure_transport_interface import ( +from libp2p.abc import ( + IMuxedConn, ISecureTransport, + ReadWriteCloser, ) -from libp2p.stream_muxer.abc import ( - IMuxedConn, +from libp2p.custom_types import ( + TProtocol, ) THandler = Callable[[ReadWriteCloser], Awaitable[None]] diff --git a/libp2p/transport/upgrader.py b/libp2p/transport/upgrader.py index 4203b7187..0e9f046ad 100644 --- a/libp2p/transport/upgrader.py +++ b/libp2p/transport/upgrader.py @@ -1,5 +1,9 @@ -from libp2p.network.connection.raw_connection_interface import ( +from libp2p.abc import ( + IListener, + IMuxedConn, IRawConnection, + ISecureConn, + ITransport, ) from libp2p.peer.id import ( ID, @@ -11,15 +15,9 @@ from libp2p.security.exceptions import ( HandshakeFailure, ) -from libp2p.security.secure_conn_interface import ( - ISecureConn, -) from libp2p.security.security_multistream import ( SecurityMultistream, ) -from libp2p.stream_muxer.abc import ( - IMuxedConn, -) from libp2p.stream_muxer.muxer_multistream import ( MuxerMultistream, ) @@ -32,13 +30,6 @@ TSecurityOptions, ) -from .listener_interface import ( - IListener, -) -from .transport_interface import ( - ITransport, -) - class TransportUpgrader: security_multistream: SecurityMultistream diff --git a/libp2p/utils.py b/libp2p/utils.py index 45111c232..9db241833 100644 --- a/libp2p/utils.py +++ b/libp2p/utils.py @@ -1,12 +1,12 @@ import itertools import math +from libp2p.abc import ( + Reader, +) from libp2p.exceptions import ( ParseError, ) -from libp2p.io.abc import ( - Reader, -) from .io.utils import ( read_exactly, diff --git a/tests/core/network/test_notify.py b/tests/core/network/test_notify.py index b9b7ed8e7..f74f3aec2 100644 --- a/tests/core/network/test_notify.py +++ b/tests/core/network/test_notify.py @@ -13,7 +13,7 @@ import pytest import trio -from libp2p.network.notifee_interface import ( +from libp2p.abc import ( INotifee, ) from libp2p.tools.async_service import ( diff --git a/tests/utils/interop/utils.py b/tests/utils/interop/utils.py index fe0997a0c..276dd6944 100644 --- a/tests/utils/interop/utils.py +++ b/tests/utils/interop/utils.py @@ -7,7 +7,7 @@ ) import trio -from libp2p.host.host_interface import ( +from libp2p.abc import ( IHost, ) from libp2p.peer.id import ( From fa28c4dc655f7e683693b691e35cab2512fb4bdb Mon Sep 17 00:00:00 2001 From: Khwahish Patel Date: Wed, 5 Feb 2025 15:40:58 +0530 Subject: [PATCH 2/2] refactored exports to abc.py --- libp2p/transport/typing.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libp2p/transport/typing.py b/libp2p/transport/typing.py index b6e449e1e..9f2361396 100644 --- a/libp2p/transport/typing.py +++ b/libp2p/transport/typing.py @@ -3,14 +3,17 @@ Mapping, ) from typing import ( + TYPE_CHECKING, Callable, ) -from libp2p.abc import ( - IMuxedConn, - ISecureTransport, - ReadWriteCloser, -) +if TYPE_CHECKING: + from libp2p.abc import ( + IMuxedConn, + ISecureTransport, + ReadWriteCloser, + ) + from libp2p.custom_types import ( TProtocol, )