diff --git a/src/core/components/index.js b/src/core/components/index.js index 4a9bcab492..95d3ae7e77 100644 --- a/src/core/components/index.js +++ b/src/core/components/index.js @@ -23,6 +23,7 @@ exports.object = { put: require('./object/put'), stat: require('./object/stat') } +exports.libp2p = require('./libp2p') exports.ping = require('./ping') exports.start = require('./start') exports.stop = require('./stop') @@ -37,6 +38,5 @@ exports.version = require('./version') exports.legacy = { // TODO: these will be removed as the new API is completed dag: require('./dag'), - libp2p: require('./libp2p'), pin: require('./pin') } diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index e0b670b7d9..348c2509f7 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -9,25 +9,27 @@ const DelegatedPeerRouter = require('libp2p-delegated-peer-routing') const DelegatedContentRouter = require('libp2p-delegated-content-routing') const PubsubRouters = require('../runtime/libp2p-pubsub-routers-nodejs') -module.exports = function libp2p (self, config) { - const options = self._options || {} - config = config || {} +module.exports = ({ + constructorOptions, + peerInfo, + repo, + print, + config +}) => { + const { datastore } = repo + const libp2pOptions = getLibp2pOptions({ options: constructorOptions, config, datastore, peerInfo }) - const { datastore } = self._repo - const peerInfo = self._peerInfo - const peerBook = self._peerInfoBook - - const libp2pOptions = getLibp2pOptions({ options, config, datastore, peerInfo, peerBook }) let libp2p if (typeof options.libp2p === 'function') { - libp2p = options.libp2p({ libp2pOptions, options, config, datastore, peerInfo, peerBook }) + libp2p = options.libp2p({ libp2pOptions, options, config, datastore, peerInfo }) } else { // Required inline to reduce startup time const Libp2p = require('libp2p') libp2p = new Libp2p(mergeOptions(libp2pOptions, get(options, 'libp2p', {}))) } + libp2p.on('stop', () => { // Clear our addresses so we can start clean peerInfo.multiaddrs.clear() @@ -35,16 +37,14 @@ module.exports = function libp2p (self, config) { libp2p.on('start', () => { peerInfo.multiaddrs.forEach((ma) => { - self._print('Swarm listening on', ma.toString()) + print('Swarm listening on', ma.toString()) }) }) - libp2p.on('peer:connect', peerInfo => peerBook.put(peerInfo)) - return libp2p } -function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) { +function getLibp2pOptions ({ options, config, datastore, peerInfo }) { // Set up Delegate Routing based on the presence of Delegates in the config let contentRouting let peerRouting @@ -82,7 +82,6 @@ function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) { const libp2pDefaults = { datastore, peerInfo, - peerBook, modules: { contentRouting, peerRouting @@ -138,8 +137,8 @@ function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) { }, connectionManager: get(options, 'connectionManager', { - maxPeers: get(config, 'Swarm.ConnMgr.HighWater'), - minPeers: get(config, 'Swarm.ConnMgr.LowWater') + maxConnections: get(config, 'Swarm.ConnMgr.HighWater'), + minConnections: get(config, 'Swarm.ConnMgr.LowWater') }) } @@ -150,7 +149,7 @@ function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) { // Merge defaults with Node.js/browser/other environments options and configuration return mergeOptions( libp2pDefaults, - getEnvLibp2pOptions({ options, config, datastore, peerInfo, peerBook }), + getEnvLibp2pOptions(), libp2pOptions ) } diff --git a/src/core/components/start.js b/src/core/components/start.js index 2465be61f7..0c31349636 100644 --- a/src/core/components/start.js +++ b/src/core/components/start.js @@ -1,7 +1,6 @@ 'use strict' const Bitswap = require('ipfs-bitswap') -const PeerBook = require('peer-book') const IPNS = require('../ipns') const routingConfig = require('../ipns/routing/config') const defer = require('p-defer') @@ -33,14 +32,13 @@ module.exports = ({ const config = await repo.config.get() - const peerBook = new PeerBook() - const libp2p = Commands.legacy.libp2p({ - _options: constructorOptions, - _repo: repo, - _peerInfo: peerInfo, - _peerInfoBook: peerBook, - _print: print - }, config) + const libp2p = Commands.libp2p({ + constructorOptions, + repo, + peerInfo, + print, + config + }) await libp2p.start() diff --git a/src/core/runtime/libp2p-browser.js b/src/core/runtime/libp2p-browser.js index 7c8a921886..9b18bec8dd 100644 --- a/src/core/runtime/libp2p-browser.js +++ b/src/core/runtime/libp2p-browser.js @@ -2,36 +2,23 @@ const WS = require('libp2p-websockets') const WebRTCStar = require('libp2p-webrtc-star') -const WebSocketStarMulti = require('libp2p-websocket-star-multi') const Multiplex = require('pull-mplex') const SECIO = require('libp2p-secio') const Bootstrap = require('libp2p-bootstrap') const KadDHT = require('libp2p-kad-dht') const GossipSub = require('libp2p-gossipsub') -const multiaddr = require('multiaddr') - -module.exports = ({ peerInfo, options }) => { - const wrtcstar = new WebRTCStar({ id: peerInfo.id }) - - // this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41 - // const wsstar = new WebSocketStar({ id: _options.peerInfo.id }) - const wsstarServers = peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star')) - peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers - const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: peerInfo.id, ignore_no_online: !wsstarServers.length || options.wsStarIgnoreErrors }) +module.exports = () => { return { - switch: { - denyTTL: 2 * 60 * 1e3, // 2 minute base - denyAttempts: 5, // back off 5 times - maxParallelDials: 100, - maxColdCalls: 25, - dialTimeout: 20e3 + dialer: { + maxParallelDials: 150, // 150 total parallel multiaddr dials + maxDialsPerPeer: 4, // Allow 4 multiaddrs to be dialed per peer in parallel + dialTimeout: 10e3 // 10 second dial timeout per peer dial }, modules: { transport: [ WS, - wrtcstar, - wsstar + WebRTCStar ], streamMuxer: [ Multiplex diff --git a/src/core/runtime/libp2p-nodejs.js b/src/core/runtime/libp2p-nodejs.js index fb8d488752..9ac35ccca5 100644 --- a/src/core/runtime/libp2p-nodejs.js +++ b/src/core/runtime/libp2p-nodejs.js @@ -3,28 +3,18 @@ const TCP = require('libp2p-tcp') const MulticastDNS = require('libp2p-mdns') const WS = require('libp2p-websockets') -const WebSocketStarMulti = require('libp2p-websocket-star-multi') const Bootstrap = require('libp2p-bootstrap') const KadDHT = require('libp2p-kad-dht') const GossipSub = require('libp2p-gossipsub') const Multiplex = require('pull-mplex') const SECIO = require('libp2p-secio') -const multiaddr = require('multiaddr') - -module.exports = ({ peerInfo, options }) => { - // this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41 - // const wsstar = new WebSocketStar({ id: _options.peerInfo.id }) - const wsstarServers = peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star')) - peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers - const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: peerInfo.id, ignore_no_online: !wsstarServers.length || options.wsStarIgnoreErrors }) +module.exports = () => { return { - switch: { - denyTTL: 2 * 60 * 1e3, // 2 minute base - denyAttempts: 5, // back off 5 times - maxParallelDials: 150, - maxColdCalls: 50, - dialTimeout: 10e3 // Be strict with dial time + dialer: { + maxParallelDials: 150, // 150 total parallel multiaddr dials + maxDialsPerPeer: 4, // Allow 4 multiaddrs to be dialed per peer in parallel + dialTimeout: 10e3 // 10 second dial timeout per peer dial }, modules: { transport: [