Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename peer-store properties #613

Merged
merged 2 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ Iterates over all peer routers in series to find the given peer. If the DHT is e

```js
// ...
const peerData = await libp2p.peerRouting.findPeer(peerId, options)
const peer = await libp2p.peerRouting.findPeer(peerId, options)
```

### contentRouting.findProviders
Expand Down Expand Up @@ -585,7 +585,7 @@ peerStore.addressBook.delete(peerId)

### 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)`

Expand All @@ -599,7 +599,7 @@ Get the known [`MultiaddrInfos`][multiaddr-info] of a provided peer.

| 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

Expand Down Expand Up @@ -818,7 +818,7 @@ peerStore.delete(peerId2)

### 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)`

Expand All @@ -832,7 +832,7 @@ Get the stored information of a given peer, namely its [`PeerId`][peer-id], know

| 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

Expand All @@ -844,7 +844,7 @@ peerStore.protoBook.set(peerId, protocols)
peerStore.get(peerId)
// {
// id: {},
// MultiaddrInfos: [...],
// addresses: [...],
// protocols: [...]
// }
```
Expand All @@ -859,13 +859,13 @@ Get all the stored information of every peer.

| 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

```js
for (let [peerIdString, peerData] of peerStore.peers.entries()) {
// peerData
for (let [peerIdString, peer] of peerStore.peers.entries()) {
// peer { id, addresses, protocols }
}
```

Expand Down Expand Up @@ -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.
- `['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
[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
[multiaddr]: https://github.com/multiformats/js-multiaddr
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"libp2p-delegated-peer-routing": "^0.5.0",
"libp2p-floodsub": "^0.21.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-mplex": "^0.9.1",
"libp2p-secio": "^0.12.1",
Expand Down
4 changes: 2 additions & 2 deletions src/content-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ module.exports = (node) => {
})
)

for (const peerData of result) {
yield peerData
for (const peer of result) {
yield peer
}
},

Expand Down
22 changes: 11 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,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`
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
* @param {PeerId|Multiaddr|string} peer The peer to dial
* @param {object} options
Expand All @@ -280,7 +280,7 @@ class Libp2p extends EventEmitter {

/**
* 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`,
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
* and the `Connection` will be returned
* @async
* @param {PeerId|Multiaddr|string} peer The peer to dial
Expand All @@ -295,7 +295,7 @@ class Libp2p extends EventEmitter {

if (!connection) {
connection = await this.dialer.connectToPeer(peer, options)
} else {
} else if (multiaddrs) {
this.peerStore.addressBook.add(id, multiaddrs)
}

Expand Down Expand Up @@ -412,26 +412,26 @@ class Libp2p extends EventEmitter {
await this._setupPeerDiscovery()

// Once we start, emit and dial any peers we may have already discovered
for (const peerData of this.peerStore.peers.values()) {
this.emit('peer:discovery', peerData.id)
this._maybeConnect(peerData.id)
for (const peer of this.peerStore.peers.values()) {
this.emit('peer:discovery', peer.id)
this._maybeConnect(peer.id)
}
}

/**
* Called whenever peer discovery services emit `peer` events.
* Known peers may be emitted.
* @private
* @param {PeerDara} peerData
* @param {PeerDara} peer
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
*/
_onDiscoveryPeer (peerData) {
if (peerData.id.toB58String() === this.peerId.toB58String()) {
_onDiscoveryPeer (peer) {
if (peer.id.toB58String() === this.peerId.toB58String()) {
log.error(new Error(codes.ERR_DISCOVERED_SELF))
return
}

peerData.multiaddrs && this.peerStore.addressBook.add(peerData.id, peerData.multiaddrs)
peerData.protocols && this.peerStore.protoBook.set(peerData.id, peerData.protocols)
peer.multiaddrs && this.peerStore.addressBook.add(peer.id, peer.multiaddrs)
peer.protocols && this.peerStore.protoBook.set(peer.id, peer.protocols)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/peer-store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

`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:
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved

```js
{
Expand Down
76 changes: 38 additions & 38 deletions src/peer-store/address-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const {
*/
class AddressBook extends Book {
/**
* MultiaddrInfo object
* @typedef {Object} MultiaddrInfo
* Address object
* @typedef {Object} Address
* @property {Multiaddr} multiaddr peer multiaddr.
*/

Expand All @@ -38,37 +38,37 @@ class AddressBook extends Book {
super(peerStore, 'change:multiaddrs', 'multiaddrs')

/**
* Map known peers to their known multiaddrs.
* @type {Map<string, Array<MultiaddrInfo>>}
* Map known peers to their known Addresses.
* @type {Map<string, Array<Address>>}
*/
this.data = new Map()
}

/**
* Set known addresses of a provided peer.
* Set known multiaddrs of a provided peer.
* @override
* @param {PeerId} peerId
* @param {Array<Multiaddr>} addresses
* @param {Array<Multiaddr>} multiaddrs
* @returns {AddressBook}
*/
set (peerId, addresses) {
set (peerId, multiaddrs) {
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)
}

const multiaddrInfos = this._toMultiaddrInfos(addresses)
const addresses = this._toAddresses(multiaddrs)
const id = peerId.toB58String()
const rec = this.data.get(id)

// Not replace multiaddrs
if (!multiaddrInfos.length) {
if (!addresses.length) {
return this
}

// Already knows the peer
if (rec && rec.length === multiaddrInfos.length) {
const intersection = rec.filter((mi) => multiaddrInfos.some((newMi) => mi.multiaddr.equals(newMi.multiaddr)))
if (rec && rec.length === addresses.length) {
const intersection = rec.filter((mi) => addresses.some((newMi) => mi.multiaddr.equals(newMi.multiaddr)))

// Are new addresses equal to the old ones?
// If yes, no changes needed!
Expand All @@ -78,7 +78,7 @@ class AddressBook extends Book {
}
}

this.data.set(id, multiaddrInfos)
this.data.set(id, addresses)
this._setPeerId(peerId)
log(`stored provided multiaddrs for ${id}`)

Expand All @@ -89,7 +89,7 @@ class AddressBook extends Book {

this._ps.emit('change:multiaddrs', {
peerId,
multiaddrs: multiaddrInfos.map((mi) => mi.multiaddr)
multiaddrs: addresses.map((mi) => mi.multiaddr)
})

return this
Expand All @@ -100,41 +100,41 @@ class AddressBook extends Book {
* If the peer is not known, it is set with the given addresses.
* @override
* @param {PeerId} peerId
* @param {Array<Multiaddr>} addresses
* @param {Array<Multiaddr>} multiaddrs
* @returns {AddressBook}
*/
add (peerId, addresses) {
add (peerId, multiaddrs) {
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)
}

const multiaddrInfos = this._toMultiaddrInfos(addresses)
const addresses = this._toAddresses(multiaddrs)
const id = peerId.toB58String()
const rec = this.data.get(id)

// Add recorded uniquely to the new array (Union)
rec && rec.forEach((mi) => {
if (!multiaddrInfos.find(r => r.multiaddr.equals(mi.multiaddr))) {
multiaddrInfos.push(mi)
if (!addresses.find(r => r.multiaddr.equals(mi.multiaddr))) {
addresses.push(mi)
}
})

// If the recorded length is equal to the new after the unique union
// 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}`)
return this
}

this._setPeerId(peerId)
this.data.set(id, multiaddrInfos)
this.data.set(id, addresses)

log(`added provided multiaddrs for ${id}`)

this._ps.emit('change:multiaddrs', {
peerId,
multiaddrs: multiaddrInfos.map((mi) => mi.multiaddr)
multiaddrs: addresses.map((mi) => mi.multiaddr)
})

// Notify the existance of a new peer
Expand All @@ -146,30 +146,30 @@ class AddressBook extends Book {
}

/**
* Transforms received multiaddrs into MultiaddrInfo.
* @param {Array<Multiaddr>} addresses
* @returns {Array<MultiaddrInfo>}
* Transforms received multiaddrs into Address.
* @param {Array<Multiaddr>} multiaddrs
* @returns {Array<Address>}
*/
_toMultiaddrInfos (addresses) {
if (!addresses) {
log.error('addresses must be provided to store data')
throw errcode(new Error('addresses must be provided'), ERR_INVALID_PARAMETERS)
_toAddresses (multiaddrs) {
if (!multiaddrs) {
log.error('multiaddrs must be provided to store data')
throw errcode(new Error('multiaddrs must be provided'), ERR_INVALID_PARAMETERS)
}

// create MultiaddrInfo for each address
const multiaddrInfos = []
addresses.forEach((addr) => {
// create Address for each address
const addresses = []
multiaddrs.forEach((addr) => {
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)
}

multiaddrInfos.push({
addresses.push({
multiaddr: addr
})
})

return multiaddrInfos
return addresses
}

/**
Expand All @@ -189,13 +189,13 @@ class AddressBook extends Book {
return undefined
}

return record.map((multiaddrInfo) => {
const addr = multiaddrInfo.multiaddr
return record.map((address) => {
const multiaddr = address.multiaddr

const idString = addr.getPeerId()
if (idString && idString === peerId.toB58String()) return addr
const idString = multiaddr.getPeerId()
if (idString && idString === peerId.toB58String()) return multiaddr

return addr.encapsulate(`/p2p/${peerId.toB58String()}`)
return multiaddr.encapsulate(`/p2p/${peerId.toB58String()}`)
})
}
}
Expand Down
Loading