Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get skips bitfield check #559

Merged
merged 4 commits into from
Sep 6, 2024
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
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
28 changes: 14 additions & 14 deletions test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand Down
3 changes: 3 additions & 0 deletions test/replicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
Loading