libp2p.relay package
Subpackages
- libp2p.relay.circuit_v2 package
- Subpackages
- Submodules
- libp2p.relay.circuit_v2.protocol module
- libp2p.relay.circuit_v2.transport module
- libp2p.relay.circuit_v2.discovery module
- libp2p.relay.circuit_v2.resources module
RelayLimitsRelayResourceManagerRelayResourceManager.can_accept_connection()RelayResourceManager.can_accept_reservation()RelayResourceManager.create_reservation()RelayResourceManager.has_reservation()RelayResourceManager.refresh_reservation()RelayResourceManager.reserve()RelayResourceManager.track_data_transfer()RelayResourceManager.verify_reservation()
ReservationReservationStatusReservationVoucher
- libp2p.relay.circuit_v2.config module
ClientConfigHopConfigRelayConfigRelayConfig.bootstrap_relaysRelayConfig.discovery_intervalRelayConfig.enable_clientRelayConfig.enable_dht_discoveryRelayConfig.enable_hopRelayConfig.enable_stopRelayConfig.limitsRelayConfig.max_circuit_bytesRelayConfig.max_circuit_durationRelayConfig.max_relaysRelayConfig.min_relaysRelayConfig.reservation_ttlRelayConfig.rolesRelayConfig.timeouts
RelayRoleTimeoutConfig
- libp2p.relay.circuit_v2.protocol_buffer module
- libp2p.relay.circuit_v2.performance_tracker module
RelayPerformanceTrackerRelayPerformanceTracker.export_metrics()RelayPerformanceTracker.get_all_relay_stats()RelayPerformanceTracker.get_relay_score()RelayPerformanceTracker.get_relay_stats()RelayPerformanceTracker.record_circuit_closed()RelayPerformanceTracker.record_circuit_opened()RelayPerformanceTracker.record_connection_attempt()RelayPerformanceTracker.reset()RelayPerformanceTracker.select_best_relay()
RelayStats
- Module contents
Module contents
Relay module for libp2p.
This package includes implementations of circuit relay protocols for enabling connectivity between peers behind NATs or firewalls.
- class libp2p.relay.CircuitV2Protocol(host: IHost, limits: RelayLimits | None = None, allow_hop: bool = False, read_timeout: int = 15, write_timeout: int = 15, close_timeout: int = 10)
Bases:
ServiceCircuitV2Protocol implements the Circuit Relay v2 protocol.
This protocol allows peers to establish connections through relay nodes when direct connections are not possible (e.g., due to NAT).
- async handle_incoming_connection(stream: INetStream, remote_peer_id: bytes) None
Handle an incoming relay connection.
Parameters
- streamINetStream
The incoming stream
- remote_peer_idID
The remote peer’s ID
Raises
- ConnectionError
If the connection cannot be established
- class libp2p.relay.CircuitV2Transport(host: IHost, protocol: CircuitV2Protocol, config: RelayConfig)
Bases:
ITransportCircuitV2Transport implements the transport interface for Circuit Relay v2.
This transport allows peers to establish connections through relay nodes when direct connections are not possible.
- create_listener(handler_function: Callable[[ReadWriteCloser], Awaitable[None]]) IListener
Create a listener on the transport.
Parameters
- handler_functionTHandler
A function that is called when a new connection is received. The function should accept a connection (that implements the connection interface) as its argument.
Returns
- IListener
A listener instance.
- async dial(maddr: Multiaddr) INetConn
Dial a peer using the multiaddr.
Parameters
- maddrmultiaddr.Multiaddr
The multiaddr to dial
Returns
- INetConn
The established connection
Raises
- ConnectionError
If the connection cannot be established
- async dial_peer_info(dest_info: PeerInfo, *, relay_info: PeerInfo | None = None) IRawConnection
Dial a destination peer using a relay.
Parameters
- dest_infoPeerInfo
The destination peer to dial.
- relay_infoOptional[PeerInfo], optional
An optional specific relay peer to use.
Returns
- RawConnection
The established raw connection to the destination peer through the relay.
Raises
- ConnectionError
If the connection cannot be established.
- parse_circuit_ma(ma: Multiaddr) tuple[Multiaddr, ID]
Parse a /p2p-circuit/p2p/<targetPeerID> path from a relay Multiaddr.
- Returns:
relay_ma: Multiaddr to the relay target_peer_id: ID of the target peer
- Raises:
ValueError: if the Multiaddr is not a valid circuit address
- async reserve(stream: INetStream, relay_peer_id: ID, nursery: Nursery) bool
Public method to create a reservation and start refresher if needed.
- class libp2p.relay.DCUtRProtocol(host: IHost, read_timeout: int = 30, write_timeout: int = 30, dial_timeout: int = 10)
Bases:
ServiceDCUtRProtocol implements the Direct Connection Upgrade through Relay protocol.
This protocol allows two NATed peers to establish direct connections through hole punching, after they have established an initial connection through a relay.
- class libp2p.relay.ReachabilityChecker(host: IHost)
Bases:
objectUtility class for checking peer reachability.
This class assesses whether a peer’s addresses are likely to be directly reachable or behind NAT.
- async check_peer_reachability(peer_id: ID) bool
Check if a peer is directly reachable.
Parameters
- peer_idID
The peer ID to check
Returns
- bool
True if peer is likely directly reachable
- async check_self_reachability() tuple[bool, list[Multiaddr]]
Check if this host is likely directly reachable.
Returns
- Tuple[bool, List[Multiaddr]]
Tuple of (is_reachable, public_addresses)
- class libp2p.relay.RelayDiscovery(host: IHost, auto_reserve: bool = False, discovery_interval: int = 300, max_relays: int = 10, stream_timeout: int = 10, peer_protocol_timeout: int = 5)
Bases:
ServiceDiscovery service for Circuit Relay v2 nodes.
This service discovers and keeps track of available relay nodes, and optionally makes reservations with them.
- async discover_relays() None
Discover relay nodes in the network.
This method queries the network for peers that support the Circuit Relay v2 protocol.
- get_relay() ID | None
Get a single relay peer ID for connection purposes. Prioritizes relays with active reservations.
Returns
- Optional[ID]
ID of a discovered relay, or None if no relays found
- get_relay_info(peer_id: ID) RelayInfo | None
Get information about a specific relay.
Parameters
- peer_idID
The ID of the relay to get information about
Returns
- Optional[RelayInfo]
Information about the relay, or None if not found
- get_relays() list[ID]
Get a list of discovered relay peer IDs.
Returns
- list[ID]
List of discovered relay peer IDs
- class libp2p.relay.RelayLimits(duration: int, data: int, max_circuit_conns: int, max_reservations: int)
Bases:
objectConfiguration for relay resource limits.
- data: int
- duration: int
- max_circuit_conns: int
- max_reservations: int
- class libp2p.relay.RelayResourceManager(limits: RelayLimits, host: IHost | None = None)
Bases:
objectManages resources and reservations for relay operations.
This class handles: - Tracking active reservations - Enforcing resource limits - Managing connection quotas
- can_accept_connection(peer_id: ID) bool
Check if a new connection can be accepted for the given peer.
Parameters
- peer_idID
The peer ID requesting the connection
Returns
- bool
True if the connection can be accepted
- can_accept_reservation(peer_id: ID) bool
Check if a new reservation can be accepted for the given peer.
Parameters
- peer_idID
The peer ID requesting the reservation
Returns
- bool
True if the reservation can be accepted
- create_reservation(peer_id: ID) Reservation
Create a new reservation for the given peer.
Parameters
- peer_idID
The peer ID to create the reservation for
Returns
- Reservation
The newly created reservation
- has_reservation(peer_id: ID) bool
Check if a reservation already exists for a peer
Parameters
- peer_idID
The peer ID to check for
Returns
- bool
True if reservation exists, False otherwise
- reserve(peer_id: ID) int
Create or update a reservation for a peer and return the TTL.
Parameters
- peer_idID
The peer ID to reserve for
Returns
- int
The TTL of the reservation in seconds
- track_data_transfer(peer_id: ID, bytes_transferred: int) bool
Track data transferred for a peer’s reservation.
Parameters
- peer_idID
The peer ID
- bytes_transferredint
Number of bytes transferred
Returns
- bool
True if the data limit has not been exceeded, False otherwise
- verify_reservation(peer_id: ID, proto_res: Reservation) bool
Verify a reservation from a protobuf message.
Parameters
- peer_idID
The peer ID the reservation is for
- proto_resPbReservation
The protobuf reservation message
Returns
- bool
True if the reservation is valid
- class libp2p.relay.Reservation(peer_id: ID, limits: RelayLimits, host: IHost | None = None)
Bases:
objectRepresents a relay reservation.
- can_accept_connection() bool
Check if a new connection can be accepted.
- get_data_to_sign() bytes
Get the data that should be signed for this reservation.
Returns
- bytes
The data to sign, which includes the domain separator, voucher, and expiration
- is_expired() bool
Check if the reservation has expired.
- property status: ReservationStatus
Return the current status as a
ReservationStatusenum.
- to_proto() Reservation
Convert the reservation to its protobuf representation.
Returns
- PbReservation
The protobuf representation of this reservation