Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 7f1fb26

Browse files
jacobheunAlan Shaw
authored andcommitted
feat: use libp2p auto dial (#1983)
1 parent 04bbd24 commit 7f1fb26

File tree

8 files changed

+28
-34
lines changed

8 files changed

+28
-34
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ You can see the bundle in action in the [custom libp2p example](examples/custom-
340340
- `peerDiscovery` (Array<[libp2p.PeerDiscovery](https://github.com/libp2p/interface-peer-discovery)>): An array of Libp2p peer discovery classes/instances to use _instead_ of the defaults. See [libp2p/peer-discovery](https://github.com/libp2p/interface-peer-discovery) for details. If passing a class, configuration can be passed using the config section below under the key corresponding to you module's unique `tag` (a static property on the class)
341341
- `config` (object):
342342
- `peerDiscovery` (object):
343+
- `autoDial` (boolean): Dial to discovered peers when under the Connection Manager min peer count watermark. (default `true`)
343344
- `[PeerDiscovery.tag]` (object): configuration for a peer discovery module
344345
- `enabled` (boolean): whether this module is enabled or disabled
345346
- `[custom config]` (any): other keys are specific to the module

examples/custom-libp2p/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ const libp2pBundle = (opts) => {
4444
peerBook,
4545
// Lets limit the connection managers peers and have it check peer health less frequently
4646
connectionManager: {
47-
maxPeers: 25,
47+
minPeers: 25,
48+
maxPeers: 100,
4849
pollInterval: 5000
4950
},
5051
modules: {
@@ -68,12 +69,13 @@ const libp2pBundle = (opts) => {
6869
},
6970
config: {
7071
peerDiscovery: {
72+
autoDial: true, // auto dial to peers we find when we have less peers than `connectionManager.minPeers`
7173
mdns: {
7274
interval: 10000,
7375
enabled: true
7476
},
7577
bootstrap: {
76-
interval: 10000,
78+
interval: 30e3,
7779
enabled: true,
7880
list: bootstrapList
7981
}
@@ -87,10 +89,15 @@ const libp2pBundle = (opts) => {
8789
}
8890
},
8991
dht: {
90-
kBucketSize: 20
92+
enabled: true,
93+
kBucketSize: 20,
94+
randomWalk: {
95+
enabled: true,
96+
interval: 10e3, // This is set low intentionally, so more peers are discovered quickly. Higher intervals are recommended
97+
timeout: 2e3 // End the query quickly since we're running so frequently
98+
}
9199
},
92100
EXPERIMENTAL: {
93-
dht: true,
94101
pubsub: true
95102
}
96103
}

examples/custom-libp2p/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
"license": "MIT",
1111
"dependencies": {
1212
"ipfs": "file:../../",
13-
"libp2p": "~0.24.0",
14-
"libp2p-bootstrap": "~0.9.3",
15-
"libp2p-kad-dht": "~0.11.1",
16-
"libp2p-mdns": "~0.12.0",
17-
"libp2p-mplex": "~0.8.4",
18-
"libp2p-secio": "~0.10.1",
19-
"libp2p-spdy": "~0.13.0",
13+
"libp2p": "~0.25.0",
14+
"libp2p-bootstrap": "~0.9.7",
15+
"libp2p-kad-dht": "~0.14.12",
16+
"libp2p-mdns": "~0.12.2",
17+
"libp2p-mplex": "~0.8.5",
18+
"libp2p-secio": "~0.11.1",
19+
"libp2p-spdy": "~0.13.3",
2020
"libp2p-tcp": "~0.13.0",
21-
"libp2p-websocket-star": "~0.9.0"
21+
"libp2p-websocket-star": "~0.10.2"
2222
}
2323
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
"joi": "^14.3.0",
128128
"just-flatten-it": "^2.1.0",
129129
"just-safe-set": "^2.1.0",
130-
"libp2p": "~0.25.0-rc.5",
130+
"libp2p": "~0.25.0-rc.6",
131131
"libp2p-bootstrap": "~0.9.3",
132132
"libp2p-crypto": "~0.16.0",
133133
"libp2p-kad-dht": "~0.14.12",

src/core/components/libp2p.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ module.exports = function libp2p (self, config) {
1717
const peerInfo = self._peerInfo
1818
const peerBook = self._peerInfoBook
1919
const libp2p = createBundle({ options, config, datastore, peerInfo, peerBook })
20-
let discoveredPeers = []
21-
22-
const noop = () => {}
23-
const putAndDial = peerInfo => {
24-
peerInfo = peerBook.put(peerInfo)
25-
if (!peerInfo.isConnected()) {
26-
libp2p.dial(peerInfo, noop)
27-
}
28-
}
2920

3021
libp2p.on('stop', () => {
3122
// Clear our addresses so we can start clean
@@ -36,16 +27,6 @@ module.exports = function libp2p (self, config) {
3627
peerInfo.multiaddrs.forEach((ma) => {
3728
self._print('Swarm listening on', ma.toString())
3829
})
39-
discoveredPeers.forEach(putAndDial)
40-
discoveredPeers = []
41-
})
42-
43-
libp2p.on('peer:discovery', (peerInfo) => {
44-
if (self.isOnline()) {
45-
putAndDial(peerInfo)
46-
} else {
47-
discoveredPeers.push(peerInfo)
48-
}
4930
})
5031

5132
libp2p.on('peer:connect', peerInfo => peerBook.put(peerInfo))
@@ -108,7 +89,6 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
10889
}
10990

11091
const libp2pOptions = mergeOptions(libp2pDefaults, get(options, 'libp2p', {}))
111-
11292
// Required inline to reduce startup time
11393
// Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified
11494
const Node = require('../runtime/libp2p-nodejs')

src/core/runtime/libp2p-browser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Node extends libp2p {
4343
},
4444
config: {
4545
peerDiscovery: {
46+
autoDial: true,
4647
bootstrap: {
4748
enabled: true
4849
},

src/core/runtime/libp2p-nodejs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Node extends libp2p {
4242
},
4343
config: {
4444
peerDiscovery: {
45+
autoDial: true,
4546
mdns: {
4647
enabled: true
4748
},

test/core/libp2p.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ describe('libp2p customization', function () {
106106

107107
_libp2p.start((err) => {
108108
expect(err).to.not.exist()
109-
expect(_libp2p._config).to.not.have.property('peerDiscovery')
109+
expect(_libp2p._config.peerDiscovery).to.eql({
110+
autoDial: true
111+
})
110112
expect(_libp2p._transport).to.have.length(1)
111113
done()
112114
})
@@ -130,6 +132,7 @@ describe('libp2p customization', function () {
130132
expect(err).to.not.exist()
131133
expect(_libp2p._config).to.deep.include({
132134
peerDiscovery: {
135+
autoDial: true,
133136
bootstrap: {
134137
enabled: true,
135138
list: []
@@ -193,6 +196,7 @@ describe('libp2p customization', function () {
193196
expect(err).to.not.exist()
194197
expect(_libp2p._config).to.deep.include({
195198
peerDiscovery: {
199+
autoDial: true,
196200
bootstrap: {
197201
enabled: true,
198202
list: []

0 commit comments

Comments
 (0)