@helia/bitswap 3.1.2 → 3.1.3-83e1f40d

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 (45) hide show
  1. package/dist/index.min.js +1 -1
  2. package/dist/index.min.js.map +3 -3
  3. package/dist/src/bitswap.d.ts +6 -6
  4. package/dist/src/bitswap.d.ts.map +1 -1
  5. package/dist/src/bitswap.js +5 -5
  6. package/dist/src/constants.d.ts +2 -0
  7. package/dist/src/constants.d.ts.map +1 -1
  8. package/dist/src/constants.js +2 -0
  9. package/dist/src/constants.js.map +1 -1
  10. package/dist/src/index.d.ts +20 -3
  11. package/dist/src/index.d.ts.map +1 -1
  12. package/dist/src/index.js +1 -1
  13. package/dist/src/index.js.map +1 -1
  14. package/dist/src/network.d.ts +5 -5
  15. package/dist/src/network.js +4 -4
  16. package/dist/src/peer-want-lists/index.d.ts +9 -5
  17. package/dist/src/peer-want-lists/index.d.ts.map +1 -1
  18. package/dist/src/peer-want-lists/index.js +16 -36
  19. package/dist/src/peer-want-lists/index.js.map +1 -1
  20. package/dist/src/peer-want-lists/ledger.d.ts +39 -4
  21. package/dist/src/peer-want-lists/ledger.d.ts.map +1 -1
  22. package/dist/src/peer-want-lists/ledger.js +146 -10
  23. package/dist/src/peer-want-lists/ledger.js.map +1 -1
  24. package/dist/src/session.d.ts +3 -3
  25. package/dist/src/utils/bitswap-message.d.ts +1 -1
  26. package/dist/src/utils/cid-prefix.js +1 -1
  27. package/dist/src/utils/merge-messages.d.ts +1 -1
  28. package/dist/src/utils/split-message.d.ts +1 -1
  29. package/dist/src/utils/split-message.js +2 -2
  30. package/dist/src/want-list.d.ts +3 -3
  31. package/dist/src/want-list.js +4 -4
  32. package/package.json +3 -3
  33. package/src/bitswap.ts +7 -7
  34. package/src/constants.ts +2 -0
  35. package/src/index.ts +25 -4
  36. package/src/network.ts +8 -8
  37. package/src/peer-want-lists/index.ts +23 -40
  38. package/src/peer-want-lists/ledger.ts +210 -14
  39. package/src/session.ts +3 -3
  40. package/src/utils/bitswap-message.ts +1 -1
  41. package/src/utils/cid-prefix.ts +1 -1
  42. package/src/utils/merge-messages.ts +1 -1
  43. package/src/utils/split-message.ts +3 -3
  44. package/src/want-list.ts +7 -7
  45. package/dist/typedoc-urls.json +0 -26
@@ -1,8 +1,10 @@
1
1
  import toBuffer from 'it-to-buffer';
2
- import { DEFAULT_MAX_SIZE_REPLACE_HAS_WITH_BLOCK } from '../constants.js';
3
- import { BlockPresenceType, WantType } from '../pb/message.js';
4
- import { QueuedBitswapMessage } from '../utils/bitswap-message.js';
5
- import { cidToPrefix } from '../utils/cid-prefix.js';
2
+ import { CID } from 'multiformats/cid';
3
+ import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
4
+ import { DEFAULT_MAX_SIZE_REPLACE_HAS_WITH_BLOCK, DEFAULT_DO_NOT_RESEND_BLOCK_WINDOW, DEFAULT_MAX_WANTLIST_SIZE } from "../constants.js";
5
+ import { BlockPresenceType, WantType } from "../pb/message.js";
6
+ import { QueuedBitswapMessage } from "../utils/bitswap-message.js";
7
+ import { cidToPrefix } from "../utils/cid-prefix.js";
6
8
  export class Ledger {
7
9
  peerId;
8
10
  blockstore;
@@ -14,6 +16,8 @@ export class Ledger {
14
16
  lastExchange;
15
17
  maxSizeReplaceHasWithBlock;
16
18
  log;
19
+ doNotResendBlockWindow;
20
+ maxWantListSize;
17
21
  constructor(components, init) {
18
22
  this.peerId = components.peerId;
19
23
  this.blockstore = components.blockstore;
@@ -24,6 +28,8 @@ export class Ledger {
24
28
  this.bytesSent = 0;
25
29
  this.bytesReceived = 0;
26
30
  this.maxSizeReplaceHasWithBlock = init.maxSizeReplaceHasWithBlock ?? DEFAULT_MAX_SIZE_REPLACE_HAS_WITH_BLOCK;
31
+ this.doNotResendBlockWindow = init.doNotResendBlockWindow ?? DEFAULT_DO_NOT_RESEND_BLOCK_WINDOW;
32
+ this.maxWantListSize = init.maxWantListSize ?? DEFAULT_MAX_WANTLIST_SIZE;
27
33
  }
28
34
  sentBytes(n) {
29
35
  this.exchangeCount++;
@@ -38,12 +44,141 @@ export class Ledger {
38
44
  debtRatio() {
39
45
  return (this.bytesSent / (this.bytesReceived + 1)); // +1 is to prevent division by zero
40
46
  }
47
+ removeExpiredWants() {
48
+ // remove any expired wants
49
+ this.wants.forEach((value, key) => {
50
+ if (value.expires != null && value.expires < Date.now()) {
51
+ this.wants.delete(key);
52
+ }
53
+ });
54
+ }
55
+ addWants(wantlist) {
56
+ if (wantlist == null) {
57
+ return;
58
+ }
59
+ // if the message has a full wantlist, remove all entries not currently
60
+ // being sent to the peer
61
+ if (wantlist.full === true) {
62
+ this.wants.forEach((value, key) => {
63
+ if (value.status === 'want') {
64
+ this.wants.delete(key);
65
+ }
66
+ });
67
+ }
68
+ // clear cancelled wants and add new wants to the ledger
69
+ for (const entry of wantlist.entries) {
70
+ const cid = CID.decode(entry.cid);
71
+ const cidStr = uint8ArrayToString(cid.multihash.bytes, 'base64');
72
+ if (entry.cancel === true) {
73
+ this.log('peer %p cancelled want of block for %c', this.peerId, cid);
74
+ this.wants.delete(cidStr);
75
+ }
76
+ else {
77
+ if (entry.wantType === WantType.WantHave) {
78
+ this.log('peer %p wanted block presence for %c', this.peerId, cid);
79
+ }
80
+ else {
81
+ this.log('peer %p wanted block for %c', this.peerId, cid);
82
+ }
83
+ const existingWant = this.wants.get(cidStr);
84
+ // we are already tracking a want for this CID, just update the fields
85
+ if (existingWant != null) {
86
+ const sentOrSending = existingWant.status === 'sent' || existingWant.status === 'sending';
87
+ const wantTypeUpgrade = existingWant.wantType === WantType.WantHave && (entry.wantType == null || entry.wantType === WantType.WantBlock);
88
+ // allow upgrade from WantHave to WantBlock if we've previously
89
+ // sent or are sending a WantHave
90
+ if (sentOrSending && wantTypeUpgrade) {
91
+ existingWant.status = 'want';
92
+ }
93
+ existingWant.priority = entry.priority;
94
+ existingWant.wantType = entry.wantType ?? WantType.WantBlock;
95
+ existingWant.sendDontHave = entry.sendDontHave ?? false;
96
+ continue;
97
+ }
98
+ // add a new want
99
+ this.wants.set(cidStr, {
100
+ cid,
101
+ priority: entry.priority,
102
+ wantType: entry.wantType ?? WantType.WantBlock,
103
+ sendDontHave: entry.sendDontHave ?? false,
104
+ status: 'want',
105
+ created: Date.now()
106
+ });
107
+ }
108
+ }
109
+ // if we have exceeded maxWantListSize, truncate the list - first select
110
+ // wants that are not currently being sent to the user
111
+ const wants = [...this.wants.entries()]
112
+ .filter(([key, entry]) => entry.status === 'want');
113
+ if (wants.length > this.maxWantListSize) {
114
+ this.truncateWants(wants);
115
+ }
116
+ }
117
+ truncateWants(wants) {
118
+ // sort wants by priority, lack of block presence, then age so the wants
119
+ // to be evicted are a older, low priority wants that we don't have the
120
+ // block for
121
+ wants = wants
122
+ .sort((a, b) => {
123
+ if (a[1].created < b[1].created) {
124
+ return -1;
125
+ }
126
+ if (b[1].created < a[1].created) {
127
+ return 1;
128
+ }
129
+ return 0;
130
+ })
131
+ .sort((a, b) => {
132
+ if (a[1].haveBlock === false) {
133
+ return -1;
134
+ }
135
+ if (b[1].haveBlock === false) {
136
+ return 1;
137
+ }
138
+ return 0;
139
+ })
140
+ .sort((a, b) => {
141
+ if (a[1].priority < b[1].priority) {
142
+ return -1;
143
+ }
144
+ if (b[1].priority < a[1].priority) {
145
+ return 1;
146
+ }
147
+ return 0;
148
+ });
149
+ const toRemove = wants.length - this.maxWantListSize;
150
+ for (let i = 0; i < toRemove; i++) {
151
+ this.wants.delete(wants[i][0]);
152
+ }
153
+ }
154
+ getWants() {
155
+ return [...this.wants.values()];
156
+ }
157
+ hasWant(cid) {
158
+ const cidStr = uint8ArrayToString(cid.multihash.bytes, 'base64');
159
+ return this.wants.has(cidStr);
160
+ }
41
161
  async sendBlocksToPeer(options) {
42
162
  const message = new QueuedBitswapMessage();
43
163
  const sentBlocks = new Set();
44
- for (const [key, entry] of this.wants.entries()) {
164
+ // remove any expired wants
165
+ this.removeExpiredWants();
166
+ // pick unsent wants
167
+ const unsent = [...this.wants.entries()]
168
+ .filter(([key, value]) => value.status === 'want');
169
+ // update status, ensure we don't send the same blocks repeatedly
170
+ unsent.forEach(([key, value]) => {
171
+ value.status = 'sending';
172
+ });
173
+ for (const [key, entry] of unsent) {
45
174
  try {
46
175
  const block = await toBuffer(this.blockstore.get(entry.cid, options));
176
+ // ensure we still need to send the block/status, status may have
177
+ // changed due to incoming message while we were waiting for async block
178
+ // load
179
+ if (entry.status !== 'sending') {
180
+ continue;
181
+ }
47
182
  // do they want the block or just us to tell them we have the block
48
183
  if (entry.wantType === WantType.WantHave) {
49
184
  if (block.byteLength < this.maxSizeReplaceHasWithBlock) {
@@ -73,11 +208,17 @@ export class Ledger {
73
208
  prefix: cidToPrefix(entry.cid)
74
209
  });
75
210
  }
211
+ entry.status = 'sent';
212
+ entry.expires = Date.now() + this.doNotResendBlockWindow;
76
213
  }
77
214
  catch (err) {
78
215
  if (err.name !== 'NotFoundError') {
79
216
  throw err;
80
217
  }
218
+ // reset status to try again later
219
+ entry.status = 'want';
220
+ // used to maybe delete this want later if the want list grows too large
221
+ entry.haveBlock = false;
81
222
  this.log('do not have block for %c', entry.cid);
82
223
  // we don't have the requested block and the remote is not interested
83
224
  // in us telling them that
@@ -102,11 +243,6 @@ export class Ledger {
102
243
  this.log('sent message');
103
244
  // update accounting
104
245
  this.sentBytes([...message.blocks.values()].reduce((acc, curr) => acc + curr.data.byteLength, 0));
105
- // remove sent blocks from local copy of their want list - they can still
106
- // re-request if required
107
- for (const key of sentBlocks) {
108
- this.wants.delete(key);
109
- }
110
246
  }
111
247
  }
112
248
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ledger.js","sourceRoot":"","sources":["../../../src/peer-want-lists/ledger.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,cAAc,CAAA;AACnC,OAAO,EAAE,uCAAuC,EAAE,MAAM,iBAAiB,CAAA;AACzE,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,kBAAkB,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,CAAC,YAAY,EAAE,CAAC;oBACxB,SAAQ;gBACV,CAAC;gBAED,oDAAoD;gBACpD,IAAI,KAAK,CAAC,aAAa,KAAK,IAAI,EAAE,CAAC;oBACjC,SAAQ;gBACV,CAAC;gBAED,KAAK,CAAC,aAAa,GAAG,IAAI,CAAA;gBAC1B,OAAO,CAAC,gBAAgB,CAAC,KAAK,CAAC,GAAG,EAAE;oBAClC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK;oBACpB,IAAI,EAAE,iBAAiB,CAAC,cAAc;iBACvC,CAAC,CAAA;YACJ,CAAC;QACH,CAAC;QAED,8DAA8D;QAC9D,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,IAAI,OAAO,CAAC,cAAc,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;YAC/D,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAA;YAC3B,MAAM,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;YAC7D,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC,CAAA;YAExB,oBAAoB;YACpB,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAA;QACnG,CAAC;IACH,CAAC;CACF"}
@@ -1,7 +1,7 @@
1
1
  import { AbstractSession } from '@helia/utils';
2
- import type { BitswapWantProgressEvents } from './index.js';
3
- import type { Network } from './network.js';
4
- import type { WantList } from './want-list.js';
2
+ import type { BitswapWantProgressEvents } from './index.ts';
3
+ import type { Network } from './network.ts';
4
+ import type { WantList } from './want-list.ts';
5
5
  import type { BlockRetrievalOptions, CreateSessionOptions } from '@helia/interface';
6
6
  import type { ComponentLogger, Libp2p, PeerId } from '@libp2p/interface';
7
7
  import type { Multiaddr } from '@multiformats/multiaddr';
@@ -1,4 +1,4 @@
1
- import type { Block, BlockPresence, WantlistEntry } from '../pb/message.js';
1
+ import type { Block, BlockPresence, WantlistEntry } from '../pb/message.ts';
2
2
  import type { CID } from 'multiformats';
3
3
  /**
4
4
  * A bitswap message that is in the send queue. So implemented to be
@@ -1,4 +1,4 @@
1
- import ve from './varint-encoder.js';
1
+ import ve from "./varint-encoder.js";
2
2
  export function cidToPrefix(cid) {
3
3
  return ve([
4
4
  cid.version, cid.code, cid.multihash.code, cid.multihash.digest.byteLength
@@ -1,3 +1,3 @@
1
- import type { QueuedBitswapMessage } from './bitswap-message.js';
1
+ import type { QueuedBitswapMessage } from './bitswap-message.ts';
2
2
  export declare function mergeMessages(existingMessage: QueuedBitswapMessage, newMessage: QueuedBitswapMessage): QueuedBitswapMessage;
3
3
  //# sourceMappingURL=merge-messages.d.ts.map
@@ -1,4 +1,4 @@
1
- import type { QueuedBitswapMessage } from './bitswap-message.js';
1
+ import type { QueuedBitswapMessage } from './bitswap-message.ts';
2
2
  /**
3
3
  * https://github.com/ipfs/kubo/issues/4473#issuecomment-350390693
4
4
  */
@@ -1,6 +1,6 @@
1
1
  import { encodingLength } from 'uint8-varint';
2
- import { BlockTooLargeError } from '../errors.js';
3
- import { BitswapMessage, Block, BlockPresence, WantlistEntry } from '../pb/message.js';
2
+ import { BlockTooLargeError } from "../errors.js";
3
+ import { BitswapMessage, Block, BlockPresence, WantlistEntry } from "../pb/message.js";
4
4
  /**
5
5
  * https://github.com/ipfs/kubo/issues/4473#issuecomment-350390693
6
6
  */
@@ -1,8 +1,8 @@
1
1
  import { TypedEventEmitter } from '@libp2p/interface';
2
2
  import { CID } from 'multiformats/cid';
3
- import { WantType } from './pb/message.js';
4
- import type { BitswapNotifyProgressEvents, MultihashHasherLoader } from './index.js';
5
- import type { BitswapNetworkWantProgressEvents, Network } from './network.js';
3
+ import { WantType } from './pb/message.ts';
4
+ import type { BitswapNotifyProgressEvents, MultihashHasherLoader } from './index.ts';
5
+ import type { BitswapNetworkWantProgressEvents, Network } from './network.ts';
6
6
  import type { ComponentLogger, PeerId, Startable, AbortOptions, Libp2p, TypedEventTarget, Metrics } from '@libp2p/interface';
7
7
  import type { PeerMap } from '@libp2p/peer-collections';
8
8
  import type { ProgressOptions } from 'progress-events';
@@ -7,10 +7,10 @@ import pDefer from 'p-defer';
7
7
  import { raceEvent } from 'race-event';
8
8
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
9
9
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string';
10
- import { DEFAULT_MESSAGE_SEND_DELAY } from './constants.js';
11
- import { BlockPresenceType, WantType } from './pb/message.js';
12
- import { QueuedBitswapMessage } from './utils/bitswap-message.js';
13
- import vd from './utils/varint-decoder.js';
10
+ import { DEFAULT_MESSAGE_SEND_DELAY } from "./constants.js";
11
+ import { BlockPresenceType, WantType } from "./pb/message.js";
12
+ import { QueuedBitswapMessage } from "./utils/bitswap-message.js";
13
+ import vd from "./utils/varint-decoder.js";
14
14
  export class WantList extends TypedEventEmitter {
15
15
  /**
16
16
  * Tracks what CIDs we've previously sent to which peers
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helia/bitswap",
3
- "version": "3.1.2",
3
+ "version": "3.1.3-83e1f40d",
4
4
  "description": "JavaScript implementation of the Bitswap data exchange protocol used by Helia",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/ipfs/helia/tree/main/packages/bitswap#readme",
@@ -51,8 +51,8 @@
51
51
  "docs": "aegir docs"
52
52
  },
53
53
  "dependencies": {
54
- "@helia/interface": "^6.1.1",
55
- "@helia/utils": "^2.4.2",
54
+ "@helia/interface": "6.1.1-83e1f40d",
55
+ "@helia/utils": "2.4.2-83e1f40d",
56
56
  "@libp2p/interface": "^3.1.0",
57
57
  "@libp2p/logger": "^6.0.5",
58
58
  "@libp2p/peer-collections": "^7.0.5",
package/src/bitswap.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import { setMaxListeners } from '@libp2p/interface'
2
2
  import { anySignal } from 'any-signal'
3
3
  import { CustomProgressEvent } from 'progress-events'
4
- import { Network } from './network.js'
5
- import { PeerWantLists } from './peer-want-lists/index.js'
6
- import { createBitswapSession } from './session.js'
7
- import { Stats } from './stats.js'
8
- import { WantList } from './want-list.js'
9
- import type { BitswapOptions, Bitswap as BitswapInterface, BitswapWantProgressEvents, BitswapNotifyProgressEvents, WantListEntry, BitswapComponents } from './index.js'
4
+ import { Network } from './network.ts'
5
+ import { PeerWantLists } from './peer-want-lists/index.ts'
6
+ import { createBitswapSession } from './session.ts'
7
+ import { Stats } from './stats.ts'
8
+ import { WantList } from './want-list.ts'
9
+ import type { BitswapOptions, Bitswap as BitswapInterface, BitswapWantProgressEvents, BitswapNotifyProgressEvents, WantListEntry, BitswapComponents, PeerWantListEntry } from './index.ts'
10
10
  import type { CreateSessionOptions, ProviderOptions, SessionBlockBroker } from '@helia/interface'
11
11
  import type { ComponentLogger, Libp2p, PeerId, AbortOptions } from '@libp2p/interface'
12
12
  import type { Logger } from '@libp2p/logger'
@@ -127,7 +127,7 @@ export class Bitswap implements BitswapInterface {
127
127
  }))
128
128
  }
129
129
 
130
- getPeerWantlist (peer: PeerId): WantListEntry[] | undefined {
130
+ getPeerWantlist (peer: PeerId): PeerWantListEntry[] | undefined {
131
131
  return this.peerWantLists.wantListForPeer(peer)
132
132
  }
133
133
 
package/src/constants.ts CHANGED
@@ -11,3 +11,5 @@ export const DEFAULT_SESSION_ROOT_PRIORITY = 1
11
11
  export const DEFAULT_MAX_PROVIDERS_PER_REQUEST = 3
12
12
  export const DEFAULT_MAX_OUTGOING_MESSAGE_SIZE = 1024 * 1024 * 4
13
13
  export const DEFAULT_MAX_INCOMING_MESSAGE_SIZE = DEFAULT_MAX_OUTGOING_MESSAGE_SIZE
14
+ export const DEFAULT_DO_NOT_RESEND_BLOCK_WINDOW = 1_000 * 5
15
+ export const DEFAULT_MAX_WANTLIST_SIZE = 1024
package/src/index.ts CHANGED
@@ -6,9 +6,9 @@
6
6
  * It supersedes the older [ipfs-bitswap](https://www.npmjs.com/package/ipfs-bitswap) module with the aim of being smaller, faster, better integrated with libp2p/helia, having fewer dependencies and using standard JavaScript instead of Node.js APIs.
7
7
  */
8
8
 
9
- import { Bitswap as BitswapClass } from './bitswap.js'
10
- import type { BitswapNetworkNotifyProgressEvents, BitswapNetworkWantProgressEvents, BitswapNetworkProgressEvents } from './network.js'
11
- import type { WantType } from './pb/message.js'
9
+ import { Bitswap as BitswapClass } from './bitswap.ts'
10
+ import type { BitswapNetworkNotifyProgressEvents, BitswapNetworkWantProgressEvents, BitswapNetworkProgressEvents } from './network.ts'
11
+ import type { WantType } from './pb/message.ts'
12
12
  import type { CreateSessionOptions, ProviderOptions, SessionBlockBroker } from '@helia/interface'
13
13
  import type { Routing } from '@helia/interface/routing'
14
14
  import type { Libp2p, AbortOptions, Startable, ComponentLogger, Metrics, PeerId } from '@libp2p/interface'
@@ -35,12 +35,18 @@ export type { BitswapNetworkProgressEvents }
35
35
  export type { WantType }
36
36
  export type { BitswapProvider } from './network.ts'
37
37
 
38
+ export type WantStatus = 'want' | 'sending' | 'sent'
39
+
38
40
  export interface WantListEntry {
39
41
  cid: CID
40
42
  priority: number
41
43
  wantType: WantType
42
44
  }
43
45
 
46
+ export interface PeerWantListEntry extends WantListEntry {
47
+ status: WantStatus
48
+ }
49
+
44
50
  export interface Bitswap extends Startable {
45
51
  /**
46
52
  * Returns the current state of the wantlist
@@ -51,7 +57,7 @@ export interface Bitswap extends Startable {
51
57
  * Returns the current state of the wantlist for a peer, if it is being
52
58
  * tracked
53
59
  */
54
- getPeerWantlist(peerId: PeerId): WantListEntry[] | undefined
60
+ getPeerWantlist(peerId: PeerId): PeerWantListEntry[] | undefined
55
61
 
56
62
  /**
57
63
  * Notify bitswap that a new block is available
@@ -188,6 +194,21 @@ export interface BitswapOptions {
188
194
  * @default 2097152
189
195
  */
190
196
  maxIncomingMessageSize?: number
197
+
198
+ /**
199
+ * If a block has been sent to a peer and it is requested again by the same
200
+ * peer, do not send it again until this many ms have elapsed
201
+ *
202
+ * @default 5000
203
+ */
204
+ doNotResendBlockWindow?: number
205
+
206
+ /**
207
+ * Restrict the local copy of each peer wantlist to this many entries
208
+ *
209
+ * @default 1024
210
+ */
211
+ maxWantlistSize?: number
191
212
  }
192
213
 
193
214
  export const createBitswap = (components: BitswapComponents, options: BitswapOptions = {}): Bitswap => {
package/src/network.ts CHANGED
@@ -7,14 +7,14 @@ import { pushable } from 'it-pushable'
7
7
  import take from 'it-take'
8
8
  import { CustomProgressEvent } from 'progress-events'
9
9
  import { raceEvent } from 'race-event'
10
- import { BITSWAP_120, DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_INCOMING_MESSAGE_SIZE, DEFAULT_MAX_OUTBOUND_STREAMS, DEFAULT_MAX_OUTGOING_MESSAGE_SIZE, DEFAULT_MAX_PROVIDERS_PER_REQUEST, DEFAULT_MESSAGE_RECEIVE_TIMEOUT, DEFAULT_MESSAGE_SEND_CONCURRENCY, DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS } from './constants.js'
11
- import { BitswapMessage } from './pb/message.js'
12
- import { mergeMessages } from './utils/merge-messages.js'
13
- import { splitMessage } from './utils/split-message.js'
14
- import type { WantOptions } from './bitswap.js'
15
- import type { MultihashHasherLoader } from './index.js'
16
- import type { Block } from './pb/message.js'
17
- import type { QueuedBitswapMessage } from './utils/bitswap-message.js'
10
+ import { BITSWAP_120, DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_INCOMING_MESSAGE_SIZE, DEFAULT_MAX_OUTBOUND_STREAMS, DEFAULT_MAX_OUTGOING_MESSAGE_SIZE, DEFAULT_MAX_PROVIDERS_PER_REQUEST, DEFAULT_MESSAGE_RECEIVE_TIMEOUT, DEFAULT_MESSAGE_SEND_CONCURRENCY, DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS } from './constants.ts'
11
+ import { BitswapMessage } from './pb/message.ts'
12
+ import { mergeMessages } from './utils/merge-messages.ts'
13
+ import { splitMessage } from './utils/split-message.ts'
14
+ import type { WantOptions } from './bitswap.ts'
15
+ import type { MultihashHasherLoader } from './index.ts'
16
+ import type { Block } from './pb/message.ts'
17
+ import type { QueuedBitswapMessage } from './utils/bitswap-message.ts'
18
18
  import type { Provider, Routing } from '@helia/interface/routing'
19
19
  import type { Libp2p, AbortOptions, Connection, PeerId, Topology, ComponentLogger, IdentifyResult, Counter, Metrics, Stream } from '@libp2p/interface'
20
20
  import type { Logger } from '@libp2p/logger'
@@ -1,11 +1,9 @@
1
1
  import { trackedPeerMap } from '@libp2p/peer-collections'
2
2
  import { CID } from 'multiformats/cid'
3
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
- import { WantType } from '../pb/message.js'
5
- import { Ledger } from './ledger.js'
6
- import type { BitswapNotifyProgressEvents, WantListEntry } from '../index.js'
7
- import type { Network } from '../network.js'
8
- import type { BitswapMessage } from '../pb/message.js'
3
+ import { Ledger } from './ledger.ts'
4
+ import type { BitswapNotifyProgressEvents, PeerWantListEntry } from '../index.ts'
5
+ import type { Network } from '../network.ts'
6
+ import type { BitswapMessage } from '../pb/message.ts'
9
7
  import type { AbortOptions, ComponentLogger, Libp2p, Logger, Metrics, PeerId } from '@libp2p/interface'
10
8
  import type { PeerMap } from '@libp2p/peer-collections'
11
9
  import type { Blockstore } from 'interface-blockstore'
@@ -13,6 +11,8 @@ import type { ProgressOptions } from 'progress-events'
13
11
 
14
12
  export interface PeerWantListsInit {
15
13
  maxSizeReplaceHasWithBlock?: number
14
+ doNotResendBlockWindow?: number
15
+ maxWantListSize?: number
16
16
  }
17
17
 
18
18
  export interface PeerWantListsComponents {
@@ -36,6 +36,8 @@ export class PeerWantLists {
36
36
  public network: Network
37
37
  public readonly ledgerMap: PeerMap<Ledger>
38
38
  private readonly maxSizeReplaceHasWithBlock?: number
39
+ private readonly doNotResendBlockWindow?: number
40
+ private readonly maxWantListSize?: number
39
41
  private readonly log: Logger
40
42
  private readonly logger: ComponentLogger
41
43
 
@@ -43,6 +45,8 @@ export class PeerWantLists {
43
45
  this.blockstore = components.blockstore
44
46
  this.network = components.network
45
47
  this.maxSizeReplaceHasWithBlock = init.maxSizeReplaceHasWithBlock
48
+ this.doNotResendBlockWindow = init.doNotResendBlockWindow
49
+ this.maxWantListSize = init.maxWantListSize
46
50
  this.log = components.logger.forComponent('helia:bitswap:peer-want-lists')
47
51
  this.logger = components.logger
48
52
 
@@ -78,14 +82,17 @@ export class PeerWantLists {
78
82
  }
79
83
  }
80
84
 
81
- wantListForPeer (peerId: PeerId): WantListEntry[] | undefined {
85
+ wantListForPeer (peerId: PeerId): PeerWantListEntry[] | undefined {
82
86
  const ledger = this.ledgerMap.get(peerId)
83
87
 
84
88
  if (ledger == null) {
85
89
  return undefined
86
90
  }
87
91
 
88
- return [...ledger.wants.values()]
92
+ // remove any expired wants
93
+ ledger.removeExpiredWants()
94
+
95
+ return ledger.getWants()
89
96
  }
90
97
 
91
98
  peers (): PeerId[] {
@@ -105,7 +112,9 @@ export class PeerWantLists {
105
112
  network: this.network,
106
113
  logger: this.logger
107
114
  }, {
108
- maxSizeReplaceHasWithBlock: this.maxSizeReplaceHasWithBlock
115
+ maxSizeReplaceHasWithBlock: this.maxSizeReplaceHasWithBlock,
116
+ doNotResendBlockWindow: this.doNotResendBlockWindow,
117
+ maxWantListSize: this.maxWantListSize
109
118
  })
110
119
  this.ledgerMap.set(peerId, ledger)
111
120
  }
@@ -113,47 +122,21 @@ export class PeerWantLists {
113
122
  // record the amount of block data received
114
123
  ledger.receivedBytes(message.blocks?.reduce((acc, curr) => acc + curr.data.byteLength, 0) ?? 0)
115
124
 
116
- if (message.wantlist != null) {
117
- // if the message has a full wantlist, clear the current wantlist
118
- if (message.wantlist.full === true) {
119
- ledger.wants.clear()
120
- }
125
+ // remove any expired wants
126
+ ledger.removeExpiredWants()
121
127
 
122
- // clear cancelled wants and add new wants to the ledger
123
- for (const entry of message.wantlist.entries) {
124
- const cid = CID.decode(entry.cid)
125
- const cidStr = uint8ArrayToString(cid.multihash.bytes, 'base64')
126
-
127
- if (entry.cancel === true) {
128
- this.log('peer %p cancelled want of block for %c', peerId, cid)
129
- ledger.wants.delete(cidStr)
130
- } else {
131
- if (entry.wantType === WantType.WantHave) {
132
- this.log('peer %p wanted block presence for %c', peerId, cid)
133
- } else {
134
- this.log('peer %p wanted block for %c', peerId, cid)
135
- }
136
-
137
- ledger.wants.set(cidStr, {
138
- cid,
139
- priority: entry.priority,
140
- wantType: entry.wantType ?? WantType.WantBlock,
141
- sendDontHave: entry.sendDontHave ?? false
142
- })
143
- }
144
- }
145
- }
128
+ // add new wants
129
+ ledger.addWants(message.wantlist)
146
130
 
147
131
  this.log('send blocks to peer')
148
132
  await ledger.sendBlocksToPeer()
149
133
  }
150
134
 
151
135
  async receivedBlock (cid: CID, options: ProgressOptions<BitswapNotifyProgressEvents> & AbortOptions): Promise<void> {
152
- const cidStr = uint8ArrayToString(cid.multihash.bytes, 'base64')
153
136
  const ledgers: Ledger[] = []
154
137
 
155
138
  for (const ledger of this.ledgerMap.values()) {
156
- if (ledger.wants.has(cidStr)) {
139
+ if (ledger.hasWant(cid)) {
157
140
  ledgers.push(ledger)
158
141
  }
159
142
  }