2020-04-09 16:07:18 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const errcode = require('err-code')
|
|
|
|
const debug = require('debug')
|
|
|
|
const log = debug('libp2p:peer-store:address-book')
|
|
|
|
log.error = debug('libp2p:peer-store:address-book:error')
|
|
|
|
|
|
|
|
const multiaddr = require('multiaddr')
|
|
|
|
const PeerId = require('peer-id')
|
|
|
|
|
|
|
|
const Book = require('./book')
|
2020-04-23 14:08:34 +02:00
|
|
|
const Protobuf = require('./pb/address-book.proto')
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
const {
|
2020-04-23 14:08:34 +02:00
|
|
|
codes: { ERR_INVALID_PARAMETERS }
|
2020-04-09 16:07:18 +02:00
|
|
|
} = require('../errors')
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The AddressBook is responsible for keeping the known multiaddrs
|
|
|
|
* of a peer.
|
2020-04-23 14:08:34 +02:00
|
|
|
* This data will be persisted in the PeerStore datastore as follows:
|
|
|
|
* /peers/addrs/<b32 peer id no padding>
|
2020-04-09 16:07:18 +02:00
|
|
|
*/
|
|
|
|
class AddressBook extends Book {
|
|
|
|
/**
|
2020-04-24 15:54:59 +02:00
|
|
|
* Address object
|
|
|
|
* @typedef {Object} Address
|
2020-04-09 16:07:18 +02:00
|
|
|
* @property {Multiaddr} multiaddr peer multiaddr.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
2020-04-16 15:20:42 +02:00
|
|
|
* @param {PeerStore} peerStore
|
2020-04-09 16:07:18 +02:00
|
|
|
*/
|
|
|
|
constructor (peerStore) {
|
|
|
|
/**
|
|
|
|
* PeerStore Event emitter, used by the AddressBook to emit:
|
|
|
|
* "peer" - emitted when a peer is discovered by the node.
|
|
|
|
* "change:multiaddrs" - emitted when the known multiaddrs of a peer change.
|
|
|
|
*/
|
2020-04-23 14:08:34 +02:00
|
|
|
super({
|
|
|
|
peerStore,
|
2020-04-28 09:34:13 +02:00
|
|
|
event: {
|
|
|
|
name: 'change:multiaddrs',
|
|
|
|
property: 'multiaddrs',
|
|
|
|
transformer: (data) => data.map((address) => address.multiaddr)
|
|
|
|
},
|
|
|
|
ds: {
|
|
|
|
prefix: '/peers/addrs/',
|
|
|
|
setTransformer: (data) => Protobuf.encode({
|
|
|
|
addrs: data.map((address) => address.multiaddr.buffer)
|
|
|
|
}),
|
|
|
|
getTransformer: (encData) => {
|
|
|
|
const data = Protobuf.decode(encData)
|
|
|
|
|
|
|
|
return data.addrs.map((a) => ({
|
|
|
|
multiaddr: multiaddr(a)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 14:08:34 +02:00
|
|
|
})
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
/**
|
2020-04-24 15:54:59 +02:00
|
|
|
* Map known peers to their known Addresses.
|
|
|
|
* @type {Map<string, Array<Address>>}
|
2020-04-09 16:07:18 +02:00
|
|
|
*/
|
|
|
|
this.data = new Map()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-24 15:54:59 +02:00
|
|
|
* Set known multiaddrs of a provided peer.
|
2020-04-09 16:07:18 +02:00
|
|
|
* @override
|
|
|
|
* @param {PeerId} peerId
|
2020-04-24 15:54:59 +02:00
|
|
|
* @param {Array<Multiaddr>} multiaddrs
|
2020-04-09 16:07:18 +02:00
|
|
|
* @returns {AddressBook}
|
|
|
|
*/
|
2020-04-24 15:54:59 +02:00
|
|
|
set (peerId, multiaddrs) {
|
2020-04-09 16:07:18 +02:00
|
|
|
if (!PeerId.isPeerId(peerId)) {
|
|
|
|
log.error('peerId must be an instance of peer-id to store data')
|
|
|
|
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:54:59 +02:00
|
|
|
const addresses = this._toAddresses(multiaddrs)
|
2020-04-09 16:07:18 +02:00
|
|
|
const id = peerId.toB58String()
|
|
|
|
const rec = this.data.get(id)
|
|
|
|
|
|
|
|
// Not replace multiaddrs
|
2020-04-24 15:54:59 +02:00
|
|
|
if (!addresses.length) {
|
2020-04-09 16:07:18 +02:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
// Already knows the peer
|
2020-04-24 15:54:59 +02:00
|
|
|
if (rec && rec.length === addresses.length) {
|
|
|
|
const intersection = rec.filter((mi) => addresses.some((newMi) => mi.multiaddr.equals(newMi.multiaddr)))
|
2020-04-09 16:07:18 +02:00
|
|
|
|
|
|
|
// Are new addresses equal to the old ones?
|
|
|
|
// If yes, no changes needed!
|
|
|
|
if (intersection.length === rec.length) {
|
|
|
|
log(`the addresses provided to store are equal to the already stored for ${id}`)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:08:34 +02:00
|
|
|
this._setData(peerId, addresses)
|
2020-04-09 16:07:18 +02:00
|
|
|
log(`stored provided multiaddrs for ${id}`)
|
|
|
|
|
|
|
|
// Notify the existance of a new peer
|
|
|
|
if (!rec) {
|
2020-04-14 14:05:30 +02:00
|
|
|
this._ps.emit('peer', peerId)
|
2020-04-09 16:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add known addresses of a provided peer.
|
|
|
|
* If the peer is not known, it is set with the given addresses.
|
|
|
|
* @override
|
|
|
|
* @param {PeerId} peerId
|
2020-04-24 15:54:59 +02:00
|
|
|
* @param {Array<Multiaddr>} multiaddrs
|
2020-04-09 16:07:18 +02:00
|
|
|
* @returns {AddressBook}
|
|
|
|
*/
|
2020-04-24 15:54:59 +02:00
|
|
|
add (peerId, multiaddrs) {
|
2020-04-09 16:07:18 +02:00
|
|
|
if (!PeerId.isPeerId(peerId)) {
|
|
|
|
log.error('peerId must be an instance of peer-id to store data')
|
|
|
|
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:54:59 +02:00
|
|
|
const addresses = this._toAddresses(multiaddrs)
|
2020-04-09 16:07:18 +02:00
|
|
|
const id = peerId.toB58String()
|
|
|
|
const rec = this.data.get(id)
|
|
|
|
|
|
|
|
// Add recorded uniquely to the new array (Union)
|
|
|
|
rec && rec.forEach((mi) => {
|
2020-04-24 15:54:59 +02:00
|
|
|
if (!addresses.find(r => r.multiaddr.equals(mi.multiaddr))) {
|
|
|
|
addresses.push(mi)
|
2020-04-09 16:07:18 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// If the recorded length is equal to the new after the unique union
|
|
|
|
// The content is the same, no need to update.
|
2020-04-24 15:54:59 +02:00
|
|
|
if (rec && rec.length === addresses.length) {
|
2020-04-09 16:07:18 +02:00
|
|
|
log(`the addresses provided to store are already stored for ${id}`)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:08:34 +02:00
|
|
|
this._setData(peerId, addresses)
|
2020-04-09 16:07:18 +02:00
|
|
|
log(`added provided multiaddrs for ${id}`)
|
|
|
|
|
|
|
|
// Notify the existance of a new peer
|
|
|
|
if (!rec) {
|
2020-04-14 14:05:30 +02:00
|
|
|
this._ps.emit('peer', peerId)
|
2020-04-09 16:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-24 15:54:59 +02:00
|
|
|
* Transforms received multiaddrs into Address.
|
2020-04-23 14:08:34 +02:00
|
|
|
* @private
|
2020-04-24 15:54:59 +02:00
|
|
|
* @param {Array<Multiaddr>} multiaddrs
|
|
|
|
* @returns {Array<Address>}
|
2020-04-09 16:07:18 +02:00
|
|
|
*/
|
2020-04-24 15:54:59 +02:00
|
|
|
_toAddresses (multiaddrs) {
|
|
|
|
if (!multiaddrs) {
|
|
|
|
log.error('multiaddrs must be provided to store data')
|
|
|
|
throw errcode(new Error('multiaddrs must be provided'), ERR_INVALID_PARAMETERS)
|
2020-04-09 16:07:18 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 15:54:59 +02:00
|
|
|
// create Address for each address
|
|
|
|
const addresses = []
|
|
|
|
multiaddrs.forEach((addr) => {
|
2020-04-09 16:07:18 +02:00
|
|
|
if (!multiaddr.isMultiaddr(addr)) {
|
|
|
|
log.error(`multiaddr ${addr} must be an instance of multiaddr`)
|
|
|
|
throw errcode(new Error(`multiaddr ${addr} must be an instance of multiaddr`), ERR_INVALID_PARAMETERS)
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:54:59 +02:00
|
|
|
addresses.push({
|
2020-04-09 16:07:18 +02:00
|
|
|
multiaddr: addr
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-24 15:54:59 +02:00
|
|
|
return addresses
|
2020-04-09 16:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the known multiaddrs for a given peer. All returned multiaddrs
|
|
|
|
* will include the encapsulated `PeerId` of the peer.
|
|
|
|
* @param {PeerId} peerId
|
|
|
|
* @returns {Array<Multiaddr>}
|
|
|
|
*/
|
|
|
|
getMultiaddrsForPeer (peerId) {
|
|
|
|
if (!PeerId.isPeerId(peerId)) {
|
|
|
|
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
|
|
|
}
|
|
|
|
|
|
|
|
const record = this.data.get(peerId.toB58String())
|
|
|
|
|
|
|
|
if (!record) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:54:59 +02:00
|
|
|
return record.map((address) => {
|
|
|
|
const multiaddr = address.multiaddr
|
2020-04-09 16:07:18 +02:00
|
|
|
|
2020-04-24 15:54:59 +02:00
|
|
|
const idString = multiaddr.getPeerId()
|
|
|
|
if (idString && idString === peerId.toB58String()) return multiaddr
|
2020-04-09 16:07:18 +02:00
|
|
|
|
2020-04-24 15:54:59 +02:00
|
|
|
return multiaddr.encapsulate(`/p2p/${peerId.toB58String()}`)
|
2020-04-09 16:07:18 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AddressBook
|