@@ -64,38 +64,7 @@ class Bitswap {
64
64
}
65
65
66
66
// handle messages received through the network
67
- // _receiveMessage (peerId, incoming, callback) {
68
67
async _receiveMessage ( peerId , incoming ) {
69
- // this.engine.messageReceived(peerId, incoming, (err) => {
70
- // if (err) {
71
- // // Only logging the issue to process as much as possible
72
- // // of the message. Currently `messageReceived` does not
73
- // // return any errors, but this could change in the future.
74
- // this._log('failed to receive message', incoming)
75
- // }
76
-
77
- // if (incoming.blocks.size === 0) {
78
- // return callback()
79
- // }
80
-
81
- // const blocks = Array.from(incoming.blocks.values())
82
-
83
- // // quickly send out cancels, reduces chances of duplicate block receives
84
- // const wanted = blocks
85
- // .filter((b) => this.wm.wantlist.contains(b.cid))
86
- // .map((b) => b.cid)
87
-
88
- // this.wm.cancelWants(wanted)
89
-
90
- // each(
91
- // blocks,
92
- // (b, cb) => {
93
- // const wasWanted = wanted.includes(b.cid)
94
- // this._handleReceivedBlock(peerId, b, wasWanted, cb)
95
- // },
96
- // callback
97
- // )
98
- // })
99
68
try {
100
69
await this . engine . messageReceived ( peerId , incoming )
101
70
} catch ( err ) {
@@ -128,17 +97,6 @@ class Bitswap {
128
97
async _handleReceivedBlock ( peerId , block , wasWanted ) {
129
98
this . _log ( 'received block' )
130
99
131
- // waterfall([
132
- // (cb) => this.blockstore.has(block.cid, cb),
133
- // (has, cb) => {
134
- // this._updateReceiveCounters(peerId.toB58String(), block, has)
135
- // if (has || !wasWanted) {
136
- // return nextTick(cb)
137
- // }
138
-
139
- // this._putBlock(block, cb)
140
- // }
141
- // ], callback)
142
100
const has = await this . blockstore . has ( block . cid )
143
101
this . _updateReceiveCounters ( peerId . toB58String ( ) , block , has )
144
102
if ( has || ! wasWanted ) {
@@ -175,23 +133,7 @@ class Bitswap {
175
133
this . _stats . disconnected ( peerId )
176
134
}
177
135
178
- // _putBlock (block, callback) {
179
136
async _putBlock ( block ) {
180
- // this.blockstore.put(block, (err) => {
181
- // if (err) {
182
- // return callback(err)
183
- // }
184
-
185
- // this.notifications.hasBlock(block)
186
- // this.network.provide(block.cid, (err) => {
187
- // if (err) {
188
- // this._log.error('Failed to provide: %s', err.message)
189
- // }
190
- // })
191
-
192
- // this.engine.receivedBlocks([block.cid])
193
- // callback()
194
- // })
195
137
await this . blockstore . put ( block )
196
138
197
139
this . notifications . hasBlock ( block )
@@ -240,23 +182,9 @@ class Bitswap {
240
182
* @param {function(Error, Block) } callback
241
183
* @returns {void }
242
184
*/
243
- // get (cid, callback) {
244
185
async get ( cid ) {
245
- // this.getMany([cid], (err, blocks) => {
246
- // if (err) {
247
- // return callback(err)
248
- // }
249
-
250
- // if (blocks && blocks.length > 0) {
251
- // callback(null, blocks[0])
252
- // } else {
253
- // // when a unwant happens
254
- // callback()
255
- // }
256
- // })
257
- const blocks = await this . getMany ( [ cid ] )
258
- if ( blocks && blocks . length > 0 ) {
259
- return blocks [ 0 ]
186
+ for await ( const block of this . getMany ( [ cid ] ) ) {
187
+ return block
260
188
}
261
189
}
262
190
@@ -268,61 +196,11 @@ class Bitswap {
268
196
* @param {function(Error, Blocks) } callback
269
197
* @returns {void }
270
198
*/
271
- getMany ( cids ) {
199
+ async * getMany ( cids ) {
272
200
let pendingStart = cids . length
273
201
const wantList = [ ]
274
202
let promptedNetwork = false
275
203
276
- // const getFromOutside = (cid, cb) => {
277
- // wantList.push(cid)
278
-
279
- // this.notifications.wantBlock(
280
- // cid,
281
- // // called on block receive
282
- // (block) => {
283
- // this.wm.cancelWants([cid])
284
- // cb(null, block)
285
- // },
286
- // // called on unwant
287
- // () => {
288
- // this.wm.cancelWants([cid])
289
- // cb(null, undefined)
290
- // }
291
- // )
292
-
293
- // if (!pendingStart) {
294
- // this.wm.wantBlocks(wantList)
295
- // }
296
- // }
297
-
298
- // map(cids, (cid, cb) => {
299
- // waterfall(
300
- // [
301
- // (cb) => this.blockstore.has(cid, cb),
302
- // (has, cb) => {
303
- // pendingStart--
304
- // if (has) {
305
- // if (!pendingStart) {
306
- // this.wm.wantBlocks(wantList)
307
- // }
308
- // return this.blockstore.get(cid, cb)
309
- // }
310
-
311
- // if (!promptedNetwork) {
312
- // promptedNetwork = true
313
- // this.network.findAndConnect(cids[0], (err) => {
314
- // if (err) {
315
- // this._log.error(err)
316
- // }
317
- // })
318
- // }
319
-
320
- // // we don't have the block here
321
- // getFromOutside(cid, cb)
322
- // }
323
- // ],
324
- // cb)
325
- // }, callback)
326
204
const fetchFromNetwork = async ( cid ) => {
327
205
wantList . push ( cid )
328
206
@@ -334,17 +212,20 @@ class Bitswap {
334
212
335
213
const block = await blockP
336
214
this . wm . cancelWants ( [ cid ] )
215
+
337
216
return block
338
217
}
339
218
340
- return Promise . all ( cids . map ( async ( cid ) => {
219
+ for ( const cid of cids ) {
341
220
const has = await this . blockstore . has ( cid )
342
221
pendingStart --
343
222
if ( has ) {
344
223
if ( ! pendingStart ) {
345
224
this . wm . wantBlocks ( wantList )
346
225
}
347
- return this . blockstore . get ( cid )
226
+ yield this . blockstore . get ( cid )
227
+
228
+ continue
348
229
}
349
230
350
231
if ( ! promptedNetwork ) {
@@ -353,8 +234,8 @@ class Bitswap {
353
234
}
354
235
355
236
// we don't have the block locally so fetch it from the network
356
- return fetchFromNetwork ( cid )
357
- } ) )
237
+ yield fetchFromNetwork ( cid )
238
+ }
358
239
}
359
240
360
241
// removes the given cids from the wantlist independent of any ref counts
@@ -383,22 +264,14 @@ class Bitswap {
383
264
* @param {function(Error) } callback
384
265
* @returns {void }
385
266
*/
386
- // put (block, callback) {
387
267
async put ( block ) {
388
268
this . _log ( 'putting block' )
389
269
390
- // waterfall([
391
- // (cb) => this.blockstore.has(block.cid, cb),
392
- // (has, cb) => {
393
- // if (has) {
394
- // return nextTick(cb)
395
- // }
396
-
397
- // this._putBlock(block, cb)
398
- // }
399
- // ], callback)
400
270
const has = await this . blockstore . has ( block . cid )
401
- if ( has ) return
271
+
272
+ if ( has ) {
273
+ return
274
+ }
402
275
403
276
await this . _putBlock ( block )
404
277
}
@@ -411,29 +284,7 @@ class Bitswap {
411
284
* @param {function(Error) } callback
412
285
* @returns {void }
413
286
*/
414
- // putMany (blocks, callback) {
415
287
async putMany ( blocks ) {
416
- // waterfall([
417
- // (cb) => reject(blocks, (b, cb) => {
418
- // this.blockstore.has(b.cid, cb)
419
- // }, cb),
420
- // (newBlocks, cb) => this.blockstore.putMany(newBlocks, (err) => {
421
- // if (err) {
422
- // return cb(err)
423
- // }
424
-
425
- // newBlocks.forEach((block) => {
426
- // this.notifications.hasBlock(block)
427
- // this.engine.receivedBlocks([block.cid])
428
- // this.network.provide(block.cid, (err) => {
429
- // if (err) {
430
- // this._log.error('Failed to provide: %s', err.message)
431
- // }
432
- // })
433
- // })
434
- // cb()
435
- // })
436
- // ], callback)
437
288
const newBlocks = await Promise . all ( blocks . map ( async ( b ) => {
438
289
return ! ( await this . blockstore . has ( b . cid ) )
439
290
} ) ) . filter ( Boolean )
0 commit comments