Skip to content

Commit 871aa4d

Browse files
committed
chore: address pr comments
1 parent 2dae35f commit 871aa4d

File tree

6 files changed

+33
-83
lines changed

6 files changed

+33
-83
lines changed

src/index.js

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const statsKeys = [
3030
*
3131
* @param {Libp2p} libp2p
3232
* @param {Blockstore} blockstore
33+
* @param {Object} options
3334
*/
3435
class Bitswap {
3536
constructor (libp2p, blockstore, options) {
@@ -93,17 +94,17 @@ class Bitswap {
9394
}))
9495
}
9596

96-
// _handleReceivedBlock (peerId, block, wasWanted, callback) {
9797
async _handleReceivedBlock (peerId, block, wasWanted) {
9898
this._log('received block')
9999

100100
const has = await this.blockstore.has(block.cid)
101101
this._updateReceiveCounters(peerId.toB58String(), block, has)
102-
if (has || !wasWanted) {
102+
103+
if (!wasWanted) {
103104
return
104105
}
105106

106-
await this._putBlock(block)
107+
await this.put(block)
107108
}
108109

109110
_updateReceiveCounters (peerId, block, exists) {
@@ -133,23 +134,16 @@ class Bitswap {
133134
this._stats.disconnected(peerId)
134135
}
135136

136-
async _putBlock (block) {
137-
await this.blockstore.put(block)
138-
139-
this.notifications.hasBlock(block)
140-
141-
// Note: Don't wait for provide to finish before returning
142-
this.network.provide(block.cid).catch((err) => {
143-
this._log.error('Failed to provide: %s', err.message)
144-
})
145-
146-
this.engine.receivedBlocks([block.cid])
147-
}
148-
137+
/**
138+
* @returns {void}
139+
*/
149140
enableStats () {
150141
this._stats.enable()
151142
}
152143

144+
/**
145+
* @returns {void}
146+
*/
153147
disableStats () {
154148
this._stats.disable()
155149
}
@@ -158,7 +152,7 @@ class Bitswap {
158152
* Return the current wantlist for a given `peerId`
159153
*
160154
* @param {PeerId} peerId
161-
* @returns {Wantlist}
155+
* @returns {Map}
162156
*/
163157
wantlistForPeer (peerId) {
164158
return this.engine.wantlistForPeer(peerId)
@@ -168,7 +162,7 @@ class Bitswap {
168162
* Return ledger information for a given `peerId`
169163
*
170164
* @param {PeerId} peerId
171-
* @returns {?Object}
165+
* @returns {Object}
172166
*/
173167
ledgerForPeer (peerId) {
174168
return this.engine.ledgerForPeer(peerId)
@@ -179,8 +173,7 @@ class Bitswap {
179173
* blockstore it is returned, otherwise the block is added to the wantlist and returned once another node sends it to us.
180174
*
181175
* @param {CID} cid
182-
* @param {function(Error, Block)} callback
183-
* @returns {void}
176+
* @returns {Promise<Block>}
184177
*/
185178
async get (cid) {
186179
for await (const block of this.getMany([cid])) {
@@ -192,9 +185,8 @@ class Bitswap {
192185
* Fetch a a list of blocks by cid. If the blocks are in the local
193186
* blockstore they are returned, otherwise the blocks are added to the wantlist and returned once another node sends them to us.
194187
*
195-
* @param {Array<CID>} cids
196-
* @param {function(Error, Blocks)} callback
197-
* @returns {void}
188+
* @param {Iterable<CID>} cids
189+
* @returns {Promise<AsyncIterator<Block>>}
198190
*/
199191
async * getMany (cids) {
200192
let pendingStart = cids.length
@@ -238,7 +230,12 @@ class Bitswap {
238230
}
239231
}
240232

241-
// removes the given cids from the wantlist independent of any ref counts
233+
/**
234+
* Removes the given CIDs from the wantlist independent of any ref counts
235+
*
236+
* @param {Iterable<CID>} cids
237+
* @returns {void}
238+
*/
242239
unwant (cids) {
243240
if (!Array.isArray(cids)) {
244241
cids = [cids]
@@ -248,7 +245,12 @@ class Bitswap {
248245
cids.forEach((cid) => this.notifications.unwantBlock(cid))
249246
}
250247

251-
// removes the given keys from the want list
248+
/**
249+
* Removes the given keys from the want list
250+
*
251+
* @param {Iterable<CID>} cids
252+
* @returns {void}
253+
*/
252254
cancelWants (cids) {
253255
if (!Array.isArray(cids)) {
254256
cids = [cids]
@@ -261,26 +263,18 @@ class Bitswap {
261263
* send it to nodes that have it in their wantlist.
262264
*
263265
* @param {Block} block
264-
* @param {function(Error)} callback
265-
* @returns {void}
266+
* @returns {Promise<void>}
266267
*/
267268
async put (block) { // eslint-disable-line require-await
268-
if (!Array.isArray(block)) {
269-
block = [
270-
block
271-
]
272-
}
273-
274-
return this.putMany(block)
269+
return this.putMany([block])
275270
}
276271

277272
/**
278273
* Put the given blocks to the underlying blockstore and
279274
* send it to nodes that have it them their wantlist.
280275
*
281-
* @param {AsyncIterable<Block>} blocks
282-
* @param {function(Error)} callback
283-
* @returns {void}
276+
* @param {AsyncIterable<Block>|Iterable<Block>} blocks
277+
* @returns {Promise<void>}
284278
*/
285279
async putMany (blocks) { // eslint-disable-line require-await
286280
const self = this
@@ -315,7 +309,7 @@ class Bitswap {
315309
/**
316310
* Get the current list of partners.
317311
*
318-
* @returns {Array<PeerId>}
312+
* @returns {Iterator<PeerId>}
319313
*/
320314
peers () {
321315
return this.engine.peers()

src/network.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ class Network {
124124

125125
// Connect to the given peer
126126
// Send the given msg (instance of Message) to the given peer
127-
// sendMessage (peer, msg, callback) {
128127
async sendMessage (peer, msg) {
129-
// if (!this._running) { return callback(new Error(`network isn't running`)) }
130128
if (!this._running) throw new Error('network isn\'t running')
131129

132130
const stringId = peer.toB58String() ? peer.toB58String() : peer.id.toB58String()

src/types/message/entry.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
'use strict'
22

33
const WantlistEntry = require('../wantlist').Entry
4-
const CID = require('cids')
5-
const assert = require('assert')
64

75
module.exports = class BitswapMessageEntry {
86
constructor (cid, priority, cancel) {
9-
assert(CID.isCID(cid), 'needs valid cid')
107
this.entry = new WantlistEntry(cid, priority)
118
this.cancel = Boolean(cancel)
129
}

src/types/message/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const protons = require('protons')
44
const Block = require('ipfs-block')
55
const isEqualWith = require('lodash.isequalwith')
6-
const assert = require('assert')
76
const CID = require('cids')
87
const codecName = require('multicodec/src/name-table')
98
const vd = require('varint-decoder')
@@ -25,7 +24,6 @@ class BitswapMessage {
2524
}
2625

2726
addEntry (cid, priority, cancel) {
28-
assert(cid && CID.isCID(cid), 'must be a valid cid')
2927
const cidStr = cid.toString('base58btc')
3028

3129
const entry = this.wantlist.get(cidStr)
@@ -39,13 +37,11 @@ class BitswapMessage {
3937
}
4038

4139
addBlock (block) {
42-
assert(Block.isBlock(block), 'must be a valid block')
4340
const cidStr = block.cid.toString('base58btc')
4441
this.blocks.set(cidStr, block)
4542
}
4643

4744
cancel (cid) {
48-
assert(CID.isCID(cid), 'must be a valid cid')
4945
const cidStr = cid.toString('base58btc')
5046
this.wantlist.delete(cidStr)
5147
this.addEntry(cid, 0, true)

src/types/wantlist/entry.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
'use strict'
22

3-
const assert = require('assert')
4-
const CID = require('cids')
5-
63
class WantListEntry {
74
constructor (cid, priority) {
8-
assert(CID.isCID(cid), 'must be valid CID')
9-
105
// Keep track of how many requests we have for this key
116
this._refCounter = 1
127

src/utils.js

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -80,41 +80,11 @@ const sortBy = (fn, list) => {
8080
})
8181
}
8282

83-
const first = async (iterator) => {
84-
for await (const value of iterator) {
85-
return value
86-
}
87-
}
88-
89-
const last = async (iterator) => {
90-
let value
91-
for await (value of iterator) {
92-
// Intentionally empty
93-
}
94-
return value
95-
}
96-
97-
const all = async (iterator) => {
98-
const values = []
99-
for await (const value of iterator) {
100-
values.push(value)
101-
}
102-
return values
103-
}
104-
105-
const extendIterator = (iterator) => {
106-
iterator.first = () => first(iterator)
107-
iterator.last = () => last(iterator)
108-
iterator.all = () => all(iterator)
109-
return iterator
110-
}
111-
11283
module.exports = {
11384
logger,
11485
includesWith,
11586
uniqWith,
11687
groupBy,
11788
pullAllWith,
118-
sortBy,
119-
extendIterator
89+
sortBy
12090
}

0 commit comments

Comments
 (0)