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

feat: keybook #626

Merged
merged 4 commits into from
May 7, 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
86 changes: 86 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 @@ -811,6 +814,89 @@ Add known `protocols` of a given peer.
peerStore.protoBook.add(peerId, protocols)
```

* [`peerStore.keyBook.get`](#peerstorekeybookget)
* [`peerStore.keyBook.set`](#peerstorekeybookset)
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved

### 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)
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) // with inline public key
peerStore.keyBook.get(peerId)
// PublicKey
```

### peerStore.keyBook.set

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

`peerStore.keyBook.set(peerId)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if there is no public key on the PeerId instance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just keeps the PeerId in it, and might update in the future. I would see the peer-id existing when we discover a new peer


#### Parameters

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

#### Returns

| Type | Description |
|------|-------------|
| `KeyBook` | Returns the Key Book component |

#### Example

```js
peerStore.keyBook.set(peerId)
```

### peerStore.protoBook.delete

Delete the provided peer from the book.
Expand Down
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",
"cids": "^0.8.0",
"delay": "^4.3.0",
"dirty-chai": "^2.0.1",
Expand Down
13 changes: 8 additions & 5 deletions src/peer-store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ 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 publick keys of the peers by keeping their [`PeerId`][peer-id].
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved

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

A `peerId.toString()` identifier mapping to a `PeerId` of the peer. This instance contains the peer public key.
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved

#### Protocol Book

Expand All @@ -74,8 +76,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 +110,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 +128,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
10 changes: 0 additions & 10 deletions src/peer-store/address-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ class AddressBook extends Book {
this._setData(peerId, addresses)
log(`stored provided multiaddrs for ${id}`)

// Notify the existance of a new peer
if (!rec) {
this._ps.emit('peer', peerId)
}

return this
}

Expand Down Expand Up @@ -130,11 +125,6 @@ class AddressBook extends Book {

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

// Notify the existance of a new peer
if (!rec) {
this._ps.emit('peer', peerId)
}

return this
}

Expand Down
41 changes: 16 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,27 @@ class Book {

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

// Store PeerId
if (!PeerId.isPeerId(data)) {
this._ps.keyBook.set(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 +109,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