You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This document describes the new transport integration system in py-libp2p, which enables dynamic transport selection and easy integration of new transport protocols.
Overview
The transport integration system provides a flexible, extensible architecture for managing different transport protocols in py-libp2p. It replaces the previous hardcoded transport selection with a dynamic registry-based approach.
Key Features
Dynamic Transport Selection: Automatically selects the appropriate transport based on multiaddr protocols
Transport Registry: Central registry for all transport implementations
WebSocket Support: Full support for /ws protocol, /wss in development
Extensible Architecture: Easy to add new transport protocols
Backward Compatibility: Existing TCP code continues to work unchanged
Architecture
Transport Registry
The TransportRegistry class manages the mapping between protocol identifiers and transport implementations:
fromlibp2p.transportimportget_transport_registryregistry=get_transport_registry()
# Register a new transportregistry.register_transport("quic", QUICTransport)
# Get a transport classtransport_class=registry.get_transport("tcp")
# Create a transport instancetransport=registry.create_transport("ws", upgrader)
Transport Factory
The create_transport_for_multiaddr() function automatically creates the appropriate transport based on a multiaddr:
Status: ❌ NOT YET IMPLEMENTED - Currently throws NotImplementedError("/wss (TLS) not yet supported")
Note: TLS support planned for future implementation
Usage Examples
Basic Usage
fromlibp2pimportnew_hostimportmultiaddr# Create host with WebSocket transporthost=new_host(listen_addrs=[
multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/8080/ws")
])
# Create host with TCP transporthost=new_host(listen_addrs=[
multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/8080")
])
Custom Transport Registration
fromlibp2p.transportimportregister_transportclassCustomTransport:
asyncdefdial(self, maddr):
# Implementationpassdefcreate_listener(self, handler):
# Implementationpass# Register the custom transportregister_transport("custom", CustomTransport)
# Now it can be used in multiaddrs# /ip4/127.0.0.1/custom/8080
Direct Transport Creation
fromlibp2p.transportimportcreate_transportfromlibp2p.transport.upgraderimportTransportUpgraderupgrader=TransportUpgrader({}, {})
# Create specific transport typestcp_transport=create_transport("tcp")
ws_transport=create_transport("ws", upgrader)
# wss_transport = create_transport("wss", upgrader) # Not yet implemented
Integration with libp2p
The new transport system integrates seamlessly with existing libp2p code:
Automatic Transport Selection
When you call new_host() or new_swarm() with listen_addrs, the system automatically:
Parses the multiaddr to identify the transport protocol
Creates the appropriate transport instance
Falls back to TCP if no specific transport is found
Provides clear error messages for unsupported protocols
No Breaking Changes
Existing code continues to work unchanged:
# This still works exactly as beforefromlibp2pimportnew_host# Automatically uses TCP transporthost=new_host()
# Automatically uses WebSocket transporthost=new_host(listen_addrs=["/ip4/127.0.0.1/tcp/8080/ws"])
# Now you can use /new_protocol in multiaddrsmaddr=Multiaddr("/ip4/127.0.0.1/new_protocol/8080")
Testing
The transport system includes comprehensive tests:
# Run transport registry tests
pytest tests/core/transport/test_transport_registry.py
# Run WebSocket transport tests
pytest tests/core/transport/test_websocket.py
# Run all transport tests
pytest tests/core/transport/
Demo Scripts
Transport Integration Demo
python examples/transport_integration_demo.py
This demonstrates:
Transport registry functionality
Dynamic transport creation
Multiaddr-based transport selection
Custom transport registration
WebSocket Demo
# Start WebSocket server
python examples/websocket/websocket_demo.py -p 8080
# Connect to server
python examples/websocket/websocket_demo.py -p 8081 -d /ip4/127.0.0.1/tcp/8080/ws/p2p/...
Error Handling
The system provides clear error messages for common issues:
# Unsupported protocoltry:
host=new_host(listen_addrs=["/ip4/127.0.0.1/udp/8080"])
exceptValueErrorase:
print(e) # "Unknown transport in listen_addrs: ['/ip4/127.0.0.1/udp/8080']. Supported protocols: ['tcp', 'ws']"# Missing upgrader for WebSockettry:
transport=create_transport("ws") # Missing upgraderexceptValueErrorase:
print(e) # "WebSocket transport requires an upgrader"
Performance Considerations
Lazy Loading: Transports are created only when needed
Registry Caching: Transport registry is cached globally
Minimal Overhead: Transport selection adds minimal runtime overhead
Memory Efficient: Transports are created on-demand and can be garbage collected
Future Enhancements
Planned improvements include:
TLS WebSocket Support: Implementation of /wss protocol (planned)
QUIC Transport: Native QUIC protocol support
Transport Metrics: Performance monitoring and metrics
Load Balancing: Multiple transport support for the same peer
Protocol Negotiation: Runtime protocol negotiation between peers
Troubleshooting
Common Issues
Transport Not Found: Ensure the transport is registered in the registry
Upgrader Required: WebSocket transports require a TransportUpgrader instance
Protocol Mismatch: Check that the multiaddr uses supported protocols
WSS Not Supported: /wss addresses currently throw NotImplementedError("/wss (TLS) not yet supported") - use /ws for now
Review the demo scripts for working implementations
Enable debug logging to trace transport selection
Check that all required dependencies are installed
Conclusion
The new transport integration system provides a robust, extensible foundation for py-libp2p transport protocols. It maintains backward compatibility while enabling easy integration of new transport types and automatic protocol selection based on multiaddrs.
The system is production-ready and provides a solid foundation for future transport protocol implementations.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Transport Integration System
This document describes the new transport integration system in py-libp2p, which enables dynamic transport selection and easy integration of new transport protocols.
Overview
The transport integration system provides a flexible, extensible architecture for managing different transport protocols in py-libp2p. It replaces the previous hardcoded transport selection with a dynamic registry-based approach.
Key Features
/ws
protocol,/wss
in developmentArchitecture
Transport Registry
The
TransportRegistry
class manages the mapping between protocol identifiers and transport implementations:Transport Factory
The
create_transport_for_multiaddr()
function automatically creates the appropriate transport based on a multiaddr:Supported Protocols
TCP Transport (
/tcp
)/tcp
/ip4/127.0.0.1/tcp/8080
libp2p.transport.tcp.tcp.TCP
WebSocket Transport (
/ws
)/ws
/ip4/127.0.0.1/tcp/8080/ws
libp2p.transport.websocket.transport.WebsocketTransport
TransportUpgrader
WebSocket Secure Transport (
/wss
)/wss
/ip4/127.0.0.1/tcp/8080/wss
libp2p.transport.websocket.transport.WebsocketTransport
TransportUpgrader
NotImplementedError("/wss (TLS) not yet supported")
Usage Examples
Basic Usage
Custom Transport Registration
Direct Transport Creation
Integration with libp2p
The new transport system integrates seamlessly with existing libp2p code:
Automatic Transport Selection
When you call
new_host()
ornew_swarm()
withlisten_addrs
, the system automatically:No Breaking Changes
Existing code continues to work unchanged:
Adding New Transport Protocols
To add a new transport protocol:
1. Implement the Transport Interface
2. Register the Transport
3. Use in Multiaddrs
Testing
The transport system includes comprehensive tests:
Demo Scripts
Transport Integration Demo
This demonstrates:
WebSocket Demo
Error Handling
The system provides clear error messages for common issues:
Performance Considerations
Future Enhancements
Planned improvements include:
/wss
protocol (planned)Troubleshooting
Common Issues
TransportUpgrader
instance/wss
addresses currently throwNotImplementedError("/wss (TLS) not yet supported")
- use/ws
for nowDebug Mode
Enable debug logging to see transport selection:
Getting Help
Conclusion
The new transport integration system provides a robust, extensible foundation for py-libp2p transport protocols. It maintains backward compatibility while enabling easy integration of new transport types and automatic protocol selection based on multiaddrs.
The system is production-ready and provides a solid foundation for future transport protocol implementations.
Beta Was this translation helpful? Give feedback.
All reactions