Skip to content

Commit ce38033

Browse files
vasco-santosjacobheun
authored andcommitted
feat: keybook
1 parent 3f2b06d commit ce38033

12 files changed

+363
-72
lines changed

doc/API.md

+86
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
* [`peerStore.addressBook.get`](#peerstoreaddressbookget)
2727
* [`peerStore.addressBook.getMultiaddrsForPeer`](#peerstoreaddressbookgetmultiaddrsforpeer)
2828
* [`peerStore.addressBook.set`](#peerstoreaddressbookset)
29+
* [`peerStore.keyBook.delete`](#peerstorekeybookdelete)
30+
* [`peerStore.keyBook.get`](#peerstorekeybookget)
31+
* [`peerStore.keyBook.set`](#peerstorekeybookset)
2932
* [`peerStore.protoBook.add`](#peerstoreprotobookadd)
3033
* [`peerStore.protoBook.delete`](#peerstoreprotobookdelete)
3134
* [`peerStore.protoBook.get`](#peerstoreprotobookget)
@@ -811,6 +814,89 @@ Add known `protocols` of a given peer.
811814
peerStore.protoBook.add(peerId, protocols)
812815
```
813816

817+
* [`peerStore.keyBook.get`](#peerstorekeybookget)
818+
* [`peerStore.keyBook.set`](#peerstorekeybookset)
819+
820+
### peerStore.keyBook.delete
821+
822+
Delete the provided peer from the book.
823+
824+
`peerStore.keyBook.delete(peerId)`
825+
826+
#### Parameters
827+
828+
| Name | Type | Description |
829+
|------|------|-------------|
830+
| peerId | [`PeerId`][peer-id] | peerId to remove |
831+
832+
#### Returns
833+
834+
| Type | Description |
835+
|------|-------------|
836+
| `boolean` | true if found and removed |
837+
838+
#### Example
839+
840+
```js
841+
peerStore.keyBook.delete(peerId)
842+
// false
843+
peerStore.keyBook.set(peerId)
844+
peerStore.keyBook.delete(peerId)
845+
// true
846+
```
847+
848+
### peerStore.keyBook.get
849+
850+
Get the known `PublicKey` of a provided peer.
851+
852+
`peerStore.keyBook.get(peerId)`
853+
854+
#### Parameters
855+
856+
| Name | Type | Description |
857+
|------|------|-------------|
858+
| peerId | [`PeerId`][peer-id] | peerId to get |
859+
860+
#### Returns
861+
862+
| Type | Description |
863+
|------|-------------|
864+
| `RsaPublicKey|Ed25519PublicKey|Secp256k1PublicKey` | Peer PublicKey |
865+
866+
#### Example
867+
868+
```js
869+
peerStore.keyBook.get(peerId)
870+
// undefined
871+
peerStore.keyBook.set(peerId) // with inline public key
872+
peerStore.keyBook.get(peerId)
873+
// PublicKey
874+
```
875+
876+
### peerStore.keyBook.set
877+
878+
Set known `peerId`. This can include its Public Key.
879+
880+
`peerStore.keyBook.set(peerId)`
881+
882+
#### Parameters
883+
884+
| Name | Type | Description |
885+
|------|------|-------------|
886+
| peerId | [`PeerId`][peer-id] | peerId to set |
887+
888+
#### Returns
889+
890+
| Type | Description |
891+
|------|-------------|
892+
| `KeyBook` | Returns the Key Book component |
893+
894+
#### Example
895+
896+
```js
897+
peerStore.keyBook.set(peerId)
898+
```
899+
814900
### peerStore.protoBook.delete
815901

816902
Delete the provided peer from the book.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"aegir": "^22.0.0",
8484
"chai": "^4.2.0",
8585
"chai-as-promised": "^7.1.1",
86+
"chai-bytes": "^0.1.2",
8687
"cids": "^0.8.0",
8788
"delay": "^4.3.0",
8889
"dirty-chai": "^2.0.1",

src/peer-store/README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ A `peerId.toString()` identifier mapping to a `Address` object, which should hav
5252

5353
#### Key Book
5454

55-
The `keyBook` tracks the keys of the peers.
55+
The `keyBook` tracks the publick keys of the peers by keeping their [`PeerId`][peer-id].
5656

57-
**Not Yet Implemented**
57+
`Map<string, PeerId`
58+
59+
A `peerId.toString()` identifier mapping to a `PeerId` of the peer. This instance contains the peer public key.
5860

5961
#### Protocol Book
6062

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

7577
Access to its underlying books:
7678

77-
- `peerStore.protoBook.*`
7879
- `peerStore.addressBook.*`
80+
- `peerStore.keyBook.*`
81+
- `peerStore.protoBook.*`
7982

8083
### Events
8184

@@ -107,8 +110,6 @@ All the known peer protocols are stored with a key pattern as follows:
107110

108111
**KeyBook**
109112

110-
_NOT_YET_IMPLEMENTED_
111-
112113
All public keys are stored under the following pattern:
113114

114115
` /peers/keys/<b32 peer id no padding>`
@@ -127,3 +128,5 @@ Metadata is stored under the following key pattern:
127128
- Further API methods will probably need to be added in the context of multiaddr validity and confidence.
128129
- When improving libp2p configuration for specific runtimes, we should take into account the PeerStore recommended datastore.
129130
- 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.
131+
132+
[peer-id]: https://github.com/libp2p/js-peer-id

src/peer-store/address-book.js

-10
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@ class AddressBook extends Book {
8686
this._setData(peerId, addresses)
8787
log(`stored provided multiaddrs for ${id}`)
8888

89-
// Notify the existance of a new peer
90-
if (!rec) {
91-
this._ps.emit('peer', peerId)
92-
}
93-
9489
return this
9590
}
9691

@@ -130,11 +125,6 @@ class AddressBook extends Book {
130125

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

133-
// Notify the existance of a new peer
134-
if (!rec) {
135-
this._ps.emit('peer', peerId)
136-
}
137-
138128
return this
139129
}
140130

src/peer-store/book.js

+16-25
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Book {
4747
* Set data into the datastructure, persistence and emit it using the provided transformers.
4848
* @private
4949
* @param {PeerId} peerId peerId of the data to store
50-
* @param {Array<*>} data data to store.
50+
* @param {*} data data to store.
5151
* @param {Object} [options] storing options.
5252
* @param {boolean} [options.emit = true] emit the provided data.
5353
* @return {void}
@@ -57,22 +57,27 @@ class Book {
5757

5858
// Store data in memory
5959
this.data.set(b58key, data)
60-
this._setPeerId(peerId)
60+
61+
// Store PeerId
62+
if (!PeerId.isPeerId(data)) {
63+
this._ps.keyBook.set(peerId)
64+
}
6165

6266
// Emit event
63-
emit && this._ps.emit(this.eventName, {
64-
peerId,
65-
[this.eventProperty]: this.eventTransformer(data)
66-
})
67+
emit && this._emit(peerId, data)
6768
}
6869

6970
/**
70-
* Add known data of a provided peer.
71+
* Emit data.
72+
* @private
7173
* @param {PeerId} peerId
72-
* @param {Array<Data>|Data} data
74+
* @param {*} data
7375
*/
74-
add (peerId, data) {
75-
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
76+
_emit (peerId, data) {
77+
this._ps.emit(this.eventName, {
78+
peerId,
79+
[this.eventProperty]: this.eventTransformer(data)
80+
})
7681
}
7782

7883
/**
@@ -104,24 +109,10 @@ class Book {
104109
return false
105110
}
106111

107-
this._ps.emit(this.eventName, {
108-
peerId,
109-
[this.eventProperty]: []
110-
})
112+
this._emit(peerId, [])
111113

112114
return true
113115
}
114-
115-
/**
116-
* Set PeerId into peerStore datastructure.
117-
* @private
118-
* @param {PeerId} peerId
119-
*/
120-
_setPeerId (peerId) {
121-
if (!this._ps.peerIds.get(peerId)) {
122-
this._ps.peerIds.set(peerId.toB58String(), peerId)
123-
}
124-
}
125116
}
126117

127118
module.exports = Book

src/peer-store/index.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { EventEmitter } = require('events')
99
const PeerId = require('peer-id')
1010

1111
const AddressBook = require('./address-book')
12+
const KeyBook = require('./key-book')
1213
const ProtoBook = require('./proto-book')
1314

1415
const {
@@ -42,16 +43,14 @@ class PeerStore extends EventEmitter {
4243
this.addressBook = new AddressBook(this)
4344

4445
/**
45-
* ProtoBook containing a map of peerIdStr to supported protocols.
46+
* KeyBook containing a map of peerIdStr to their PeerId with public keys.
4647
*/
47-
this.protoBook = new ProtoBook(this)
48+
this.keyBook = new KeyBook(this)
4849

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

5756
/**
@@ -73,7 +72,7 @@ class PeerStore extends EventEmitter {
7372

7473
// AddressBook
7574
for (const [idStr, addresses] of this.addressBook.data.entries()) {
76-
const id = PeerId.createFromCID(idStr)
75+
const id = this.keyBook.data.get(idStr) || PeerId.createFromCID(idStr)
7776
peersData.set(idStr, {
7877
id,
7978
addresses,
@@ -84,10 +83,11 @@ class PeerStore extends EventEmitter {
8483
// ProtoBook
8584
for (const [idStr, protocols] of this.protoBook.data.entries()) {
8685
const pData = peersData.get(idStr)
86+
const id = this.keyBook.data.get(idStr) || PeerId.createFromCID(idStr)
8787

8888
if (!pData) {
8989
peersData.set(idStr, {
90-
id: PeerId.createFromCID(idStr),
90+
id,
9191
addresses: [],
9292
protocols: Array.from(protocols)
9393
})
@@ -104,8 +104,10 @@ class PeerStore extends EventEmitter {
104104
*/
105105
delete (peerId) {
106106
const addressesDeleted = this.addressBook.delete(peerId)
107+
const keyDeleted = this.keyBook.delete(peerId)
107108
const protocolsDeleted = this.protoBook.delete(peerId)
108-
return addressesDeleted || protocolsDeleted
109+
110+
return addressesDeleted || keyDeleted || protocolsDeleted
109111
}
110112

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

121-
const id = this.peerIds.get(peerId.toB58String())
123+
const id = this.keyBook.data.get(peerId.toB58String())
122124
const addresses = this.addressBook.get(peerId)
123125
const protocols = this.protoBook.get(peerId)
124126

0 commit comments

Comments
 (0)