diff --git a/lib/replicator.js b/lib/replicator.js index 6be5dff4..70b3ffdd 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -1836,34 +1836,18 @@ module.exports = class Replicator { async _resolveBlocksLocally () { // TODO: check if fork compat etc. Requires that we pass down truncation info - let clear = null + const clear = [] + const blocks = [] const reader = this.core.storage.createReadBatch() - - const blocks = new Map() for (const b of this._blocks) { - if (this.core.bitfield.get(b.index) === false) continue - - const request = this.core.blocks.get(reader, b.index) - blocks.set(b, request) - - request.catch(safetyCatch) // safety catch in meantime + blocks.push(this._resolveLocalBlock(b, reader, clear)) } - reader.tryFlush() - for (const [b, request] of blocks) { - try { - b.resolve(await request) - } catch (err) { - b.reject(err) - } - - if (clear === null) clear = [] - clear.push(b) - } + await Promise.all(blocks) - if (clear === null) return + if (!clear.length) return // Currently the block tracker does not support deletes during iteration, so we make // sure to clear them afterwards. @@ -1872,6 +1856,19 @@ module.exports = class Replicator { } } + async _resolveLocalBlock (b, reader, resolved) { + if (this.core.bitfield.get(b.index) === false) return + + try { + b.resolve(await this.core.blocks.get(reader, b.index)) + } catch (err) { + b.reject(err) + return + } + + resolved.push(b) + } + _resolveBlockRequest (tracker, index, value, req) { const b = tracker.remove(index) if (b === null) return false