@libp2p/peer-store 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +44 -0
  3. package/dist/src/address-book.d.ts +31 -0
  4. package/dist/src/address-book.d.ts.map +1 -0
  5. package/dist/src/address-book.js +265 -0
  6. package/dist/src/address-book.js.map +1 -0
  7. package/dist/src/errors.d.ts +5 -0
  8. package/dist/src/errors.d.ts.map +1 -0
  9. package/dist/src/errors.js +5 -0
  10. package/dist/src/errors.js.map +1 -0
  11. package/dist/src/index.d.ts +43 -0
  12. package/dist/src/index.d.ts.map +1 -0
  13. package/dist/src/index.js +88 -0
  14. package/dist/src/index.js.map +1 -0
  15. package/dist/src/key-book.d.ts +21 -0
  16. package/dist/src/key-book.d.ts.map +1 -0
  17. package/dist/src/key-book.js +98 -0
  18. package/dist/src/key-book.js.map +1 -0
  19. package/dist/src/metadata-book.d.ts +28 -0
  20. package/dist/src/metadata-book.d.ts.map +1 -0
  21. package/dist/src/metadata-book.js +168 -0
  22. package/dist/src/metadata-book.js.map +1 -0
  23. package/dist/src/pb/peer.d.ts +222 -0
  24. package/dist/src/pb/peer.js +641 -0
  25. package/dist/src/proto-book.d.ts +18 -0
  26. package/dist/src/proto-book.d.ts.map +1 -0
  27. package/dist/src/proto-book.js +170 -0
  28. package/dist/src/proto-book.js.map +1 -0
  29. package/dist/src/store.d.ts +37 -0
  30. package/dist/src/store.d.ts.map +1 -0
  31. package/dist/src/store.js +169 -0
  32. package/dist/src/store.js.map +1 -0
  33. package/package.json +162 -0
  34. package/src/README.md +145 -0
  35. package/src/address-book.ts +330 -0
  36. package/src/errors.ts +5 -0
  37. package/src/index.ts +123 -0
  38. package/src/key-book.ts +117 -0
  39. package/src/metadata-book.ts +200 -0
  40. package/src/pb/peer.d.ts +222 -0
  41. package/src/pb/peer.js +641 -0
  42. package/src/pb/peer.proto +31 -0
  43. package/src/proto-book.ts +204 -0
  44. package/src/store.ts +224 -0
package/src/README.md ADDED
@@ -0,0 +1,145 @@
1
+ # PeerStore
2
+
3
+ Libp2p's PeerStore is responsible for keeping an updated register with the relevant information of the known peers. It should be the single source of truth for all peer data, where a subsystem can learn about peers' data and where someone can listen for updates. The PeerStore comprises four main components: `addressBook`, `keyBook`, `protocolBook` and `metadataBook`.
4
+
5
+ The PeerStore manages the high level operations on its inner books. Moreover, the PeerStore should be responsible for notifying interested parties of relevant events, through its Event Emitter.
6
+
7
+ ## Submitting records to the PeerStore
8
+
9
+ Several libp2p subsystems will perform operations that might gather relevant information about peers.
10
+
11
+ ### Identify
12
+ - The Identify protocol automatically runs on every connection when multiplexing is enabled. The protocol will put the multiaddrs and protocols provided by the peer to the PeerStore.
13
+ - In the background, the Identify Service is also waiting for protocol change notifications of peers via the IdentifyPush protocol. Peers may leverage the `identify-push` message to communicate protocol changes to all connected peers, so that their PeerStore can be updated with the updated protocols.
14
+ - While it is currently not supported in js-libp2p, future iterations may also support the [IdentifyDelta protocol](https://github.com/libp2p/specs/pull/176).
15
+ - Taking into account that the Identify protocol records are directly from the peer, they should be considered the source of truth and weighted accordingly.
16
+
17
+ ### Peer Discovery
18
+ - Libp2p discovery protocols aim to discover new peers in the network. In a typical discovery protocol, addresses of the peer are discovered along with its peer id. Once this happens, a libp2p discovery protocol should emit a `peer` event with the information of the discovered peer and this information will be added to the PeerStore by libp2p.
19
+
20
+ ### Dialer
21
+ - Libp2p API supports dialing a peer given a `multiaddr`, and no prior knowledge of the peer. If the node is able to establish a connection with the peer, it and its multiaddr is added to the PeerStore.
22
+ - 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`.
23
+
24
+ ### DHT
25
+ - On some DHT operations, such as finding providers for a given CID, nodes may exchange peer data as part of the query. This passive peer discovery should result in the DHT emitting the `peer` event in the same way [Peer Discovery](#peerdiscovery) does.
26
+
27
+ ## Retrieving records from the PeerStore
28
+
29
+ When data in the PeerStore is updated the PeerStore will emit events based on the changes, to allow applications and other subsystems to take action on those changes. Any subsystem interested in these notifications should subscribe the [`PeerStore events`][peer-store-events].
30
+
31
+ ### Peer
32
+ - Each time a new peer is discovered, the PeerStore should emit a [`peer` event][peer-store-events], so that interested parties can leverage this peer and establish a connection with it.
33
+
34
+ ### Protocols
35
+ - When the known protocols of a peer change, the PeerStore emits a [`change:protocols` event][peer-store-events].
36
+
37
+ ### Multiaddrs
38
+ - When the known listening `multiaddrs` of a peer change, the PeerStore emits a [`change:multiaddrs` event][peer-store-events].
39
+
40
+ ## PeerStore implementation
41
+
42
+ The PeerStore wraps four main components: `addressBook`, `keyBook`, `protocolBook` and `metadataBook`. Moreover, it provides a high level API for those components, as well as data events.
43
+
44
+ ### Components
45
+
46
+ #### Address Book
47
+
48
+ 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.
49
+
50
+ `Map<string, Address>`
51
+
52
+ A `peerId.toString(base58btc)` identifier mapping to a `Address` object, which should have the following structure:
53
+
54
+ ```js
55
+ {
56
+ multiaddr: <Multiaddr>
57
+ }
58
+ ```
59
+
60
+ #### Key Book
61
+
62
+ The `keyBook` tracks the public keys of the peers by keeping their [`PeerId`][peer-id].
63
+
64
+ `Map<string, PeerId`
65
+
66
+ A `peerId.toString(base58btc)` identifier mapping to a `PeerId` of the peer. This instance contains the peer public key.
67
+
68
+ #### Protocol Book
69
+
70
+ 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.
71
+
72
+ `Map<string, Set<string>>`
73
+
74
+ A `peerId.toString(base58btc)` identifier mapping to a `Set` of protocol identifier strings.
75
+
76
+ #### Metadata Book
77
+
78
+ The `metadataBook` keeps track of the known metadata of a peer. Its metadata is stored in a key value fashion, where a key identifier (`string`) represents a metadata value (`Uint8Array`).
79
+
80
+ `Map<string, Map<string, Uint8Array>>`
81
+
82
+ A `peerId.toString(base58btc)` identifier mapping to the peer metadata Map.
83
+
84
+ ### API
85
+
86
+ For the complete API documentation, you should check the [API.md](../../doc/API.md).
87
+
88
+ Access to its underlying books:
89
+
90
+ - `peerStore.addressBook.*`
91
+ - `peerStore.keyBook.*`
92
+ - `peerStore.metadataBook.*`
93
+ - `peerStore.protoBook.*`
94
+
95
+ ### Events
96
+
97
+ - `peer` - emitted when a new peer is added.
98
+ - `change:multiaadrs` - emitted when a known peer has a different set of multiaddrs.
99
+ - `change:protocols` - emitted when a known peer supports a different set of protocols.
100
+ - `change:pubkey` - emitted when a peer's public key is known.
101
+ - `change:metadata` - emitted when known metadata of a peer changes.
102
+
103
+ ## Data Persistence
104
+
105
+ 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.
106
+
107
+ 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.
108
+
109
+ 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.
110
+
111
+ The peer id will be appended to the datastore key for each data namespace. The namespaces were defined as follows:
112
+
113
+ **AddressBook**
114
+
115
+ All the known peer addresses are stored with a key pattern as follows:
116
+
117
+ `/peers/addrs/<b32 peer id no padding>`
118
+
119
+ **ProtoBook**
120
+
121
+ All the known peer protocols are stored with a key pattern as follows:
122
+
123
+ `/peers/protos/<b32 peer id no padding>`
124
+
125
+ **KeyBook**
126
+
127
+ All public keys are stored under the following pattern:
128
+
129
+ ` /peers/keys/<b32 peer id no padding>`
130
+
131
+ **MetadataBook**
132
+
133
+ Metadata is stored under the following key pattern:
134
+
135
+ `/peers/metadata/<b32 peer id no padding>/<key>`
136
+
137
+ ## Future Considerations
138
+
139
+ - If multiaddr TTLs are added, the PeerStore may schedule jobs to delete all addresses that exceed the TTL to prevent AddressBook bloating
140
+ - Further API methods will probably need to be added in the context of multiaddr validity and confidence.
141
+ - When improving libp2p configuration for specific runtimes, we should take into account the PeerStore recommended datastore.
142
+ - 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.
143
+
144
+ [peer-id]: https://github.com/libp2p/js-peer-id
145
+ [peer-store-events]: ../../doc/API.md#libp2ppeerstore
@@ -0,0 +1,330 @@
1
+ import { logger } from '@libp2p/logger'
2
+ import errcode from 'err-code'
3
+ import { Multiaddr } from '@multiformats/multiaddr'
4
+ import { codes } from './errors.js'
5
+ import { PeerRecord, RecordEnvelope } from '@libp2p/peer-record'
6
+ import { pipe } from 'it-pipe'
7
+ import all from 'it-all'
8
+ import filter from 'it-filter'
9
+ import map from 'it-map'
10
+ import each from 'it-foreach'
11
+ import { base58btc } from 'multiformats/bases/base58'
12
+ import { PeerId } from '@libp2p/peer-id'
13
+ import type { PeerStore } from '@libp2p/interfaces/peer-store'
14
+ import type { Store } from './store.js'
15
+ import type { AddressFilter, AddressSorter } from './index.js'
16
+ import type { Envelope } from '@libp2p/interfaces/record'
17
+
18
+ const log = logger('libp2p:peer-store:address-book')
19
+ const EVENT_NAME = 'change:multiaddrs'
20
+
21
+ async function allowAll () {
22
+ return true
23
+ }
24
+
25
+ export class PeerStoreAddressBook {
26
+ private emit: PeerStore["emit"]
27
+ private store: Store
28
+ private addressFilter: AddressFilter
29
+
30
+ constructor (emit: PeerStore["emit"], store: Store, addressFilter?: AddressFilter) {
31
+ this.emit = emit
32
+ this.store = store
33
+ this.addressFilter = addressFilter ?? allowAll
34
+ }
35
+
36
+ /**
37
+ * ConsumePeerRecord adds addresses from a signed peer record contained in a record envelope.
38
+ * This will return a boolean that indicates if the record was successfully processed and added
39
+ * into the AddressBook.
40
+ */
41
+ async consumePeerRecord (envelope: Envelope) {
42
+ log('consumePeerRecord await write lock')
43
+ const release = await this.store.lock.writeLock()
44
+ log('consumePeerRecord got write lock')
45
+
46
+ let peerId
47
+ let updatedPeer
48
+
49
+ try {
50
+ let peerRecord
51
+ try {
52
+ peerRecord = PeerRecord.createFromProtobuf(envelope.payload)
53
+ } catch (err: any) {
54
+ log.error('invalid peer record received')
55
+ return false
56
+ }
57
+
58
+ peerId = peerRecord.peerId
59
+ const multiaddrs = peerRecord.multiaddrs
60
+
61
+ // Verify peerId
62
+ if (!peerId.equals(envelope.peerId)) {
63
+ log('signing key does not match PeerId in the PeerRecord')
64
+ return false
65
+ }
66
+
67
+ // ensure the record has multiaddrs
68
+ if (!multiaddrs || !multiaddrs.length) {
69
+ return false
70
+ }
71
+
72
+ if (await this.store.has(peerId)) {
73
+ const peer = await this.store.load(peerId)
74
+
75
+ if (peer.peerRecordEnvelope) {
76
+ const storedEnvelope = await RecordEnvelope.createFromProtobuf(peer.peerRecordEnvelope)
77
+ const storedRecord = PeerRecord.createFromProtobuf(storedEnvelope.payload)
78
+
79
+ // ensure seq is greater than, or equal to, the last received
80
+ if (storedRecord.seqNumber >= peerRecord.seqNumber) {
81
+ return false
82
+ }
83
+ }
84
+ }
85
+
86
+ // Replace unsigned addresses by the new ones from the record
87
+ // TODO: Once we have ttls for the addresses, we should merge these in
88
+ updatedPeer = await this.store.patchOrCreate(peerId, {
89
+ addresses: await filterMultiaddrs(peerId, multiaddrs, this.addressFilter, true),
90
+ peerRecordEnvelope: envelope.marshal()
91
+ })
92
+
93
+ log(`stored provided peer record for ${peerRecord.peerId.toString(base58btc)}`)
94
+ } finally {
95
+ log('consumePeerRecord release write lock')
96
+ release()
97
+ }
98
+
99
+ this.emit(EVENT_NAME, { peerId, multiaddrs: updatedPeer.addresses.map(({ multiaddr }) => multiaddr) })
100
+
101
+ return true
102
+ }
103
+
104
+ async getRawEnvelope (peerId: PeerId) {
105
+ log('getRawEnvelope await read lock')
106
+ const release = await this.store.lock.readLock()
107
+ log('getRawEnvelope got read lock')
108
+
109
+ try {
110
+ const peer = await this.store.load(peerId)
111
+
112
+ return peer.peerRecordEnvelope
113
+ } catch (err: any) {
114
+ if (err.code !== codes.ERR_NOT_FOUND) {
115
+ throw err
116
+ }
117
+ } finally {
118
+ log('getRawEnvelope release read lock')
119
+ release()
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Get an Envelope containing a PeerRecord for the given peer.
125
+ * Returns undefined if no record exists.
126
+ */
127
+ async getPeerRecord (peerId: PeerId) {
128
+ const raw = await this.getRawEnvelope(peerId)
129
+
130
+ if (!raw) {
131
+ return undefined
132
+ }
133
+
134
+ return RecordEnvelope.createFromProtobuf(raw)
135
+ }
136
+
137
+ async get (peerId: PeerId) {
138
+ peerId = PeerId.fromPeerId(peerId)
139
+
140
+ log('get wait for read lock')
141
+ const release = await this.store.lock.readLock()
142
+ log('get got read lock')
143
+
144
+ try {
145
+ const peer = await this.store.load(peerId)
146
+
147
+ return peer.addresses
148
+ } catch (err: any) {
149
+ if (err.code !== codes.ERR_NOT_FOUND) {
150
+ throw err
151
+ }
152
+ } finally {
153
+ log('get release read lock')
154
+ release()
155
+ }
156
+
157
+ return []
158
+ }
159
+
160
+ async set (peerId: PeerId, multiaddrs: Multiaddr[]) {
161
+ peerId = PeerId.fromPeerId(peerId)
162
+
163
+ if (!Array.isArray(multiaddrs)) {
164
+ log.error('multiaddrs must be an array of Multiaddrs')
165
+ throw errcode(new Error('multiaddrs must be an array of Multiaddrs'), codes.ERR_INVALID_PARAMETERS)
166
+ }
167
+
168
+ log('set await write lock')
169
+ const release = await this.store.lock.writeLock()
170
+ log('set got write lock')
171
+
172
+ let hasPeer = false
173
+ let updatedPeer
174
+
175
+ try {
176
+ const addresses = await filterMultiaddrs(peerId, multiaddrs, this.addressFilter)
177
+
178
+ // No valid addresses found
179
+ if (!addresses.length) {
180
+ return
181
+ }
182
+
183
+ try {
184
+ const peer = await this.store.load(peerId)
185
+ hasPeer = true
186
+
187
+ if (new Set([
188
+ ...addresses.map(({ multiaddr }) => multiaddr.toString()),
189
+ ...peer.addresses.map(({ multiaddr }) => multiaddr.toString())
190
+ ]).size === peer.addresses.length && addresses.length === peer.addresses.length) {
191
+ // not changing anything, no need to update
192
+ return
193
+ }
194
+ } catch (err: any) {
195
+ if (err.code !== codes.ERR_NOT_FOUND) {
196
+ throw err
197
+ }
198
+ }
199
+
200
+ updatedPeer = await this.store.patchOrCreate(peerId, { addresses })
201
+
202
+ log(`set multiaddrs for ${peerId.toString(base58btc)}`)
203
+ } finally {
204
+ log('set release write lock')
205
+ release()
206
+ }
207
+
208
+ this.emit(EVENT_NAME, { peerId, multiaddrs: updatedPeer.addresses.map(addr => addr.multiaddr) })
209
+
210
+ // Notify the existence of a new peer
211
+ if (!hasPeer) {
212
+ this.emit('peer', peerId)
213
+ }
214
+ }
215
+
216
+ async add (peerId: PeerId, multiaddrs: Multiaddr[]) {
217
+ peerId = PeerId.fromPeerId(peerId)
218
+
219
+ if (!Array.isArray(multiaddrs)) {
220
+ log.error('multiaddrs must be an array of Multiaddrs')
221
+ throw errcode(new Error('multiaddrs must be an array of Multiaddrs'), codes.ERR_INVALID_PARAMETERS)
222
+ }
223
+
224
+ log('add await write lock')
225
+ const release = await this.store.lock.writeLock()
226
+ log('add got write lock')
227
+
228
+ let hasPeer
229
+ let updatedPeer
230
+
231
+ try {
232
+ const addresses = await filterMultiaddrs(peerId, multiaddrs, this.addressFilter)
233
+
234
+ // No valid addresses found
235
+ if (!addresses.length) {
236
+ return
237
+ }
238
+
239
+ try {
240
+ const peer = await this.store.load(peerId)
241
+ hasPeer = true
242
+
243
+ if (new Set([
244
+ ...addresses.map(({ multiaddr }) => multiaddr.toString()),
245
+ ...peer.addresses.map(({ multiaddr }) => multiaddr.toString())
246
+ ]).size === peer.addresses.length) {
247
+ return
248
+ }
249
+ } catch (err: any) {
250
+ if (err.code !== codes.ERR_NOT_FOUND) {
251
+ throw err
252
+ }
253
+ }
254
+
255
+ updatedPeer = await this.store.mergeOrCreate(peerId, { addresses })
256
+
257
+ log(`added multiaddrs for ${peerId.toString(base58btc)}`)
258
+ } finally {
259
+ log('set release write lock')
260
+ release()
261
+ }
262
+
263
+ this.emit(EVENT_NAME, { peerId, multiaddrs: updatedPeer.addresses.map(addr => addr.multiaddr) })
264
+
265
+ // Notify the existence of a new peer
266
+ if (!hasPeer) {
267
+ this.emit('peer', peerId)
268
+ }
269
+ }
270
+
271
+ async delete (peerId: PeerId) {
272
+ peerId = PeerId.fromPeerId(peerId)
273
+
274
+ log('delete await write lock')
275
+ const release = await this.store.lock.writeLock()
276
+ log('delete got write lock')
277
+
278
+ let has
279
+
280
+ try {
281
+ has = await this.store.has(peerId)
282
+
283
+ await this.store.patchOrCreate(peerId, {
284
+ addresses: []
285
+ })
286
+ } finally {
287
+ log('delete release write lock')
288
+ release()
289
+ }
290
+
291
+ if (has) {
292
+ this.emit(EVENT_NAME, { peerId, multiaddrs: [] })
293
+ }
294
+ }
295
+
296
+ async getMultiaddrsForPeer (peerId: PeerId, addressSorter: AddressSorter = (mas) => mas) {
297
+ const addresses = await this.get(peerId)
298
+
299
+ return addressSorter(
300
+ addresses
301
+ ).map((address) => {
302
+ const multiaddr = address.multiaddr
303
+
304
+ const idString = multiaddr.getPeerId()
305
+ if (idString && idString === peerId.toString()) return multiaddr
306
+
307
+ return multiaddr.encapsulate(`/p2p/${peerId.toString(base58btc)}`)
308
+ })
309
+ }
310
+ }
311
+
312
+ function filterMultiaddrs (peerId: PeerId, multiaddrs: Multiaddr[], addressFilter: AddressFilter, isCertified: boolean = false) {
313
+ return pipe(
314
+ multiaddrs,
315
+ (source) => each(source, (multiaddr) => {
316
+ if (!Multiaddr.isMultiaddr(multiaddr)) {
317
+ log.error('multiaddr must be an instance of Multiaddr')
318
+ throw errcode(new Error('multiaddr must be an instance of Multiaddr'), codes.ERR_INVALID_PARAMETERS)
319
+ }
320
+ }),
321
+ (source) => filter(source, (multiaddr) => addressFilter(peerId, multiaddr)),
322
+ (source) => map(source, (multiaddr) => {
323
+ return {
324
+ multiaddr: new Multiaddr(multiaddr.toString()),
325
+ isCertified
326
+ }
327
+ }),
328
+ (source) => all(source)
329
+ )
330
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,5 @@
1
+
2
+ export const codes = {
3
+ ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS',
4
+ ERR_NOT_FOUND: 'ERR_NOT_FOUND'
5
+ }
package/src/index.ts ADDED
@@ -0,0 +1,123 @@
1
+ import { logger } from '@libp2p/logger'
2
+ import { EventEmitter } from 'events'
3
+ import { PeerStoreAddressBook } from './address-book.js'
4
+ import { PeerStoreKeyBook } from './key-book.js'
5
+ import { PeerStoreMetadataBook } from './metadata-book.js'
6
+ import { PeerStoreProtoBook } from './proto-book.js'
7
+ import { PersistentStore, Store } from './store.js'
8
+ import type { PeerStore, Address, AddressBook, KeyBook, MetadataBook, ProtoBook } from '@libp2p/interfaces/peer-store'
9
+ import type { PeerId } from '@libp2p/interfaces/peer-id'
10
+ import type { Multiaddr } from '@multiformats/multiaddr'
11
+ import type { Datastore } from 'interface-datastore'
12
+ import { base58btc } from 'multiformats/bases/base58'
13
+
14
+ const log = logger('libp2p:peer-store')
15
+
16
+ export interface AddressFilter {
17
+ (peerId: PeerId, multiaddr: Multiaddr): Promise<boolean>
18
+ }
19
+
20
+ export interface AddressSorter {
21
+ (addresses: Address[]): Address[]
22
+ }
23
+
24
+ export interface PeerStoreOptions {
25
+ peerId: PeerId
26
+ datastore: Datastore
27
+ addressFilter?: AddressFilter
28
+ }
29
+
30
+ /**
31
+ * An implementation of PeerStore that stores data in a Datastore
32
+ */
33
+ export class DefaultPeerStore extends EventEmitter implements PeerStore {
34
+ public addressBook: AddressBook
35
+ public keyBook: KeyBook
36
+ public metadataBook: MetadataBook
37
+ public protoBook: ProtoBook
38
+
39
+ private peerId: PeerId
40
+ private store: Store
41
+
42
+ constructor (options: PeerStoreOptions) {
43
+ super()
44
+
45
+ const { peerId, datastore, addressFilter } = options
46
+
47
+ this.peerId = peerId
48
+ this.store = new PersistentStore(datastore)
49
+
50
+ this.addressBook = new PeerStoreAddressBook(this.emit.bind(this), this.store, addressFilter)
51
+ this.keyBook = new PeerStoreKeyBook(this.emit.bind(this), this.store)
52
+ this.metadataBook = new PeerStoreMetadataBook(this.emit.bind(this), this.store)
53
+ this.protoBook = new PeerStoreProtoBook(this.emit.bind(this), this.store)
54
+ }
55
+
56
+ async * getPeers () {
57
+ log('getPeers await read lock')
58
+ const release = await this.store.lock.readLock()
59
+ log('getPeers got read lock')
60
+
61
+ try {
62
+ for await (const peer of this.store.all()) {
63
+ if (peer.id.toString(base58btc) === this.peerId.toString(base58btc)) {
64
+ // Remove self peer if present
65
+ continue
66
+ }
67
+
68
+ yield peer
69
+ }
70
+ } finally {
71
+ log('getPeers release read lock')
72
+ release()
73
+ }
74
+ }
75
+
76
+ /**
77
+ * Delete the information of the given peer in every book
78
+ */
79
+ async delete (peerId: PeerId) {
80
+ log('delete await write lock')
81
+ const release = await this.store.lock.writeLock()
82
+ log('delete got write lock')
83
+
84
+ try {
85
+ await this.store.delete(peerId)
86
+ } finally {
87
+ log('delete release write lock')
88
+ release()
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Get the stored information of a given peer
94
+ */
95
+ async get (peerId: PeerId) {
96
+ log('get await read lock')
97
+ const release = await this.store.lock.readLock()
98
+ log('get got read lock')
99
+
100
+ try {
101
+ return this.store.load(peerId)
102
+ } finally {
103
+ log('get release read lock')
104
+ release()
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Returns true if we have a record of the peer
110
+ */
111
+ async has (peerId: PeerId) {
112
+ log('has await read lock')
113
+ const release = await this.store.lock.readLock()
114
+ log('has got read lock')
115
+
116
+ try {
117
+ return this.store.has(peerId)
118
+ } finally {
119
+ log('has release read lock')
120
+ release()
121
+ }
122
+ }
123
+ }