libp2p 0.35.8 → 0.35.9-rc.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 (80) hide show
  1. package/dist/src/circuit/auto-relay.d.ts +8 -10
  2. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  3. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  4. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  5. package/dist/src/connection-manager/index.d.ts +1 -2
  6. package/dist/src/connection-manager/index.d.ts.map +1 -1
  7. package/dist/src/content-routing/utils.d.ts +3 -3
  8. package/dist/src/content-routing/utils.d.ts.map +1 -1
  9. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  10. package/dist/src/dialer/index.d.ts +5 -5
  11. package/dist/src/dialer/index.d.ts.map +1 -1
  12. package/dist/src/identify/index.d.ts +4 -4
  13. package/dist/src/identify/index.d.ts.map +1 -1
  14. package/dist/src/index.d.ts +6 -6
  15. package/dist/src/index.d.ts.map +1 -1
  16. package/dist/src/peer-routing.d.ts +1 -1
  17. package/dist/src/peer-routing.d.ts.map +1 -1
  18. package/dist/src/peer-store/address-book.d.ts +28 -84
  19. package/dist/src/peer-store/address-book.d.ts.map +1 -1
  20. package/dist/src/peer-store/index.d.ts +25 -94
  21. package/dist/src/peer-store/index.d.ts.map +1 -1
  22. package/dist/src/peer-store/key-book.d.ts +31 -15
  23. package/dist/src/peer-store/key-book.d.ts.map +1 -1
  24. package/dist/src/peer-store/metadata-book.d.ts +33 -29
  25. package/dist/src/peer-store/metadata-book.d.ts.map +1 -1
  26. package/dist/src/peer-store/pb/peer.d.ts +222 -0
  27. package/dist/src/peer-store/proto-book.d.ts +28 -29
  28. package/dist/src/peer-store/proto-book.d.ts.map +1 -1
  29. package/dist/src/peer-store/store.d.ts +46 -0
  30. package/dist/src/peer-store/store.d.ts.map +1 -0
  31. package/dist/src/peer-store/types.d.ts +199 -0
  32. package/dist/src/peer-store/types.d.ts.map +1 -0
  33. package/dist/src/ping/index.d.ts.map +1 -1
  34. package/dist/src/record/peer-record/index.d.ts +2 -2
  35. package/dist/src/record/peer-record/index.d.ts.map +1 -1
  36. package/dist/src/registrar.d.ts +5 -5
  37. package/dist/src/registrar.d.ts.map +1 -1
  38. package/dist/src/upgrader.d.ts.map +1 -1
  39. package/package.json +11 -16
  40. package/src/circuit/auto-relay.js +19 -19
  41. package/src/connection-manager/auto-dialler.js +6 -3
  42. package/src/connection-manager/index.js +29 -20
  43. package/src/content-routing/utils.js +4 -4
  44. package/src/dialer/dial-request.js +4 -1
  45. package/src/dialer/index.js +9 -6
  46. package/src/identify/index.js +28 -23
  47. package/src/index.js +33 -27
  48. package/src/peer-routing.js +4 -1
  49. package/src/peer-store/address-book.js +238 -227
  50. package/src/peer-store/index.js +77 -110
  51. package/src/peer-store/key-book.js +99 -54
  52. package/src/peer-store/metadata-book.js +171 -101
  53. package/src/peer-store/pb/peer.d.ts +222 -0
  54. package/src/peer-store/pb/peer.js +643 -0
  55. package/src/peer-store/pb/peer.proto +31 -0
  56. package/src/peer-store/proto-book.js +171 -105
  57. package/src/peer-store/store.js +250 -0
  58. package/src/peer-store/types.ts +245 -0
  59. package/src/ping/index.js +6 -0
  60. package/src/record/peer-record/index.js +1 -1
  61. package/src/record/utils.js +1 -1
  62. package/src/registrar.js +4 -4
  63. package/src/upgrader.js +3 -1
  64. package/dist/src/peer-store/book.d.ts +0 -81
  65. package/dist/src/peer-store/book.d.ts.map +0 -1
  66. package/dist/src/peer-store/persistent/consts.d.ts +0 -6
  67. package/dist/src/peer-store/persistent/consts.d.ts.map +0 -1
  68. package/dist/src/peer-store/persistent/index.d.ts +0 -129
  69. package/dist/src/peer-store/persistent/index.d.ts.map +0 -1
  70. package/dist/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  71. package/dist/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  72. package/src/peer-store/book.js +0 -124
  73. package/src/peer-store/persistent/consts.js +0 -15
  74. package/src/peer-store/persistent/index.js +0 -408
  75. package/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  76. package/src/peer-store/persistent/pb/address-book.js +0 -522
  77. package/src/peer-store/persistent/pb/address-book.proto +0 -27
  78. package/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  79. package/src/peer-store/persistent/pb/proto-book.js +0 -157
  80. package/src/peer-store/persistent/pb/proto-book.proto +0 -5
@@ -1,74 +1,36 @@
1
1
  'use strict'
2
2
 
3
3
  const debug = require('debug')
4
- const log = Object.assign(debug('libp2p:peer-store:address-book'), {
5
- error: debug('libp2p:peer-store:address-book:err')
6
- })
7
4
  const errcode = require('err-code')
8
-
9
5
  const { Multiaddr } = require('multiaddr')
10
6
  const PeerId = require('peer-id')
11
-
12
- const Book = require('./book')
7
+ const { codes } = require('../errors')
13
8
  const PeerRecord = require('../record/peer-record')
14
-
15
- const {
16
- codes: { ERR_INVALID_PARAMETERS }
17
- } = require('../errors')
18
9
  const Envelope = require('../record/envelope')
19
10
 
20
11
  /**
21
- * @typedef {import('./')} PeerStore
12
+ * @typedef {import('./types').PeerStore} PeerStore
13
+ * @typedef {import('./types').Address} Address
14
+ * @typedef {import('./types').AddressBook} AddressBook
22
15
  */
23
16
 
24
- /**
25
- * @typedef {Object} Address
26
- * @property {Multiaddr} multiaddr peer multiaddr.
27
- * @property {boolean} isCertified obtained from a signed peer record.
28
- *
29
- * @typedef {Object} CertifiedRecord
30
- * @property {Uint8Array} raw raw envelope.
31
- * @property {number} seqNumber seq counter.
32
- *
33
- * @typedef {Object} Entry
34
- * @property {Address[]} addresses peer Addresses.
35
- * @property {CertifiedRecord} record certified peer record.
36
- */
17
+ const log = Object.assign(debug('libp2p:peer-store:address-book'), {
18
+ error: debug('libp2p:peer-store:address-book:err')
19
+ })
20
+
21
+ const EVENT_NAME = 'change:multiaddrs'
37
22
 
38
23
  /**
39
- * @extends {Book}
24
+ * @implements {AddressBook}
40
25
  */
41
- class AddressBook extends Book {
26
+ class PeerStoreAddressBook {
42
27
  /**
43
- * The AddressBook is responsible for keeping the known multiaddrs of a peer.
44
- *
45
- * @class
46
- * @param {PeerStore} peerStore
28
+ * @param {PeerStore["emit"]} emit
29
+ * @param {import('./types').Store} store
47
30
  */
48
- constructor (peerStore) {
49
- /**
50
- * PeerStore Event emitter, used by the AddressBook to emit:
51
- * "peer" - emitted when a peer is discovered by the node.
52
- * "change:multiaddrs" - emitted when the known multiaddrs of a peer change.
53
- */
54
- super({
55
- peerStore,
56
- eventName: 'change:multiaddrs',
57
- eventProperty: 'multiaddrs',
58
- eventTransformer: (data) => {
59
- if (!data.addresses) {
60
- return []
61
- }
62
- return data.addresses.map((/** @type {Address} */ address) => address.multiaddr)
63
- }
64
- })
65
-
66
- /**
67
- * Map known peers to their known Address Entries.
68
- *
69
- * @type {Map<string, Entry>}
70
- */
71
- this.data = new Map()
31
+ constructor (emit, store) {
32
+ this._emit = emit
33
+ this._store = store
72
34
  }
73
35
 
74
36
  /**
@@ -77,69 +39,86 @@ class AddressBook extends Book {
77
39
  * into the AddressBook.
78
40
  *
79
41
  * @param {Envelope} envelope
80
- * @returns {boolean}
81
42
  */
82
- consumePeerRecord (envelope) {
83
- let peerRecord
43
+ async consumePeerRecord (envelope) {
44
+ log('consumePeerRecord await write lock')
45
+ const release = await this._store.lock.writeLock()
46
+ log('consumePeerRecord got write lock')
47
+
48
+ let peerId
49
+ let updatedPeer
50
+
84
51
  try {
85
- peerRecord = PeerRecord.createFromProtobuf(envelope.payload)
86
- } catch (/** @type {any} */ err) {
87
- log.error('invalid peer record received')
88
- return false
89
- }
52
+ let peerRecord
53
+ try {
54
+ peerRecord = PeerRecord.createFromProtobuf(envelope.payload)
55
+ } catch (/** @type {any} */ err) {
56
+ log.error('invalid peer record received')
57
+ return false
58
+ }
90
59
 
91
- // Verify peerId
92
- if (!peerRecord.peerId.equals(envelope.peerId)) {
93
- log('signing key does not match PeerId in the PeerRecord')
94
- return false
95
- }
60
+ peerId = peerRecord.peerId
61
+ const multiaddrs = peerRecord.multiaddrs
96
62
 
97
- // ensure the record has multiaddrs
98
- if (!peerRecord.multiaddrs || !peerRecord.multiaddrs.length) {
99
- return false
100
- }
63
+ // Verify peerId
64
+ if (!peerId.equals(envelope.peerId)) {
65
+ log('signing key does not match PeerId in the PeerRecord')
66
+ return false
67
+ }
101
68
 
102
- const peerId = peerRecord.peerId
103
- const id = peerId.toB58String()
104
- const entry = this.data.get(id) || { record: undefined }
105
- const storedRecord = entry.record
69
+ // ensure the record has multiaddrs
70
+ if (!multiaddrs || !multiaddrs.length) {
71
+ return false
72
+ }
106
73
 
107
- // ensure seq is greater than, or equal to, the last received
108
- if (storedRecord && storedRecord.seqNumber >= peerRecord.seqNumber) {
109
- return false
110
- }
74
+ if (await this._store.has(peerId)) {
75
+ const peer = await this._store.load(peerId)
111
76
 
112
- const addresses = this._toAddresses(peerRecord.multiaddrs, true)
77
+ if (peer.peerRecordEnvelope) {
78
+ const storedEnvelope = await Envelope.createFromProtobuf(peer.peerRecordEnvelope)
79
+ const storedRecord = PeerRecord.createFromProtobuf(storedEnvelope.payload)
113
80
 
114
- // Replace unsigned addresses by the new ones from the record
115
- // TODO: Once we have ttls for the addresses, we should merge these in.
116
- this._setData(peerId, {
117
- addresses,
118
- record: {
119
- raw: envelope.marshal(),
120
- seqNumber: peerRecord.seqNumber
81
+ // ensure seq is greater than, or equal to, the last received
82
+ if (storedRecord.seqNumber >= peerRecord.seqNumber) {
83
+ return false
84
+ }
85
+ }
121
86
  }
122
- })
123
- log(`stored provided peer record for ${id}`)
87
+
88
+ // Replace unsigned addresses by the new ones from the record
89
+ // TODO: Once we have ttls for the addresses, we should merge these in
90
+ updatedPeer = await this._store.patchOrCreate(peerId, {
91
+ addresses: convertMultiaddrsToAddresses(multiaddrs, true),
92
+ peerRecordEnvelope: envelope.marshal()
93
+ })
94
+
95
+ log(`stored provided peer record for ${peerRecord.peerId.toB58String()}`)
96
+ } finally {
97
+ log('consumePeerRecord release write lock')
98
+ release()
99
+ }
100
+
101
+ this._emit(EVENT_NAME, { peerId, multiaddrs: updatedPeer.addresses.map(({ multiaddr }) => multiaddr) })
124
102
 
125
103
  return true
126
104
  }
127
105
 
128
106
  /**
129
- * Get the raw Envelope for a peer. Returns
130
- * undefined if no Envelope is found.
131
- *
132
107
  * @param {PeerId} peerId
133
- * @returns {Uint8Array|undefined}
134
108
  */
135
- getRawEnvelope (peerId) {
136
- const entry = this.data.get(peerId.toB58String())
109
+ async getRawEnvelope (peerId) {
110
+ log('getRawEnvelope await read lock')
111
+ const release = await this._store.lock.readLock()
112
+ log('getRawEnvelope got read lock')
137
113
 
138
- if (!entry || !entry.record || !entry.record.raw) {
139
- return undefined
140
- }
114
+ try {
115
+ const peer = await this._store.load(peerId)
141
116
 
142
- return entry.record.raw
117
+ return peer.peerRecordEnvelope
118
+ } finally {
119
+ log('getRawEnvelope release read lock')
120
+ release()
121
+ }
143
122
  }
144
123
 
145
124
  /**
@@ -147,10 +126,9 @@ class AddressBook extends Book {
147
126
  * Returns undefined if no record exists.
148
127
  *
149
128
  * @param {PeerId} peerId
150
- * @returns {Promise<Envelope|void>|undefined}
151
129
  */
152
- getPeerRecord (peerId) {
153
- const raw = this.getRawEnvelope(peerId)
130
+ async getPeerRecord (peerId) {
131
+ const raw = await this.getRawEnvelope(peerId)
154
132
 
155
133
  if (!raw) {
156
134
  return undefined
@@ -160,186 +138,189 @@ class AddressBook extends Book {
160
138
  }
161
139
 
162
140
  /**
163
- * Set known multiaddrs of a provided peer.
164
- * This will replace previously stored multiaddrs, if available.
165
- * Replacing stored multiaddrs might result in losing obtained certified addresses.
166
- * If you are not sure, it's recommended to use `add` instead.
167
- *
168
- * @override
169
141
  * @param {PeerId} peerId
170
- * @param {Multiaddr[]} multiaddrs
171
- * @returns {AddressBook}
172
142
  */
173
- set (peerId, multiaddrs) {
143
+ async get (peerId) {
174
144
  if (!PeerId.isPeerId(peerId)) {
175
145
  log.error('peerId must be an instance of peer-id to store data')
176
- throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
146
+ throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
177
147
  }
178
148
 
179
- const addresses = this._toAddresses(multiaddrs)
149
+ log('get wait for read lock')
150
+ const release = await this._store.lock.readLock()
151
+ log('get got read lock')
180
152
 
181
- // Not replace multiaddrs
182
- if (!addresses.length) {
183
- return this
184
- }
185
-
186
- const id = peerId.toB58String()
187
- const entry = this.data.get(id)
188
-
189
- // Already knows the peer
190
- if (entry && entry.addresses && entry.addresses.length === addresses.length) {
191
- const intersection = entry.addresses.filter((addr) => addresses.some((newAddr) => addr.multiaddr.equals(newAddr.multiaddr)))
153
+ try {
154
+ const peer = await this._store.load(peerId)
192
155
 
193
- // Are new addresses equal to the old ones?
194
- // If yes, no changes needed!
195
- if (intersection.length === entry.addresses.length) {
196
- log(`the addresses provided to store are equal to the already stored for ${id}`)
197
- return this
156
+ return peer.addresses
157
+ } catch (/** @type {any} */ err) {
158
+ if (err.code !== codes.ERR_NOT_FOUND) {
159
+ throw err
198
160
  }
161
+ } finally {
162
+ log('get release read lock')
163
+ release()
199
164
  }
200
165
 
201
- this._setData(peerId, {
202
- addresses,
203
- record: entry && entry.record
204
- })
205
- log(`stored provided multiaddrs for ${id}`)
206
-
207
- // Notify the existance of a new peer
208
- if (!entry) {
209
- this._ps.emit('peer', peerId)
210
- }
211
-
212
- return this
166
+ return []
213
167
  }
214
168
 
215
169
  /**
216
- * Add known addresses of a provided peer.
217
- * If the peer is not known, it is set with the given addresses.
218
- *
219
170
  * @param {PeerId} peerId
220
171
  * @param {Multiaddr[]} multiaddrs
221
- * @returns {AddressBook}
222
172
  */
223
- add (peerId, multiaddrs) {
173
+ async set (peerId, multiaddrs) {
224
174
  if (!PeerId.isPeerId(peerId)) {
225
175
  log.error('peerId must be an instance of peer-id to store data')
226
- throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
176
+ throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
227
177
  }
228
178
 
229
- const addresses = this._toAddresses(multiaddrs)
230
- const id = peerId.toB58String()
179
+ log('set await write lock')
180
+ const release = await this._store.lock.writeLock()
181
+ log('set got write lock')
231
182
 
232
- // No addresses to be added
233
- if (!addresses.length) {
234
- return this
235
- }
183
+ let hasPeer = false
184
+ let updatedPeer
236
185
 
237
- const entry = this.data.get(id)
238
-
239
- if (entry && entry.addresses) {
240
- // Add recorded uniquely to the new array (Union)
241
- entry.addresses.forEach((addr) => {
242
- if (!addresses.find(r => r.multiaddr.equals(addr.multiaddr))) {
243
- addresses.push(addr)
244
- }
245
- })
186
+ try {
187
+ const addresses = convertMultiaddrsToAddresses(multiaddrs)
246
188
 
247
- // If the recorded length is equal to the new after the unique union
248
- // The content is the same, no need to update.
249
- if (entry.addresses.length === addresses.length) {
250
- log(`the addresses provided to store are already stored for ${id}`)
251
- return this
189
+ // No valid addresses found
190
+ if (!addresses.length) {
191
+ return
252
192
  }
253
- }
254
193
 
255
- this._setData(peerId, {
256
- addresses,
257
- record: entry && entry.record
258
- })
194
+ try {
195
+ const peer = await this._store.load(peerId)
196
+ hasPeer = true
197
+
198
+ if (new Set([
199
+ ...addresses.map(({ multiaddr }) => multiaddr.toString()),
200
+ ...peer.addresses.map(({ multiaddr }) => multiaddr.toString())
201
+ ]).size === peer.addresses.length && addresses.length === peer.addresses.length) {
202
+ // not changing anything, no need to update
203
+ return
204
+ }
205
+ } catch (/** @type {any} */ err) {
206
+ if (err.code !== codes.ERR_NOT_FOUND) {
207
+ throw err
208
+ }
209
+ }
259
210
 
260
- log(`added provided multiaddrs for ${id}`)
211
+ updatedPeer = await this._store.patchOrCreate(peerId, { addresses })
261
212
 
262
- // Notify the existance of a new peer
263
- if (!(entry && entry.addresses)) {
264
- this._ps.emit('peer', peerId)
213
+ log(`set multiaddrs for ${peerId.toB58String()}`)
214
+ } finally {
215
+ log('set release write lock')
216
+ release()
265
217
  }
266
218
 
267
- return this
219
+ this._emit(EVENT_NAME, { peerId, multiaddrs: updatedPeer.addresses.map(addr => addr.multiaddr) })
220
+
221
+ // Notify the existence of a new peer
222
+ if (!hasPeer) {
223
+ this._emit('peer', peerId)
224
+ }
268
225
  }
269
226
 
270
227
  /**
271
- * Get the known data of a provided peer.
272
- *
273
- * @override
274
228
  * @param {PeerId} peerId
275
- * @returns {Address[]|undefined}
229
+ * @param {Multiaddr[]} multiaddrs
276
230
  */
277
- get (peerId) {
231
+ async add (peerId, multiaddrs) {
278
232
  if (!PeerId.isPeerId(peerId)) {
279
- throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
233
+ log.error('peerId must be an instance of peer-id to store data')
234
+ throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
280
235
  }
281
236
 
282
- const entry = this.data.get(peerId.toB58String())
237
+ log('add await write lock')
238
+ const release = await this._store.lock.writeLock()
239
+ log('add got write lock')
283
240
 
284
- return entry && entry.addresses ? [...entry.addresses] : undefined
285
- }
241
+ let hasPeer
242
+ let updatedPeer
286
243
 
287
- /**
288
- * Transforms received multiaddrs into Address.
289
- *
290
- * @private
291
- * @param {Multiaddr[]} multiaddrs
292
- * @param {boolean} [isCertified]
293
- * @returns {Address[]}
294
- */
295
- _toAddresses (multiaddrs, isCertified = false) {
296
- if (!multiaddrs) {
297
- log.error('multiaddrs must be provided to store data')
298
- throw errcode(new Error('multiaddrs must be provided'), ERR_INVALID_PARAMETERS)
299
- }
244
+ try {
245
+ const addresses = convertMultiaddrsToAddresses(multiaddrs)
300
246
 
301
- // create Address for each address
302
- /** @type {Address[]} */
303
- const addresses = []
304
- multiaddrs.forEach((addr) => {
305
- if (!Multiaddr.isMultiaddr(addr)) {
306
- log.error(`multiaddr ${addr} must be an instance of multiaddr`)
307
- throw errcode(new Error(`multiaddr ${addr} must be an instance of multiaddr`), ERR_INVALID_PARAMETERS)
247
+ // No valid addresses found
248
+ if (!addresses.length) {
249
+ return
308
250
  }
309
251
 
310
- // Guarantee no replicates
311
- if (!addresses.find((a) => a.multiaddr.equals(addr))) {
312
- addresses.push({
313
- multiaddr: addr,
314
- isCertified
315
- })
252
+ try {
253
+ const peer = await this._store.load(peerId)
254
+ hasPeer = true
255
+
256
+ if (new Set([
257
+ ...addresses.map(({ multiaddr }) => multiaddr.toString()),
258
+ ...peer.addresses.map(({ multiaddr }) => multiaddr.toString())
259
+ ]).size === peer.addresses.length) {
260
+ return
261
+ }
262
+ } catch (/** @type {any} */ err) {
263
+ if (err.code !== codes.ERR_NOT_FOUND) {
264
+ throw err
265
+ }
316
266
  }
317
- })
318
267
 
319
- return addresses
268
+ updatedPeer = await this._store.mergeOrCreate(peerId, { addresses })
269
+
270
+ log(`added multiaddrs for ${peerId}`)
271
+ } finally {
272
+ log('set release write lock')
273
+ release()
274
+ }
275
+
276
+ this._emit(EVENT_NAME, { peerId, multiaddrs: updatedPeer.addresses.map(addr => addr.multiaddr) })
277
+
278
+ // Notify the existence of a new peer
279
+ if (!hasPeer) {
280
+ this._emit('peer', peerId)
281
+ }
320
282
  }
321
283
 
322
284
  /**
323
- * Get the known multiaddrs for a given peer. All returned multiaddrs
324
- * will include the encapsulated `PeerId` of the peer.
325
- * Returns `undefined` if there are no known multiaddrs for the given peer.
326
- *
327
285
  * @param {PeerId} peerId
328
- * @param {(addresses: Address[]) => Address[]} [addressSorter]
329
- * @returns {Multiaddr[]|undefined}
330
286
  */
331
- getMultiaddrsForPeer (peerId, addressSorter = (ms) => ms) {
287
+ async delete (peerId) {
332
288
  if (!PeerId.isPeerId(peerId)) {
333
- throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
289
+ log.error('peerId must be an instance of peer-id to store data')
290
+ throw errcode(new Error('peerId must be an instance of peer-id'), codes.ERR_INVALID_PARAMETERS)
334
291
  }
335
292
 
336
- const entry = this.data.get(peerId.toB58String())
337
- if (!entry || !entry.addresses) {
338
- return undefined
293
+ log('delete await write lock')
294
+ const release = await this._store.lock.writeLock()
295
+ log('delete got write lock')
296
+
297
+ let has
298
+
299
+ try {
300
+ has = await this._store.has(peerId)
301
+
302
+ await this._store.patchOrCreate(peerId, {
303
+ addresses: []
304
+ })
305
+ } finally {
306
+ log('delete release write lock')
307
+ release()
308
+ }
309
+
310
+ if (has) {
311
+ this._emit(EVENT_NAME, { peerId, multiaddrs: [] })
339
312
  }
313
+ }
314
+
315
+ /**
316
+ * @param {PeerId} peerId
317
+ * @param {(addresses: Address[]) => Address[]} [addressSorter]
318
+ */
319
+ async getMultiaddrsForPeer (peerId, addressSorter = (ms) => ms) {
320
+ const addresses = await this.get(peerId)
340
321
 
341
322
  return addressSorter(
342
- entry.addresses || []
323
+ addresses
343
324
  ).map((address) => {
344
325
  const multiaddr = address.multiaddr
345
326
 
@@ -351,4 +332,34 @@ class AddressBook extends Book {
351
332
  }
352
333
  }
353
334
 
354
- module.exports = AddressBook
335
+ /**
336
+ * Transforms received multiaddrs into Address.
337
+ *
338
+ * @private
339
+ * @param {Multiaddr[]} multiaddrs
340
+ * @param {boolean} [isCertified]
341
+ * @returns {Address[]}
342
+ */
343
+ function convertMultiaddrsToAddresses (multiaddrs, isCertified = false) {
344
+ if (!multiaddrs) {
345
+ log.error('multiaddrs must be provided to store data')
346
+ throw errcode(new Error('multiaddrs must be provided'), codes.ERR_INVALID_PARAMETERS)
347
+ }
348
+
349
+ // create Address for each address with no duplicates
350
+ return Array.from(
351
+ new Set(multiaddrs.map(ma => ma.toString()))
352
+ )
353
+ .map(addr => {
354
+ try {
355
+ return {
356
+ multiaddr: new Multiaddr(addr),
357
+ isCertified
358
+ }
359
+ } catch (err) {
360
+ throw errcode(err, codes.ERR_INVALID_PARAMETERS)
361
+ }
362
+ })
363
+ }
364
+
365
+ module.exports = PeerStoreAddressBook