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

Commit a7c14b7

Browse files
committed
refactor: update libp2p config, component and startup to use async
1 parent cb04ad7 commit a7c14b7

File tree

5 files changed

+35
-61
lines changed

5 files changed

+35
-61
lines changed

src/core/components/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports.object = {
2323
put: require('./object/put'),
2424
stat: require('./object/stat')
2525
}
26+
exports.libp2p = require('./libp2p')
2627
exports.ping = require('./ping')
2728
exports.start = require('./start')
2829
exports.stop = require('./stop')
@@ -37,6 +38,5 @@ exports.version = require('./version')
3738

3839
exports.legacy = { // TODO: these will be removed as the new API is completed
3940
dag: require('./dag'),
40-
libp2p: require('./libp2p'),
4141
pin: require('./pin')
4242
}

src/core/components/libp2p.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,42 @@ const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
99
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
1010
const PubsubRouters = require('../runtime/libp2p-pubsub-routers-nodejs')
1111

12-
module.exports = function libp2p (self, config) {
13-
const options = self._options || {}
14-
config = config || {}
12+
module.exports = ({
13+
constructorOptions,
14+
peerInfo,
15+
repo,
16+
print,
17+
config
18+
}) => {
19+
const { datastore } = repo
20+
const libp2pOptions = getLibp2pOptions({ options: constructorOptions, config, datastore, peerInfo })
1521

16-
const { datastore } = self._repo
17-
const peerInfo = self._peerInfo
18-
const peerBook = self._peerInfoBook
19-
20-
const libp2pOptions = getLibp2pOptions({ options, config, datastore, peerInfo, peerBook })
2122
let libp2p
2223

2324
if (typeof options.libp2p === 'function') {
24-
libp2p = options.libp2p({ libp2pOptions, options, config, datastore, peerInfo, peerBook })
25+
libp2p = options.libp2p({ libp2pOptions, options, config, datastore, peerInfo })
2526
} else {
2627
// Required inline to reduce startup time
2728
const Libp2p = require('libp2p')
2829
libp2p = new Libp2p(mergeOptions(libp2pOptions, get(options, 'libp2p', {})))
2930
}
3031

32+
3133
libp2p.on('stop', () => {
3234
// Clear our addresses so we can start clean
3335
peerInfo.multiaddrs.clear()
3436
})
3537

3638
libp2p.on('start', () => {
3739
peerInfo.multiaddrs.forEach((ma) => {
38-
self._print('Swarm listening on', ma.toString())
40+
print('Swarm listening on', ma.toString())
3941
})
4042
})
4143

42-
libp2p.on('peer:connect', peerInfo => peerBook.put(peerInfo))
43-
4444
return libp2p
4545
}
4646

47-
function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) {
47+
function getLibp2pOptions ({ options, config, datastore, peerInfo }) {
4848
// Set up Delegate Routing based on the presence of Delegates in the config
4949
let contentRouting
5050
let peerRouting
@@ -82,7 +82,6 @@ function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) {
8282
const libp2pDefaults = {
8383
datastore,
8484
peerInfo,
85-
peerBook,
8685
modules: {
8786
contentRouting,
8887
peerRouting
@@ -138,8 +137,8 @@ function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) {
138137
},
139138
connectionManager: get(options, 'connectionManager',
140139
{
141-
maxPeers: get(config, 'Swarm.ConnMgr.HighWater'),
142-
minPeers: get(config, 'Swarm.ConnMgr.LowWater')
140+
maxConnections: get(config, 'Swarm.ConnMgr.HighWater'),
141+
minConnections: get(config, 'Swarm.ConnMgr.LowWater')
143142
})
144143
}
145144

@@ -150,7 +149,7 @@ function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) {
150149
// Merge defaults with Node.js/browser/other environments options and configuration
151150
return mergeOptions(
152151
libp2pDefaults,
153-
getEnvLibp2pOptions({ options, config, datastore, peerInfo, peerBook }),
152+
getEnvLibp2pOptions(),
154153
libp2pOptions
155154
)
156155
}

src/core/components/start.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const Bitswap = require('ipfs-bitswap')
4-
const PeerBook = require('peer-book')
54
const IPNS = require('../ipns')
65
const routingConfig = require('../ipns/routing/config')
76
const defer = require('p-defer')
@@ -33,14 +32,13 @@ module.exports = ({
3332

3433
const config = await repo.config.get()
3534

36-
const peerBook = new PeerBook()
37-
const libp2p = Commands.legacy.libp2p({
38-
_options: constructorOptions,
39-
_repo: repo,
40-
_peerInfo: peerInfo,
41-
_peerInfoBook: peerBook,
42-
_print: print
43-
}, config)
35+
const libp2p = Commands.libp2p({
36+
constructorOptions,
37+
repo,
38+
peerInfo,
39+
print,
40+
config
41+
})
4442

4543
await libp2p.start()
4644

src/core/runtime/libp2p-browser.js

+6-19
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,23 @@
22

33
const WS = require('libp2p-websockets')
44
const WebRTCStar = require('libp2p-webrtc-star')
5-
const WebSocketStarMulti = require('libp2p-websocket-star-multi')
65
const Multiplex = require('pull-mplex')
76
const SECIO = require('libp2p-secio')
87
const Bootstrap = require('libp2p-bootstrap')
98
const KadDHT = require('libp2p-kad-dht')
109
const GossipSub = require('libp2p-gossipsub')
11-
const multiaddr = require('multiaddr')
12-
13-
module.exports = ({ peerInfo, options }) => {
14-
const wrtcstar = new WebRTCStar({ id: peerInfo.id })
15-
16-
// this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41
17-
// const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
18-
const wsstarServers = peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star'))
19-
peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers
20-
const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: peerInfo.id, ignore_no_online: !wsstarServers.length || options.wsStarIgnoreErrors })
2110

11+
module.exports = () => {
2212
return {
23-
switch: {
24-
denyTTL: 2 * 60 * 1e3, // 2 minute base
25-
denyAttempts: 5, // back off 5 times
26-
maxParallelDials: 100,
27-
maxColdCalls: 25,
28-
dialTimeout: 20e3
13+
dialer: {
14+
maxParallelDials: 150, // 150 total parallel multiaddr dials
15+
maxDialsPerPeer: 4, // Allow 4 multiaddrs to be dialed per peer in parallel
16+
dialTimeout: 10e3 // 10 second dial timeout per peer dial
2917
},
3018
modules: {
3119
transport: [
3220
WS,
33-
wrtcstar,
34-
wsstar
21+
WebRTCStar
3522
],
3623
streamMuxer: [
3724
Multiplex

src/core/runtime/libp2p-nodejs.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,18 @@
33
const TCP = require('libp2p-tcp')
44
const MulticastDNS = require('libp2p-mdns')
55
const WS = require('libp2p-websockets')
6-
const WebSocketStarMulti = require('libp2p-websocket-star-multi')
76
const Bootstrap = require('libp2p-bootstrap')
87
const KadDHT = require('libp2p-kad-dht')
98
const GossipSub = require('libp2p-gossipsub')
109
const Multiplex = require('pull-mplex')
1110
const SECIO = require('libp2p-secio')
12-
const multiaddr = require('multiaddr')
13-
14-
module.exports = ({ peerInfo, options }) => {
15-
// this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41
16-
// const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
17-
const wsstarServers = peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star'))
18-
peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers
19-
const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: peerInfo.id, ignore_no_online: !wsstarServers.length || options.wsStarIgnoreErrors })
2011

12+
module.exports = () => {
2113
return {
22-
switch: {
23-
denyTTL: 2 * 60 * 1e3, // 2 minute base
24-
denyAttempts: 5, // back off 5 times
25-
maxParallelDials: 150,
26-
maxColdCalls: 50,
27-
dialTimeout: 10e3 // Be strict with dial time
14+
dialer: {
15+
maxParallelDials: 150, // 150 total parallel multiaddr dials
16+
maxDialsPerPeer: 4, // Allow 4 multiaddrs to be dialed per peer in parallel
17+
dialTimeout: 10e3 // 10 second dial timeout per peer dial
2818
},
2919
modules: {
3020
transport: [

0 commit comments

Comments
 (0)