Skip to content

Commit

Permalink
chore: merge 0.28x updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed May 7, 2020
2 parents b1a2b8a + 9eaed58 commit c0bbda2
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 64 deletions.
87 changes: 87 additions & 0 deletions doc/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* [`peerStore.addressBook.get`](#peerstoreaddressbookget)
* [`peerStore.addressBook.getMultiaddrsForPeer`](#peerstoreaddressbookgetmultiaddrsforpeer)
* [`peerStore.addressBook.set`](#peerstoreaddressbookset)
* [`peerStore.keyBook.delete`](#peerstorekeybookdelete)
* [`peerStore.keyBook.get`](#peerstorekeybookget)
* [`peerStore.keyBook.set`](#peerstorekeybookset)
* [`peerStore.protoBook.add`](#peerstoreprotobookadd)
* [`peerStore.protoBook.delete`](#peerstoreprotobookdelete)
* [`peerStore.protoBook.get`](#peerstoreprotobookget)
Expand Down Expand Up @@ -870,6 +873,89 @@ Add known `protocols` of a given peer.
peerStore.protoBook.add(peerId, protocols)
```


### peerStore.keyBook.delete

Delete the provided peer from the book.

`peerStore.keyBook.delete(peerId)`

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| peerId | [`PeerId`][peer-id] | peerId to remove |

#### Returns

| Type | Description |
|------|-------------|
| `boolean` | true if found and removed |

#### Example

```js
peerStore.keyBook.delete(peerId)
// false
peerStore.keyBook.set(peerId, publicKey)
peerStore.keyBook.delete(peerId)
// true
```

### peerStore.keyBook.get

Get the known `PublicKey` of a provided peer.

`peerStore.keyBook.get(peerId)`

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| peerId | [`PeerId`][peer-id] | peerId to get |

#### Returns

| Type | Description |
|------|-------------|
| `RsaPublicKey|Ed25519PublicKey|Secp256k1PublicKey` | Peer PublicKey |

#### Example

```js
peerStore.keyBook.get(peerId)
// undefined
peerStore.keyBook.set(peerId, publicKey)
peerStore.keyBook.get(peerId)
// PublicKey
```

### peerStore.keyBook.set

Set known `peerId`. This can include its Public Key.

`peerStore.keyBook.set(peerId, publicKey)`

#### Parameters

| Name | Type | Description |
|------|------|-------------|
| peerId | [`PeerId`][peer-id] | peerId to set |
| publicKey | [`RsaPublicKey|Ed25519PublicKey|Secp256k1PublicKey`][keys] | peer's public key |
#### Returns
| Type | Description |
|------|-------------|
| `KeyBook` | Returns the Key Book component |
#### Example
```js
const publicKey = peerId.pubKey
peerStore.keyBook.set(peerId, publicKey)
```
### peerStore.protoBook.delete
Delete the provided peer from the book.
Expand Down Expand Up @@ -1393,3 +1479,4 @@ This event will be triggered anytime we are disconnected from another peer, rega
[connection]: https://github.com/libp2p/js-interfaces/tree/master/src/connection
[multiaddr]: https://github.com/multiformats/js-multiaddr
[peer-id]: https://github.com/libp2p/js-peer-id
[keys]: https://github.com/libp2p/js-libp2p-crypto/tree/master/src/keys
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"aegir": "^21.9.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"chai-bytes": "^0.1.2",
"chai-string": "^1.5.0",
"cids": "^0.8.0",
"datastore-fs": "^1.0.0",
Expand Down
14 changes: 9 additions & 5 deletions src/connection-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,22 @@ class ConnectionManager extends EventEmitter {
* @param {Connection} connection
*/
onConnect (connection) {
const peerId = connection.remotePeer.toB58String()
const storedConn = this.connections.get(peerId)
const peerId = connection.remotePeer
const peerIdStr = peerId.toB58String()
const storedConn = this.connections.get(peerIdStr)

if (storedConn) {
storedConn.push(connection)
} else {
this.connections.set(peerId, [connection])
this.connections.set(peerIdStr, [connection])
this.emit('peer:connect', connection)
}

if (!this._peerValues.has(peerId)) {
this._peerValues.set(peerId, this._options.defaultPeerValue)
this._libp2p.peerStore.addressBook.add(peerId, [connection.remoteAddr])
this._libp2p.peerStore.keyBook.set(peerId, peerId.pubKey)

if (!this._peerValues.has(peerIdStr)) {
this._peerValues.set(peerIdStr, this._options.defaultPeerValue)
}

this._checkLimit('maxConnections', this.size)
Expand Down
21 changes: 13 additions & 8 deletions src/peer-store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Several libp2p subsystems will perform operations, which will gather relevant in

In a libp2p node's life, it will discover peers through its discovery protocols. In a typical discovery protocol, addresses of the peer are discovered along with its peer id. Once this happens, the PeerStore should collect this information for future (or immediate) usage by other subsystems. When the information is stored, the PeerStore should inform interested parties of the peer discovered (`peer` event).

Taking into account a different scenario, a peer might perform/receive a dial request to/from a unkwown peer. In such a scenario, the PeerStore must store the peer's multiaddr once a connection is established.
Taking into account a different scenario, a peer might perform/receive a dial request to/from a unkwown peer. In such a scenario, the PeerStore must store the peer's multiaddr once a connection is established.

When a connection is being upgraded, more precisely after its encryption, or even in a discovery protocol, a libp2p node can get to know other parties public keys. In this scenario, libp2p will add the peer's public key to its `KeyBook`.

After a connection is established with a peer, the Identify protocol will run automatically. A stream is created and peers exchange their information (Multiaddrs, running protocols and their public key). Once this information is obtained, it should be added to the PeerStore. In this specific case, as we are speaking to the source of truth, we should ensure the PeerStore is prioritizing these records. If the recorded `multiaddrs` or `protocols` have changed, interested parties must be informed via the `change:multiaddrs` or `change:protocols` events respectively.

Expand Down Expand Up @@ -42,7 +44,7 @@ The `addressBook` keeps the known multiaddrs of a peer. The multiaddrs of each p

`Map<string, Address>`

A `peerId.toString()` identifier mapping to a `Address` object, which should have the following structure:
A `peerId.toB58String()` identifier mapping to a `Address` object, which should have the following structure:

```js
{
Expand All @@ -52,17 +54,19 @@ A `peerId.toString()` identifier mapping to a `Address` object, which should hav

#### Key Book

The `keyBook` tracks the keys of the peers.
The `keyBook` tracks the public keys of the peers by keeping their [`PeerId`][peer-id].

**Not Yet Implemented**
`Map<string, PeerId`

A `peerId.toB58String()` identifier mapping to a `PeerId` of the peer. This instance contains the peer public key.

#### Protocol Book

The `protoBook` holds the identifiers of the protocols supported by each peer. The protocols supported by each peer are dynamic and will change over time.

`Map<string, Set<string>>`

A `peerId.toString()` identifier mapping to a `Set` of protocol identifier strings.
A `peerId.toB58String()` identifier mapping to a `Set` of protocol identifier strings.

#### Metadata Book

Expand All @@ -74,8 +78,9 @@ For the complete API documentation, you should check the [API.md](../../doc/API.

Access to its underlying books:

- `peerStore.protoBook.*`
- `peerStore.addressBook.*`
- `peerStore.keyBook.*`
- `peerStore.protoBook.*`

### Events

Expand Down Expand Up @@ -107,8 +112,6 @@ All the known peer protocols are stored with a key pattern as follows:

**KeyBook**

_NOT_YET_IMPLEMENTED_

All public keys are stored under the following pattern:

` /peers/keys/<b32 peer id no padding>`
Expand All @@ -127,3 +130,5 @@ Metadata is stored under the following key pattern:
- Further API methods will probably need to be added in the context of multiaddr validity and confidence.
- When improving libp2p configuration for specific runtimes, we should take into account the PeerStore recommended datastore.
- When improving libp2p configuration, we should think about a possible way of allowing the configuration of Bootstrap to be influenced by the persisted peers, as a way to decrease the load on Bootstrap nodes.

[peer-id]: https://github.com/libp2p/js-peer-id
36 changes: 11 additions & 25 deletions src/peer-store/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Book {
* Set data into the datastructure, persistence and emit it using the provided transformers.
* @private
* @param {PeerId} peerId peerId of the data to store
* @param {Array<*>} data data to store.
* @param {*} data data to store.
* @param {Object} [options] storing options.
* @param {boolean} [options.emit = true] emit the provided data.
* @return {void}
Expand All @@ -57,22 +57,22 @@ class Book {

// Store data in memory
this.data.set(b58key, data)
this._setPeerId(peerId)

// Emit event
emit && this._ps.emit(this.eventName, {
peerId,
[this.eventProperty]: this.eventTransformer(data)
})
emit && this._emit(peerId, data)
}

/**
* Add known data of a provided peer.
* Emit data.
* @private
* @param {PeerId} peerId
* @param {Array<Data>|Data} data
* @param {*} data
*/
add (peerId, data) {
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
_emit (peerId, data) {
this._ps.emit(this.eventName, {
peerId,
[this.eventProperty]: this.eventTransformer(data)
})
}

/**
Expand Down Expand Up @@ -104,24 +104,10 @@ class Book {
return false
}

this._ps.emit(this.eventName, {
peerId,
[this.eventProperty]: []
})
this._emit(peerId, [])

return true
}

/**
* Set PeerId into peerStore datastructure.
* @private
* @param {PeerId} peerId
*/
_setPeerId (peerId) {
if (!this._ps.peerIds.get(peerId)) {
this._ps.peerIds.set(peerId.toB58String(), peerId)
}
}
}

module.exports = Book
22 changes: 12 additions & 10 deletions src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { EventEmitter } = require('events')
const PeerId = require('peer-id')

const AddressBook = require('./address-book')
const KeyBook = require('./key-book')
const ProtoBook = require('./proto-book')

const {
Expand Down Expand Up @@ -42,16 +43,14 @@ class PeerStore extends EventEmitter {
this.addressBook = new AddressBook(this)

/**
* ProtoBook containing a map of peerIdStr to supported protocols.
* KeyBook containing a map of peerIdStr to their PeerId with public keys.
*/
this.protoBook = new ProtoBook(this)
this.keyBook = new KeyBook(this)

/**
* TODO: this should only exist until we have the key-book
* Map known peers to their peer-id.
* @type {Map<string, Array<PeerId>}
* ProtoBook containing a map of peerIdStr to supported protocols.
*/
this.peerIds = new Map()
this.protoBook = new ProtoBook(this)
}

/**
Expand All @@ -73,7 +72,7 @@ class PeerStore extends EventEmitter {

// AddressBook
for (const [idStr, addresses] of this.addressBook.data.entries()) {
const id = PeerId.createFromCID(idStr)
const id = this.keyBook.data.get(idStr) || PeerId.createFromCID(idStr)
peersData.set(idStr, {
id,
addresses,
Expand All @@ -84,10 +83,11 @@ class PeerStore extends EventEmitter {
// ProtoBook
for (const [idStr, protocols] of this.protoBook.data.entries()) {
const pData = peersData.get(idStr)
const id = this.keyBook.data.get(idStr) || PeerId.createFromCID(idStr)

if (!pData) {
peersData.set(idStr, {
id: PeerId.createFromCID(idStr),
id,
addresses: [],
protocols: Array.from(protocols)
})
Expand All @@ -104,8 +104,10 @@ class PeerStore extends EventEmitter {
*/
delete (peerId) {
const addressesDeleted = this.addressBook.delete(peerId)
const keyDeleted = this.keyBook.delete(peerId)
const protocolsDeleted = this.protoBook.delete(peerId)
return addressesDeleted || protocolsDeleted

return addressesDeleted || keyDeleted || protocolsDeleted
}

/**
Expand All @@ -118,7 +120,7 @@ class PeerStore extends EventEmitter {
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
}

const id = this.peerIds.get(peerId.toB58String())
const id = this.keyBook.data.get(peerId.toB58String())
const addresses = this.addressBook.get(peerId)
const protocols = this.protoBook.get(peerId)

Expand Down
Loading

0 comments on commit c0bbda2

Please sign in to comment.