Skip to content

Commit 9b22c6e

Browse files
Robert KielRobert Kielachingbrain
authoredJan 24, 2022
fix: prevent auto-dialer from dialing self (#1104)
Co-authored-by: Robert Kiel <robert.kiel@hoprnet.io> Co-authored-by: achingbrain <alex@achingbrain.net>
1 parent d8ceb0b commit 9b22c6e

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"it-map": "^1.0.4",
101101
"it-merge": "^1.0.0",
102102
"it-pipe": "^1.1.0",
103+
"it-sort": "^1.0.1",
103104
"it-take": "^1.0.0",
104105
"libp2p-crypto": "^0.21.2",
105106
"libp2p-interfaces": "^4.0.0",

‎src/connection-manager/auto-dialler.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const mergeOptions = require('merge-options')
55
// @ts-ignore retimer does not have types
66
const retimer = require('retimer')
77
const all = require('it-all')
8+
const { pipe } = require('it-pipe')
9+
const filter = require('it-filter')
10+
const sort = require('it-sort')
811

912
const log = Object.assign(debug('libp2p:connection-manager:auto-dialler'), {
1013
error: debug('libp2p:connection-manager:auto-dialler:err')
@@ -90,21 +93,27 @@ class AutoDialler {
9093
// Sort peers on whether we know protocols of public keys for them
9194
// TODO: assuming the `peerStore.getPeers()` order is stable this will mean
9295
// we keep trying to connect to the same peers?
93-
const peers = (await all(this._libp2p.peerStore.getPeers()))
94-
.sort((a, b) => {
96+
const peers = await pipe(
97+
this._libp2p.peerStore.getPeers(),
98+
(source) => filter(source, (peer) => !peer.id.equals(this._libp2p.peerId)),
99+
(source) => sort(source, (a, b) => {
95100
if (b.protocols && b.protocols.length && (!a.protocols || !a.protocols.length)) {
96101
return 1
97102
} else if (b.id.pubKey && !a.id.pubKey) {
98103
return 1
99104
}
100105
return -1
101-
})
106+
}),
107+
(source) => all(source)
108+
)
102109

103110
for (let i = 0; this._running && i < peers.length && this._libp2p.connections.size < minConnections; i++) {
104-
if (!this._libp2p.connectionManager.get(peers[i].id)) {
105-
log('connecting to a peerStore stored peer %s', peers[i].id.toB58String())
111+
const peer = peers[i]
112+
113+
if (!this._libp2p.connectionManager.get(peer.id)) {
114+
log('connecting to a peerStore stored peer %s', peer.id.toB58String())
106115
try {
107-
await this._libp2p.dialer.connectToPeer(peers[i].id)
116+
await this._libp2p.dialer.connectToPeer(peer.id)
108117
} catch (/** @type {any} */ err) {
109118
log.error('could not connect to peerStore stored peer', err)
110119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict'
2+
/* eslint-env mocha */
3+
4+
const { expect } = require('aegir/utils/chai')
5+
const sinon = require('sinon')
6+
const AutoDialler = require('../../src/connection-manager/auto-dialler')
7+
const pWaitFor = require('p-wait-for')
8+
const PeerId = require('peer-id')
9+
const delay = require('delay')
10+
11+
describe('Auto-dialler', () => {
12+
let autoDialler
13+
let libp2p
14+
let options
15+
16+
beforeEach(async () => {
17+
libp2p = {}
18+
options = {}
19+
autoDialler = new AutoDialler(libp2p, options)
20+
})
21+
22+
afterEach(async () => {
23+
sinon.restore()
24+
})
25+
26+
it('should not dial self', async () => {
27+
// peers with protocols are dialled before peers without protocols
28+
const self = {
29+
id: await PeerId.create(),
30+
protocols: [
31+
'/foo/bar'
32+
]
33+
}
34+
const other = {
35+
id: await PeerId.create(),
36+
protocols: []
37+
}
38+
39+
autoDialler._options.minConnections = 10
40+
libp2p.peerId = self.id
41+
libp2p.connections = {
42+
size: 1
43+
}
44+
libp2p.peerStore = {
45+
getPeers: sinon.stub().returns([self, other])
46+
}
47+
libp2p.connectionManager = {
48+
get: () => {}
49+
}
50+
libp2p.dialer = {
51+
connectToPeer: sinon.stub().resolves()
52+
}
53+
54+
await autoDialler.start()
55+
56+
await pWaitFor(() => libp2p.dialer.connectToPeer.callCount === 1)
57+
await delay(1000)
58+
59+
await autoDialler.stop()
60+
61+
expect(libp2p.dialer.connectToPeer.callCount).to.equal(1)
62+
expect(libp2p.dialer.connectToPeer.calledWith(self.id)).to.be.false()
63+
})
64+
})

0 commit comments

Comments
 (0)
Please sign in to comment.