diff --git a/index.js b/index.js index 1c0f865f..58a43bc8 100644 --- a/index.js +++ b/index.js @@ -887,16 +887,16 @@ module.exports = class Hypercore extends EventEmitter { } async _get (index, opts) { - let block - if (this.core.isFlushing) await this.core.flushed() - if (this.state.bitfield.get(index)) { - const reader = this.state.storage.createReadBatch() - block = this.core.blocks.get(reader, index) + const reader = this.state.storage.createReadBatch() + const promise = reader.getBlock(index) + reader.tryFlush() + + let block = await promise - if (this.cache) this.cache.set(index, block) - await reader.flush() + if (block !== null) { + if (this.cache) this.cache.set(index, Promise.resolve(block)) } else { if (!this._shouldWait(opts, this.wait)) return null diff --git a/lib/core.js b/lib/core.js index 7c37ecf4..d025e789 100644 --- a/lib/core.js +++ b/lib/core.js @@ -103,6 +103,7 @@ class Update { } this.bitfield.setRange(batch.ancestors, batch.treeLength, false) + this.batch.deleteBlockRange(bitfield.start, bitfield.start + bitfield.length) const status = (batch.length > batch.ancestors) ? 0b0011 : 0b0010 @@ -269,7 +270,7 @@ class SessionState { update.bitfield.setRange(start, end, false) - start = this.bitfield.firstSet(start + 1) + end = this.bitfield.firstSet(end) // TODO: verify this: // start = state.bitfield.lastSet(start) + 1 @@ -279,7 +280,6 @@ class SessionState { if (start === -1 || start >= this.tree.length) return this.blocks.clear(update.batch, start, end - start) - update.coreUpdate({ status: 0, bitfield, value: null, from: null }) if (start < this.treeLength) this.treeLength = start diff --git a/test/cache.js b/test/cache.js index ab9cec2b..da12a575 100644 --- a/test/cache.js +++ b/test/cache.js @@ -8,10 +8,10 @@ test('cache', async function (t) { const a = await create(t, { cache: true }) await a.append(['a', 'b', 'c']) - const p = a.get(0) - const q = a.get(0) + const p = await a.get(0) + const q = await a.get(0) - t.is(await p, await q, 'blocks are identical') + t.is(p, q, 'blocks are identical') }) test('session cache inheritance', async function (t) { @@ -20,10 +20,10 @@ test('session cache inheritance', async function (t) { const s = a.session() - const p = a.get(0) - const q = s.get(0) + const p = await a.get(0) + const q = await s.get(0) - t.is(await p, await q, 'blocks are identical') + t.is(p, q, 'blocks are identical') await s.close() }) @@ -34,10 +34,10 @@ test('session cache opt-out', async function (t) { const s = a.session({ cache: false }) - const p = a.get(0) - const q = s.get(0) + const p = await a.get(0) + const q = await s.get(0) - t.not(await p, await q, 'blocks are not identical') + t.not(p, q, 'blocks are not identical') await s.close() }) @@ -48,12 +48,12 @@ test('session cache override', async function (t) { const s = a.session({ cache: new Xache({ maxSize: 64, maxAge: 0 }) }) - const p = a.get(0) - const q = s.get(0) - const r = s.get(0) + const p = await a.get(0) + const q = await s.get(0) + const r = await s.get(0) - t.not(await p, await q, 'blocks are not identical') - t.is(await q, await r, 'blocks are identical') + t.not(p, q, 'blocks are not identical') + t.is(q, r, 'blocks are identical') await s.close() }) diff --git a/test/replicate.js b/test/replicate.js index 7c6036ad..cedaa013 100644 --- a/test/replicate.js +++ b/test/replicate.js @@ -650,6 +650,9 @@ test('request cancellation regression', async function (t) { b.get(1).catch(onerror) b.get(2).catch(onerror) + // have to wait for the storage lookup here, TODO: add a flush sort of api for testing this + await new Promise(resolve => setTimeout(resolve, 500)) + // No explict api to trigger this (maybe we add a cancel signal / abort controller?) but cancel get(1) b.activeRequests[1].context.detach(b.activeRequests[1])