Skip to content

Commit 31331c2

Browse files
committed
refactor: async/await
Still pending PeerId and PeerInfo async/await - they've been done but we can't use them until libp2p update libp2p-secio so use promisify in the interim where necessary, which is only in the tests.
1 parent 0e2eba8 commit 31331c2

27 files changed

+415
-584
lines changed

package.json

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,47 @@
4343
"homepage": "https://github.com/ipfs/js-ipfs-bitswap#readme",
4444
"devDependencies": {
4545
"@nodeutils/defaults-deep": "^1.1.0",
46-
"aegir": "^18.2.1",
46+
"aegir": "^20.3.1",
47+
"async": "^2.6.1",
48+
"async-iterator-all": "^1.0.0",
4749
"benchmark": "^2.1.4",
4850
"chai": "^4.2.0",
4951
"dirty-chai": "^2.0.1",
50-
"ipfs-repo": "~0.26.3",
51-
"libp2p": "~0.24.2",
52-
"libp2p-kad-dht": "~0.15.0",
53-
"libp2p-mplex": "~0.8.4",
52+
"ipfs-repo": "^0.27.1",
53+
"libp2p": "^0.26.1",
54+
"libp2p-kad-dht": "^0.16.0",
55+
"libp2p-mplex": "^0.8.0",
5456
"libp2p-secio": "~0.11.1",
55-
"libp2p-tcp": "~0.13.0",
56-
"lodash": "^4.17.11",
57+
"libp2p-tcp": "^0.13.0",
58+
"lodash.difference": "^4.5.0",
59+
"lodash.flatten": "^4.4.0",
5760
"lodash.range": "^3.2.0",
5861
"lodash.without": "^4.4.0",
5962
"ncp": "^2.0.0",
63+
"p-event": "^4.1.0",
6064
"peer-book": "~0.9.0",
61-
"peer-id": "~0.12.0",
62-
"peer-info": "~0.15.0",
63-
"rimraf": "^2.6.2",
65+
"peer-id": "^0.12.2",
66+
"peer-info": "~0.15.1",
67+
"promisify-es6": "^1.0.3",
68+
"rimraf": "^3.0.0",
6469
"safe-buffer": "^5.1.2",
6570
"stats-lite": "^2.2.0",
6671
"uuid": "^3.3.2"
6772
},
6873
"dependencies": {
69-
"async": "^2.6.1",
70-
"bignumber.js": "^8.0.1",
74+
"bignumber.js": "^9.0.0",
7175
"cids": "~0.7.0",
7276
"debug": "^4.1.0",
7377
"ipfs-block": "~0.8.0",
7478
"just-debounce-it": "^1.1.0",
7579
"lodash.isequalwith": "^4.4.0",
7680
"moving-average": "^1.0.0",
7781
"multicodec": "~0.5.0",
78-
"multihashing-async": "~0.5.1",
79-
"promisify-es6": "^1.0.3",
82+
"multihashing-async": "^0.8.0",
8083
"protons": "^1.0.1",
8184
"pull-length-prefixed": "^1.3.1",
8285
"pull-stream": "^3.6.9",
83-
"typical": "^4.0.0",
86+
"typical": "^5.1.0",
8487
"varint-decoder": "~0.1.1"
8588
},
8689
"pre-push": [

src/decision-engine/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ class DecisionEngine {
242242
return
243243
}
244244

245-
let cancels = []
246-
let wants = []
245+
const cancels = []
246+
const wants = []
247247
msg.wantlist.forEach((entry) => {
248248
if (entry.cancel) {
249249
ledger.cancelWant(entry.cid)

src/index.js

Lines changed: 14 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -64,38 +64,7 @@ class Bitswap {
6464
}
6565

6666
// handle messages received through the network
67-
// _receiveMessage (peerId, incoming, callback) {
6867
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-
// })
9968
try {
10069
await this.engine.messageReceived(peerId, incoming)
10170
} catch (err) {
@@ -128,17 +97,6 @@ class Bitswap {
12897
async _handleReceivedBlock (peerId, block, wasWanted) {
12998
this._log('received block')
13099

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)
142100
const has = await this.blockstore.has(block.cid)
143101
this._updateReceiveCounters(peerId.toB58String(), block, has)
144102
if (has || !wasWanted) {
@@ -175,23 +133,7 @@ class Bitswap {
175133
this._stats.disconnected(peerId)
176134
}
177135

178-
// _putBlock (block, callback) {
179136
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-
// })
195137
await this.blockstore.put(block)
196138

197139
this.notifications.hasBlock(block)
@@ -240,23 +182,9 @@ class Bitswap {
240182
* @param {function(Error, Block)} callback
241183
* @returns {void}
242184
*/
243-
// get (cid, callback) {
244185
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
260188
}
261189
}
262190

@@ -268,61 +196,11 @@ class Bitswap {
268196
* @param {function(Error, Blocks)} callback
269197
* @returns {void}
270198
*/
271-
getMany (cids) {
199+
async * getMany (cids) {
272200
let pendingStart = cids.length
273201
const wantList = []
274202
let promptedNetwork = false
275203

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)
326204
const fetchFromNetwork = async (cid) => {
327205
wantList.push(cid)
328206

@@ -334,17 +212,20 @@ class Bitswap {
334212

335213
const block = await blockP
336214
this.wm.cancelWants([cid])
215+
337216
return block
338217
}
339218

340-
return Promise.all(cids.map(async (cid) => {
219+
for (const cid of cids) {
341220
const has = await this.blockstore.has(cid)
342221
pendingStart--
343222
if (has) {
344223
if (!pendingStart) {
345224
this.wm.wantBlocks(wantList)
346225
}
347-
return this.blockstore.get(cid)
226+
yield this.blockstore.get(cid)
227+
228+
continue
348229
}
349230

350231
if (!promptedNetwork) {
@@ -353,8 +234,8 @@ class Bitswap {
353234
}
354235

355236
// we don't have the block locally so fetch it from the network
356-
return fetchFromNetwork(cid)
357-
}))
237+
yield fetchFromNetwork(cid)
238+
}
358239
}
359240

360241
// removes the given cids from the wantlist independent of any ref counts
@@ -383,22 +264,14 @@ class Bitswap {
383264
* @param {function(Error)} callback
384265
* @returns {void}
385266
*/
386-
// put (block, callback) {
387267
async put (block) {
388268
this._log('putting block')
389269

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)
400270
const has = await this.blockstore.has(block.cid)
401-
if (has) return
271+
272+
if (has) {
273+
return
274+
}
402275

403276
await this._putBlock(block)
404277
}
@@ -411,29 +284,7 @@ class Bitswap {
411284
* @param {function(Error)} callback
412285
* @returns {void}
413286
*/
414-
// putMany (blocks, callback) {
415287
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)
437288
const newBlocks = await Promise.all(blocks.map(async (b) => {
438289
return !(await this.blockstore.has(b.cid))
439290
})).filter(Boolean)

0 commit comments

Comments
 (0)