libp2p.host package

Submodules

libp2p.host.basic_host module

class libp2p.host.basic_host.BasicHost(network: libp2p.network.network_interface.INetworkService, default_protocols: OrderedDict[TProtocol, StreamHandlerFn] = None)

Bases: libp2p.host.host_interface.IHost

BasicHost is a wrapper of a INetwork implementation.

It performs protocol negotiation on a stream with multistream-select right after a stream is initialized.

async close() → None
async connect(peer_info: libp2p.peer.peerinfo.PeerInfo) → None

connect ensures there is a connection between this host and the peer with given peer_info.peer_id. connect will absorb the addresses in peer_info into its internal peerstore. If there is not an active connection, connect will issue a dial, and block until a connection is opened, or an error is returned.

Parameters

peer_info (peer.peerinfo.PeerInfo) – peer_info of the peer we want to connect to

async disconnect(peer_id: libp2p.peer.id.ID) → None
get_addrs() → List[multiaddr.multiaddr.Multiaddr]
Returns

all the multiaddr addresses this host is listening to

get_id() → libp2p.peer.id.ID
Returns

peer_id of host

get_mux() → libp2p.protocol_muxer.multiselect.Multiselect
Returns

mux instance of host

get_network() → libp2p.network.network_interface.INetworkService
Returns

network instance of host

get_peerstore() → libp2p.peer.peerstore_interface.IPeerStore
Returns

peerstore of the host (same one as in its network instance)

get_private_key() → libp2p.crypto.keys.PrivateKey
Returns

the private key belonging to the peer

get_public_key() → libp2p.crypto.keys.PublicKey
Returns

the public key belonging to the peer

multiselect: Multiselect = None
multiselect_client: MultiselectClient = None
async new_stream(peer_id: libp2p.peer.id.ID, protocol_ids: Sequence[NewType.<locals>.new_type]) → libp2p.network.stream.net_stream_interface.INetStream
Parameters
  • peer_id – peer_id that host is connecting

  • protocol_ids – available protocol ids to use for stream

Returns

stream: new stream created

peerstore: IPeerStore = None
run(listen_addrs: Sequence[multiaddr.multiaddr.Multiaddr]) → AsyncIterator[None]

run the host instance and listen to listen_addrs.

Parameters

listen_addrs – a sequence of multiaddrs that we want to listen to

set_stream_handler(protocol_id: NewType.<locals>.new_type, stream_handler: Callable[[INetStream], Awaitable[None]]) → None

set stream handler for given protocol_id

Parameters
  • protocol_id – protocol id used on stream

  • stream_handler – a stream handler function

libp2p.host.defaults module

libp2p.host.defaults.get_default_protocols(host: libp2p.host.host_interface.IHost) → OrderedDict[TProtocol, StreamHandlerFn]

libp2p.host.exceptions module

exception libp2p.host.exceptions.ConnectionFailure

Bases: libp2p.host.exceptions.HostException

exception libp2p.host.exceptions.HostException

Bases: libp2p.exceptions.BaseLibp2pError

A generic exception in IHost.

exception libp2p.host.exceptions.StreamFailure

Bases: libp2p.host.exceptions.HostException

libp2p.host.host_interface module

class libp2p.host.host_interface.IHost

Bases: abc.ABC

abstract async close() → None
abstract async connect(peer_info: libp2p.peer.peerinfo.PeerInfo) → None

connect ensures there is a connection between this host and the peer with given peer_info.peer_id. connect will absorb the addresses in peer_info into its internal peerstore. If there is not an active connection, connect will issue a dial, and block until a connection is opened, or an error is returned.

Parameters

peer_info (peer.peerinfo.PeerInfo) – peer_info of the peer we want to connect to

abstract async disconnect(peer_id: libp2p.peer.id.ID) → None
abstract get_addrs() → List[multiaddr.multiaddr.Multiaddr]
Returns

all the multiaddr addresses this host is listening to

abstract get_id() → libp2p.peer.id.ID
Returns

peer_id of host

abstract get_mux() → Any
Returns

mux instance of host

abstract get_network() → libp2p.network.network_interface.INetworkService
Returns

network instance of host

abstract get_private_key() → libp2p.crypto.keys.PrivateKey
Returns

the private key belonging to the peer

abstract get_public_key() → libp2p.crypto.keys.PublicKey
Returns

the public key belonging to the peer

abstract async new_stream(peer_id: libp2p.peer.id.ID, protocol_ids: Sequence[NewType.<locals>.new_type]) → libp2p.network.stream.net_stream_interface.INetStream
Parameters
  • peer_id – peer_id that host is connecting

  • protocol_ids – available protocol ids to use for stream

Returns

stream: new stream created

abstract run(listen_addrs: Sequence[multiaddr.multiaddr.Multiaddr]) → AbstractAsyncContextManager[None]

run the host instance and listen to listen_addrs.

Parameters

listen_addrs – a sequence of multiaddrs that we want to listen to

abstract set_stream_handler(protocol_id: NewType.<locals>.new_type, stream_handler: Callable[[INetStream], Awaitable[None]]) → None

set stream handler for host.

Parameters
  • protocol_id – protocol id used on stream

  • stream_handler – a stream handler function

libp2p.host.ping module

async libp2p.host.ping.handle_ping(stream: libp2p.network.stream.net_stream_interface.INetStream) → None

handle_ping responds to incoming ping requests until one side errors or closes the stream.

libp2p.host.routed_host module

class libp2p.host.routed_host.RoutedHost(network: libp2p.network.network_interface.INetworkService, router: libp2p.routing.interfaces.IPeerRouting)

Bases: libp2p.host.basic_host.BasicHost

async connect(peer_info: libp2p.peer.peerinfo.PeerInfo) → None

connect ensures there is a connection between this host and the peer with given peer_info.peer_id. See (basic_host).connect for more information.

RoutedHost’s Connect differs in that if the host has no addresses for a given peer, it will use its routing system to try to find some.

Parameters

peer_info (peer.peerinfo.PeerInfo) – peer_info of the peer we want to connect to

Module contents