-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathindex.js
More file actions
311 lines (278 loc) · 8.09 KB
/
Copy pathindex.js
File metadata and controls
311 lines (278 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
'use strict'
const IPLDBlock = require('ipld-block')
const CID = require('cids')
const { getName } = require('multicodec')
// @ts-ignore
const vd = require('varint-decoder')
const multihashing = require('multihashing-async')
const { isMapEqual } = require('../../utils')
const { Message } = require('./message.proto')
const Entry = require('./entry')
class BitswapMessage {
/**
* @param {boolean} full
*/
constructor (full) {
this.full = full
/** @type {Map<string, Entry>} */
this.wantlist = new Map()
/** @type {Map<string, import('ipfs-core-types/src/block-service').Block>} */
this.blocks = new Map()
/** @type {Map<string, import('./message.proto').BlockPresenceType>} */
this.blockPresences = new Map()
this.pendingBytes = 0
}
get empty () {
return this.blocks.size === 0 &&
this.wantlist.size === 0 &&
this.blockPresences.size === 0
}
/**
*
* @param {CID} cid
* @param {number} priority
* @param {import('./message.proto').WantType} [wantType]
* @param {boolean} [cancel]
* @param {boolean} [sendDontHave]
* @returns {void}
*/
addEntry (cid, priority, wantType, cancel, sendDontHave) {
if (wantType == null) {
wantType = BitswapMessage.WantType.Block
}
const cidStr = cid.toString('base58btc')
const entry = this.wantlist.get(cidStr)
if (entry) {
// Only change priority if want is of the same type
if (entry.wantType === wantType) {
entry.priority = priority
}
// Only change from "dont cancel" to "do cancel"
if (cancel) {
entry.cancel = Boolean(cancel)
}
// Only change from "dont send" to "do send" DONT_HAVE
if (sendDontHave) {
entry.sendDontHave = Boolean(sendDontHave)
}
// want-block overrides existing want-have
if (wantType === BitswapMessage.WantType.Block && entry.wantType === BitswapMessage.WantType.Have) {
entry.wantType = wantType
}
} else {
this.wantlist.set(cidStr, new Entry(cid, priority, wantType, cancel, sendDontHave))
}
}
/**
* @param {import('ipfs-core-types/src/block-service').Block} block
* @returns {void}
*/
addBlock (block) {
const cidStr = block.cid.toString('base58btc')
this.blocks.set(cidStr, block)
}
/**
* @param {CID} cid
*/
addHave (cid) {
const cidStr = cid.toString('base58btc')
if (!this.blockPresences.has(cidStr)) {
this.blockPresences.set(cidStr, BitswapMessage.BlockPresenceType.Have)
}
}
/**
* @param {CID} cid
*/
addDontHave (cid) {
const cidStr = cid.toString('base58btc')
if (!this.blockPresences.has(cidStr)) {
this.blockPresences.set(cidStr, BitswapMessage.BlockPresenceType.DontHave)
}
}
/**
* @param {CID} cid
*/
cancel (cid) {
const cidStr = cid.toString('base58btc')
this.wantlist.delete(cidStr)
this.addEntry(cid, 0, BitswapMessage.WantType.Block, true, false)
}
/**
* @param {number} size
*/
setPendingBytes (size) {
this.pendingBytes = size
}
/**
* Serializes to Bitswap Message protobuf of
* version 1.0.0
*
* @returns {Uint8Array}
*/
serializeToBitswap100 () {
/** @type {import('./message.proto').Message100} */
const msg = {
wantlist: {
entries: Array.from(this.wantlist.values()).map((entry) => {
return {
block: entry.cid.bytes, // cid
priority: Number(entry.priority),
cancel: Boolean(entry.cancel)
}
})
},
blocks: Array.from(this.blocks.values())
.map((block) => block.data)
}
if (this.full) {
msg.wantlist.full = true
}
return Message.encode(msg)
}
/**
* Serializes to Bitswap Message protobuf of
* version 1.1.0
*
* @returns {Uint8Array}
*/
serializeToBitswap110 () {
/** @type {import('./message.proto').Message110} */
const msg = {
wantlist: {
entries: Array.from(this.wantlist.values()).map((entry) => {
return {
block: entry.cid.bytes, // cid
priority: Number(entry.priority),
wantType: entry.wantType,
cancel: Boolean(entry.cancel),
sendDontHave: Boolean(entry.sendDontHave)
}
})
},
blockPresences: [],
payload: [],
pendingBytes: this.pendingBytes
}
if (this.full) {
msg.wantlist.full = true
}
this.blocks.forEach((block) => {
msg.payload.push({
prefix: block.cid.prefix,
data: block.data
})
})
for (const [cidStr, bpType] of this.blockPresences) {
msg.blockPresences.push({
cid: new CID(cidStr).bytes,
type: bpType
})
}
if (this.pendingBytes > 0) {
msg.pendingBytes = this.pendingBytes
}
return Message.encode(msg)
}
/**
* @param {BitswapMessage} other
* @returns {boolean}
*/
equals (other) {
if (this.full !== other.full ||
this.pendingBytes !== other.pendingBytes ||
!isMapEqual(this.wantlist, other.wantlist) ||
!isMapEqual(this.blocks, other.blocks) ||
// @TODO - Is this a bug ?
// @ts-expect-error - isMap equals map values to be objects not numbers
!isMapEqual(this.blockPresences, other.blockPresences)
) {
return false
}
return true
}
get [Symbol.toStringTag] () {
const list = Array.from(this.wantlist.keys())
const blocks = Array.from(this.blocks.keys())
return `BitswapMessage <full: ${this.full}, list: ${list}, blocks: ${blocks}>`
}
}
/**
*
* @param {Uint8Array} raw
* @returns {Promise<BitswapMessage>}
*/
BitswapMessage.deserialize = async (raw) => {
const decoded = Message.decode(raw)
const isFull = (decoded.wantlist && decoded.wantlist.full) || false
const msg = new BitswapMessage(isFull)
if (decoded.wantlist) {
decoded.wantlist.entries.forEach((entry) => {
// note: entry.block is the CID here
const cid = new CID(entry.block)
msg.addEntry(cid, entry.priority, entry.wantType, entry.cancel, entry.sendDontHave)
})
}
if (decoded.blockPresences) {
decoded.blockPresences.forEach((blockPresence) => {
const cid = new CID(blockPresence.cid)
if (blockPresence.type === BitswapMessage.BlockPresenceType.Have) {
msg.addHave(cid)
} else {
msg.addDontHave(cid)
}
})
}
// Bitswap 1.0.0
// decoded.blocks are just the byte arrays
if (decoded.blocks.length > 0) {
await Promise.all(decoded.blocks.map(async (b) => {
const hash = await multihashing(b, 'sha2-256')
const cid = new CID(hash)
msg.addBlock(new IPLDBlock(b, cid))
}))
return msg
}
// Bitswap 1.1.0
if (decoded.payload.length > 0) {
await Promise.all(decoded.payload.map(async (p) => {
if (!p.prefix || !p.data) {
return
}
const values = vd(p.prefix)
const cidVersion = values[0]
const multicodec = values[1]
const hashAlg = values[2]
// const hashLen = values[3] // We haven't need to use this so far
const hash = await multihashing(p.data, hashAlg)
const cid = new CID(cidVersion, getName(multicodec), hash)
msg.addBlock(new IPLDBlock(p.data, cid))
}))
msg.setPendingBytes(decoded.pendingBytes)
return msg
}
return msg
}
/**
* @param {CID} cid
*/
BitswapMessage.blockPresenceSize = (cid) => {
// It's ok if this is not exactly right: it's used to estimate the size of
// the HAVE / DONT_HAVE on the wire, but when doing that calculation we leave
// plenty of padding under the maximum message size.
// (It's more important for this to be fast).
return cid.bytes.length + 1
}
BitswapMessage.Entry = Entry
BitswapMessage.WantType = {
/** @type {import('./message.proto').WantBlock} */
Block: Message.Wantlist.WantType.Block,
/** @type {import('./message.proto').HaveBlock} */
Have: Message.Wantlist.WantType.Have
}
BitswapMessage.BlockPresenceType = {
/** @type {import('./message.proto').Have} */
Have: Message.BlockPresenceType.Have,
/** @type {import('./message.proto').DontHave} */
DontHave: Message.BlockPresenceType.DontHave
}
module.exports = BitswapMessage