Replies: 3 comments 1 reply
-
I will start working on this this week and also open up issues on it.
|
Beta Was this translation helpful? Give feedback.
-
Libp2p Notification System: Use Cases & Core Module IntegrationOverviewThe notification system in py-libp2p is the nervous system of the peer-to-peer network, allowing different components to stay synchronized with network state changes without direct dependencies. It uses an event-driven architecture where components can register as "notifees" to receive notifications about network lifecycle events. Core Notification EventsThe system provides 6 key notification events:
Core Modules Using the Notification System1. Swarm (Core Network Layer)File: Role: Central orchestrator of the notification system What it does:
Key methods: def register_notifee(self, notifee: INotifee) -> None
async def notify_opened_stream(self, stream: INetStream) -> None
async def notify_connected(self, conn: INetConn) -> None
async def notify_disconnected(self, conn: INetConn) -> None
async def notify_listen(self, multiaddr: Multiaddr) -> None
async def notify_listen_close(self, multiaddr: Multiaddr) -> None
async def notify_closed_stream(self, stream: INetStream) -> None Integration points:
2. PubSub (Publish/Subscribe System)Files: Role: Primary consumer of the notification system What it does:
Key implementation: # In Pubsub.__init__():
self.host.get_network().register_notifee(
PubsubNotifee(peer_send, dead_peer_send)
) Event handling:
Use case: Automatic peer discovery and management - When peers connect/disconnect, pubsub automatically creates or tears down the appropriate pubsub streams without manual intervention. 3. SwarmConnection (Connection Management)File: Role: Handles individual connection lifecycle What it does:
Key methods: async def _add_stream(self, muxed_stream: IMuxedStream) -> NetStream:
net_stream = NetStream(muxed_stream)
self.streams.add(net_stream)
await self.swarm.notify_opened_stream(net_stream) # ← Notification here
return net_stream
async def _notify_disconnected(self) -> None:
await self.swarm.notify_disconnected(self) # ← Notification here 4. NetStream (Stream Management)File: Role: Manages individual stream lifecycle What it does:
Key implementation: async def _notify_closed(self) -> None:
async with self._notify_lock:
if hasattr(self.muxed_conn, "swarm"):
swarm = getattr(self.muxed_conn, "swarm")
if hasattr(swarm, "notify_all"):
await swarm.notify_all(
lambda notifiee: notifiee.closed_stream(swarm, self)
) 5. BasicHost (Host Layer)File: Role: Provides access to the notification system What it does:
Practical Use Cases1. Peer Discovery and ManagementScenario: New peer connects to the network
2. Resource ManagementScenario: Stream is closed
3. Listener Lifecycle ManagementScenario: Host starts/stops listening
4. Network Partition HandlingScenario: Connection drops unexpectedly
5. Debugging and MonitoringScenario: Network state tracking
Benefits of the Notification System1. Decoupled Architecture
2. Event-Driven Design
3. Resource Management
4. Extensibility
Integration Patterns1. Registration Pattern# Component registers with swarm
swarm.register_notifee(MyNotifee()) 2. Event Handling Patternclass MyNotifee(INotifee):
async def connected(self, network: INetwork, conn: INetConn) -> None:
# Handle new connection
pass
async def disconnected(self, network: INetwork, conn: INetConn) -> None:
# Handle connection loss
pass 3. Queue-Based Processing# PubSub uses queues for async processing
peer_send, peer_receive = trio.open_memory_channel[ID](0)
dead_peer_send, dead_peer_receive = trio.open_memory_channel[ID](0) Future ExtensionsThe notification system is designed to be extensible. Potential future additions could include:
ConclusionThe notification system is fundamental to py-libp2p's architecture, providing a robust, scalable way for components to stay synchronized with network state changes. It enables the complex peer-to-peer networking functionality while maintaining clean, decoupled code architecture. |
Beta Was this translation helpful? Give feedback.
-
@acul71 : Great, thank you so much Luca for the wonderful efforts and this great write-up. Appreciate your initiative. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
TODO Analysis: Test Notify Issues
Overview
This document analyzes three TODO issues in the test_notify.py module:
tests/core/network/test_notify.py:8
tests/core/network/test_notify.py:47
tests/core/network/test_notify.py:60
Issue 1: Missing Tests for Closed Stream and Listen Close
Issue Location
File:
tests/core/network/test_notify.py:8
TODO Comment:
TODO: Add tests for closed_stream, listen_close when those features are implemented in swarm
Current Status Analysis
Current Implementation
The test file has commented-out assertions for the unimplemented features:
The Problem
What Must Be Done
Required Changes
Implementation Options
Option A: Basic Test Implementation
Option B: Comprehensive Test Suite
Option C: Edge Case Testing
Impact Analysis
Issue 2: Closed Stream Method Not Implemented
Issue Location
File:
tests/core/network/test_notify.py:47
Method:
closed_stream()
inMyNotifee
classTODO Comment:
# TODO: It is not implemented yet.
Current Status Analysis
Current Implementation
The
closed_stream
method is a stub that does nothing:The Problem
What Must Be Done
Required Changes
Implementation Options
Option A: Simple Implementation
Option B: Enhanced Implementation with Validation
Option C: Implementation with Event Metadata
Impact Analysis
Issue 3: Listen Close Method Not Implemented
Issue Location
File:
tests/core/network/test_notify.py:60
Method:
listen_close()
inMyNotifee
classTODO Comment:
# TODO: It is not implemented yet.
Current Status Analysis
Current Implementation
The
listen_close
method is a stub that does nothing:The Problem
What Must Be Done
Required Changes
Implementation Options
Option A: Simple Implementation
Option B: Enhanced Implementation with Validation
Option C: Implementation with Event Metadata
Impact Analysis
Swarm Implementation Requirements
Current Swarm Status
The swarm implementation has stub methods for the missing notifications:
Required Swarm Changes
1. Implement notify_closed_stream
2. Implement notify_listen_close
3. Integration Points
Stream Closure Integration:
Listener Closure Integration:
Summary and Recommendations
Priority Order
High Priority: Implement closed_stream method in MyNotifee (Issue 2)
High Priority: Implement listen_close method in MyNotifee (Issue 3)
Medium Priority: Add comprehensive tests for closed_stream and listen_close (Issue 1)
Implementation Strategy
Testing Requirements
Documentation Needs
Backward Compatibility
Beta Was this translation helpful? Give feedback.
All reactions