Skip to content
Open
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: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,7 @@ class Corestore extends ReadyResource {
if (opts.preload) opts = { ...opts, ...(await opts.preload) }
if (this.opened === false) await this.ready()

const discoveryKey = opts.name
? await this.storage.getAlias({ name: opts.name, namespace: this.ns })
: null
const discoveryKey = opts.name ? await this._getAlias(opts.name) : null
this._maybeClosed()

const core = this._openCore(discoveryKey, opts)
Expand All @@ -648,6 +646,16 @@ class Corestore extends ReadyResource {
}
}

async _getAlias(name) {
const alias = { name, namespace: this.ns }
const discoveryKey = await this.storage.getAlias(alias)
if (discoveryKey === null) return null
if (this.cores.get(toHex(discoveryKey)) !== null) return discoveryKey
if (await this.storage.hasCore(discoveryKey)) return discoveryKey

throw STORAGE_EMPTY('Alias ' + name + ' points at a core that no longer exists', discoveryKey)
}

_auth(discoveryKey, opts) {
const result = {
keyPair: null,
Expand Down
9 changes: 9 additions & 0 deletions lib/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ module.exports = async function* audit(store, { dryRun = false } = {}) {
// ignore if failed, we are auditing...
}
}

// an alias whose core is gone makes get({ name }) fail until it is dropped
for await (const { alias, discoveryKey } of store.storage.createAliasStream(null)) {
const dangling = !(await store.storage.hasCore(discoveryKey))
const pruned =
dangling && !dryRun ? await store.storage.deleteAlias(alias, discoveryKey) : false

yield { discoveryKey, alias, dangling, pruned }
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting the dangling alias in audit is great! Also love the dryRun usage.

Should the yielded value be inconsistent though? I think it could be refactored to be a composite of different types of audits via a type field and maybe an audit so it matches the audit from the first type above, and then return alias, dangling and pruned together as the audit. This could then allow the type of audits to expand over time, but allow discriminating based on type.

Aka the return type would be something like:

{
  discoveryKey: Buffer,
  key: Buffer,
  type: 'core-audit',
  audit: ReturnType<typeof core.audit>
} | {
  discoveryKey: Buffer,
  key: Buffer,
  type: 'alias-audit',
  audit: { dangling: Boolean, alias: Buffer, pruned: Boolean }
}

aka a tagged union type.

Currently code that assumes audit existing will fail.

}
87 changes: 87 additions & 0 deletions test/alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const test = require('brittle')
const crypto = require('hypercore-crypto')

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

test('alias - a dangling alias fails the open instead of resolving to nothing', async function (t) {
const store = await create(t)

const fine = store.get({ name: 'fine' })
await fine.append('ok')
await fine.close()

// an alias row whose core is gone, as left behind by storages that deleted
// cores without their alias
const dangling = crypto.discoveryKey(crypto.keyPair().publicKey)
const getAlias = store.storage.getAlias.bind(store.storage)
store.storage.getAlias = (alias) => (alias.name === 'bee' ? dangling : getAlias(alias))
Comment on lines +13 to +17

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather injectDanglingAlias() this instead of monkey patching it to return a made up key.


const bee = store.get({ name: 'bee' })
const err = await bee.ready().then(
() => null,
(err) => err
)
t.ok(err, 'the open fails')
t.is(err.code, 'STORAGE_EMPTY')
t.ok(/bee/.test(err.message), 'names the alias: ' + err.message)
t.is(
await store.storage.getAlias({ name: 'bee', namespace: store.ns }),
dangling,
'the alias is left alone'
)

const again = store.get({ name: 'fine' })
await again.ready()
t.is(again.length, 1, 'other names still open')
await again.close()

await store.close()
})

// a dangling row as left behind by storages that deleted cores without their alias
function injectDanglingAlias(store, name) {
const discoveryKey = crypto.discoveryKey(crypto.keyPair().publicKey)
const createAliasStream = store.storage.createAliasStream.bind(store.storage)
store.storage.createAliasStream = async function* (namespace) {
yield* createAliasStream(namespace)
yield { alias: { name, namespace: store.ns }, discoveryKey }
}
return discoveryKey
}
Comment on lines +42 to +50

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should add an actual dangling alias instead of monkey patching to pretend there is one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Helpers should be at the bottom of the file if not nested in the test case. If it doesn't get used above and it's only used below, maybe it should be nested in the test case? Otherwise the bottom of the file is preferable.


test('alias - audit reports every alias and flags the dangling ones', async function (t) {
const store = await create(t)

const fine = store.get({ name: 'fine' })
await fine.append('ok')
await fine.close()

injectDanglingAlias(store, 'ghost')
const deleted = []
store.storage.deleteAlias = (alias) => {
deleted.push(alias.name)
return Promise.resolve(true)
}
Comment on lines +61 to +64

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't, think this needs monkey patching. It can list the aliases afterwards and verify they where deleted instead.


const seen = []
for await (const entry of store.audit({ dryRun: true })) {
if (entry.alias) seen.push([entry.alias.name, entry.dangling, entry.pruned])
}
t.alike(seen, [
['fine', false, false],
['ghost', true, false]
])
t.alike(deleted, [], 'a dry run prunes nothing')

const pruned = []
for await (const entry of store.audit()) {
if (entry.alias) pruned.push([entry.alias.name, entry.dangling, entry.pruned])
}
t.alike(pruned, [
['fine', false, false],
['ghost', true, true]
])
t.alike(deleted, ['ghost'], 'the dangling alias is pruned')

await store.close()
})
1 change: 1 addition & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ async function runTests() {

test.pause()

await import('./alias.js')
await import('./basic.js')
await import('./groups.js')
await import('./notify.js')
Expand Down
9 changes: 8 additions & 1 deletion test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,21 @@ test('audit', async function (t) {
}

let n = 0
for await (const { audit } of store.audit()) {
let aliases = 0
for await (const { audit, alias, dangling } of store.audit()) {
if (alias) {
aliases++
if (dangling) t.fail('dangling alias')
continue
}
n++
if (audit.droppedBits || audit.droppedBlocks || audit.droppedTreeNodes || audit.corrupt) {
t.fail('bad core')
}
}

t.is(n, 4)
t.is(aliases, 2, 'one alias per named core')

await a.close()
await b.close()
Expand Down
Loading