fix: add echo example

This commit is contained in:
Akash Mondal
2025-06-16 18:22:54 +00:00
committed by lla-dane
parent 94d920f365
commit ac01cc5038
8 changed files with 894 additions and 412 deletions

153
examples/echo/echo_quic.py Normal file
View File

@ -0,0 +1,153 @@
#!/usr/bin/env python3
"""
QUIC Echo Example - Direct replacement for examples/echo/echo.py
This program demonstrates a simple echo protocol using QUIC transport where a peer
listens for connections and copies back any input received on a stream.
Modified from the original TCP version to use QUIC transport, providing:
- Built-in TLS security
- Native stream multiplexing
- Better performance over UDP
- Modern QUIC protocol features
"""
import argparse
import multiaddr
import trio
from libp2p import new_host
from libp2p.crypto.secp256k1 import create_new_key_pair
from libp2p.custom_types import TProtocol
from libp2p.network.stream.net_stream import INetStream
from libp2p.peer.peerinfo import info_from_p2p_addr
from libp2p.transport.quic.config import QUICTransportConfig
PROTOCOL_ID = TProtocol("/echo/1.0.0")
async def _echo_stream_handler(stream: INetStream) -> None:
"""
Echo stream handler - unchanged from TCP version.
Demonstrates transport abstraction: same handler works for both TCP and QUIC.
"""
# Wait until EOF
msg = await stream.read()
await stream.write(msg)
await stream.close()
async def run(port: int, destination: str, seed: int | None = None) -> None:
"""
Run echo server or client with QUIC transport.
Key changes from TCP version:
1. UDP multiaddr instead of TCP
2. QUIC transport configuration
3. Everything else remains the same!
"""
# CHANGED: UDP + QUIC instead of TCP
listen_addr = multiaddr.Multiaddr(f"/ip4/0.0.0.0/udp/{port}/quic")
if seed:
import random
random.seed(seed)
secret_number = random.getrandbits(32 * 8)
secret = secret_number.to_bytes(length=32, byteorder="big")
else:
import secrets
secret = secrets.token_bytes(32)
# NEW: QUIC transport configuration
quic_config = QUICTransportConfig(
idle_timeout=30.0,
max_concurrent_streams=1000,
connection_timeout=10.0,
)
# CHANGED: Add QUIC transport options
host = new_host(
key_pair=create_new_key_pair(secret),
transport_opt={"quic_config": quic_config},
)
async with host.run(listen_addrs=[listen_addr]):
print(f"I am {host.get_id().to_string()}")
if not destination: # Server mode
host.set_stream_handler(PROTOCOL_ID, _echo_stream_handler)
print(
"Run this from the same folder in another console:\n\n"
f"python3 ./examples/echo/echo_quic.py "
f"-d {host.get_addrs()[0]}\n"
)
print("Waiting for incoming QUIC connections...")
await trio.sleep_forever()
else: # Client mode
maddr = multiaddr.Multiaddr(destination)
info = info_from_p2p_addr(maddr)
# Associate the peer with local ip address
await host.connect(info)
# Start a stream with the destination.
# Multiaddress of the destination peer is fetched from the peerstore
# using 'peerId'.
stream = await host.new_stream(info.peer_id, [PROTOCOL_ID])
msg = b"hi, there!\n"
await stream.write(msg)
# Notify the other side about EOF
await stream.close()
response = await stream.read()
print(f"Sent: {msg.decode('utf-8')}")
print(f"Got: {response.decode('utf-8')}")
def main() -> None:
"""Main function - help text updated for QUIC."""
description = """
This program demonstrates a simple echo protocol using QUIC
transport where a peer listens for connections and copies back
any input received on a stream.
QUIC provides built-in TLS security and stream multiplexing over UDP.
To use it, first run 'python ./echo.py -p <PORT>', where <PORT> is
the UDP port number.Then, run another host with ,
'python ./echo.py -p <ANOTHER_PORT> -d <DESTINATION>'
where <DESTINATION> is the QUIC multiaddress of the previous listener host.
"""
example_maddr = "/ip4/127.0.0.1/udp/8000/quic/p2p/QmQn4SwGkDZKkUEpBRBv"
parser = argparse.ArgumentParser(description=description)
parser.add_argument("-p", "--port", default=8000, type=int, help="UDP port number")
parser.add_argument(
"-d",
"--destination",
type=str,
help=f"destination multiaddr string, e.g. {example_maddr}",
)
parser.add_argument(
"-s",
"--seed",
type=int,
help="provide a seed to the random number generator",
)
args = parser.parse_args()
try:
trio.run(run, args.port, args.destination, args.seed)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()

View File

@ -1,3 +1,7 @@
from libp2p.transport.quic.utils import is_quic_multiaddr
from typing import Any
from libp2p.transport.quic.transport import QUICTransport
from libp2p.transport.quic.config import QUICTransportConfig
from collections.abc import (
Mapping,
Sequence,
@ -5,16 +9,12 @@ from collections.abc import (
from importlib.metadata import version as __version
from typing import (
Literal,
Optional,
Type,
cast,
)
import multiaddr
from libp2p.abc import (
IHost,
IMuxedConn,
INetworkService,
IPeerRouting,
IPeerStore,
@ -163,6 +163,7 @@ def new_swarm(
peerstore_opt: IPeerStore | None = None,
muxer_preference: Literal["YAMUX", "MPLEX"] | None = None,
listen_addrs: Sequence[multiaddr.Multiaddr] | None = None,
transport_opt: dict[Any, Any] | None = None,
) -> INetworkService:
"""
Create a swarm instance based on the parameters.
@ -173,6 +174,7 @@ def new_swarm(
:param peerstore_opt: optional peerstore
:param muxer_preference: optional explicit muxer preference
:param listen_addrs: optional list of multiaddrs to listen on
:param transport_opt: options for transport
:return: return a default swarm instance
Note: Yamux (/yamux/1.0.0) is the preferred stream multiplexer
@ -185,14 +187,24 @@ def new_swarm(
id_opt = generate_peer_id_from(key_pair)
transport: TCP | QUICTransport
if listen_addrs is None:
transport = TCP()
transport_opt = transport_opt or {}
quic_config: QUICTransportConfig | None = transport_opt.get('quic_config')
if quic_config:
transport = QUICTransport(key_pair.private_key, quic_config)
else:
transport = TCP()
else:
addr = listen_addrs[0]
if addr.__contains__("tcp"):
transport = TCP()
elif addr.__contains__("quic"):
raise ValueError("QUIC not yet supported")
transport_opt = transport_opt or {}
quic_config = transport_opt.get('quic_config', QUICTransportConfig())
transport = QUICTransport(key_pair.private_key, quic_config)
else:
raise ValueError(f"Unknown transport in listen_addrs: {listen_addrs}")
@ -253,6 +265,7 @@ def new_host(
enable_mDNS: bool = False,
bootstrap: list[str] | None = None,
negotiate_timeout: int = DEFAULT_NEGOTIATE_TIMEOUT,
transport_opt: dict[Any, Any] | None = None,
) -> IHost:
"""
Create a new libp2p host based on the given parameters.
@ -266,8 +279,10 @@ def new_host(
:param listen_addrs: optional list of multiaddrs to listen on
:param enable_mDNS: whether to enable mDNS discovery
:param bootstrap: optional list of bootstrap peer addresses as strings
:param transport_opt: optional dictionary of properties of transport
:return: return a host instance
"""
print("INIT")
swarm = new_swarm(
key_pair=key_pair,
muxer_opt=muxer_opt,
@ -275,6 +290,7 @@ def new_host(
peerstore_opt=peerstore_opt,
muxer_preference=muxer_preference,
listen_addrs=listen_addrs,
transport_opt=transport_opt
)
if disc_opt is not None:

View File

@ -170,14 +170,7 @@ class Swarm(Service, INetworkService):
async def dial_addr(self, addr: Multiaddr, peer_id: ID) -> INetConn:
"""
Try to create a connection to peer_id with addr.
:param addr: the address we want to connect with
:param peer_id: the peer we want to connect to
:raises SwarmException: raised when an error occurs
:return: network connection
"""
# Dial peer (connection to peer does not yet exist)
# Transport dials peer (gets back a raw conn)
try:
raw_conn = await self.transport.dial(addr)
except OpenConnectionError as error:
@ -188,8 +181,15 @@ class Swarm(Service, INetworkService):
logger.debug("dialed peer %s over base transport", peer_id)
# Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
# the conn and then mux the conn
# NEW: Check if this is a QUIC connection (already secure and muxed)
if isinstance(raw_conn, IMuxedConn):
# QUIC connections are already secure and muxed, skip upgrade steps
logger.debug("detected QUIC connection, skipping upgrade steps")
swarm_conn = await self.add_conn(raw_conn)
logger.debug("successfully dialed peer %s via QUIC", peer_id)
return swarm_conn
# Standard TCP flow - security then mux upgrade
try:
secured_conn = await self.upgrader.upgrade_security(raw_conn, True, peer_id)
except SecurityUpgradeFailure as error:
@ -211,9 +211,7 @@ class Swarm(Service, INetworkService):
logger.debug("upgraded mux for peer %s", peer_id)
swarm_conn = await self.add_conn(muxed_conn)
logger.debug("successfully dialed peer %s", peer_id)
return swarm_conn
async def new_stream(self, peer_id: ID) -> INetStream:

View File

@ -34,6 +34,11 @@ if TYPE_CHECKING:
from .security import QUICTLSConfigManager
from .transport import QUICTransport
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler()],
)
logger = logging.getLogger(__name__)
@ -286,11 +291,13 @@ class QUICConnection(IRawConnection, IMuxedConn):
try:
with QUICErrorContext("connection_establishment", "connection"):
# Start the connection if not already started
print("STARTING TO CONNECT")
if not self._started:
await self.start()
# Start background event processing
if not self._background_tasks_started:
print("STARTING BACKGROUND TASK")
await self._start_background_tasks()
# Wait for handshake completion with timeout
@ -324,16 +331,17 @@ class QUICConnection(IRawConnection, IMuxedConn):
self._background_tasks_started = True
# Start event processing task
self._nursery.start_soon(self._event_processing_loop)
self._nursery.start_soon(async_fn=self._event_processing_loop)
# Start periodic tasks
self._nursery.start_soon(self._periodic_maintenance)
# self._nursery.start_soon(async_fn=self._periodic_maintenance)
logger.debug("Started background tasks for QUIC connection")
async def _event_processing_loop(self) -> None:
"""Main event processing loop for the connection."""
logger.debug("Started QUIC event processing loop")
print("Started QUIC event processing loop")
try:
while not self._closed:
@ -347,7 +355,7 @@ class QUICConnection(IRawConnection, IMuxedConn):
await self._transmit()
# Short sleep to prevent busy waiting
await trio.sleep(0.001) # 1ms
await trio.sleep(0.01)
except Exception as e:
logger.error(f"Error in event processing loop: {e}")
@ -381,6 +389,7 @@ class QUICConnection(IRawConnection, IMuxedConn):
QUICPeerVerificationError: If peer verification fails
"""
print("VERIFYING PEER IDENTITY")
if not self._security_manager:
logger.warning("No security manager available for peer verification")
return
@ -719,6 +728,7 @@ class QUICConnection(IRawConnection, IMuxedConn):
async def _handle_quic_event(self, event: events.QuicEvent) -> None:
"""Handle a single QUIC event."""
print(f"QUIC event: {type(event).__name__}")
if isinstance(event, events.ConnectionTerminated):
await self._handle_connection_terminated(event)
elif isinstance(event, events.HandshakeCompleted):
@ -731,6 +741,7 @@ class QUICConnection(IRawConnection, IMuxedConn):
await self._handle_datagram_received(event)
else:
logger.debug(f"Unhandled QUIC event: {type(event).__name__}")
print(f"Unhandled QUIC event: {type(event).__name__}")
async def _handle_handshake_completed(
self, event: events.HandshakeCompleted
@ -897,6 +908,7 @@ class QUICConnection(IRawConnection, IMuxedConn):
"""Send pending datagrams using trio."""
sock = self._socket
if not sock:
print("No socket to transmit")
return
try:

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@ from aioquic.quic.configuration import (
QuicConfiguration,
)
from aioquic.quic.connection import (
QuicConnection,
QuicConnection as NativeQUICConnection,
)
import multiaddr
import trio
@ -60,6 +60,11 @@ from .security import (
QUIC_V1_PROTOCOL = QUICTransportConfig.PROTOCOL_QUIC_V1
QUIC_DRAFT29_PROTOCOL = QUICTransportConfig.PROTOCOL_QUIC_DRAFT29
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler()],
)
logger = logging.getLogger(__name__)
@ -279,20 +284,24 @@ class QUICTransport(ITransport):
# Get appropriate QUIC client configuration
config_key = TProtocol(f"{quic_version}_client")
print("config_key", config_key, self._quic_configs.keys())
config = self._quic_configs.get(config_key)
if not config:
raise QUICDialError(f"Unsupported QUIC version: {quic_version}")
config.is_client = True
logger.debug(
f"Dialing QUIC connection to {host}:{port} (version: {quic_version})"
)
print("Start QUIC Connection")
# Create QUIC connection using aioquic's sans-IO core
quic_connection = QuicConnection(configuration=config)
native_quic_connection = NativeQUICConnection(configuration=config)
print("QUIC Connection Created")
# Create trio-based QUIC connection wrapper with security
connection = QUICConnection(
quic_connection=quic_connection,
quic_connection=native_quic_connection,
remote_addr=(host, port),
peer_id=peer_id,
local_peer_id=self._peer_id,
@ -354,6 +363,7 @@ class QUICTransport(ITransport):
)
logger.info(f"Peer identity verified: {verified_peer_id}")
print(f"Peer identity verified: {verified_peer_id}")
except Exception as e:
raise QUICSecurityError(f"Peer identity verification failed: {e}") from e

View File

@ -5,14 +5,19 @@ Based on go-libp2p and js-libp2p QUIC implementations.
"""
import ipaddress
import logging
from aioquic.quic.configuration import QuicConfiguration
import multiaddr
from libp2p.custom_types import TProtocol
from libp2p.transport.quic.security import QUICTLSConfigManager
from .config import QUICTransportConfig
from .exceptions import QUICInvalidMultiaddrError, QUICUnsupportedVersionError
logger = logging.getLogger(__name__)
# Protocol constants
QUIC_V1_PROTOCOL = QUICTransportConfig.PROTOCOL_QUIC_V1
QUIC_DRAFT29_PROTOCOL = QUICTransportConfig.PROTOCOL_QUIC_DRAFT29
@ -20,6 +25,18 @@ UDP_PROTOCOL = "udp"
IP4_PROTOCOL = "ip4"
IP6_PROTOCOL = "ip6"
SERVER_CONFIG_PROTOCOL_V1 = f"{QUIC_V1_PROTOCOL}_SERVER"
SERVER_CONFIG_PROTOCOL_DRAFT_29 = f"{QUIC_V1_PROTOCOL}_SERVER"
CLIENT_CONFIG_PROTCOL_V1 = f"{QUIC_DRAFT29_PROTOCOL}_SERVER"
CLIENT_CONFIG_PROTOCOL_DRAFT_29 = f"{QUIC_DRAFT29_PROTOCOL}_SERVER"
CUSTOM_QUIC_VERSION_MAPPING = {
SERVER_CONFIG_PROTOCOL_V1: 0x00000001, # RFC 9000
CLIENT_CONFIG_PROTCOL_V1: 0x00000001, # RFC 9000
SERVER_CONFIG_PROTOCOL_DRAFT_29: 0xFF00001D, # draft-29
CLIENT_CONFIG_PROTOCOL_DRAFT_29: 0xFF00001D, # draft-29
}
# QUIC version to wire format mappings (required for aioquic)
QUIC_VERSION_MAPPINGS = {
QUIC_V1_PROTOCOL: 0x00000001, # RFC 9000
@ -218,6 +235,27 @@ def quic_version_to_wire_format(version: TProtocol) -> int:
return wire_version
def custom_quic_version_to_wire_format(version: TProtocol) -> int:
"""
Convert QUIC version string to wire format integer for aioquic.
Args:
version: QUIC version string ("quic-v1" or "quic")
Returns:
Wire format version number
Raises:
QUICUnsupportedVersionError: If version is not supported
"""
wire_version = QUIC_VERSION_MAPPINGS.get(version)
if wire_version is None:
raise QUICUnsupportedVersionError(f"Unsupported QUIC version: {version}")
return wire_version
def get_alpn_protocols() -> list[str]:
"""
Get ALPN protocols for libp2p over QUIC.
@ -250,3 +288,94 @@ def normalize_quic_multiaddr(maddr: multiaddr.Multiaddr) -> multiaddr.Multiaddr:
version = multiaddr_to_quic_version(maddr)
return create_quic_multiaddr(host, port, version)
def create_server_config_from_base(
base_config: QuicConfiguration,
security_manager: QUICTLSConfigManager | None = None,
transport_config: QUICTransportConfig | None = None,
) -> QuicConfiguration:
"""
Create a server configuration without using deepcopy.
Manually copies attributes while handling cryptography objects properly.
"""
try:
# Create new server configuration from scratch
server_config = QuicConfiguration(is_client=False)
# Copy basic configuration attributes (these are safe to copy)
copyable_attrs = [
"alpn_protocols",
"verify_mode",
"max_datagram_frame_size",
"idle_timeout",
"max_concurrent_streams",
"supported_versions",
"max_data",
"max_stream_data",
"stateless_retry",
"quantum_readiness_test",
]
for attr in copyable_attrs:
if hasattr(base_config, attr):
value = getattr(base_config, attr)
if value is not None:
setattr(server_config, attr, value)
# Handle cryptography objects - these need direct reference, not copying
crypto_attrs = [
"certificate",
"private_key",
"certificate_chain",
"ca_certs",
]
for attr in crypto_attrs:
if hasattr(base_config, attr):
value = getattr(base_config, attr)
if value is not None:
setattr(server_config, attr, value)
# Apply security manager configuration if available
if security_manager:
try:
server_tls_config = security_manager.create_server_config()
# Override with security manager's TLS configuration
if "certificate" in server_tls_config:
server_config.certificate = server_tls_config["certificate"]
if "private_key" in server_tls_config:
server_config.private_key = server_tls_config["private_key"]
if "certificate_chain" in server_tls_config:
# type: ignore
server_config.certificate_chain = server_tls_config[ # type: ignore
"certificate_chain" # type: ignore
]
if "alpn_protocols" in server_tls_config:
# type: ignore
server_config.alpn_protocols = server_tls_config["alpn_protocols"] # type: ignore
except Exception as e:
logger.warning(f"Failed to apply security manager config: {e}")
# Set transport-specific defaults if provided
if transport_config:
if server_config.idle_timeout == 0:
server_config.idle_timeout = getattr(
transport_config, "idle_timeout", 30.0
)
if server_config.max_datagram_frame_size is None:
server_config.max_datagram_frame_size = getattr(
transport_config, "max_datagram_size", 1200
)
# Ensure we have ALPN protocols
if server_config.alpn_protocols:
server_config.alpn_protocols = ["libp2p"]
logger.debug("Successfully created server config without deepcopy")
return server_config
except Exception as e:
logger.error(f"Failed to create server config: {e}")
raise

View File

@ -183,10 +183,13 @@ def test_new_swarm_tcp_multiaddr_supported():
assert isinstance(swarm.transport, TCP)
def test_new_swarm_quic_multiaddr_raises():
def test_new_swarm_quic_multiaddr_supported():
from libp2p.transport.quic.transport import QUICTransport
addr = Multiaddr("/ip4/127.0.0.1/udp/9999/quic")
with pytest.raises(ValueError, match="QUIC not yet supported"):
new_swarm(listen_addrs=[addr])
swarm = new_swarm(listen_addrs=[addr])
assert isinstance(swarm, Swarm)
assert isinstance(swarm.transport, QUICTransport)
@pytest.mark.trio