Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: do not write blocks we already have (#3801)
Browse files Browse the repository at this point in the history
We had this filter previously, it got removed somewhere.
  • Loading branch information
achingbrain authored Aug 10, 2021
1 parent db302d0 commit 4f532a5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/ipfs-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"is-ipfs": "^6.0.1",
"it-all": "^1.0.4",
"it-drain": "^1.0.3",
"it-filter": "^1.0.2",
"it-first": "^1.0.4",
"it-last": "^1.0.4",
"it-map": "^1.0.4",
Expand Down
11 changes: 9 additions & 2 deletions packages/ipfs-core/src/block-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { BlockstoreAdapter } = require('interface-blockstore')
const merge = require('it-merge')
const pushable = require('it-pushable')
const filter = require('it-filter')

/**
* @typedef {import('interface-blockstore').Blockstore} Blockstore
Expand Down Expand Up @@ -55,6 +56,10 @@ class BlockStorage extends BlockstoreAdapter {
* @param {AbortOptions} [options]
*/
async put (cid, block, options = {}) {
if (await this.has(cid)) {
return
}

if (this.bitswap.isStarted()) {
await this.bitswap.put(cid, block, options)
} else {
Expand All @@ -69,10 +74,12 @@ class BlockStorage extends BlockstoreAdapter {
* @param {AbortOptions} [options]
*/
async * putMany (blocks, options = {}) {
const missingBlocks = filter(blocks, async ({ key }) => { return !(await this.has(key)) })

if (this.bitswap.isStarted()) {
yield * this.bitswap.putMany(blocks, options)
yield * this.bitswap.putMany(missingBlocks, options)
} else {
yield * this.child.putMany(blocks, options)
yield * this.child.putMany(missingBlocks, options)
}
}

Expand Down

0 comments on commit 4f532a5

Please sign in to comment.