@@ -8,7 +8,7 @@ const { MemoryDatastore } = require('interface-datastore')
88const { equals : uint8ArrayEquals } = require ( 'uint8arrays/equals' )
99const { toString : uint8ArrayToString } = require ( 'uint8arrays/to-string' )
1010
11- const RoutingTable = require ( './routing-table ' )
11+ const RoutingTable = require ( './routing' )
1212const utils = require ( './utils' )
1313const c = require ( './constants' )
1414const Network = require ( './network' )
@@ -17,6 +17,7 @@ const contentRouting = require('./content-routing')
1717const peerRouting = require ( './peer-routing' )
1818const Message = require ( './message' )
1919const Providers = require ( './providers' )
20+ const RandomWalk = require ( './random-walk' )
2021const QueryManager = require ( './query-manager' )
2122
2223const Record = libp2pRecord . Record
@@ -33,6 +34,13 @@ const Record = libp2pRecord.Record
3334 * @typedef {object } PeerData
3435 * @property {PeerId } id
3536 * @property {Multiaddr[] } multiaddrs
37+ *
38+ * @typedef {object } RandomWalkOptions
39+ * @property {boolean } enabled discovery enabled (default: true)
40+ * @property {number } queriesPerPeriod how many queries to run per period (default: 1)
41+ * @property {number } interval how often to run the the random-walk process, in milliseconds (default: 300000)
42+ * @property {number } timeout how long to wait for the the random-walk query to run, in milliseconds (default: 30000)
43+ * @property {number } delay how long to wait before starting the first random walk, in milliseconds (default: 10000)
3644 */
3745
3846/**
@@ -57,6 +65,7 @@ class KadDHT extends EventEmitter {
5765 * @param {Datastore } props.datastore - datastore (default MemoryDatastore)
5866 * @param {object } props.validators - validators object with namespace as keys and function(key, record, callback)
5967 * @param {object } props.selectors - selectors object with namespace as keys and function(key, records)
68+ * @param {RandomWalkOptions } props.randomWalk - randomWalk options
6069 * @param {function(import('libp2p-record').Record, PeerId): void } [props.onPut] - Called when an entry is added to or changed in the datastore
6170 * @param {function(import('libp2p-record').Record): void } [props.onRemove] - Called when an entry is removed from the datastore
6271 */
@@ -74,6 +83,13 @@ class KadDHT extends EventEmitter {
7483 concurrency = c . ALPHA ,
7584 validators = { } ,
7685 selectors = { } ,
86+ randomWalk = {
87+ enabled : false ,
88+ queriesPerPeriod : 1 ,
89+ interval : 300000 ,
90+ timeout : 30000 ,
91+ delay : 10000
92+ } ,
7793 onPut = ( ) => { } ,
7894 onRemove = ( ) => { }
7995 } ) {
@@ -154,7 +170,7 @@ class KadDHT extends EventEmitter {
154170 *
155171 * @type {RoutingTable }
156172 */
157- this . routingTable = new RoutingTable ( this , { kBucketSize : this . kBucketSize } )
173+ this . routingTable = new RoutingTable ( this . peerId , this . kBucketSize )
158174
159175 /**
160176 * Reference to the datastore, uses an in-memory store if none given.
@@ -184,6 +200,13 @@ class KadDHT extends EventEmitter {
184200
185201 this . _log = utils . logger ( this . peerId )
186202
203+ /**
204+ * Random walk management
205+ *
206+ * @type {RandomWalk }
207+ */
208+ this . randomWalk = new RandomWalk ( this , randomWalk )
209+
187210 /**
188211 * Keeps track of running queries
189212 *
@@ -214,14 +237,17 @@ class KadDHT extends EventEmitter {
214237 * Start listening to incoming connections.
215238 */
216239 start ( ) {
240+ if ( this . _running ) {
241+ return
242+ }
243+
217244 this . _running = true
245+ this . providers . start ( )
246+ this . _queryManager . start ( )
247+ this . network . start ( )
218248
219- return Promise . all ( [
220- this . providers . start ( ) ,
221- this . _queryManager . start ( ) ,
222- this . network . start ( ) ,
223- this . routingTable . start ( )
224- ] )
249+ // Start random walk, it will not run if it's disabled
250+ this . randomWalk . start ( )
225251 }
226252
227253 /**
@@ -230,13 +256,10 @@ class KadDHT extends EventEmitter {
230256 */
231257 stop ( ) {
232258 this . _running = false
233-
234- return Promise . all ( [
235- this . providers . stop ( ) ,
236- this . _queryManager . stop ( ) ,
237- this . network . stop ( ) ,
238- this . routingTable . stop ( )
239- ] )
259+ this . randomWalk . stop ( )
260+ this . network . stop ( )
261+ this . _queryManager . stop ( )
262+ this . providers . stop ( )
240263 }
241264
242265 /**
0 commit comments