@helia/bitswap 0.0.0-9c8a2c0 → 0.0.0-9ea934e

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.
package/src/want-list.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { TypedEventEmitter, setMaxListeners } from '@libp2p/interface'
2
- import { trackedPeerMap, PeerSet } from '@libp2p/peer-collections'
2
+ import { trackedPeerMap } from '@libp2p/peer-collections'
3
3
  import { trackedMap } from '@libp2p/utils/tracked-map'
4
4
  import all from 'it-all'
5
5
  import filter from 'it-filter'
@@ -60,19 +60,9 @@ export interface WantListEntry {
60
60
  * Whether the remote should tell us if they have the block or not
61
61
  */
62
62
  sendDontHave: boolean
63
-
64
- /**
65
- * If this set has members, the want will only be sent to these peers
66
- */
67
- session: PeerSet
68
63
  }
69
64
 
70
65
  export interface WantOptions extends AbortOptions, ProgressOptions<BitswapNetworkWantProgressEvents> {
71
- /**
72
- * If set, this WantList entry will only be sent to this peer
73
- */
74
- peerId?: PeerId
75
-
76
66
  /**
77
67
  * Allow prioritising blocks
78
68
  */
@@ -123,11 +113,11 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
123
113
 
124
114
  setMaxListeners(Infinity, this)
125
115
  this.peers = trackedPeerMap({
126
- name: 'ipfs_bitswap_peers',
116
+ name: 'helia_bitswap_peers',
127
117
  metrics: components.libp2p.metrics
128
118
  })
129
119
  this.wants = trackedMap({
130
- name: 'ipfs_bitswap_wantlist',
120
+ name: 'helia_bitswap_wantlist',
131
121
  metrics: components.libp2p.metrics
132
122
  })
133
123
  this.network = components.network
@@ -161,17 +151,12 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
161
151
  if (entry == null) {
162
152
  entry = {
163
153
  cid,
164
- session: new PeerSet(),
165
154
  priority: options.priority ?? 1,
166
155
  wantType: options.wantType ?? WantType.WantBlock,
167
156
  cancel: false,
168
157
  sendDontHave: true
169
158
  }
170
159
 
171
- if (options.peerId != null) {
172
- entry.session.add(options.peerId)
173
- }
174
-
175
160
  this.wants.set(cidStr, entry)
176
161
  }
177
162
 
@@ -181,22 +166,6 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
181
166
  entry.wantType = WantType.WantBlock
182
167
  }
183
168
 
184
- // if this want was part of a session..
185
- if (entry.session.size > 0) {
186
- // if the new want is also part of a session, expand the want session to
187
- // include both sets of peers
188
- if (options.peerId != null) {
189
- entry.session.add(options.peerId)
190
- }
191
-
192
- // if the new want is not part of a session, make this want a non-session
193
- // want - nb. this will cause this WantList entry to be sent to every peer
194
- // instead of just the ones in the session
195
- if (options.peerId == null) {
196
- entry.session.clear()
197
- }
198
- }
199
-
200
169
  // broadcast changes
201
170
  await this.sendMessagesDebounced()
202
171
 
@@ -255,11 +224,6 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
255
224
  entries: pipe(
256
225
  this.wants.entries(),
257
226
  (source) => filter(source, ([key, entry]) => {
258
- // skip session-only wants
259
- if (entry.session.size > 0 && !entry.session.has(peerId)) {
260
- return false
261
- }
262
-
263
227
  const sentPreviously = sentWants.has(key)
264
228
 
265
229
  // don't cancel if we've not sent it to them before
@@ -329,37 +293,28 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
329
293
  /**
330
294
  * Add a CID to the wantlist
331
295
  */
332
- async wantPresence (cid: CID, options: WantOptions = {}): Promise<WantPresenceResult> {
333
- if (options.peerId != null) {
334
- const peer = options.peerId
335
-
336
- // sending WantHave directly to peer
337
- await this.network.sendMessage(options.peerId, {
338
- wantlist: {
339
- full: false,
340
- entries: [{
341
- cid: cid.bytes,
342
- sendDontHave: true,
343
- wantType: WantType.WantHave,
344
- priority: 1
345
- }]
346
- }
347
- })
348
-
349
- // wait for peer response
350
- const event = await raceEvent<CustomEvent<WantHaveResult | WantDontHaveResult>>(this, 'presence', options.signal, {
351
- filter: (event) => {
352
- return peer.equals(event.detail.sender) && uint8ArrayEquals(cid.multihash.digest, event.detail.cid.multihash.digest)
353
- }
354
- })
355
-
356
- return event.detail
357
- }
296
+ async wantSessionPresence (cid: CID, peerId: PeerId, options: WantOptions = {}): Promise<WantPresenceResult> {
297
+ // sending WantHave directly to peer
298
+ await this.network.sendMessage(peerId, {
299
+ wantlist: {
300
+ full: false,
301
+ entries: [{
302
+ cid: cid.bytes,
303
+ sendDontHave: true,
304
+ wantType: WantType.WantHave,
305
+ priority: 1
306
+ }]
307
+ }
308
+ })
358
309
 
359
- return this.addEntry(cid, {
360
- ...options,
361
- wantType: WantType.WantHave
310
+ // wait for peer response
311
+ const event = await raceEvent<CustomEvent<WantHaveResult | WantDontHaveResult>>(this, 'presence', options.signal, {
312
+ filter: (event) => {
313
+ return peerId.equals(event.detail.sender) && uint8ArrayEquals(cid.multihash.digest, event.detail.cid.multihash.digest)
314
+ }
362
315
  })
316
+
317
+ return event.detail
363
318
  }
364
319
 
365
320
  /**
@@ -372,6 +327,33 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
372
327
  })
373
328
  }
374
329
 
330
+ /**
331
+ * Add a CID to the wantlist
332
+ */
333
+ async wantSessionBlock (cid: CID, peerId: PeerId, options: WantOptions = {}): Promise<WantPresenceResult> {
334
+ // sending WantBlockResult directly to peer
335
+ await this.network.sendMessage(peerId, {
336
+ wantlist: {
337
+ full: false,
338
+ entries: [{
339
+ cid: cid.bytes,
340
+ sendDontHave: true,
341
+ wantType: WantType.WantBlock,
342
+ priority: 1
343
+ }]
344
+ }
345
+ })
346
+
347
+ // wait for peer response
348
+ const event = await raceEvent<CustomEvent<WantPresenceResult>>(this, 'presence', options.signal, {
349
+ filter: (event) => {
350
+ return peerId.equals(event.detail.sender) && uint8ArrayEquals(cid.multihash.digest, event.detail.cid.multihash.digest)
351
+ }
352
+ })
353
+
354
+ return event.detail
355
+ }
356
+
375
357
  /**
376
358
  * Invoked when a block has been received from an external source
377
359
  */
@@ -483,7 +465,6 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
483
465
  full: true,
484
466
  entries: pipe(
485
467
  this.wants.entries(),
486
- (source) => filter(source, ([key, entry]) => !entry.cancel && (entry.session.size > 0 && !entry.session.has(peerId))),
487
468
  (source) => filter(source, ([key, entry]) => !entry.cancel),
488
469
  (source) => map(source, ([key, entry]) => {
489
470
  sentWants.add(key)
@@ -531,5 +512,6 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
531
512
 
532
513
  stop (): void {
533
514
  this.peers.clear()
515
+ clearTimeout(this.sendMessagesTimeout)
534
516
  }
535
517
  }