Skip to content

Commit

Permalink
chore: use blockstore.put instead of putMany
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Mar 5, 2020
1 parent f748a1d commit 2d8c892
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
16 changes: 7 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,15 @@ class Bitswap {
async putMany (blocks) { // eslint-disable-line require-await
const self = this

return this.blockstore.putMany(async function * () {
for await (const block of blocks) {
if (await self.blockstore.has(block.cid)) {
continue
}
for await (const block of blocks) {
if (await self.blockstore.has(block.cid)) {
continue
}

yield block
await this.blockstore.put(block)

self._sendHaveBlockNotifications(block)
}
}())
self._sendHaveBlockNotifications(block)
}
}

/**
Expand Down
10 changes: 4 additions & 6 deletions test/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,15 @@ describe('bitswap without DHT', function () {
// slow blockstore
nodes[0].bitswap.blockstore = {
has: sinon.stub().withArgs(block.cid).returns(false),
putMany: async function * (source) { // eslint-disable-line require-await
yield * source
}
put: sinon.stub()
}

// add the block to our want list
const wantBlockPromise1 = nodes[0].bitswap.get(block.cid)

// oh look, a peer has sent it to us - this will trigger a `blockstore.putMany` which
// for our purposes is a batch operation so `self.blockstore.has(cid)` will still return
// false even though we've just yielded a block with that cid
// oh look, a peer has sent it to us - this will trigger a `blockstore.put` which
// is an async operation so `self.blockstore.has(cid)` will still return false
// until the write has completed
await nodes[0].bitswap._receiveMessage(peerId, message)

// block store did not have it
Expand Down

0 comments on commit 2d8c892

Please sign in to comment.