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

fix: do not write blocks we already have #3801

Merged
merged 3 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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