diff --git a/README.md b/README.md index 823355d6..d590c58c 100644 --- a/README.md +++ b/README.md @@ -308,7 +308,15 @@ 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 // Returned `cleared` object is null unless you enable this +} +``` + +`cleared` object when `diff` is `true` will look like: + +``` +{ + blocks: Number // The number of blocks cleared } ``` diff --git a/lib/session-state.js b/lib/session-state.js index 38af3d05..0c6bbe8a 100644 --- a/lib/session-state.js +++ b/lib/session-state.js @@ -547,6 +547,10 @@ class SessionState { } } + if (cleared) { + cleared.blocks = this.core.bitfield.countSet(start, end - start) + } + if (end - start === 1) tx.deleteBlock(start) else tx.deleteBlockRange(start, end) diff --git a/test/clear.js b/test/clear.js index b715219b..c78ed00e 100644 --- a/test/clear.js +++ b/test/clear.js @@ -95,13 +95,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.ok(cleared2.blocks > 0) - // 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) await core.close() })