Skip to content

Commit

Permalink
dont use storage helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Sep 23, 2024
1 parent cb077a8 commit d9de824
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
9 changes: 8 additions & 1 deletion lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ module.exports = class Core {
const treeLength = length === undefined ? this.tree.length : length

const storage = await this.storage.registerBatch(name, treeLength, overwrite)
const treeInfo = await storage.getCoreHead()
const treeInfo = await getCoreHead(storage)
const bitfield = await Bitfield.open(storage)

bitfield.merge(this.bitfield, treeLength)
Expand Down Expand Up @@ -1220,3 +1220,10 @@ function minimumSegmentEnd (start, src, dst) {
if (b === -1) return a
return a < b ? a : b
}

function getCoreHead (storage) {
const b = storage.createReadBatch()
const p = b.getCoreHead()
b.tryFlush()
return p
}
9 changes: 8 additions & 1 deletion lib/merkle-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ module.exports = class MerkleTree {

let cnt = 0
// TODO: we could prop use a read batch here and do this in blocks of X for perf
while (!ite.contains(head) && !(await this.storage.hasTreeNode(ite.index, false))) {
while (!ite.contains(head) && !(await hasTreeNode(this.storage, ite.index))) {
cnt++
ite.parent()
}
Expand Down Expand Up @@ -1285,6 +1285,13 @@ function getTreeNode (storage, index, error) {
return node
}

function hasTreeNode (storage, index) {
const batch = storage.createReadBatch()
const has = batch.hasTreeNode(index)
batch.tryFlush()
return has
}

async function settleProof (p) {
const result = [
p.node && Promise.all(p.node),
Expand Down
36 changes: 25 additions & 11 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,30 @@ test('core - user data', async function (t) {
const { core, reopen } = await create(t)

await setUserData(core, 'hello', b4a.from('world'))
t.alike(await core.storage.getUserData('hello'), b4a.from('world'))
t.alike(await getUserData(core.storage, 'hello'), b4a.from('world'))

await setUserData(core, 'hej', b4a.from('verden'))
t.alike(await core.storage.getUserData('hello'), b4a.from('world'))
t.alike(await core.storage.getUserData('hej'), b4a.from('verden'))
t.alike(await getUserData(core.storage, 'hello'), b4a.from('world'))
t.alike(await getUserData(core.storage, 'hej'), b4a.from('verden'))

await setUserData(core, 'hello', null)
t.alike(await core.storage.getUserData('hello'), null)
t.alike(await core.storage.getUserData('hej'), b4a.from('verden'))
t.alike(await getUserData(core.storage, 'hello'), null)
t.alike(await getUserData(core.storage, 'hej'), b4a.from('verden'))

await setUserData(core, 'hej', b4a.from('world'))
t.alike(await core.storage.getUserData('hej'), b4a.from('world'))
t.alike(await getUserData(core.storage, 'hej'), b4a.from('world'))

// check that it was persisted
const coreReopen = await reopen()

t.alike(await coreReopen.storage.getUserData('hej'), b4a.from('world'))
t.alike(await getUserData(coreReopen.storage, 'hej'), b4a.from('world'))

function getUserData (storage, key) {
const b = storage.createReadBatch()
const p = b.getUserData(key)
b.tryFlush()
return p
}
})

test('core - header does not retain slabs', async function (t) {
Expand Down Expand Up @@ -165,8 +172,8 @@ test('core - verify', async function (t) {
await clone.verify(p)
}

const tree1 = await core.storage.getCoreHead()
const tree2 = await clone.storage.getCoreHead()
const tree1 = await getCoreHead(core.storage)
const tree2 = await getCoreHead(clone.storage)

t.is(tree1.length, 2)
t.alike(tree1.signature, tree2.signature)
Expand Down Expand Up @@ -196,8 +203,8 @@ test('core - verify parallel upgrades', async function (t) {
await v2
}

const tree1 = await core.storage.getCoreHead()
const tree2 = await clone.storage.getCoreHead()
const tree1 = await getCoreHead(core.storage)
const tree2 = await getCoreHead(clone.storage)

t.is(tree2.length, tree1.length)
t.alike(tree2.signature, tree1.signature)
Expand Down Expand Up @@ -652,3 +659,10 @@ async function getProof (core, req) {
if (block) proof.block.value = await block
return proof
}

function getCoreHead (storage) {
const b = storage.createReadBatch()
const p = b.getCoreHead()
b.tryFlush()
return p
}
11 changes: 9 additions & 2 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Hypercore = require('../../')
const createTempDir = require('test-tmp')
const CoreStorage = require('hypercore-on-the-rocks')
const safetyCatch = require('safety-catch')

exports.create = async function (t, ...args) {
const dir = await createTempDir(t)
Expand Down Expand Up @@ -38,8 +39,14 @@ exports.replicate = function replicate (a, b, t, opts = {}) {
const closed1 = new Promise(resolve => s1.once('close', resolve))
const closed2 = new Promise(resolve => s2.once('close', resolve))

s1.on('error', err => t.comment(`replication stream error (initiator): ${err}`))
s2.on('error', err => t.comment(`replication stream error (responder): ${err}`))
s1.on('error', err => {
safetyCatch(err)
t.comment(`replication stream error (initiator): ${err}`)
})
s2.on('error', err => {
safetyCatch(err)
t.comment(`replication stream error (responder): ${err}`)
})

if (opts.teardown !== false) {
t.teardown(async function () {
Expand Down

0 comments on commit d9de824

Please sign in to comment.