mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-26 11:02:14 +00:00
chore: rename peer-store properties
This commit is contained in:
parent
ea149cd1fe
commit
e96f01ae74
20
doc/API.md
20
doc/API.md
@ -381,7 +381,7 @@ Iterates over all peer routers in series to find the given peer. If the DHT is e
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
// ...
|
// ...
|
||||||
const peerData = await libp2p.peerRouting.findPeer(peerId, options)
|
const peer = await libp2p.peerRouting.findPeer(peerId, options)
|
||||||
```
|
```
|
||||||
|
|
||||||
### contentRouting.findProviders
|
### contentRouting.findProviders
|
||||||
@ -585,7 +585,7 @@ peerStore.addressBook.delete(peerId)
|
|||||||
|
|
||||||
### peerStore.addressBook.get
|
### peerStore.addressBook.get
|
||||||
|
|
||||||
Get the known [`MultiaddrInfos`][multiaddr-info] of a provided peer.
|
Get the known [`Addresses`][address] of a provided peer.
|
||||||
|
|
||||||
`peerStore.addressBook.get(peerId)`
|
`peerStore.addressBook.get(peerId)`
|
||||||
|
|
||||||
@ -599,7 +599,7 @@ Get the known [`MultiaddrInfos`][multiaddr-info] of a provided peer.
|
|||||||
|
|
||||||
| Type | Description |
|
| Type | Description |
|
||||||
|------|-------------|
|
|------|-------------|
|
||||||
| `Array<MultiaddrInfo>` | Array of peer's multiaddr with their relevant information [`MultiaddrInfo`][multiaddr-info] |
|
| `Array<Address>` | Array of peer's [`Addresses`][address] containing the multiaddr and its metadata |
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
|
|
||||||
@ -818,7 +818,7 @@ peerStore.delete(peerId2)
|
|||||||
|
|
||||||
### peerStore.get
|
### peerStore.get
|
||||||
|
|
||||||
Get the stored information of a given peer, namely its [`PeerId`][peer-id], known [`MultiaddrInfos`][multiaddr-info] and supported protocols.
|
Get the stored information of a given peer, namely its [`PeerId`][peer-id], known [`Addresses`][address] and supported protocols.
|
||||||
|
|
||||||
`peerStore.get(peerId)`
|
`peerStore.get(peerId)`
|
||||||
|
|
||||||
@ -832,7 +832,7 @@ Get the stored information of a given peer, namely its [`PeerId`][peer-id], know
|
|||||||
|
|
||||||
| Type | Description |
|
| Type | Description |
|
||||||
|------|-------------|
|
|------|-------------|
|
||||||
| `{ id: PeerId, multiaddrInfos: Array<MultiaddrInfo>, protocols: Array<string> }` | Peer information of the provided peer |
|
| `{ id: PeerId, addresses: Array<Address>, protocols: Array<string> }` | Peer information of the provided peer |
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
|
|
||||||
@ -844,7 +844,7 @@ peerStore.protoBook.set(peerId, protocols)
|
|||||||
peerStore.get(peerId)
|
peerStore.get(peerId)
|
||||||
// {
|
// {
|
||||||
// id: {},
|
// id: {},
|
||||||
// MultiaddrInfos: [...],
|
// addresses: [...],
|
||||||
// protocols: [...]
|
// protocols: [...]
|
||||||
// }
|
// }
|
||||||
```
|
```
|
||||||
@ -859,13 +859,13 @@ Get all the stored information of every peer.
|
|||||||
|
|
||||||
| Type | Description |
|
| Type | Description |
|
||||||
|------|-------------|
|
|------|-------------|
|
||||||
| `Map<string, { id: PeerId, multiaddrInfos: Array<MultiaddrInfo>, protocols: Array<string> }>` | Peer data of every peer known |
|
| `Map<string, { id: PeerId, addresses: Array<Address>, protocols: Array<string> }>` | Peer data of every peer known |
|
||||||
|
|
||||||
#### Example
|
#### Example
|
||||||
|
|
||||||
```js
|
```js
|
||||||
for (let [peerIdString, peerData] of peerStore.peers.entries()) {
|
for (let [peerIdString, peer] of peerStore.peers.entries()) {
|
||||||
// peerData
|
// peer { id, addresses, protocols }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -1184,7 +1184,7 @@ This event will be triggered anytime we are disconnected from another peer, rega
|
|||||||
- `['300000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 5 minute interval.
|
- `['300000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 5 minute interval.
|
||||||
- `['900000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 15 minute interval.
|
- `['900000']<MovingAverage>`: The [MovingAverage](https://www.npmjs.com/package/moving-averages) at a 15 minute interval.
|
||||||
|
|
||||||
[multiaddr-info]: https://github.com/libp2p/js-libp2p/tree/master/src/peer-store/address-book.js
|
[address]: https://github.com/libp2p/js-libp2p/tree/master/src/peer-store/address-book.js
|
||||||
[cid]: https://github.com/multiformats/js-cid
|
[cid]: https://github.com/multiformats/js-cid
|
||||||
[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
|
[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
|
||||||
[multiaddr]: https://github.com/multiformats/js-multiaddr
|
[multiaddr]: https://github.com/multiformats/js-multiaddr
|
||||||
|
@ -95,7 +95,7 @@
|
|||||||
"libp2p-delegated-peer-routing": "^0.5.0",
|
"libp2p-delegated-peer-routing": "^0.5.0",
|
||||||
"libp2p-floodsub": "^0.21.0",
|
"libp2p-floodsub": "^0.21.0",
|
||||||
"libp2p-gossipsub": "^0.4.0",
|
"libp2p-gossipsub": "^0.4.0",
|
||||||
"libp2p-kad-dht": "^0.19.0",
|
"libp2p-kad-dht": "libp2p/js-libp2p-kad-dht#chore/rename-peer-store-properties",
|
||||||
"libp2p-mdns": "^0.14.0",
|
"libp2p-mdns": "^0.14.0",
|
||||||
"libp2p-mplex": "^0.9.5",
|
"libp2p-mplex": "^0.9.5",
|
||||||
"libp2p-secio": "^0.12.4",
|
"libp2p-secio": "^0.12.4",
|
||||||
|
@ -42,8 +42,8 @@ module.exports = (node) => {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
for (const peerData of result) {
|
for (const peer of result) {
|
||||||
yield peerData
|
yield peer
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
22
src/index.js
22
src/index.js
@ -270,7 +270,7 @@ class Libp2p extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dials to the provided peer. If successful, the known `PeerData` of the
|
* Dials to the provided peer. If successful, the known `Peer` data of the
|
||||||
* peer will be added to the nodes `peerStore`
|
* peer will be added to the nodes `peerStore`
|
||||||
* @param {PeerId|Multiaddr|string} peer The peer to dial
|
* @param {PeerId|Multiaddr|string} peer The peer to dial
|
||||||
* @param {object} options
|
* @param {object} options
|
||||||
@ -283,7 +283,7 @@ class Libp2p extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Dials to the provided peer and handshakes with the given protocol.
|
* Dials to the provided peer and handshakes with the given protocol.
|
||||||
* If successful, the known `PeerData` of the peer will be added to the nodes `peerStore`,
|
* If successful, the known `Peer` data of the peer will be added to the nodes `peerStore`,
|
||||||
* and the `Connection` will be returned
|
* and the `Connection` will be returned
|
||||||
* @async
|
* @async
|
||||||
* @param {PeerId|Multiaddr|string} peer The peer to dial
|
* @param {PeerId|Multiaddr|string} peer The peer to dial
|
||||||
@ -298,7 +298,7 @@ class Libp2p extends EventEmitter {
|
|||||||
|
|
||||||
if (!connection) {
|
if (!connection) {
|
||||||
connection = await this.dialer.connectToPeer(peer, options)
|
connection = await this.dialer.connectToPeer(peer, options)
|
||||||
} else {
|
} else if (multiaddrs) {
|
||||||
this.peerStore.addressBook.add(id, multiaddrs)
|
this.peerStore.addressBook.add(id, multiaddrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -421,9 +421,9 @@ class Libp2p extends EventEmitter {
|
|||||||
await this._setupPeerDiscovery()
|
await this._setupPeerDiscovery()
|
||||||
|
|
||||||
// Once we start, emit and dial any peers we may have already discovered
|
// Once we start, emit and dial any peers we may have already discovered
|
||||||
for (const peerData of this.peerStore.peers.values()) {
|
for (const peer of this.peerStore.peers.values()) {
|
||||||
this.emit('peer:discovery', peerData.id)
|
this.emit('peer:discovery', peer.id)
|
||||||
this._maybeConnect(peerData.id)
|
this._maybeConnect(peer.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -431,16 +431,16 @@ class Libp2p extends EventEmitter {
|
|||||||
* Called whenever peer discovery services emit `peer` events.
|
* Called whenever peer discovery services emit `peer` events.
|
||||||
* Known peers may be emitted.
|
* Known peers may be emitted.
|
||||||
* @private
|
* @private
|
||||||
* @param {PeerDara} peerData
|
* @param {PeerDara} peer
|
||||||
*/
|
*/
|
||||||
_onDiscoveryPeer (peerData) {
|
_onDiscoveryPeer (peer) {
|
||||||
if (peerData.id.toB58String() === this.peerId.toB58String()) {
|
if (peer.id.toB58String() === this.peerId.toB58String()) {
|
||||||
log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peerData.multiaddrs && this.peerStore.addressBook.add(peerData.id, peerData.multiaddrs)
|
peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs)
|
||||||
peerData.protocols && this.peerStore.protoBook.set(peerData.id, peerData.protocols)
|
peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,9 +40,9 @@ The PeerStore wraps four main components: `addressBook`, `keyBook`, `protocolBoo
|
|||||||
|
|
||||||
The `addressBook` keeps the known multiaddrs of a peer. The multiaddrs of each peer may change over time and the Address Book must account for this.
|
The `addressBook` keeps the known multiaddrs of a peer. The multiaddrs of each peer may change over time and the Address Book must account for this.
|
||||||
|
|
||||||
`Map<string, multiaddrInfo>`
|
`Map<string, Address>`
|
||||||
|
|
||||||
A `peerId.toString()` identifier mapping to a `multiaddrInfo` object, which should have the following structure:
|
A `peerId.toString()` identifier mapping to a `Address` object, which should have the following structure:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
{
|
{
|
||||||
|
@ -20,8 +20,8 @@ const {
|
|||||||
*/
|
*/
|
||||||
class AddressBook extends Book {
|
class AddressBook extends Book {
|
||||||
/**
|
/**
|
||||||
* MultiaddrInfo object
|
* Address object
|
||||||
* @typedef {Object} MultiaddrInfo
|
* @typedef {Object} Address
|
||||||
* @property {Multiaddr} multiaddr peer multiaddr.
|
* @property {Multiaddr} multiaddr peer multiaddr.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -38,37 +38,37 @@ class AddressBook extends Book {
|
|||||||
super(peerStore, 'change:multiaddrs', 'multiaddrs')
|
super(peerStore, 'change:multiaddrs', 'multiaddrs')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map known peers to their known multiaddrs.
|
* Map known peers to their known Addresses.
|
||||||
* @type {Map<string, Array<MultiaddrInfo>>}
|
* @type {Map<string, Array<Address>>}
|
||||||
*/
|
*/
|
||||||
this.data = new Map()
|
this.data = new Map()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set known addresses of a provided peer.
|
* Set known multiaddrs of a provided peer.
|
||||||
* @override
|
* @override
|
||||||
* @param {PeerId} peerId
|
* @param {PeerId} peerId
|
||||||
* @param {Array<Multiaddr>} addresses
|
* @param {Array<Multiaddr>} multiaddrs
|
||||||
* @returns {AddressBook}
|
* @returns {AddressBook}
|
||||||
*/
|
*/
|
||||||
set (peerId, addresses) {
|
set (peerId, multiaddrs) {
|
||||||
if (!PeerId.isPeerId(peerId)) {
|
if (!PeerId.isPeerId(peerId)) {
|
||||||
log.error('peerId must be an instance of peer-id to store data')
|
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)
|
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
||||||
}
|
}
|
||||||
|
|
||||||
const multiaddrInfos = this._toMultiaddrInfos(addresses)
|
const addresses = this._toAddresses(multiaddrs)
|
||||||
const id = peerId.toB58String()
|
const id = peerId.toB58String()
|
||||||
const rec = this.data.get(id)
|
const rec = this.data.get(id)
|
||||||
|
|
||||||
// Not replace multiaddrs
|
// Not replace multiaddrs
|
||||||
if (!multiaddrInfos.length) {
|
if (!addresses.length) {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
// Already knows the peer
|
// Already knows the peer
|
||||||
if (rec && rec.length === multiaddrInfos.length) {
|
if (rec && rec.length === addresses.length) {
|
||||||
const intersection = rec.filter((mi) => multiaddrInfos.some((newMi) => mi.multiaddr.equals(newMi.multiaddr)))
|
const intersection = rec.filter((mi) => addresses.some((newMi) => mi.multiaddr.equals(newMi.multiaddr)))
|
||||||
|
|
||||||
// Are new addresses equal to the old ones?
|
// Are new addresses equal to the old ones?
|
||||||
// If yes, no changes needed!
|
// If yes, no changes needed!
|
||||||
@ -78,7 +78,7 @@ class AddressBook extends Book {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.data.set(id, multiaddrInfos)
|
this.data.set(id, addresses)
|
||||||
this._setPeerId(peerId)
|
this._setPeerId(peerId)
|
||||||
log(`stored provided multiaddrs for ${id}`)
|
log(`stored provided multiaddrs for ${id}`)
|
||||||
|
|
||||||
@ -89,7 +89,7 @@ class AddressBook extends Book {
|
|||||||
|
|
||||||
this._ps.emit('change:multiaddrs', {
|
this._ps.emit('change:multiaddrs', {
|
||||||
peerId,
|
peerId,
|
||||||
multiaddrs: multiaddrInfos.map((mi) => mi.multiaddr)
|
multiaddrs: addresses.map((mi) => mi.multiaddr)
|
||||||
})
|
})
|
||||||
|
|
||||||
return this
|
return this
|
||||||
@ -100,41 +100,41 @@ class AddressBook extends Book {
|
|||||||
* If the peer is not known, it is set with the given addresses.
|
* If the peer is not known, it is set with the given addresses.
|
||||||
* @override
|
* @override
|
||||||
* @param {PeerId} peerId
|
* @param {PeerId} peerId
|
||||||
* @param {Array<Multiaddr>} addresses
|
* @param {Array<Multiaddr>} multiaddrs
|
||||||
* @returns {AddressBook}
|
* @returns {AddressBook}
|
||||||
*/
|
*/
|
||||||
add (peerId, addresses) {
|
add (peerId, multiaddrs) {
|
||||||
if (!PeerId.isPeerId(peerId)) {
|
if (!PeerId.isPeerId(peerId)) {
|
||||||
log.error('peerId must be an instance of peer-id to store data')
|
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)
|
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
||||||
}
|
}
|
||||||
|
|
||||||
const multiaddrInfos = this._toMultiaddrInfos(addresses)
|
const addresses = this._toAddresses(multiaddrs)
|
||||||
const id = peerId.toB58String()
|
const id = peerId.toB58String()
|
||||||
const rec = this.data.get(id)
|
const rec = this.data.get(id)
|
||||||
|
|
||||||
// Add recorded uniquely to the new array (Union)
|
// Add recorded uniquely to the new array (Union)
|
||||||
rec && rec.forEach((mi) => {
|
rec && rec.forEach((mi) => {
|
||||||
if (!multiaddrInfos.find(r => r.multiaddr.equals(mi.multiaddr))) {
|
if (!addresses.find(r => r.multiaddr.equals(mi.multiaddr))) {
|
||||||
multiaddrInfos.push(mi)
|
addresses.push(mi)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// If the recorded length is equal to the new after the unique union
|
// If the recorded length is equal to the new after the unique union
|
||||||
// The content is the same, no need to update.
|
// The content is the same, no need to update.
|
||||||
if (rec && rec.length === multiaddrInfos.length) {
|
if (rec && rec.length === addresses.length) {
|
||||||
log(`the addresses provided to store are already stored for ${id}`)
|
log(`the addresses provided to store are already stored for ${id}`)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
this._setPeerId(peerId)
|
this._setPeerId(peerId)
|
||||||
this.data.set(id, multiaddrInfos)
|
this.data.set(id, addresses)
|
||||||
|
|
||||||
log(`added provided multiaddrs for ${id}`)
|
log(`added provided multiaddrs for ${id}`)
|
||||||
|
|
||||||
this._ps.emit('change:multiaddrs', {
|
this._ps.emit('change:multiaddrs', {
|
||||||
peerId,
|
peerId,
|
||||||
multiaddrs: multiaddrInfos.map((mi) => mi.multiaddr)
|
multiaddrs: addresses.map((mi) => mi.multiaddr)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Notify the existance of a new peer
|
// Notify the existance of a new peer
|
||||||
@ -146,30 +146,30 @@ class AddressBook extends Book {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms received multiaddrs into MultiaddrInfo.
|
* Transforms received multiaddrs into Address.
|
||||||
* @param {Array<Multiaddr>} addresses
|
* @param {Array<Multiaddr>} multiaddrs
|
||||||
* @returns {Array<MultiaddrInfo>}
|
* @returns {Array<Address>}
|
||||||
*/
|
*/
|
||||||
_toMultiaddrInfos (addresses) {
|
_toAddresses (multiaddrs) {
|
||||||
if (!addresses) {
|
if (!multiaddrs) {
|
||||||
log.error('addresses must be provided to store data')
|
log.error('multiaddrs must be provided to store data')
|
||||||
throw errcode(new Error('addresses must be provided'), ERR_INVALID_PARAMETERS)
|
throw errcode(new Error('multiaddrs must be provided'), ERR_INVALID_PARAMETERS)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create MultiaddrInfo for each address
|
// create Address for each address
|
||||||
const multiaddrInfos = []
|
const addresses = []
|
||||||
addresses.forEach((addr) => {
|
multiaddrs.forEach((addr) => {
|
||||||
if (!multiaddr.isMultiaddr(addr)) {
|
if (!multiaddr.isMultiaddr(addr)) {
|
||||||
log.error(`multiaddr ${addr} must be an instance of multiaddr`)
|
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)
|
throw errcode(new Error(`multiaddr ${addr} must be an instance of multiaddr`), ERR_INVALID_PARAMETERS)
|
||||||
}
|
}
|
||||||
|
|
||||||
multiaddrInfos.push({
|
addresses.push({
|
||||||
multiaddr: addr
|
multiaddr: addr
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return multiaddrInfos
|
return addresses
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -189,13 +189,13 @@ class AddressBook extends Book {
|
|||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
return record.map((multiaddrInfo) => {
|
return record.map((address) => {
|
||||||
const addr = multiaddrInfo.multiaddr
|
const multiaddr = address.multiaddr
|
||||||
|
|
||||||
const idString = addr.getPeerId()
|
const idString = multiaddr.getPeerId()
|
||||||
if (idString && idString === peerId.toB58String()) return addr
|
if (idString && idString === peerId.toB58String()) return multiaddr
|
||||||
|
|
||||||
return addr.encapsulate(`/p2p/${peerId.toB58String()}`)
|
return multiaddr.encapsulate(`/p2p/${peerId.toB58String()}`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,10 +23,10 @@ const {
|
|||||||
*/
|
*/
|
||||||
class PeerStore extends EventEmitter {
|
class PeerStore extends EventEmitter {
|
||||||
/**
|
/**
|
||||||
* PeerData object
|
* Peer object
|
||||||
* @typedef {Object} PeerData
|
* @typedef {Object} Peer
|
||||||
* @property {PeerId} id peer's peer-id instance.
|
* @property {PeerId} id peer's peer-id instance.
|
||||||
* @property {Array<multiaddrInfo>} multiaddrsInfos peer's information of the multiaddrs.
|
* @property {Array<Address>} addresses peer's addresses containing its multiaddrs and metadata.
|
||||||
* @property {Array<string>} protocols peer's supported protocols.
|
* @property {Array<string>} protocols peer's supported protocols.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ class PeerStore extends EventEmitter {
|
|||||||
super()
|
super()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AddressBook containing a map of peerIdStr to multiaddrsInfo
|
* AddressBook containing a map of peerIdStr to Address
|
||||||
*/
|
*/
|
||||||
this.addressBook = new AddressBook(this)
|
this.addressBook = new AddressBook(this)
|
||||||
|
|
||||||
@ -53,17 +53,17 @@ class PeerStore extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all the stored information of every peer.
|
* Get all the stored information of every peer.
|
||||||
* @returns {Map<string, PeerData>}
|
* @returns {Map<string, Peer>}
|
||||||
*/
|
*/
|
||||||
get peers () {
|
get peers () {
|
||||||
const peersData = new Map()
|
const peersData = new Map()
|
||||||
|
|
||||||
// AddressBook
|
// AddressBook
|
||||||
for (const [idStr, multiaddrInfos] of this.addressBook.data.entries()) {
|
for (const [idStr, addresses] of this.addressBook.data.entries()) {
|
||||||
const id = PeerId.createFromCID(idStr)
|
const id = PeerId.createFromCID(idStr)
|
||||||
peersData.set(idStr, {
|
peersData.set(idStr, {
|
||||||
id,
|
id,
|
||||||
multiaddrInfos,
|
addresses,
|
||||||
protocols: this.protoBook.get(id) || []
|
protocols: this.protoBook.get(id) || []
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ class PeerStore extends EventEmitter {
|
|||||||
if (!pData) {
|
if (!pData) {
|
||||||
peersData.set(idStr, {
|
peersData.set(idStr, {
|
||||||
id: PeerId.createFromCID(idStr),
|
id: PeerId.createFromCID(idStr),
|
||||||
multiaddrInfos: [],
|
addresses: [],
|
||||||
protocols: Array.from(protocols)
|
protocols: Array.from(protocols)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -98,7 +98,7 @@ class PeerStore extends EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Get the stored information of a given peer.
|
* Get the stored information of a given peer.
|
||||||
* @param {PeerId} peerId
|
* @param {PeerId} peerId
|
||||||
* @returns {PeerData}
|
* @returns {Peer}
|
||||||
*/
|
*/
|
||||||
get (peerId) {
|
get (peerId) {
|
||||||
if (!PeerId.isPeerId(peerId)) {
|
if (!PeerId.isPeerId(peerId)) {
|
||||||
@ -106,16 +106,16 @@ class PeerStore extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const id = this.peerIds.get(peerId.toB58String())
|
const id = this.peerIds.get(peerId.toB58String())
|
||||||
const multiaddrInfos = this.addressBook.get(peerId)
|
const addresses = this.addressBook.get(peerId)
|
||||||
const protocols = this.protoBook.get(peerId)
|
const protocols = this.protoBook.get(peerId)
|
||||||
|
|
||||||
if (!multiaddrInfos && !protocols) {
|
if (!addresses && !protocols) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: id || peerId,
|
id: id || peerId,
|
||||||
multiaddrInfos: multiaddrInfos || [],
|
addresses: addresses || [],
|
||||||
protocols: protocols || []
|
protocols: protocols || []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -121,9 +121,9 @@ describe('peer-routing', () => {
|
|||||||
'X-Chunked-Output', '1'
|
'X-Chunked-Output', '1'
|
||||||
])
|
])
|
||||||
|
|
||||||
const peerData = await node.peerRouting.findPeer(peerKey)
|
const peer = await node.peerRouting.findPeer(peerKey)
|
||||||
|
|
||||||
expect(peerData.id).to.equal(peerKey)
|
expect(peer.id).to.equal(peerKey)
|
||||||
expect(mockApi.isDone()).to.equal(true)
|
expect(mockApi.isDone()).to.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -69,8 +69,8 @@ describe('addressBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
ab.set(peerId, supportedMultiaddrs)
|
ab.set(peerId, supportedMultiaddrs)
|
||||||
const multiaddrInfos = ab.get(peerId)
|
const addresses = ab.get(peerId)
|
||||||
const multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr)
|
const multiaddrs = addresses.map((mi) => mi.multiaddr)
|
||||||
expect(multiaddrs).to.have.deep.members(supportedMultiaddrs)
|
expect(multiaddrs).to.have.deep.members(supportedMultiaddrs)
|
||||||
|
|
||||||
return defer.promise
|
return defer.promise
|
||||||
@ -95,8 +95,8 @@ describe('addressBook', () => {
|
|||||||
|
|
||||||
// set 2 (same content)
|
// set 2 (same content)
|
||||||
ab.set(peerId, supportedMultiaddrsB)
|
ab.set(peerId, supportedMultiaddrsB)
|
||||||
const multiaddrInfos = ab.get(peerId)
|
const addresses = ab.get(peerId)
|
||||||
const multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr)
|
const multiaddrs = addresses.map((mi) => mi.multiaddr)
|
||||||
expect(multiaddrs).to.have.deep.members(supportedMultiaddrsB)
|
expect(multiaddrs).to.have.deep.members(supportedMultiaddrsB)
|
||||||
|
|
||||||
await defer.promise
|
await defer.promise
|
||||||
@ -177,14 +177,14 @@ describe('addressBook', () => {
|
|||||||
|
|
||||||
// Replace
|
// Replace
|
||||||
ab.set(peerId, supportedMultiaddrsA)
|
ab.set(peerId, supportedMultiaddrsA)
|
||||||
let multiaddrInfos = ab.get(peerId)
|
let addresses = ab.get(peerId)
|
||||||
let multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr)
|
let multiaddrs = addresses.map((mi) => mi.multiaddr)
|
||||||
expect(multiaddrs).to.have.deep.members(supportedMultiaddrsA)
|
expect(multiaddrs).to.have.deep.members(supportedMultiaddrsA)
|
||||||
|
|
||||||
// Add
|
// Add
|
||||||
ab.add(peerId, supportedMultiaddrsB)
|
ab.add(peerId, supportedMultiaddrsB)
|
||||||
multiaddrInfos = ab.get(peerId)
|
addresses = ab.get(peerId)
|
||||||
multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr)
|
multiaddrs = addresses.map((mi) => mi.multiaddr)
|
||||||
expect(multiaddrs).to.have.deep.members(finalMultiaddrs)
|
expect(multiaddrs).to.have.deep.members(finalMultiaddrs)
|
||||||
|
|
||||||
return defer.promise
|
return defer.promise
|
||||||
@ -210,8 +210,8 @@ describe('addressBook', () => {
|
|||||||
|
|
||||||
// set 2 (content already existing)
|
// set 2 (content already existing)
|
||||||
ab.add(peerId, supportedMultiaddrsB)
|
ab.add(peerId, supportedMultiaddrsB)
|
||||||
const multiaddrInfos = ab.get(peerId)
|
const addresses = ab.get(peerId)
|
||||||
const multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr)
|
const multiaddrs = addresses.map((mi) => mi.multiaddr)
|
||||||
expect(multiaddrs).to.have.deep.members(finalMultiaddrs)
|
expect(multiaddrs).to.have.deep.members(finalMultiaddrs)
|
||||||
|
|
||||||
await defer.promise
|
await defer.promise
|
||||||
@ -261,9 +261,9 @@ describe('addressBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('returns undefined if no multiaddrs are known for the provided peer', () => {
|
it('returns undefined if no multiaddrs are known for the provided peer', () => {
|
||||||
const multiaddrInfos = ab.get(peerId)
|
const addresses = ab.get(peerId)
|
||||||
|
|
||||||
expect(multiaddrInfos).to.not.exist()
|
expect(addresses).to.not.exist()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns the multiaddrs stored', () => {
|
it('returns the multiaddrs stored', () => {
|
||||||
@ -271,8 +271,8 @@ describe('addressBook', () => {
|
|||||||
|
|
||||||
ab.set(peerId, supportedMultiaddrs)
|
ab.set(peerId, supportedMultiaddrs)
|
||||||
|
|
||||||
const multiaddrInfos = ab.get(peerId)
|
const addresses = ab.get(peerId)
|
||||||
const multiaddrs = multiaddrInfos.map((mi) => mi.multiaddr)
|
const multiaddrs = addresses.map((mi) => mi.multiaddr)
|
||||||
expect(multiaddrs).to.have.deep.members(supportedMultiaddrs)
|
expect(multiaddrs).to.have.deep.members(supportedMultiaddrs)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -292,9 +292,9 @@ describe('addressBook', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('returns undefined if no multiaddrs are known for the provided peer', () => {
|
it('returns undefined if no multiaddrs are known for the provided peer', () => {
|
||||||
const multiaddrInfos = ab.getMultiaddrsForPeer(peerId)
|
const addresses = ab.getMultiaddrsForPeer(peerId)
|
||||||
|
|
||||||
expect(multiaddrInfos).to.not.exist()
|
expect(addresses).to.not.exist()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns the multiaddrs stored', () => {
|
it('returns the multiaddrs stored', () => {
|
||||||
|
@ -45,8 +45,8 @@ describe('peer-store', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('returns undefined on trying to find a non existant peerId', () => {
|
it('returns undefined on trying to find a non existant peerId', () => {
|
||||||
const peerInfo = peerStore.get(peerIds[0])
|
const peer = peerStore.get(peerIds[0])
|
||||||
expect(peerInfo).to.not.exist()
|
expect(peer).to.not.exist()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -102,29 +102,29 @@ describe('peer-store', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('gets the stored information of a peer in all its books', () => {
|
it('gets the stored information of a peer in all its books', () => {
|
||||||
const peerInfo = peerStore.get(peerIds[0])
|
const peer = peerStore.get(peerIds[0])
|
||||||
expect(peerInfo).to.exist()
|
expect(peer).to.exist()
|
||||||
expect(peerInfo.protocols).to.have.members([proto1])
|
expect(peer.protocols).to.have.members([proto1])
|
||||||
|
|
||||||
const peerMultiaddrs = peerInfo.multiaddrInfos.map((mi) => mi.multiaddr)
|
const peerMultiaddrs = peer.addresses.map((mi) => mi.multiaddr)
|
||||||
expect(peerMultiaddrs).to.have.members([addr1, addr2])
|
expect(peerMultiaddrs).to.have.members([addr1, addr2])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('gets the stored information of a peer that is not present in all its books', () => {
|
it('gets the stored information of a peer that is not present in all its books', () => {
|
||||||
const peerInfo = peerStore.get(peerIds[2])
|
const peers = peerStore.get(peerIds[2])
|
||||||
expect(peerInfo).to.exist()
|
expect(peers).to.exist()
|
||||||
expect(peerInfo.protocols.length).to.eql(0)
|
expect(peers.protocols.length).to.eql(0)
|
||||||
|
|
||||||
const peerMultiaddrs = peerInfo.multiaddrInfos.map((mi) => mi.multiaddr)
|
const peerMultiaddrs = peers.addresses.map((mi) => mi.multiaddr)
|
||||||
expect(peerMultiaddrs).to.have.members([addr4])
|
expect(peerMultiaddrs).to.have.members([addr4])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can find all the peers supporting a protocol', () => {
|
it('can find all the peers supporting a protocol', () => {
|
||||||
const peerSupporting2 = []
|
const peerSupporting2 = []
|
||||||
|
|
||||||
for (const [, peerInfo] of peerStore.peers.entries()) {
|
for (const [, peer] of peerStore.peers.entries()) {
|
||||||
if (peerInfo.protocols.includes(proto2)) {
|
if (peer.protocols.includes(proto2)) {
|
||||||
peerSupporting2.push(peerInfo)
|
peerSupporting2.push(peer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,11 +136,11 @@ describe('peer-store', () => {
|
|||||||
it('can find all the peers listening on a given address', () => {
|
it('can find all the peers listening on a given address', () => {
|
||||||
const peerListenint4 = []
|
const peerListenint4 = []
|
||||||
|
|
||||||
for (const [, peerInfo] of peerStore.peers.entries()) {
|
for (const [, peer] of peerStore.peers.entries()) {
|
||||||
const multiaddrs = peerInfo.multiaddrInfos.map((mi) => mi.multiaddr)
|
const multiaddrs = peer.addresses.map((mi) => mi.multiaddr)
|
||||||
|
|
||||||
if (multiaddrs.includes(addr4)) {
|
if (multiaddrs.includes(addr4)) {
|
||||||
peerListenint4.push(peerInfo)
|
peerListenint4.push(peer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user