Skip to content

Commit 1e3d6f4

Browse files
vasco-santosjacobheun
andcommittedMay 28, 2020
chore: apply suggestions from code review
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
1 parent 48a9a3e commit 1e3d6f4

File tree

6 files changed

+28
-26
lines changed

6 files changed

+28
-26
lines changed
 

‎doc/API.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Creates an instance of Libp2p.
7474
| [options.dialer] | `object` | libp2p Dialer configuration
7575
| [options.metrics] | `object` | libp2p Metrics configuration
7676
| [options.peerId] | [`PeerId`][peer-id] | peerId instance (it will be created if not provided) |
77-
| [options.peerStore] | [`PeerId`][peer-id] | libp2p PeerStore configuration |
77+
| [options.peerStore] | `object` | libp2p PeerStore configuration |
7878

7979
For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).
8080

‎doc/CONFIGURATION.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,9 @@ const node = await Libp2p.create({
508508
509509
#### Configuring PeerStore
510510
511-
PeerStore persistence is disabled in libp2p by default. You can enable and configure it as follows. Aside from enabled being `false` by default, it will need an implementation of a [datastore](https://github.com/ipfs/interface-datastore).
511+
PeerStore persistence is disabled in libp2p by default. You can enable and configure it as follows. Aside from enabled being `false` by default, it will need an implementation of a [datastore](https://github.com/ipfs/interface-datastore). Take into consideration that using the memory datastore will be ineffective for persistence.
512+
513+
The threshold number represents the maximum number of "dirty peers" allowed in the PeerStore, i.e. peers that are not updated in the datastore. In this context, browser nodes should use a threshold of 1, since they might not "stop" properly in several scenarios and the PeerStore might end up with unflushed records when the window is closed.
512514
513515
```js
514516
const Libp2p = require('libp2p')
@@ -526,8 +528,8 @@ const node = await Libp2p.create({
526528
},
527529
datastore: new LevelStore('path/to/store'),
528530
peerStore: {
529-
persistence: true,
530-
threshold: 5
531+
persistence: true, // Is persistence enabled (default: false)
532+
threshold: 5 // Number of dirty peers allowed (default: 5)
531533
}
532534
})
533535
```

‎src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ class Libp2p extends EventEmitter {
4747
this.peerId = this._options.peerId
4848
this.datastore = this._options.datastore
4949

50-
this.peerStore = !(this.datastore && this._options.peerStore.persistence)
51-
? new PeerStore()
52-
: new PersistentPeerStore({
50+
this.peerStore = (this.datastore && this._options.peerStore.persistence)
51+
? new PersistentPeerStore({
5352
datastore: this.datastore,
5453
...this._options.peerStore
5554
})
55+
: new PeerStore()
5656

5757
// Addresses {listen, announce, noAnnounce}
5858
this.addresses = this._options.addresses

‎src/peer-store/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,21 @@ Access to its underlying books:
8787

8888
The data stored in the PeerStore can be persisted if configured appropriately. Keeping a record of the peers already discovered by the peer, as well as their known data aims to improve the efficiency of peers joining the network after being offline.
8989

90-
The libp2p node will need to receive a [datastore](https://github.com/ipfs/interface-datastore), in order to store this data in a persistent way. A [datastore](https://github.com/ipfs/interface-datastore) stores its data in a key-value fashion. As a result, we need coherent keys so that we do not overwrite data.
90+
The libp2p node will need to receive a [datastore](https://github.com/ipfs/interface-datastore), in order to persist this data across restarts. A [datastore](https://github.com/ipfs/interface-datastore) stores its data in a key-value fashion. As a result, we need coherent keys so that we do not overwrite data.
9191

92-
The PeerStore should not be continuously updating the datastore with the new data observed. Accordingly, it should only store new data after reaching a certain threshold of "dirty" peers, as well as when the node is stopped.
92+
The PeerStore should not continuously update the datastore whenever data is changed. Instead, it should only store new data after reaching a certain threshold of "dirty" peers, as well as when the node is stopped, in order to batch writes to the datastore.
9393

94-
Taking into account that a datastore allows queries using a key prefix, we can find all the information if we define a consistent namespace that allow us to find the content without having any information. The namespaces were defined as follows:
94+
The peer id will be appended to the datastore key for each data namespace. The namespaces were defined as follows:
9595

9696
**AddressBook**
9797

98-
All the knownw peer addresses are stored with a key pattern as follows:
98+
All the known peer addresses are stored with a key pattern as follows:
9999

100100
`/peers/addrs/<b32 peer id no padding>`
101101

102102
**ProtoBook**
103103

104-
All the knownw peer protocols are stored with a key pattern as follows:
104+
All the known peer protocols are stored with a key pattern as follows:
105105

106106
`/peers/protos/<b32 peer id no padding>`
107107

‎src/peer-store/persistent/consts.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

3-
module.exports.COMMON_NAMESPACE = '/peers/'
3+
module.exports.NAMESPACE_COMMON = '/peers/'
44

55
// /peers/protos/<b32 peer id no padding>
6-
module.exports.ADDRESS_NAMESPACE = '/peers/addrs/'
6+
module.exports.NAMESPACE_ADDRESS = '/peers/addrs/'
77

88
// /peers/addrs/<b32 peer id no padding>
9-
module.exports.PROTOCOL_NAMESPACE = '/peers/protos/'
9+
module.exports.NAMESPACE_PROTOCOL = '/peers/protos/'

‎src/peer-store/persistent/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const PeerId = require('peer-id')
1111
const PeerStore = require('..')
1212

1313
const {
14-
ADDRESS_NAMESPACE,
15-
COMMON_NAMESPACE,
16-
PROTOCOL_NAMESPACE
14+
NAMESPACE_ADDRESS,
15+
NAMESPACE_COMMON,
16+
NAMESPACE_PROTOCOL
1717
} = require('./consts')
1818

1919
const Addresses = require('./pb/address-book.proto')
@@ -51,25 +51,25 @@ class PersistentPeerStore extends PeerStore {
5151
* @return {Promise<void>}
5252
*/
5353
async start () {
54-
log('Persistent PeerStore is starting')
54+
log('PeerStore is starting')
5555

5656
// Handlers for dirty peers
5757
this.on('change:protocols', this._addDirtyPeer)
5858
this.on('change:multiaddrs', this._addDirtyPeer)
5959

6060
// Load data
61-
for await (const entry of this._datastore.query({ prefix: COMMON_NAMESPACE })) {
61+
for await (const entry of this._datastore.query({ prefix: NAMESPACE_COMMON })) {
6262
this._processDatastoreEntry(entry)
6363
}
6464

65-
log('Persistent PeerStore started')
65+
log('PeerStore started')
6666
}
6767

6868
async stop () {
69-
log('Persistent PeerStore is stopping')
69+
log('PeerStore is stopping')
7070
this.removeAllListeners()
7171
await this._commitData()
72-
log('Persistent PeerStore stopped')
72+
log('PeerStore stopped')
7373
}
7474

7575
/**
@@ -131,7 +131,7 @@ class PersistentPeerStore extends PeerStore {
131131
*/
132132
_batchAddressBook (peerId, batch) {
133133
const b32key = peerId.toString()
134-
const key = new Key(`${ADDRESS_NAMESPACE}${b32key}`)
134+
const key = new Key(`${NAMESPACE_ADDRESS}${b32key}`)
135135

136136
const addresses = this.addressBook.get(peerId)
137137

@@ -162,7 +162,7 @@ class PersistentPeerStore extends PeerStore {
162162
*/
163163
_batchProtoBook (peerId, batch) {
164164
const b32key = peerId.toString()
165-
const key = new Key(`${PROTOCOL_NAMESPACE}${b32key}`)
165+
const key = new Key(`${NAMESPACE_PROTOCOL}${b32key}`)
166166

167167
const protocols = this.protoBook.get(peerId)
168168

@@ -185,7 +185,7 @@ class PersistentPeerStore extends PeerStore {
185185
* Process datastore entry and add its data to the correct book.
186186
* @private
187187
* @param {Object} params
188-
* @param {string} params.key datastore key
188+
* @param {Key} params.key datastore key
189189
* @param {Buffer} params.value datastore value stored
190190
*/
191191
_processDatastoreEntry ({ key, value }) {

0 commit comments

Comments
 (0)
Please sign in to comment.