diff --git a/libp2p/io/msgio.py b/libp2p/io/msgio.py index 38710ff..837f642 100644 --- a/libp2p/io/msgio.py +++ b/libp2p/io/msgio.py @@ -5,7 +5,7 @@ from that repo: "a simple package to r/w length-delimited slices." NOTE: currently missing the capability to indicate lengths by "varint" method. """ -# TODO unify w/ https://github.com/libp2p/py-libp2p/blob/1aed52856f56a4b791696bbcbac31b5f9c2e88c9/libp2p/utils.py#L85-L99 # noqa: E501 + from typing import Optional from libp2p.io.abc import MsgReadWriteCloser, Reader, ReadWriteCloser diff --git a/libp2p/security/insecure/io.py b/libp2p/security/insecure/io.py deleted file mode 100644 index 1cbff36..0000000 --- a/libp2p/security/insecure/io.py +++ /dev/null @@ -1,19 +0,0 @@ -from libp2p.io.abc import MsgReadWriteCloser, ReadWriteCloser -from libp2p.utils import encode_fixedint_prefixed, read_fixedint_prefixed - - -class PlaintextHandshakeReadWriter(MsgReadWriteCloser): - conn: ReadWriteCloser - - def __init__(self, conn: ReadWriteCloser) -> None: - self.conn = conn - - async def read_msg(self) -> bytes: - return await read_fixedint_prefixed(self.conn) - - async def write_msg(self, msg: bytes) -> None: - encoded_msg_bytes = encode_fixedint_prefixed(msg) - await self.conn.write(encoded_msg_bytes) - - async def close(self) -> None: - await self.conn.close() diff --git a/libp2p/security/insecure/transport.py b/libp2p/security/insecure/transport.py index 28e7641..052d342 100644 --- a/libp2p/security/insecure/transport.py +++ b/libp2p/security/insecure/transport.py @@ -5,6 +5,7 @@ from libp2p.crypto.keys import PrivateKey, PublicKey from libp2p.crypto.pb import crypto_pb2 from libp2p.crypto.serialization import deserialize_public_key from libp2p.io.abc import ReadWriteCloser +from libp2p.io.msgio import BaseMsgReadWriter from libp2p.network.connection.exceptions import RawConnError from libp2p.network.connection.raw_connection_interface import IRawConnection from libp2p.peer.id import ID @@ -14,7 +15,6 @@ from libp2p.security.exceptions import HandshakeFailure from libp2p.security.secure_conn_interface import ISecureConn from libp2p.typing import TProtocol -from .io import PlaintextHandshakeReadWriter from .pb import plaintext_pb2 # Reference: https://github.com/libp2p/go-libp2p-core/blob/master/sec/insecure/insecure.go @@ -22,6 +22,12 @@ from .pb import plaintext_pb2 PLAINTEXT_PROTOCOL_ID = TProtocol("/plaintext/2.0.0") +SIZE_PLAINTEXT_LEN_BYTES = 4 + + +class PlaintextHandshakeReadWriter(BaseMsgReadWriter): + size_len_bytes = SIZE_PLAINTEXT_LEN_BYTES + class InsecureSession(BaseSession): def __init__( diff --git a/libp2p/utils.py b/libp2p/utils.py index 3d0794a..6493782 100644 --- a/libp2p/utils.py +++ b/libp2p/utils.py @@ -78,20 +78,3 @@ async def read_delim(reader: Reader) -> bytes: f'`msg_bytes` is not delimited by b"\\n": `msg_bytes`={msg_bytes!r}' ) return msg_bytes[:-1] - - -SIZE_LEN_BYTES = 4 - -# Fixed-prefixed read/write, used by "/plaintext/2.0.0". -# Reference: https://github.com/libp2p/go-msgio/blob/d5bbf59d3c4240266b1d2e5df9dc993454c42011/num.go#L11-L33 # noqa: E501 # noqa: E501 - - -def encode_fixedint_prefixed(msg_bytes: bytes) -> bytes: - len_prefix = len(msg_bytes).to_bytes(SIZE_LEN_BYTES, "big") - return len_prefix + msg_bytes - - -async def read_fixedint_prefixed(reader: Reader) -> bytes: - len_bytes = await reader.read(SIZE_LEN_BYTES) - len_int = int.from_bytes(len_bytes, "big") - return await reader.read(len_int)