Skip to content
Closed
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
13 changes: 10 additions & 3 deletions lib/fully-remote-proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ async function verify(storage, buffer, { referrer = null } = {}) {
let roots = await Promise.all(rootPromises)
if (roots.some(isNull)) roots = []

const length = head ? head.length : 0

if (!auth.manifest || !auth.manifest.signers.length) return null

let batch = null
Expand Down Expand Up @@ -102,7 +100,16 @@ async function verify(storage, buffer, { referrer = null } = {}) {

result.key = auth.key
result.discoveryKey = discoveryKey
result.newer = batch.length > length

// Storage may have advanced while the proof was read and verified (e.g. a
// concurrent notification applied it), so compare against the freshest head
// instead of the possibly stale one read above.
rx = core.read()
const freshHeadPromise = rx.getHead()
rx.tryFlush()
const freshHead = await freshHeadPromise

result.newer = batch.length > (freshHead ? freshHead.length : 0)
result.length = batch.length
result.block = proof.block

Expand Down
51 changes: 50 additions & 1 deletion test/fully-remote-proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const c = require('compact-encoding')
const remote = require('../lib/fully-remote-proof.js')
const messages = require('../lib/messages.js')

const { create } = require('./helpers')
const { create, replicate } = require('./helpers')

test('fully remote proof - proof and verify', async function (t) {
const core = await create(t)
Expand Down Expand Up @@ -60,3 +60,52 @@ test('fully remote proof - block, hash or seek not covered by the upgrade', asyn
const p = await remote.verify(store, await remote.proof(core, opts))
t.alike(p.block.value, b4a.from('d'), 'block past the upgrade still verifies')
})

// Simulates the race from #791: the first head read inside verify() returns a
// stale head, as if a concurrent notification advanced storage right after it
// was read.
function wrapStoreWithStaleHead(store, staleLength) {
return new Proxy(store, {
get(target, prop, receiver) {
if (prop !== 'resumeCore') return Reflect.get(target, prop, receiver)
return async function (discoveryKey) {
const core = await target.resumeCore(discoveryKey)
if (core === null) return null
let first = true
const read = core.read.bind(core)
core.read = function (...args) {
const rx = read(...args)
if (first) {
first = false
const getHead = rx.getHead.bind(rx)
rx.getHead = async function (...headArgs) {
const head = await getHead(...headArgs)
if (head === null) return null
return { ...head, length: staleLength }
}
}
return rx
}
return core
}
}
})
}

test('fully remote proof - newer uses freshest head', async function (t) {
const a = await create(t)
await a.append(['a', 'b', 'c'])

const b = await create(t, a.key)
replicate(a, b, t)
await b.get(2)

const proof = await remote.proof(a)

const store = wrapStoreWithStaleHead(b.state.storage.store, 2)
const p = await remote.verify(store, proof)

t.ok(p !== null, 'proof verifies')
t.is(p.length, 3, 'proof length reported')
t.is(p.newer, false, 'storage already has the proof length, so it is not newer')
})