From 4f532a541dfd9a1ed73ba8eb68ae86e311caeeb3 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Tue, 10 Aug 2021 09:32:53 +0100 Subject: [PATCH] fix: do not write blocks we already have (#3801) We had this filter previously, it got removed somewhere. --- packages/ipfs-core/package.json | 1 + packages/ipfs-core/src/block-storage.js | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/ipfs-core/package.json b/packages/ipfs-core/package.json index d2575c3476..1993b5f66c 100644 --- a/packages/ipfs-core/package.json +++ b/packages/ipfs-core/package.json @@ -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", diff --git a/packages/ipfs-core/src/block-storage.js b/packages/ipfs-core/src/block-storage.js index d41a4357d6..90b7b45ce6 100644 --- a/packages/ipfs-core/src/block-storage.js +++ b/packages/ipfs-core/src/block-storage.js @@ -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 @@ -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 { @@ -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) } }