From b20ddcb4794514ac1dd2762a8e2777a571223ac6 Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:00:32 +0530 Subject: [PATCH 1/3] Count the blocks actually cleared when clear() is called with diff The diff option returned a stub { blocks: 0 }. Count blocks that had data in the cleared range: from the bitfield on the default session, and from storage reads on dependency-backed sessions. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- lib/session-state.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/session-state.js b/lib/session-state.js index f5bf31f1..dd2b6d1f 100644 --- a/lib/session-state.js +++ b/lib/session-state.js @@ -616,6 +616,28 @@ class SessionState { } } + if (cleared !== null) { + // Count the blocks that actually had data, so the caller can tell how + // much was cleared. + const clearEnd = Math.min(end, this.length) + + if (this.isDefault()) { + for (let i = start; i < clearEnd; i++) { + if (this.core.bitfield.get(i)) cleared.blocks++ + } + } else { + const rx = this.storage.read() + const hasPromises = [] + for (let i = start; i < clearEnd; i++) hasPromises.push(rx.getBlock(i)) + rx.tryFlush() + + const had = await Promise.all(hasPromises) + for (const block of had) { + if (block !== null) cleared.blocks++ + } + } + } + if (end - start === 1) tx.deleteBlock(start) else tx.deleteBlockRange(start, end) From 2b83e66222a95abab96415020611f812c608e490 Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:00:53 +0530 Subject: [PATCH 2/3] Re-enable the clear() diff test Uncomments the diff-option test, asserting the first clear counts the stored block and a repeat clear counts zero. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- test/clear.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/clear.js b/test/clear.js index 4c1767ba..ac9b5d6d 100644 --- a/test/clear.js +++ b/test/clear.js @@ -181,13 +181,11 @@ test('clear blocks with diff option', async function (t) { const cleared = await core.clear(1337) t.is(cleared, null) - // todo: reenable bytes use api + const cleared2 = await core.clear(0, { diff: true }) + t.is(cleared2.blocks, 1, 'the stored block was cleared') - // const cleared2 = await core.clear(0, { diff: true }) - // t.ok(cleared2.blocks > 0) - - // const cleared3 = await core.clear(0, { diff: true }) - // t.is(cleared3.blocks, 0) + const cleared3 = await core.clear(0, { diff: true }) + t.is(cleared3.blocks, 0, 'the block was already cleared') await core.close() }) From ebdcb4601f62e2fe9c5ca96058aa888697fdf0fc Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:01:15 +0530 Subject: [PATCH 3/3] Document the clear() diff return value The diff option now returns a `cleared` object counting how many blocks were actually cleared, e.g. { blocks: 1 }. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dec30b78..74052576 100644 --- a/README.md +++ b/README.md @@ -313,7 +313,7 @@ The core will also gossip to peers it is connected to, that is no longer has the ```js { - diff: false // Returned `cleared` bytes object is null unless you enable this + diff: false // Return a `cleared` object counting how many blocks were actually cleared, e.g. { blocks: 1 }. Null unless you enable this } ```