Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b425f75
Implementing random walk in py libp2p
sumanjeet0012 Aug 7, 2025
a7556c4
Add documentation for Random Walk module implementation in py-libp2p
sumanjeet0012 Aug 7, 2025
b3c0915
Add Random Walk example for py-libp2p Kademlia DHT
sumanjeet0012 Aug 7, 2025
2941db5
refactor: peer eviction from routing table stopped
sumanjeet0012 Aug 10, 2025
4f11202
refactored location of random walk
sumanjeet0012 Aug 10, 2025
64a92ad
Merge branch 'libp2p:main' into feature/random_walk
sumanjeet0012 Aug 10, 2025
b051a63
Merge branch 'main' into feature/random_walk
seetadev Aug 11, 2025
58b959d
add nodesin routing table from peerstore
sumanjeet0012 Aug 10, 2025
bf9590f
random walk working as expected
sumanjeet0012 Aug 10, 2025
7d7cfac
removed extra functions
sumanjeet0012 Aug 12, 2025
f65a7e9
Removed all manual triggers
sumanjeet0012 Aug 12, 2025
6380c4f
added newsfragments
sumanjeet0012 Aug 12, 2025
3b862a8
fix linting issues
sumanjeet0012 Aug 12, 2025
ca31fd6
refacored logs and cleaned example file
sumanjeet0012 Aug 12, 2025
0c44188
refactor: update RandomWalk and RTRefreshManager to use query functio…
sumanjeet0012 Aug 12, 2025
05ca064
docs: added Random Walk example docs
sumanjeet0012 Aug 12, 2025
a298055
Merge branch 'main' into feature/random_walk
seetadev Aug 12, 2025
930c382
added optional argument to use random walk in kademlia DHT
sumanjeet0012 Aug 13, 2025
5e91ab9
enabled random walk in example file
sumanjeet0012 Aug 13, 2025
bbdc12a
Merge branch 'main' into feature/random_walk
seetadev Aug 14, 2025
e0a4eaf
Merge branch 'libp2p:main' into feature/random_walk
sumanjeet0012 Aug 16, 2025
6ecc175
Added tests for RandomWalk module
sumanjeet0012 Aug 17, 2025
e4ef1ae
fixed lint issues
sumanjeet0012 Aug 17, 2025
cc6ee05
Merge branch 'main' into feature/random_walk
seetadev Aug 17, 2025
ef8bd80
Merge branch 'main' into feature/random_walk
seetadev Aug 18, 2025
51c8738
Update refresh interval and some more tests are added.
sumanjeet0012 Aug 19, 2025
fcf22b1
Removed Random Walk module documentation file
sumanjeet0012 Aug 19, 2025
35b4b29
Merge branch 'libp2p:main' into feature/random_walk
sumanjeet0012 Aug 20, 2025
548f99a
Extra parentheses have been removed from the random walk logs.
sumanjeet0012 Aug 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions docs/examples.random_walk.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
Random Walk Example
===================

This example demonstrates the Random Walk module's peer discovery capabilities using real libp2p hosts and Kademlia DHT.
It shows how the Random Walk module automatically discovers new peers and maintains routing table health.

The Random Walk implementation performs the following key operations:

* **Automatic Peer Discovery**: Generates random peer IDs and queries the DHT network to discover new peers
* **Routing Table Maintenance**: Periodically refreshes the routing table to maintain network connectivity
* **Connection Management**: Maintains optimal connections to healthy peers in the network
* **Real-time Statistics**: Displays routing table size, connected peers, and peerstore statistics

.. code-block:: console

$ python -m pip install libp2p
Collecting libp2p
...
Successfully installed libp2p-x.x.x
$ cd examples/random_walk
$ python random_walk.py --mode server
2025-08-12 19:51:25,424 - random-walk-example - INFO - === Random Walk Example for py-libp2p ===
2025-08-12 19:51:25,424 - random-walk-example - INFO - Mode: server, Port: 0 Demo interval: 30s
2025-08-12 19:51:25,426 - random-walk-example - INFO - Starting server node on port 45123
2025-08-12 19:51:25,426 - random-walk-example - INFO - Node peer ID: 16Uiu2HAm7EsNv5vvjPAehGAVfChjYjD63ZHyWogQRdzntSbAg9ef
2025-08-12 19:51:25,426 - random-walk-example - INFO - Node address: /ip4/0.0.0.0/tcp/45123/p2p/16Uiu2HAm7EsNv5vvjPAehGAVfChjYjD63ZHyWogQRdzntSbAg9ef
2025-08-12 19:51:25,427 - random-walk-example - INFO - Initial routing table size: 0
2025-08-12 19:51:25,427 - random-walk-example - INFO - DHT service started in SERVER mode
2025-08-12 19:51:25,430 - libp2p.discovery.random_walk.rt_refresh_manager - INFO - RT Refresh Manager started
2025-08-12 19:51:55,432 - random-walk-example - INFO - --- Iteration 1 ---
2025-08-12 19:51:55,432 - random-walk-example - INFO - Routing table size: 15
2025-08-12 19:51:55,432 - random-walk-example - INFO - Connected peers: 8
2025-08-12 19:51:55,432 - random-walk-example - INFO - Peerstore size: 42

You can also run the example in client mode:

.. code-block:: console

$ python random_walk.py --mode client
2025-08-12 19:52:15,424 - random-walk-example - INFO - === Random Walk Example for py-libp2p ===
2025-08-12 19:52:15,424 - random-walk-example - INFO - Mode: client, Port: 0 Demo interval: 30s
2025-08-12 19:52:15,426 - random-walk-example - INFO - Starting client node on port 51234
2025-08-12 19:52:15,426 - random-walk-example - INFO - Node peer ID: 16Uiu2HAmAbc123xyz...
2025-08-12 19:52:15,427 - random-walk-example - INFO - DHT service started in CLIENT mode
2025-08-12 19:52:45,432 - random-walk-example - INFO - --- Iteration 1 ---
2025-08-12 19:52:45,432 - random-walk-example - INFO - Routing table size: 8
2025-08-12 19:52:45,432 - random-walk-example - INFO - Connected peers: 5
2025-08-12 19:52:45,432 - random-walk-example - INFO - Peerstore size: 25

Command Line Options
--------------------

The example supports several command-line options:

.. code-block:: console

$ python random_walk.py --help
usage: random_walk.py [-h] [--mode {server,client}] [--port PORT]
[--demo-interval DEMO_INTERVAL] [--verbose]

Random Walk Example for py-libp2p Kademlia DHT

optional arguments:
-h, --help show this help message and exit
--mode {server,client}
Node mode: server (DHT server), or client (DHT client)
--port PORT Port to listen on (0 for random)
--demo-interval DEMO_INTERVAL
Interval between random walk demonstrations in seconds
--verbose Enable verbose logging

Key Features Demonstrated
-------------------------

**Automatic Random Walk Discovery**
The example shows how the Random Walk module automatically:

* Generates random 256-bit peer IDs for discovery queries
* Performs concurrent random walks to maximize peer discovery
* Validates discovered peers and adds them to the routing table
* Maintains routing table health through periodic refreshes

**Real-time Network Statistics**
The example displays live statistics every 30 seconds (configurable):

* **Routing Table Size**: Number of peers in the Kademlia routing table
* **Connected Peers**: Number of actively connected peers
* **Peerstore Size**: Total number of known peers with addresses

**Connection Management**
The example includes sophisticated connection management:

* Automatically maintains connections to healthy peers
* Filters for compatible peers (TCP + IPv4 addresses)
* Reconnects to maintain optimal network connectivity
* Handles connection failures gracefully

**DHT Integration**
Shows seamless integration between Random Walk and Kademlia DHT:

* RT Refresh Manager coordinates with the DHT routing table
* Peer discovery feeds directly into DHT operations
* Both SERVER and CLIENT modes supported
* Bootstrap connectivity to public IPFS nodes

Understanding the Output
------------------------

When you run the example, you'll see periodic statistics that show how the Random Walk module is working:

* **Initial Phase**: Routing table starts empty and quickly discovers peers
* **Growth Phase**: Routing table size increases as more peers are discovered
* **Maintenance Phase**: Routing table size stabilizes as the system maintains optimal peer connections

The Random Walk module runs automatically in the background, performing peer discovery queries every few minutes to ensure the routing table remains populated with fresh, reachable peers.

Configuration
-------------

The Random Walk module can be configured through the following parameters in ``libp2p.discovery.random_walk.config``:

* ``RANDOM_WALK_ENABLED``: Enable/disable automatic random walks (default: True)
* ``REFRESH_INTERVAL``: Time between automatic refreshes in seconds (default: 300)
* ``RANDOM_WALK_CONCURRENCY``: Number of concurrent random walks (default: 3)
* ``MIN_RT_REFRESH_THRESHOLD``: Minimum routing table size before triggering refresh (default: 4)

See Also
--------

* :doc:`examples.kademlia` - Kademlia DHT value storage and content routing
* :doc:`libp2p.discovery.random_walk` - Random Walk module API documentation
1 change: 1 addition & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Examples
examples.circuit_relay
examples.kademlia
examples.mDNS
examples.random_walk
48 changes: 48 additions & 0 deletions docs/libp2p.discovery.random_walk.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule:: libp2p.discovery.random_walk.config
:members:
:undoc-members:
:show-inheritance:

libp2p.discovery.random_walk.exceptions module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule:: libp2p.discovery.random_walk.exceptions
:members:
:undoc-members:
:show-inheritance:

libp2p.discovery.random_walk.random_walk module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule:: libp2p.discovery.random_walk.random_walk
:members:
:undoc-members:
:show-inheritance:

libp2p.discovery.random_walk.rt_refresh_manager module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule:: libp2p.discovery.random_walk.rt_refresh_manager
:members:
:undoc-members:
:show-inheritance:

Module contents
---------------

.. automodule:: libp2p.discovery.random_walk
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/libp2p.discovery.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Subpackages
libp2p.discovery.bootstrap
libp2p.discovery.events
libp2p.discovery.mdns
libp2p.discovery.random_walk

Submodules
----------
Expand Down
2 changes: 1 addition & 1 deletion examples/kademlia/kademlia.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ async def run_node(

# Keep the node running
while True:
logger.debug(
logger.info(
"Status - Connected peers: %d,"
"Peers in store: %d, Values in store: %d",
len(dht.host.get_connected_peers()),
Expand Down
Loading
Loading