From 74fcae0b7d69f4c5f20ce504fbcd8cb1a60b268c Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:03:18 +0530 Subject: [PATCH 1/3] fix: report core storage byte sizes from rocksdb key ranges --- lib/info.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/info.js b/lib/info.js index 029d2f2d9..f90d19607 100644 --- a/lib/info.js +++ b/lib/info.js @@ -24,14 +24,25 @@ module.exports = class Info { } static async storage(session) { - const { oplog, tree, blocks, bitfield } = session.core try { - return { - oplog: await Info.bytesUsed(oplog.storage), - tree: await Info.bytesUsed(tree.storage), - blocks: await Info.bytesUsed(blocks.storage), - bitfield: await Info.bytesUsed(bitfield.storage) - } + const storage = session.state && session.state.storage + const db = storage && storage.db + const ptr = storage.core && storage.core.dataPointer + if (!db || ptr == null || typeof db.approximateSize !== 'function') return null + + // Keys are lexicographically ordered per core data section, so the byte + // size of a section is the approximate size of its key range. The old + // per-file split is mapped onto the rocksdb sections: the oplog covered + // the core header, its dependencies and its hints. + const { core } = require('hypercore-storage/lib/keys.js') + const opts = { includeMemtables: true } + const [oplog, tree, blocks, bitfield] = await Promise.all([ + db.approximateSize(core.head(ptr), core.block(ptr, 0), opts), + db.approximateSize(core.tree(ptr, 0), core.bitfield(ptr, 0, 0), opts), + db.approximateSize(core.block(ptr, 0), core.tree(ptr, 0), opts), + db.approximateSize(core.bitfield(ptr, 0, 0), core.userData(ptr, ''), opts) + ]) + return { oplog, tree, blocks, bitfield } } catch { return null } From 23b716de3d8b8895e09d1b2bbbad93df065f3f42 Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:03:46 +0530 Subject: [PATCH 2/3] test: unskip and modernize the storage info test Re-enable the storage info test and add assertions to check storage sections. --- test/basic.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/basic.js b/test/basic.js index d55ada396..0565892d5 100644 --- a/test/basic.js +++ b/test/basic.js @@ -520,13 +520,20 @@ test('count named session', async function (t) { await core.close() }) -test.skip('storage info', async function (t) { +test('storage info', async function (t) { const core = await create(t) await core.append(['a', 'b', 'c', 'd', 'e', 'f']) const info = await core.info({ storage: true }) - t.snapshot(info.storage) + t.ok(info.storage !== null) + for (const section of ['oplog', 'tree', 'blocks', 'bitfield']) { + t.ok( + typeof info.storage[section] === 'number' && info.storage[section] >= 0, + section + ' reports a byte size' + ) + } + t.ok(info.storage.blocks > 0, 'blocks section holds the appended data') await core.close() }) From 4e3feb5b53d6c74af332732bc5ff465e27f38fa8 Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:29:23 +0530 Subject: [PATCH 3/3] test: assert storage info ranges measure the intended keys Every section must be nonzero after appends, an appended block must grow the blocks section, and a second core on the same database must measure less in every section (cross-core isolation). Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- test/basic.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/test/basic.js b/test/basic.js index 0565892d5..e15e6726b 100644 --- a/test/basic.js +++ b/test/basic.js @@ -2,6 +2,7 @@ const test = require('brittle') const b4a = require('b4a') const HypercoreStorage = require('hypercore-storage') const crypto = require('hypercore-crypto') +const safetyCatch = require('safety-catch') const Hypercore = require('../') const { create, createStorage, replicate, eventFlush } = require('./helpers') @@ -521,21 +522,41 @@ test('count named session', async function (t) { }) test('storage info', async function (t) { - const core = await create(t) + const db = await createStorage(t) + const core = new Hypercore(db) + await core.ready() + t.teardown(() => core.close().catch(safetyCatch), { order: 1 }) + await core.append(['a', 'b', 'c', 'd', 'e', 'f']) const info = await core.info({ storage: true }) t.ok(info.storage !== null) + // appending writes every section: oplog header entries, tree nodes, the + // blocks themselves and bitfield pages - none may report zero + for (const section of ['oplog', 'tree', 'blocks', 'bitfield']) { + t.ok(info.storage[section] > 0, section + ' measures its own keys') + } + + // a large block lands in the blocks range + await core.append(b4a.alloc(64 * 1024, 1)) + const grown = await core.info({ storage: true }) + t.ok(grown.storage.blocks > info.storage.blocks, 'an appended block grows the blocks section') + + // a second core on the same storage must not measure the first core's keys + const other = new Hypercore(db, crypto.keyPair().publicKey) + await other.ready() + t.teardown(() => other.close().catch(safetyCatch), { order: 1 }) + + const otherInfo = await other.info({ storage: true }) + t.ok(otherInfo.storage !== null) for (const section of ['oplog', 'tree', 'blocks', 'bitfield']) { - t.ok( - typeof info.storage[section] === 'number' && info.storage[section] >= 0, - section + ' reports a byte size' - ) + t.ok(otherInfo.storage[section] < grown.storage[section], section + " range excludes another core's keys") } - t.ok(info.storage.blocks > 0, 'blocks section holds the appended data') + await other.close() await core.close() + await db.close() }) test('storage info, off by default', async function (t) {