libp2p.host package

Submodules

libp2p.host.basic_host module

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

Bases: 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: PeerInfo) None

Ensure 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: ID) None
get_addrs() List[Multiaddr]
Returns:

all the multiaddr addresses this host is listening to

get_id() ID
Returns:

peer_id of host

get_mux() Multiselect
Returns:

mux instance of host

get_network() INetworkService
Returns:

network instance of host

get_peerstore() IPeerStore
Returns:

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

get_private_key() PrivateKey
Returns:

the private key belonging to the peer

get_public_key() PublicKey
Returns:

the public key belonging to the peer

multiselect: Multiselect
multiselect_client: MultiselectClient
async new_stream(peer_id: ID, protocol_ids: Sequence[TProtocol]) 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
run(listen_addrs: Sequence[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: TProtocol, 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: IHost) OrderedDict[TProtocol, StreamHandlerFn]

libp2p.host.exceptions module

exception libp2p.host.exceptions.ConnectionFailure

Bases: HostException

exception libp2p.host.exceptions.HostException

Bases: BaseLibp2pError

A generic exception in IHost.

exception libp2p.host.exceptions.StreamFailure

Bases: HostException

libp2p.host.host_interface module

class libp2p.host.host_interface.IHost

Bases: ABC

abstract async close() None
abstract async connect(peer_info: PeerInfo) None

Ensure 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: ID) None
abstract get_addrs() List[Multiaddr]
Returns:

all the multiaddr addresses this host is listening to

abstract get_id() ID
Returns:

peer_id of host

abstract get_mux() Any
Returns:

mux instance of host

abstract get_network() INetworkService
Returns:

network instance of host

abstract get_private_key() PrivateKey
Returns:

the private key belonging to the peer

abstract get_public_key() PublicKey
Returns:

the public key belonging to the peer

abstract async new_stream(peer_id: ID, protocol_ids: Sequence[TProtocol]) 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]) AsyncContextManager[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: TProtocol, 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: INetStream) None

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

libp2p.host.routed_host module

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

Bases: BasicHost

async connect(peer_info: PeerInfo) None

Ensure 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