libp2p.discovery.random_walk package

The Random Walk module implements a peer discovery mechanism. It performs random walks through the DHT network to discover new peers and maintain routing table health through periodic refreshes.

Submodules

libp2p.discovery.random_walk.config module

libp2p.discovery.random_walk.exceptions module

exception libp2p.discovery.random_walk.exceptions.PeerValidationError

Bases: RoutingTableRefreshError

Exception raised when peer validation fails.

exception libp2p.discovery.random_walk.exceptions.RandomWalkError

Bases: RoutingTableRefreshError

Exception raised during random walk operations.

exception libp2p.discovery.random_walk.exceptions.RoutingTableRefreshError

Bases: BaseLibp2pError

Base exception for routing table refresh operations.

libp2p.discovery.random_walk.random_walk module

class libp2p.discovery.random_walk.random_walk.RandomWalk(host: IHost, local_peer_id: ID, query_function: Callable[[bytes], Awaitable[list[ID]]])

Bases: object

Random Walk implementation for peer discovery in Kademlia DHT.

Generates random peer IDs and performs FIND_NODE queries to discover new peers and populate the routing table.

generate_random_peer_id() str
Generate a completely random peer ID

for random walk queries.

Returns:

Random peer ID as string

async perform_random_walk() list[PeerInfo]

Perform a single random walk operation.

Returns:

List of validated peers discovered during the walk

async run_concurrent_random_walks(count: int = 3, current_routing_table_size: int = 0) list[PeerInfo]

Run multiple random walks concurrently.

Args:

count: Number of concurrent random walks to perform current_routing_table_size: Current size of routing table (for optimization)

Returns:

Combined list of all validated peers discovered

libp2p.discovery.random_walk.rt_refresh_manager module

class libp2p.discovery.random_walk.rt_refresh_manager.RTRefreshManager(host: IHost, routing_table: RoutingTableProtocol, local_peer_id: ID, query_function: Callable[[bytes], Awaitable[list[ID]]], enable_auto_refresh: bool = True, refresh_interval: float = 300.0, min_refresh_threshold: int = 4)

Bases: object

Routing Table Refresh Manager for py-libp2p.

Manages periodic routing table refreshes and random walk operations to maintain routing table health and discover new peers.

add_refresh_done_callback(callback: Callable[[], None]) None

Add a callback to be called when refresh completes.

remove_refresh_done_callback(callback: Callable[[], None]) None

Remove a refresh completion callback.

async start() None

Start the RT Refresh Manager.

async stop() None

Stop the RT Refresh Manager.

class libp2p.discovery.random_walk.rt_refresh_manager.RoutingTableProtocol(*args, **kwargs)

Bases: Protocol

Protocol for routing table operations needed by RT refresh manager.

async add_peer(peer_obj: PeerInfo) bool

Add a peer to the routing table.

size() int

Return the current size of the routing table.

Module contents

Random walk discovery modules for py-libp2p.

exception libp2p.discovery.random_walk.PeerValidationError

Bases: RoutingTableRefreshError

Exception raised when peer validation fails.

class libp2p.discovery.random_walk.RTRefreshManager(host: IHost, routing_table: RoutingTableProtocol, local_peer_id: ID, query_function: Callable[[bytes], Awaitable[list[ID]]], enable_auto_refresh: bool = True, refresh_interval: float = 300.0, min_refresh_threshold: int = 4)

Bases: object

Routing Table Refresh Manager for py-libp2p.

Manages periodic routing table refreshes and random walk operations to maintain routing table health and discover new peers.

add_refresh_done_callback(callback: Callable[[], None]) None

Add a callback to be called when refresh completes.

remove_refresh_done_callback(callback: Callable[[], None]) None

Remove a refresh completion callback.

async start() None

Start the RT Refresh Manager.

async stop() None

Stop the RT Refresh Manager.

class libp2p.discovery.random_walk.RandomWalk(host: IHost, local_peer_id: ID, query_function: Callable[[bytes], Awaitable[list[ID]]])

Bases: object

Random Walk implementation for peer discovery in Kademlia DHT.

Generates random peer IDs and performs FIND_NODE queries to discover new peers and populate the routing table.

generate_random_peer_id() str
Generate a completely random peer ID

for random walk queries.

Returns:

Random peer ID as string

async perform_random_walk() list[PeerInfo]

Perform a single random walk operation.

Returns:

List of validated peers discovered during the walk

async run_concurrent_random_walks(count: int = 3, current_routing_table_size: int = 0) list[PeerInfo]

Run multiple random walks concurrently.

Args:

count: Number of concurrent random walks to perform current_routing_table_size: Current size of routing table (for optimization)

Returns:

Combined list of all validated peers discovered

exception libp2p.discovery.random_walk.RandomWalkError

Bases: RoutingTableRefreshError

Exception raised during random walk operations.

exception libp2p.discovery.random_walk.RoutingTableRefreshError

Bases: BaseLibp2pError

Base exception for routing table refresh operations.