libp2p.network package
Subpackages
- libp2p.network.connection package
- libp2p.network.stream package
Submodules
libp2p.network.exceptions module
- exception libp2p.network.exceptions.SwarmException
Bases:
BaseLibp2pError
libp2p.network.network_interface module
- class libp2p.network.network_interface.INetwork
Bases:
ABC
- abstract async dial_peer(peer_id: ID) INetConn
dial_peer try to create a connection to peer_id.
- Parameters:
peer_id – peer if we want to dial
- Raises:
SwarmException – raised when an error occurs
- Returns:
muxed connection
- abstract async listen(*multiaddrs: Sequence[Multiaddr]) bool
- Parameters:
multiaddrs – one or many multiaddrs to start listening on
- Returns:
True if at least one success
- abstract async new_stream(peer_id: ID) INetStream
- Parameters:
peer_id – peer_id of destination
protocol_ids – available protocol ids to use for stream
- Returns:
net stream instance
- peerstore: IPeerStore
libp2p.network.notifee_interface module
- class libp2p.network.notifee_interface.INotifee
Bases:
ABC
- abstract async closed_stream(network: INetwork, stream: INetStream) None
- Parameters:
network – network the stream was closed on
stream – stream that was closed
- abstract async connected(network: INetwork, conn: INetConn) None
- Parameters:
network – network the connection was opened on
conn – connection that was opened
- abstract async disconnected(network: INetwork, conn: INetConn) None
- Parameters:
network – network the connection was closed on
conn – connection that was closed
- abstract async listen(network: INetwork, multiaddr: Multiaddr) None
- Parameters:
network – network the listener is listening on
multiaddr – multiaddress listener is listening on
- abstract async listen_close(network: INetwork, multiaddr: Multiaddr) None
- Parameters:
network – network the connection was opened on
multiaddr – multiaddress listener is no longer listening on
- abstract async opened_stream(network: INetwork, stream: INetStream) None
- Parameters:
network – network the stream was opened on
stream – stream that was opened
libp2p.network.swarm module
- class libp2p.network.swarm.Swarm(peer_id: ID, peerstore: IPeerStore, upgrader: TransportUpgrader, transport: ITransport)
Bases:
Service
,INetworkService
- async add_conn(muxed_conn: IMuxedConn) SwarmConn
Add a IMuxedConn to Swarm as a SwarmConn, notify “connected”, and start to monitor the connection for its new streams and disconnection.
- common_stream_handler: Callable[[INetStream], Awaitable[None]]
- async dial_addr(addr: Multiaddr, peer_id: ID) INetConn
Try to create a connection to peer_id with addr.
- Parameters:
addr – the address we want to connect with
peer_id – the peer we want to connect to
- Raises:
SwarmException – raised when an error occurs
- Returns:
network connection
- async dial_peer(peer_id: ID) INetConn
Try to create a connection to peer_id.
- Parameters:
peer_id – peer if we want to dial
- Raises:
SwarmException – raised when an error occurs
- Returns:
muxed connection
- event_listener_nursery_created: Event
- async listen(*multiaddrs: Multiaddr) bool
- Parameters:
multiaddrs – one or many multiaddrs to start listening on
- Returns:
true if at least one success
For each multiaddr
Check if a listener for multiaddr exists already
If listener already exists, continue
Otherwise:
Capture multiaddr in conn handler
Have conn handler delegate to stream handler
Call listener listen with the multiaddr
Map multiaddr to listener
- async new_stream(peer_id: ID) INetStream
- Parameters:
peer_id – peer_id of destination
- Raises:
SwarmException – raised when an error occurs
- Returns:
net stream instance
- async notify_closed_stream(stream: INetStream) None
- async notify_opened_stream(stream: INetStream) None
- peerstore: IPeerStore
- register_notifee(notifee: INotifee) None
- Parameters:
notifee – object implementing Notifee interface
- Returns:
true if notifee registered successfully, false otherwise
- remove_conn(swarm_conn: SwarmConn) None
Simply remove the connection from Swarm’s records, without closing the connection.
- async run() None
Primary entry point for all service logic.
Note
This method should not be directly invoked by user code.
Services may be run using the following approaches.
- set_stream_handler(stream_handler: Callable[[INetStream], Awaitable[None]]) None
Set the stream handler for all incoming streams.
- transport: ITransport
- upgrader: TransportUpgrader
- libp2p.network.swarm.create_default_stream_handler(network: INetworkService) Callable[[INetStream], Awaitable[None]]