@helia/bitswap 1.0.0-338885f → 1.0.0-59de059

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/src/constants.d.ts +2 -0
  3. package/dist/src/constants.d.ts.map +1 -1
  4. package/dist/src/constants.js +2 -0
  5. package/dist/src/constants.js.map +1 -1
  6. package/dist/src/index.d.ts +20 -7
  7. package/dist/src/index.d.ts.map +1 -1
  8. package/dist/src/index.js.map +1 -1
  9. package/dist/src/network.d.ts +5 -3
  10. package/dist/src/network.d.ts.map +1 -1
  11. package/dist/src/network.js +52 -128
  12. package/dist/src/network.js.map +1 -1
  13. package/dist/src/pb/message.d.ts +6 -6
  14. package/dist/src/pb/message.d.ts.map +1 -1
  15. package/dist/src/pb/message.js +37 -20
  16. package/dist/src/pb/message.js.map +1 -1
  17. package/dist/src/peer-want-lists/index.d.ts +1 -0
  18. package/dist/src/peer-want-lists/index.d.ts.map +1 -1
  19. package/dist/src/peer-want-lists/index.js +5 -1
  20. package/dist/src/peer-want-lists/index.js.map +1 -1
  21. package/dist/src/peer-want-lists/ledger.d.ts +3 -1
  22. package/dist/src/peer-want-lists/ledger.d.ts.map +1 -1
  23. package/dist/src/peer-want-lists/ledger.js +8 -0
  24. package/dist/src/peer-want-lists/ledger.js.map +1 -1
  25. package/dist/src/utils/merge-messages.d.ts +3 -0
  26. package/dist/src/utils/merge-messages.d.ts.map +1 -0
  27. package/dist/src/utils/merge-messages.js +51 -0
  28. package/dist/src/utils/merge-messages.js.map +1 -0
  29. package/dist/src/utils/split-message.d.ts +14 -0
  30. package/dist/src/utils/split-message.d.ts.map +1 -0
  31. package/dist/src/utils/split-message.js +99 -0
  32. package/dist/src/utils/split-message.js.map +1 -0
  33. package/dist/src/want-list.d.ts.map +1 -1
  34. package/dist/src/want-list.js +6 -3
  35. package/dist/src/want-list.js.map +1 -1
  36. package/package.json +4 -4
  37. package/src/constants.ts +2 -0
  38. package/src/index.ts +22 -8
  39. package/src/network.ts +60 -150
  40. package/src/pb/message.ts +40 -20
  41. package/src/peer-want-lists/index.ts +5 -1
  42. package/src/peer-want-lists/ledger.ts +11 -1
  43. package/src/utils/merge-messages.ts +70 -0
  44. package/src/utils/split-message.ts +131 -0
  45. package/src/want-list.ts +8 -3
@@ -0,0 +1,131 @@
1
+ /* eslint-disable complexity */
2
+ import { CodeError } from '@libp2p/interface'
3
+ import { encodingLength } from 'uint8-varint'
4
+ import { BitswapMessage, Block, BlockPresence, WantlistEntry } from '../pb/message.js'
5
+
6
+ /**
7
+ * Split the passed Bitswap message into multiple smaller messages that when
8
+ * serialized will be under the maximum message size.
9
+ *
10
+ * Since blocks are the largest thing to send, we first try to fit as many
11
+ * blocks as possible into the message, then add (smaller) block presences and
12
+ * wants until the max size is reached.
13
+ *
14
+ * If a block is encountered that is larger than the max message size an error
15
+ * will be thrown.
16
+ */
17
+ export async function * splitMessage (message: BitswapMessage, maxSize: number): AsyncGenerator<Uint8Array> {
18
+ const wantListEntries = message.wantlist?.entries ?? []
19
+ const blockPresences = message.blockPresences
20
+ const blocks = message.blocks
21
+
22
+ let wantListIndex = 0
23
+ let blockPresencesIndex = 0
24
+ let blocksIndex = 0
25
+ let messagesSent = 0
26
+ let doneSending = false
27
+
28
+ while (true) {
29
+ const subMessage: Required<BitswapMessage> = {
30
+ wantlist: {
31
+ full: false,
32
+ entries: []
33
+ },
34
+ blockPresences: [],
35
+ blocks: [],
36
+ pendingBytes: 0
37
+ }
38
+
39
+ let size = 4
40
+
41
+ let { added, hasMore, newSize } = addToMessage(blocks, subMessage.blocks, blocksIndex, maxSize, size, calculateEncodedBlockSize)
42
+
43
+ blocksIndex += added
44
+ size = newSize
45
+ const haveMoreBlocks = hasMore
46
+
47
+ ;({ added, hasMore, newSize } = addToMessage(blockPresences, subMessage.blockPresences, blockPresencesIndex, maxSize, size, calculateEncodedBlockPresenceSize))
48
+
49
+ blockPresencesIndex += added
50
+ size = newSize
51
+ const haveMorePresences = hasMore
52
+
53
+ ;({ added, hasMore, newSize } = addToMessage(wantListEntries, subMessage.wantlist.entries, wantListIndex, maxSize, size, calculateEncodedWantlistEntrySize))
54
+
55
+ wantListIndex += added
56
+ size = newSize
57
+ const haveMoreWantlistEntries = hasMore
58
+
59
+ doneSending = !haveMoreBlocks && !haveMorePresences && !haveMoreWantlistEntries
60
+
61
+ // if we're only sending one message, and that message has the full wantlist
62
+ // make sure we let the remote know it's the full list
63
+ if (doneSending && messagesSent === 0 && message.wantlist?.full === true) {
64
+ subMessage.wantlist.full = true
65
+ }
66
+
67
+ yield BitswapMessage.encode(subMessage)
68
+
69
+ messagesSent++
70
+
71
+ if (doneSending) {
72
+ break
73
+ }
74
+ }
75
+ }
76
+
77
+ interface AddResult {
78
+ hasMore: boolean
79
+ added: number
80
+ newSize: number
81
+ }
82
+
83
+ function addToMessage <T> (input: T[], output: T[], start: number, maxSize: number, size: number, calculateSize: (arg: T) => number): AddResult {
84
+ let added = 0
85
+ let hasMore = false
86
+
87
+ // try to send as many blocks as possible
88
+ for (let i = start; i < input.length; i++) {
89
+ const item = input[i]
90
+ const itemSize = calculateSize(item)
91
+
92
+ if (itemSize > maxSize) {
93
+ throw new CodeError('Cannot send block as it is over the max message size', 'ERR_BLOCK_TOO_LARGE')
94
+ }
95
+
96
+ const newSize = size + itemSize
97
+
98
+ if (newSize >= maxSize) {
99
+ hasMore = true
100
+ break
101
+ }
102
+
103
+ output.push(item)
104
+ added++
105
+ size = newSize
106
+ }
107
+
108
+ return { hasMore, added, newSize: size }
109
+ }
110
+
111
+ function calculateEncodedBlockSize (block: Block): number {
112
+ // 3 is the "blocks" field number in message.proto
113
+ return calculateLength(3, Block.encode(block))
114
+ }
115
+
116
+ function calculateEncodedBlockPresenceSize (blockPresence: BlockPresence): number {
117
+ // 4 is the "blockPresences" field number in message.proto
118
+ return calculateLength(4, BlockPresence.encode(blockPresence))
119
+ }
120
+
121
+ function calculateEncodedWantlistEntrySize (entry: WantlistEntry): number {
122
+ // 1 is the "entries" field number in message.proto
123
+ return calculateLength(1, WantlistEntry.encode(entry))
124
+ }
125
+
126
+ function calculateLength (fieldNumber: number, data: Uint8Array): number {
127
+ const fieldNumberLength = encodingLength(fieldNumber)
128
+ const dataLengthLength = encodingLength(data.byteLength)
129
+
130
+ return fieldNumberLength + dataLengthLength + data.byteLength
131
+ }
package/src/want-list.ts CHANGED
@@ -375,7 +375,7 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
375
375
  * Invoked when a message is received from a bitswap peer
376
376
  */
377
377
  private async receiveMessage (sender: PeerId, message: BitswapMessage): Promise<void> {
378
- this.log('received message from %p', sender)
378
+ this.log('received message %d from %p with %d blocks', sender, message.blocks.length)
379
379
  let blocksCancelled = false
380
380
 
381
381
  // process blocks
@@ -397,7 +397,12 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
397
397
  continue
398
398
  }
399
399
 
400
- const hash = await hasher.digest(block.data)
400
+ let hash: any = hasher.digest(block.data)
401
+
402
+ if (hash.then != null) {
403
+ hash = await hash
404
+ }
405
+
401
406
  const cid = CID.create(cidVersion === 0 ? 0 : 1, multicodec, hash)
402
407
 
403
408
  this.log('received block from %p for %c', sender, cid)
@@ -423,7 +428,7 @@ export class WantList extends TypedEventEmitter<WantListEvents> implements Start
423
428
  const entry = this.wants.get(cidStr)
424
429
 
425
430
  if (entry == null) {
426
- return
431
+ continue
427
432
  }
428
433
 
429
434
  // since we received the block, flip the cancel flag to send cancels to