Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 4f79899

Browse files
authoredNov 25, 2021
fix: do not pollute routing table with useless peers (#254)
If a remote peer doesn't have private addresses, do not add it to the routing table for the lan DHT, if they don't have public addresses, do not add them to the routing table for the wan DHT. Also adds more logging.
1 parent 39e10e3 commit 4f79899

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed
 

‎src/kad-dht.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ class KadDHT extends EventEmitter {
257257
})
258258
this._topologyListener = new TopologyListener({
259259
registrar: libp2p.registrar,
260-
protocol: this._protocol
260+
protocol: this._protocol,
261+
lan
261262
})
262263
this._querySelf = new QuerySelf({
263264
peerId: libp2p.peerId,
@@ -268,17 +269,22 @@ class KadDHT extends EventEmitter {
268269

269270
// handle peers being discovered during processing of DHT messages
270271
this._network.on('peer', (peerData) => {
271-
this._routingTable.add(peerData.id).catch(err => {
272-
this._log.error(`Could not add ${peerData.id} to routing table`, err)
272+
this.onPeerConnect(peerData).catch(err => {
273+
this._log.error(`could not add ${peerData.id} to routing table`, err)
273274
})
274275

275276
this.emit('peer', peerData)
276277
})
277278

278279
// handle peers being discovered via other peer discovery mechanisms
279280
this._topologyListener.on('peer', async (peerId) => {
280-
this._routingTable.add(peerId).catch(err => {
281-
this._log.error(`Could not add ${peerId} to routing table`, err)
281+
const peerData = {
282+
id: peerId,
283+
multiaddrs: (this._libp2p.peerStore.addressBook.get(peerId) || []).map((/** @type {{ multiaddr: Multiaddr }} */ addr) => addr.multiaddr)
284+
}
285+
286+
this.onPeerConnect(peerData).catch(err => {
287+
this._log.error(`could not add ${peerData.id} to routing table`, err)
282288
})
283289
})
284290
}
@@ -287,24 +293,23 @@ class KadDHT extends EventEmitter {
287293
* @param {PeerData} peerData
288294
*/
289295
async onPeerConnect (peerData) {
296+
this._log('peer %p connected', peerData.id)
297+
290298
if (this._lan) {
291299
peerData = removePublicAddresses(peerData)
292300
} else {
293301
peerData = removePrivateAddresses(peerData)
294302
}
295303

296304
if (!peerData.multiaddrs.length) {
305+
this._log('ignoring %p as they do not have any %s addresses in %s', peerData.id, this._lan ? 'private' : 'public', peerData.multiaddrs.map(addr => addr.toString()))
297306
return
298307
}
299308

300309
try {
301-
const has = await this._routingTable.find(peerData.id)
302-
303-
if (!has) {
304-
await this._routingTable.add(peerData.id)
305-
}
310+
await this._routingTable.add(peerData.id)
306311
} catch (/** @type {any} */ err) {
307-
this._log.error('Could not add %p to routing table', peerData.id, err)
312+
this._log.error('could not add %p to routing table', peerData.id, err)
308313
}
309314
}
310315

‎src/providers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class Providers {
180180
*/
181181
async addProvider (cid, provider) { // eslint-disable-line require-await
182182
return this.syncQueue.add(async () => {
183-
log('addProvider %s', cid.toString())
183+
log('%p provides %s', provider, cid)
184184
const provs = await this._getProvidersMap(cid)
185185

186186
log('loaded %s provs', provs.size)
@@ -202,7 +202,7 @@ class Providers {
202202
*/
203203
async getProviders (cid) { // eslint-disable-line require-await
204204
return this.syncQueue.add(async () => {
205-
log('getProviders %s', cid.toString())
205+
log('get providers for %s', cid)
206206
const provs = await this._getProvidersMap(cid)
207207

208208
return [...provs.keys()].map(peerIdStr => {

‎src/routing-table/index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ class RoutingTable {
8383

8484
try {
8585
timeoutController = new TimeoutController(this._pingTimeout)
86-
this._log(`Pinging old contact ${oldContact.peer}`)
86+
this._log(`pinging old contact ${oldContact.peer}`)
8787
const { stream } = await this._dialer.dialProtocol(oldContact.peer, PROTOCOL_DHT, {
8888
signal: timeoutController.signal
8989
})
9090
await stream.close()
9191
responded++
9292
} catch (/** @type {any} */ err) {
93-
this._log.error('Could not ping peer %p', oldContact.peer, err)
94-
this._log(`Evicting old contact after ping failed ${oldContact.peer}`)
93+
this._log.error('could not ping peer %p', oldContact.peer, err)
94+
this._log(`evicting old contact after ping failed ${oldContact.peer}`)
9595
this.kb.remove(oldContact.id)
9696
} finally {
9797
if (timeoutController) {
@@ -102,11 +102,11 @@ class RoutingTable {
102102
)
103103

104104
if (responded < oldContacts.length) {
105-
this._log(`Adding new contact ${newContact.peer}`)
105+
this._log(`adding new contact ${newContact.peer}`)
106106
this.kb.add(newContact)
107107
}
108108
} catch (/** @type {any} */ err) {
109-
this._log.error('Could not process k-bucket ping event', err)
109+
this._log.error('could not process k-bucket ping event', err)
110110
}
111111
})
112112
}
@@ -168,6 +168,8 @@ class RoutingTable {
168168
const id = await utils.convertPeerId(peer)
169169

170170
this.kb.add({ id: id, peer: peer })
171+
172+
this._log('added %p with kad id %b', peer, id)
171173
}
172174

173175
/**

‎src/rpc/handlers/get-providers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class GetProvidersHandler {
5151
throw errcode(new Error(`Invalid CID: ${err.message}`), 'ERR_INVALID_CID')
5252
}
5353

54-
log('%p asking for providers for %s', peerId, cid.toString())
54+
log('%p asking for providers for %s', peerId, cid)
5555

5656
const [peers, closer] = await Promise.all([
5757
this._providers.getProviders(cid),

‎src/topology-listener.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology')
44
const { EventEmitter } = require('events')
5+
const utils = require('./utils')
56

67
/**
78
* Receives notifications of new peers joining the network that support the DHT protocol
@@ -13,10 +14,12 @@ class TopologyListener extends EventEmitter {
1314
* @param {object} params
1415
* @param {import('./types').Registrar} params.registrar
1516
* @param {string} params.protocol
17+
* @param {boolean} params.lan
1618
*/
17-
constructor ({ registrar, protocol }) {
19+
constructor ({ registrar, protocol, lan }) {
1820
super()
1921

22+
this._log = utils.logger(`libp2p:kad-dht:topology-listener:${lan ? 'lan' : 'wan'}:network`)
2023
this._running = false
2124
this._registrar = registrar
2225
this._protocol = protocol
@@ -37,6 +40,7 @@ class TopologyListener extends EventEmitter {
3740
multicodecs: [this._protocol],
3841
handlers: {
3942
onConnect: (peerId) => {
43+
this._log('observed peer that with protocol %s %p', this._protocol, peerId)
4044
this.emit('peer', peerId)
4145
},
4246
onDisconnect: () => {}

‎test/utils/test-dht.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ class TestDHT {
140140
const [c0, c1] = ConnectionPair()
141141
const routingTableChecks = []
142142

143+
// Libp2p dial adds multiaddrs to the addressBook
144+
dhtA._libp2p.peerStore.addressBook.add(dhtB._libp2p.peerId, dhtB._libp2p.multiaddrs)
145+
dhtB._libp2p.peerStore.addressBook.add(dhtA._libp2p.peerId, dhtA._libp2p.multiaddrs)
146+
143147
// Notice peers of connection
144148
if (!dhtB._clientMode) {
145149
// B is a server, trigger connect events on A
@@ -171,10 +175,6 @@ class TestDHT {
171175
})
172176
}
173177

174-
// Libp2p dial adds multiaddrs to the addressBook
175-
dhtA._libp2p.peerStore.addressBook.add(dhtB._libp2p.peerId, dhtB._libp2p.multiaddrs)
176-
dhtB._libp2p.peerStore.addressBook.add(dhtA._libp2p.peerId, dhtA._libp2p.multiaddrs)
177-
178178
// Check routing tables
179179
return Promise.all(routingTableChecks.map(check => {
180180
return pRetry(check, { retries: 50 })

0 commit comments

Comments
 (0)
This repository has been archived.