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

Commit

Permalink
refactor: update libp2p config, component and startup to use async
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Dec 18, 2019
1 parent cb04ad7 commit a7c14b7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/core/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
}
33 changes: 16 additions & 17 deletions src/core/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@ 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()
})

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
Expand Down Expand Up @@ -82,7 +82,6 @@ function getLibp2pOptions ({ options, config, datastore, peerInfo, peerBook }) {
const libp2pDefaults = {
datastore,
peerInfo,
peerBook,
modules: {
contentRouting,
peerRouting
Expand Down Expand Up @@ -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')
})
}

Expand All @@ -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
)
}
16 changes: 7 additions & 9 deletions src/core/components/start.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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()

Expand Down
25 changes: 6 additions & 19 deletions src/core/runtime/libp2p-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 5 additions & 15 deletions src/core/runtime/libp2p-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down

0 comments on commit a7c14b7

Please sign in to comment.