Skip to content
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
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const RocksDB = require('rocksdb-native')
const b4a = require('b4a')
const rrp = require('resolve-reject-promise')
const ScopeLock = require('scope-lock')
const DeviceFile = require('device-file')
Expand Down Expand Up @@ -589,9 +590,20 @@ class CorestoreStorage {
// no core stored here
if (!auth) return

const corestoreRx = new CorestoreRX(this.db, EMPTY)
const recordPromise = corestoreRx.getCore(auth.discoveryKey)
corestoreRx.tryFlush()
const record = await recordPromise

// only drop the alias if it still points at this core
const aliased = record && record.alias ? await this.getAlias(record.alias) : null

const tx = this.db.write({ autoDestroy: true })

tx.tryDelete(store.core(auth.discoveryKey))
if (aliased && b4a.equals(aliased, auth.discoveryKey)) {
tx.tryDelete(store.coreByAlias(record.alias))
}

// clear core
const start = core.core(ptr.corePointer)
Expand Down Expand Up @@ -856,6 +868,20 @@ class CorestoreStorage {
return discoveryKeyPromise
}

// only deletes the alias if it still points at discoveryKey
async deleteAlias(alias, discoveryKey) {
if (this.version === 0) await this._migrateStore()

const current = await this.getAlias(alias)

if (current === null || !b4a.equals(current, discoveryKey)) return false

const tx = this.db.write({ autoDestroy: true })
tx.tryDelete(store.coreByAlias(alias))
await tx.flush()
return true
}

async getSeed() {
if (this.version === 0) await this._migrateStore()

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

// Fixtures
await import('./migration.js')
await import('./delete.js')

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

const { create, writeBlocks, readBlocks, toArray } = require('./helpers')

test('deleteCore - removes the core, its blocks and its alias', async function (t) {
const s = await create(t)
t.teardown(() => s.close())

const namespace = b4a.alloc(32, 1)
const alias = { name: 'bee', namespace }
const discoveryKey = crypto.randomBytes(32)

const core = await s.createCore({ key: crypto.randomBytes(32), discoveryKey, alias })
await writeBlocks(core, 5)

t.alike(await s.getAlias(alias), discoveryKey, 'alias resolves before delete')

await s.deleteCore(core.core)
await core.close()

t.is(await s.hasCore(discoveryKey), false, 'core record is gone')
t.is(await s.getAlias(alias), null, 'alias is gone')
t.alike(await toArray(s.createDiscoveryKeyStream(namespace)), [], 'namespace listing is empty')
t.alike(await toArray(s.createDiscoveryKeyStream()), [], 'store listing is empty')
})

test('deleteCore - a deleted alias can be minted again', async function (t) {
const s = await create(t)
t.teardown(() => s.close())

const namespace = b4a.alloc(32, 2)
const alias = { name: 'bee', namespace }

const first = await s.createCore({
key: crypto.randomBytes(32),
discoveryKey: crypto.randomBytes(32),
alias
})
await writeBlocks(first, 3)
await s.deleteCore(first.core)
await first.close()

const discoveryKey = crypto.randomBytes(32)
const second = await s.createCore({ key: crypto.randomBytes(32), discoveryKey, alias })
await writeBlocks(second, 2)

t.alike(await s.getAlias(alias), discoveryKey, 'alias points at the new core')
t.alike(await toArray(s.createDiscoveryKeyStream(namespace)), [discoveryKey])
t.is((await readBlocks(second, 2)).length, 2)

await second.close()
})

test('deleteCore - other aliases in the namespace survive', async function (t) {
const s = await create(t)
t.teardown(() => s.close())

const namespace = b4a.alloc(32, 3)
const cores = []

for (let i = 0; i < 3; i++) {
const discoveryKey = crypto.randomBytes(32)
const core = await s.createCore({
key: crypto.randomBytes(32),
discoveryKey,
alias: { name: 'core-' + i, namespace }
})
cores.push({ core, discoveryKey })
}

await s.deleteCore(cores[1].core.core)
for (const { core } of cores) await core.close()

t.is(await s.getAlias({ name: 'core-1', namespace }), null)
t.alike(await s.getAlias({ name: 'core-0', namespace }), cores[0].discoveryKey)
t.alike(await s.getAlias({ name: 'core-2', namespace }), cores[2].discoveryKey)
t.is((await toArray(s.createDiscoveryKeyStream(namespace))).length, 2)
})

test('deleteAlias - drops the alias and leaves the core alone', async function (t) {
const s = await create(t)
const namespace = b4a.alloc(32, 7)
const alias = { name: 'bee', namespace }
const discoveryKey = b4a.alloc(32, 1)

const core = await s.createCore({ key: crypto.randomBytes(32), discoveryKey, alias })
await writeBlocks(core, 2)
await core.close()

t.is(
await s.deleteAlias(alias, b4a.alloc(32, 9)),
false,
'a mismatching discovery key is refused'
)
t.alike(await s.getAlias(alias), discoveryKey, 'alias untouched')

t.is(await s.deleteAlias(alias, discoveryKey), true)

t.is(await s.getAlias(alias), null, 'alias is gone')
t.is(await s.hasCore(discoveryKey), true, 'core record survives')
t.alike(await toArray(s.createDiscoveryKeyStream()), [discoveryKey], 'core still listed')

t.is(await s.deleteAlias(alias, discoveryKey), false, 'deleting a missing alias is a noop')

await s.close()
})
Loading