chore: minor changes from lodestar testing

This commit is contained in:
Vasco Santos
2020-12-03 16:46:36 +01:00
parent 77a02573e0
commit 55910c8797
3 changed files with 118 additions and 114 deletions

View File

@ -18,16 +18,22 @@ const pAny = require('p-any')
* @property {Uint8Array} val * @property {Uint8Array} val
*/ */
module.exports = (node) => { class ContentRouting {
const routers = node._modules.contentRouting || [] /**
const dht = node._dht * @class
* @param {import('./')} libp2p
*/
constructor (libp2p) {
this.libp2p = libp2p
this.routers = libp2p._modules.contentRouting || []
this.dht = libp2p._dht
// If we have the dht, make it first // If we have the dht, make it first
if (dht) { if (this.dht) {
routers.unshift(dht) this.routers.unshift(this.dht)
}
} }
return {
/** /**
* Iterates over all content routers in series to find providers of the given key. * Iterates over all content routers in series to find providers of the given key.
* Once a content router succeeds, iteration will stop. * Once a content router succeeds, iteration will stop.
@ -39,12 +45,12 @@ module.exports = (node) => {
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>} * @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
*/ */
async * findProviders (key, options) { async * findProviders (key, options) {
if (!routers.length) { if (!this.routers.length) {
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE') throw errCode(new Error('No content this.routers available'), 'NO_ROUTERS_AVAILABLE')
} }
const result = await pAny( const result = await pAny(
routers.map(async (router) => { this.routers.map(async (router) => {
const provs = await all(router.findProviders(key, options)) const provs = await all(router.findProviders(key, options))
if (!provs || !provs.length) { if (!provs || !provs.length) {
@ -57,7 +63,7 @@ module.exports = (node) => {
for (const peer of result) { for (const peer of result) {
yield peer yield peer
} }
}, }
/** /**
* Iterates over all content routers in parallel to notify it is * Iterates over all content routers in parallel to notify it is
@ -67,12 +73,12 @@ module.exports = (node) => {
* @returns {Promise<void[]>} * @returns {Promise<void[]>}
*/ */
async provide (key) { // eslint-disable-line require-await async provide (key) { // eslint-disable-line require-await
if (!routers.length) { if (!this.routers.length) {
throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE') throw errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE')
} }
return Promise.all(routers.map((router) => router.provide(key))) return Promise.all(this.routers.map((router) => router.provide(key)))
}, }
/** /**
* Store the given key/value pair in the DHT. * Store the given key/value pair in the DHT.
@ -84,12 +90,12 @@ module.exports = (node) => {
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async put (key, value, options) { // eslint-disable-line require-await async put (key, value, options) { // eslint-disable-line require-await
if (!node.isStarted() || !dht.isStarted) { if (!this.libp2p.isStarted() || !this.dht.isStarted) {
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
} }
return dht.put(key, value, options) return this.dht.put(key, value, options)
}, }
/** /**
* Get the value to the given key. * Get the value to the given key.
@ -101,12 +107,12 @@ module.exports = (node) => {
* @returns {Promise<GetData>} * @returns {Promise<GetData>}
*/ */
async get (key, options) { // eslint-disable-line require-await async get (key, options) { // eslint-disable-line require-await
if (!node.isStarted() || !dht.isStarted) { if (!this.libp2p.isStarted() || !this.dht.isStarted) {
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
} }
return dht.get(key, options) return this.dht.get(key, options)
}, }
/** /**
* Get the `n` values to the given key without sorting. * Get the `n` values to the given key without sorting.
@ -118,11 +124,12 @@ module.exports = (node) => {
* @returns {Promise<GetData[]>} * @returns {Promise<GetData[]>}
*/ */
async getMany (key, nVals, options) { // eslint-disable-line require-await async getMany (key, nVals, options) { // eslint-disable-line require-await
if (!node.isStarted() || !dht.isStarted) { if (!this.libp2p.isStarted() || !this.dht.isStarted) {
throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED)
} }
return dht.getMany(key, nVals, options) return this.dht.getMany(key, nVals, options)
}
} }
} }
module.exports = ContentRouting

View File

@ -11,7 +11,7 @@ const errCode = require('err-code')
const PeerId = require('peer-id') const PeerId = require('peer-id')
const PeerRouting = require('./peer-routing') const PeerRouting = require('./peer-routing')
const contentRouting = require('./content-routing') const ContentRouting = require('./content-routing')
const getPeer = require('./get-peer') const getPeer = require('./get-peer')
const { validate: validateConfig } = require('./config') const { validate: validateConfig } = require('./config')
const { codes, messages } = require('./errors') const { codes, messages } = require('./errors')
@ -242,7 +242,7 @@ class Libp2p extends EventEmitter {
// Attach remaining APIs // Attach remaining APIs
// peer and content routing will automatically get modules from _modules and _dht // peer and content routing will automatically get modules from _modules and _dht
this.peerRouting = new PeerRouting(this) this.peerRouting = new PeerRouting(this)
this.contentRouting = contentRouting(this) this.contentRouting = new ContentRouting(this)
// Mount default protocols // Mount default protocols
ping.mount(this) ping.mount(this)

View File

@ -13,9 +13,6 @@ const passthrough = data => data
* @typedef {import('./')} PeerStore * @typedef {import('./')} PeerStore
*/ */
/**
* @template T
*/
class Book { class Book {
/** /**
* The Book is the skeleton for the PeerStore books. * The Book is the skeleton for the PeerStore books.
@ -25,7 +22,7 @@ class Book {
* @param {PeerStore} properties.peerStore - PeerStore instance. * @param {PeerStore} properties.peerStore - PeerStore instance.
* @param {string} properties.eventName - Name of the event to emit by the PeerStore. * @param {string} properties.eventName - Name of the event to emit by the PeerStore.
* @param {string} properties.eventProperty - Name of the property to emit by the PeerStore. * @param {string} properties.eventProperty - Name of the property to emit by the PeerStore.
* @param {(data: T) => T[]} [properties.eventTransformer] - Transformer function of the provided data for being emitted. * @param {(data: any) => any[]} [properties.eventTransformer] - Transformer function of the provided data for being emitted.
*/ */
constructor ({ peerStore, eventName, eventProperty, eventTransformer = passthrough }) { constructor ({ peerStore, eventName, eventProperty, eventTransformer = passthrough }) {
this._ps = peerStore this._ps = peerStore
@ -36,7 +33,7 @@ class Book {
/** /**
* Map known peers to their data. * Map known peers to their data.
* *
* @type {Map<string, T[]|T>} * @type {Map<string, any[]|any>}
*/ */
this.data = new Map() this.data = new Map()
} }
@ -45,7 +42,7 @@ class Book {
* Set known data of a provided peer. * Set known data of a provided peer.
* *
* @param {PeerId} peerId * @param {PeerId} peerId
* @param {T[]|T} data * @param {any[]|any} data
*/ */
set (peerId, data) { set (peerId, data) {
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED') throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
@ -56,7 +53,7 @@ class Book {
* *
* @protected * @protected
* @param {PeerId} peerId - peerId of the data to store * @param {PeerId} peerId - peerId of the data to store
* @param {T} data - data to store. * @param {any} data - data to store.
* @param {Object} [options] - storing options. * @param {Object} [options] - storing options.
* @param {boolean} [options.emit = true] - emit the provided data. * @param {boolean} [options.emit = true] - emit the provided data.
* @returns {void} * @returns {void}
@ -90,7 +87,7 @@ class Book {
* Returns `undefined` if there is no available data for the given peer. * Returns `undefined` if there is no available data for the given peer.
* *
* @param {PeerId} peerId * @param {PeerId} peerId
* @returns {T[]|T|undefined} * @returns {any[]|any|undefined}
*/ */
get (peerId) { get (peerId) {
if (!PeerId.isPeerId(peerId)) { if (!PeerId.isPeerId(peerId)) {