|
| 1 | +Random Walk Example |
| 2 | +=================== |
| 3 | + |
| 4 | +This example demonstrates the Random Walk module's peer discovery capabilities using real libp2p hosts and Kademlia DHT. |
| 5 | +It shows how the Random Walk module automatically discovers new peers and maintains routing table health. |
| 6 | + |
| 7 | +The Random Walk implementation performs the following key operations: |
| 8 | + |
| 9 | +* **Automatic Peer Discovery**: Generates random peer IDs and queries the DHT network to discover new peers |
| 10 | +* **Routing Table Maintenance**: Periodically refreshes the routing table to maintain network connectivity |
| 11 | +* **Connection Management**: Maintains optimal connections to healthy peers in the network |
| 12 | +* **Real-time Statistics**: Displays routing table size, connected peers, and peerstore statistics |
| 13 | + |
| 14 | +.. code-block:: console |
| 15 | +
|
| 16 | + $ python -m pip install libp2p |
| 17 | + Collecting libp2p |
| 18 | + ... |
| 19 | + Successfully installed libp2p-x.x.x |
| 20 | + $ cd examples/random_walk |
| 21 | + $ python random_walk.py --mode server |
| 22 | + 2025-08-12 19:51:25,424 - random-walk-example - INFO - === Random Walk Example for py-libp2p === |
| 23 | + 2025-08-12 19:51:25,424 - random-walk-example - INFO - Mode: server, Port: 0 Demo interval: 30s |
| 24 | + 2025-08-12 19:51:25,426 - random-walk-example - INFO - Starting server node on port 45123 |
| 25 | + 2025-08-12 19:51:25,426 - random-walk-example - INFO - Node peer ID: 16Uiu2HAm7EsNv5vvjPAehGAVfChjYjD63ZHyWogQRdzntSbAg9ef |
| 26 | + 2025-08-12 19:51:25,426 - random-walk-example - INFO - Node address: /ip4/0.0.0.0/tcp/45123/p2p/16Uiu2HAm7EsNv5vvjPAehGAVfChjYjD63ZHyWogQRdzntSbAg9ef |
| 27 | + 2025-08-12 19:51:25,427 - random-walk-example - INFO - Initial routing table size: 0 |
| 28 | + 2025-08-12 19:51:25,427 - random-walk-example - INFO - DHT service started in SERVER mode |
| 29 | + 2025-08-12 19:51:25,430 - libp2p.discovery.random_walk.rt_refresh_manager - INFO - RT Refresh Manager started |
| 30 | + 2025-08-12 19:51:55,432 - random-walk-example - INFO - --- Iteration 1 --- |
| 31 | + 2025-08-12 19:51:55,432 - random-walk-example - INFO - Routing table size: 15 |
| 32 | + 2025-08-12 19:51:55,432 - random-walk-example - INFO - Connected peers: 8 |
| 33 | + 2025-08-12 19:51:55,432 - random-walk-example - INFO - Peerstore size: 42 |
| 34 | +
|
| 35 | +You can also run the example in client mode: |
| 36 | + |
| 37 | +.. code-block:: console |
| 38 | +
|
| 39 | + $ python random_walk.py --mode client |
| 40 | + 2025-08-12 19:52:15,424 - random-walk-example - INFO - === Random Walk Example for py-libp2p === |
| 41 | + 2025-08-12 19:52:15,424 - random-walk-example - INFO - Mode: client, Port: 0 Demo interval: 30s |
| 42 | + 2025-08-12 19:52:15,426 - random-walk-example - INFO - Starting client node on port 51234 |
| 43 | + 2025-08-12 19:52:15,426 - random-walk-example - INFO - Node peer ID: 16Uiu2HAmAbc123xyz... |
| 44 | + 2025-08-12 19:52:15,427 - random-walk-example - INFO - DHT service started in CLIENT mode |
| 45 | + 2025-08-12 19:52:45,432 - random-walk-example - INFO - --- Iteration 1 --- |
| 46 | + 2025-08-12 19:52:45,432 - random-walk-example - INFO - Routing table size: 8 |
| 47 | + 2025-08-12 19:52:45,432 - random-walk-example - INFO - Connected peers: 5 |
| 48 | + 2025-08-12 19:52:45,432 - random-walk-example - INFO - Peerstore size: 25 |
| 49 | +
|
| 50 | +Command Line Options |
| 51 | +-------------------- |
| 52 | + |
| 53 | +The example supports several command-line options: |
| 54 | + |
| 55 | +.. code-block:: console |
| 56 | +
|
| 57 | + $ python random_walk.py --help |
| 58 | + usage: random_walk.py [-h] [--mode {server,client}] [--port PORT] |
| 59 | + [--demo-interval DEMO_INTERVAL] [--verbose] |
| 60 | +
|
| 61 | + Random Walk Example for py-libp2p Kademlia DHT |
| 62 | +
|
| 63 | + optional arguments: |
| 64 | + -h, --help show this help message and exit |
| 65 | + --mode {server,client} |
| 66 | + Node mode: server (DHT server), or client (DHT client) |
| 67 | + --port PORT Port to listen on (0 for random) |
| 68 | + --demo-interval DEMO_INTERVAL |
| 69 | + Interval between random walk demonstrations in seconds |
| 70 | + --verbose Enable verbose logging |
| 71 | +
|
| 72 | +Key Features Demonstrated |
| 73 | +------------------------- |
| 74 | + |
| 75 | +**Automatic Random Walk Discovery** |
| 76 | + The example shows how the Random Walk module automatically: |
| 77 | + |
| 78 | + * Generates random 256-bit peer IDs for discovery queries |
| 79 | + * Performs concurrent random walks to maximize peer discovery |
| 80 | + * Validates discovered peers and adds them to the routing table |
| 81 | + * Maintains routing table health through periodic refreshes |
| 82 | + |
| 83 | +**Real-time Network Statistics** |
| 84 | + The example displays live statistics every 30 seconds (configurable): |
| 85 | + |
| 86 | + * **Routing Table Size**: Number of peers in the Kademlia routing table |
| 87 | + * **Connected Peers**: Number of actively connected peers |
| 88 | + * **Peerstore Size**: Total number of known peers with addresses |
| 89 | + |
| 90 | +**Connection Management** |
| 91 | + The example includes sophisticated connection management: |
| 92 | + |
| 93 | + * Automatically maintains connections to healthy peers |
| 94 | + * Filters for compatible peers (TCP + IPv4 addresses) |
| 95 | + * Reconnects to maintain optimal network connectivity |
| 96 | + * Handles connection failures gracefully |
| 97 | + |
| 98 | +**DHT Integration** |
| 99 | + Shows seamless integration between Random Walk and Kademlia DHT: |
| 100 | + |
| 101 | + * RT Refresh Manager coordinates with the DHT routing table |
| 102 | + * Peer discovery feeds directly into DHT operations |
| 103 | + * Both SERVER and CLIENT modes supported |
| 104 | + * Bootstrap connectivity to public IPFS nodes |
| 105 | + |
| 106 | +Understanding the Output |
| 107 | +------------------------ |
| 108 | + |
| 109 | +When you run the example, you'll see periodic statistics that show how the Random Walk module is working: |
| 110 | + |
| 111 | +* **Initial Phase**: Routing table starts empty and quickly discovers peers |
| 112 | +* **Growth Phase**: Routing table size increases as more peers are discovered |
| 113 | +* **Maintenance Phase**: Routing table size stabilizes as the system maintains optimal peer connections |
| 114 | + |
| 115 | +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. |
| 116 | + |
| 117 | +Configuration |
| 118 | +------------- |
| 119 | + |
| 120 | +The Random Walk module can be configured through the following parameters in ``libp2p.discovery.random_walk.config``: |
| 121 | + |
| 122 | +* ``RANDOM_WALK_ENABLED``: Enable/disable automatic random walks (default: True) |
| 123 | +* ``REFRESH_INTERVAL``: Time between automatic refreshes in seconds (default: 300) |
| 124 | +* ``RANDOM_WALK_CONCURRENCY``: Number of concurrent random walks (default: 3) |
| 125 | +* ``MIN_RT_REFRESH_THRESHOLD``: Minimum routing table size before triggering refresh (default: 4) |
| 126 | + |
| 127 | +See Also |
| 128 | +-------- |
| 129 | + |
| 130 | +* :doc:`examples.kademlia` - Kademlia DHT value storage and content routing |
| 131 | +* :doc:`libp2p.discovery.random_walk` - Random Walk module API documentation |
0 commit comments