2019-11-06 15:11:13 +01:00
|
|
|
'use strict'
|
|
|
|
|
2020-02-14 13:42:23 +00:00
|
|
|
const errcode = require('err-code')
|
2019-11-06 15:11:13 +01:00
|
|
|
const debug = require('debug')
|
|
|
|
const log = debug('libp2p:peer-store')
|
|
|
|
log.error = debug('libp2p:peer-store:error')
|
|
|
|
|
|
|
|
const { EventEmitter } = require('events')
|
2019-12-02 18:13:14 +01:00
|
|
|
const PeerId = require('peer-id')
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
const AddressBook = require('./address-book')
|
|
|
|
const ProtoBook = require('./proto-book')
|
|
|
|
|
2020-02-14 13:42:23 +00:00
|
|
|
const {
|
|
|
|
ERR_INVALID_PARAMETERS
|
|
|
|
} = require('../errors')
|
2019-11-06 15:11:13 +01:00
|
|
|
|
|
|
|
/**
|
2020-04-09 16:07:18 +02:00
|
|
|
* Responsible for managing known peers, as well as their addresses, protocols and metadata.
|
|
|
|
* @fires PeerStore#peer Emitted when a new peer is added.
|
|
|
|
* @fires PeerStore#change:protocols Emitted when a known peer supports a different set of protocols.
|
|
|
|
* @fires PeerStore#change:multiaddrs Emitted when a known peer has a different set of multiaddrs.
|
2019-11-06 15:11:13 +01:00
|
|
|
*/
|
|
|
|
class PeerStore extends EventEmitter {
|
2020-04-09 16:07:18 +02:00
|
|
|
/**
|
2020-04-14 14:05:30 +02:00
|
|
|
* PeerData object
|
|
|
|
* @typedef {Object} PeerData
|
|
|
|
* @property {PeerId} id peer's peer-id instance.
|
2020-04-09 16:07:18 +02:00
|
|
|
* @property {Array<multiaddrInfo>} multiaddrsInfos peer's information of the multiaddrs.
|
|
|
|
* @property {Array<string>} protocols peer's supported protocols.
|
|
|
|
*/
|
|
|
|
|
2019-11-06 15:11:13 +01:00
|
|
|
constructor () {
|
|
|
|
super()
|
|
|
|
|
|
|
|
/**
|
2020-04-09 16:07:18 +02:00
|
|
|
* AddressBook containing a map of peerIdStr to multiaddrsInfo
|
2019-11-06 15:11:13 +01:00
|
|
|
*/
|
2020-04-09 16:07:18 +02:00
|
|
|
this.addressBook = new AddressBook(this)
|
2019-11-06 15:11:13 +01:00
|
|
|
|
2020-04-09 16:07:18 +02:00
|
|
|
/**
|
|
|
|
* ProtoBook containing a map of peerIdStr to supported protocols.
|
|
|
|
*/
|
|
|
|
this.protoBook = new ProtoBook(this)
|
2019-11-06 15:11:13 +01:00
|
|
|
|
2020-04-16 15:20:42 +02:00
|
|
|
/**
|
|
|
|
* TODO: this should only exist until we have the key-book
|
|
|
|
* Map known peers to their peer-id.
|
|
|
|
* @type {Map<string, Array<PeerId>}
|
|
|
|
*/
|
|
|
|
this.peerIds = new Map()
|
2020-04-09 16:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the stored information of every peer.
|
2020-04-14 14:05:30 +02:00
|
|
|
* @returns {Map<string, PeerData>}
|
2020-04-09 16:07:18 +02:00
|
|
|
*/
|
|
|
|
get peers () {
|
2020-04-14 14:05:30 +02:00
|
|
|
const peersData = new Map()
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
// AddressBook
|
|
|
|
for (const [idStr, multiaddrInfos] of this.addressBook.data.entries()) {
|
2020-04-14 14:05:30 +02:00
|
|
|
const id = PeerId.createFromCID(idStr)
|
|
|
|
peersData.set(idStr, {
|
|
|
|
id,
|
|
|
|
multiaddrInfos,
|
|
|
|
protocols: this.protoBook.get(id) || []
|
|
|
|
})
|
2020-04-09 16:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProtoBook
|
|
|
|
for (const [idStr, protocols] of this.protoBook.data.entries()) {
|
2020-04-14 14:05:30 +02:00
|
|
|
const pData = peersData.get(idStr)
|
|
|
|
|
|
|
|
if (!pData) {
|
|
|
|
peersData.set(idStr, {
|
|
|
|
id: PeerId.createFromCID(idStr),
|
|
|
|
multiaddrInfos: [],
|
|
|
|
protocols: Array.from(protocols)
|
|
|
|
})
|
2020-04-09 16:07:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:05:30 +02:00
|
|
|
return peersData
|
2020-04-09 16:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the information of the given peer in every book.
|
|
|
|
* @param {PeerId} peerId
|
|
|
|
* @returns {boolean} true if found and removed
|
|
|
|
*/
|
|
|
|
delete (peerId) {
|
|
|
|
const addressesDeleted = this.addressBook.delete(peerId)
|
|
|
|
const protocolsDeleted = this.protoBook.delete(peerId)
|
|
|
|
return addressesDeleted || protocolsDeleted
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-16 15:20:42 +02:00
|
|
|
* Get the stored information of a given peer.
|
2020-04-09 16:07:18 +02:00
|
|
|
* @param {PeerId} peerId
|
2020-04-14 14:05:30 +02:00
|
|
|
* @returns {PeerData}
|
2020-04-09 16:07:18 +02:00
|
|
|
*/
|
2020-04-16 15:20:42 +02:00
|
|
|
get (peerId) {
|
2020-04-09 16:07:18 +02:00
|
|
|
if (!PeerId.isPeerId(peerId)) {
|
|
|
|
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:20:42 +02:00
|
|
|
const id = this.peerIds.get(peerId.toB58String())
|
2020-04-09 16:07:18 +02:00
|
|
|
const multiaddrInfos = this.addressBook.get(peerId)
|
|
|
|
const protocols = this.protoBook.get(peerId)
|
|
|
|
|
|
|
|
if (!multiaddrInfos && !protocols) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-04-16 15:20:42 +02:00
|
|
|
id: id || peerId,
|
2020-04-09 16:07:18 +02:00
|
|
|
multiaddrInfos: multiaddrInfos || [],
|
|
|
|
protocols: protocols || []
|
|
|
|
}
|
2019-12-03 10:28:52 +01:00
|
|
|
}
|
2019-11-06 15:11:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PeerStore
|