2018-02-07 07:48:37 +00:00
|
|
|
'use strict'
|
|
|
|
|
2020-11-25 18:50:23 +01:00
|
|
|
const debug = require('debug')
|
2020-12-10 14:48:14 +01:00
|
|
|
const log = Object.assign(debug('libp2p:peer-routing'), {
|
|
|
|
error: debug('libp2p:peer-routing:err')
|
|
|
|
})
|
|
|
|
const errCode = require('err-code')
|
2021-01-21 12:41:27 +00:00
|
|
|
const {
|
|
|
|
storeAddresses,
|
|
|
|
uniquePeers,
|
|
|
|
requirePeers
|
|
|
|
} = require('./content-routing/utils')
|
|
|
|
|
|
|
|
const merge = require('it-merge')
|
|
|
|
const { pipe } = require('it-pipe')
|
|
|
|
const first = require('it-first')
|
|
|
|
const drain = require('it-drain')
|
|
|
|
const filter = require('it-filter')
|
2020-11-25 18:50:23 +01:00
|
|
|
const {
|
|
|
|
setDelayedInterval,
|
|
|
|
clearDelayedInterval
|
|
|
|
} = require('set-delayed-interval')
|
2021-01-21 12:41:27 +00:00
|
|
|
const PeerId = require('peer-id')
|
2020-11-25 18:50:23 +01:00
|
|
|
|
|
|
|
/**
|
2020-12-10 14:48:14 +01:00
|
|
|
* @typedef {import('multiaddr')} Multiaddr
|
2020-11-25 18:50:23 +01:00
|
|
|
*/
|
|
|
|
class PeerRouting {
|
|
|
|
/**
|
|
|
|
* @class
|
2020-12-10 14:48:14 +01:00
|
|
|
* @param {import('./')} libp2p
|
2020-11-25 18:50:23 +01:00
|
|
|
*/
|
|
|
|
constructor (libp2p) {
|
|
|
|
this._peerId = libp2p.peerId
|
|
|
|
this._peerStore = libp2p.peerStore
|
|
|
|
this._routers = libp2p._modules.peerRouting || []
|
|
|
|
|
2021-01-21 12:41:27 +00:00
|
|
|
// If we have the dht, add it to the available peer routers
|
2020-11-25 18:50:23 +01:00
|
|
|
if (libp2p._dht) {
|
2021-01-21 12:41:27 +00:00
|
|
|
this._routers.push(libp2p._dht)
|
2020-11-25 18:50:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this._refreshManagerOptions = libp2p._options.peerRouting.refreshManager
|
|
|
|
|
|
|
|
this._findClosestPeersTask = this._findClosestPeersTask.bind(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start peer routing service.
|
|
|
|
*/
|
|
|
|
start () {
|
|
|
|
if (!this._routers.length || this._timeoutId || !this._refreshManagerOptions.enabled) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this._timeoutId = setDelayedInterval(
|
|
|
|
this._findClosestPeersTask, this._refreshManagerOptions.interval, this._refreshManagerOptions.bootDelay
|
|
|
|
)
|
|
|
|
}
|
2018-10-19 16:28:28 +02:00
|
|
|
|
2020-11-25 18:50:23 +01:00
|
|
|
/**
|
|
|
|
* Recurrent task to find closest peers and add their addresses to the Address Book.
|
|
|
|
*/
|
|
|
|
async _findClosestPeersTask () {
|
|
|
|
try {
|
2021-01-21 12:41:27 +00:00
|
|
|
// nb getClosestPeers adds the addresses to the address book
|
|
|
|
await drain(this.getClosestPeers(this._peerId.id))
|
2020-11-25 18:50:23 +01:00
|
|
|
} catch (err) {
|
|
|
|
log.error(err)
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 16:28:28 +02:00
|
|
|
|
2020-11-25 18:50:23 +01:00
|
|
|
/**
|
|
|
|
* Stop peer routing service.
|
|
|
|
*/
|
|
|
|
stop () {
|
|
|
|
clearDelayedInterval(this._timeoutId)
|
2018-10-19 16:28:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:50:23 +01:00
|
|
|
/**
|
2021-01-21 12:41:27 +00:00
|
|
|
* Iterates over all peer routers in parallel to find the given peer.
|
2020-11-25 18:50:23 +01:00
|
|
|
*
|
2020-12-11 17:53:37 +01:00
|
|
|
* @param {PeerId} id - The id of the peer to find
|
2020-11-25 18:50:23 +01:00
|
|
|
* @param {object} [options]
|
|
|
|
* @param {number} [options.timeout] - How long the query should run
|
|
|
|
* @returns {Promise<{ id: PeerId, multiaddrs: Multiaddr[] }>}
|
|
|
|
*/
|
|
|
|
async findPeer (id, options) { // eslint-disable-line require-await
|
|
|
|
if (!this._routers.length) {
|
|
|
|
throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')
|
|
|
|
}
|
|
|
|
|
2021-01-21 12:41:27 +00:00
|
|
|
const output = await pipe(
|
|
|
|
merge(
|
|
|
|
...this._routers.map(router => [router.findPeer(id, options)])
|
|
|
|
),
|
|
|
|
(source) => filter(source, Boolean),
|
|
|
|
(source) => storeAddresses(source, this._peerStore),
|
|
|
|
(source) => first(source)
|
|
|
|
)
|
2020-11-25 18:50:23 +01:00
|
|
|
|
2021-01-21 12:41:27 +00:00
|
|
|
if (output) {
|
|
|
|
return output
|
|
|
|
}
|
2018-11-05 13:56:45 +01:00
|
|
|
|
2021-01-21 12:41:27 +00:00
|
|
|
throw errCode(new Error('not found'), 'NOT_FOUND')
|
2020-11-25 18:50:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to find the closest peers on the network to the given key.
|
|
|
|
*
|
|
|
|
* @param {Uint8Array} key - A CID like key
|
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {number} [options.timeout=30e3] - How long the query can take.
|
|
|
|
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
|
|
|
|
*/
|
|
|
|
async * getClosestPeers (key, options = { timeout: 30e3 }) {
|
|
|
|
if (!this._routers.length) {
|
|
|
|
throw errCode(new Error('No peer routers available'), 'NO_ROUTERS_AVAILABLE')
|
|
|
|
}
|
2018-10-19 16:28:28 +02:00
|
|
|
|
2021-01-21 12:41:27 +00:00
|
|
|
yield * pipe(
|
|
|
|
merge(
|
|
|
|
...this._routers.map(router => router.getClosestPeers(key, options))
|
|
|
|
),
|
|
|
|
(source) => storeAddresses(source, this._peerStore),
|
|
|
|
(source) => uniquePeers(source),
|
|
|
|
(source) => requirePeers(source)
|
2020-11-25 18:50:23 +01:00
|
|
|
)
|
2018-02-07 07:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 18:50:23 +01:00
|
|
|
|
|
|
|
module.exports = PeerRouting
|