Skip to content

Commit

Permalink
Bugfix: set bitfields to correct value (#652)
Browse files Browse the repository at this point in the history
* Bugfix: set bitfields to correct value

* Add tests

* Close core
  • Loading branch information
HDegroote authored Feb 27, 2025
1 parent bc16013 commit a951200
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/session-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ async function storeBitfieldRange (storage, tx, from, to, value) {
const pageIndex = i + firstPage
if (!pages[i]) pages[i] = b4a.alloc(Bitfield.BYTES_PER_PAGE)

index = fillBitfieldPage(pages[i], index, to, pageIndex, true)
index = fillBitfieldPage(pages[i], index, to, pageIndex, value)
tx.putBitfieldPage(pageIndex, pages[i])
}
}
Expand Down
52 changes: 52 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const test = require('brittle')
const b4a = require('b4a')
const createTempDir = require('test-tmp')
const HypercoreStorage = require('hypercore-storage')

const Hypercore = require('../')
const { create, createStorage, eventFlush } = require('./helpers')
Expand Down Expand Up @@ -594,3 +595,54 @@ test('exclusive sessions', async function (t) {

await core.close()
})

test('truncate has correct storage state in memory and persisted', async function (t) {
const tmpDir = await t.tmp()
{
const storage = new HypercoreStorage(tmpDir)
const core = new Hypercore(storage)
await core.append(['a', 'b', 'c', 'd', 'e'])
await core.truncate(2)
t.alike(getBitfields(core, 0, 5), [true, true, false, false, false])
await core.close()
}

{
const storage = new HypercoreStorage(tmpDir)
const core = new Hypercore(storage)
await core.ready()
t.alike(getBitfields(core, 0, 5), [true, true, false, false, false])
await core.close()
}
})

test('clear has correct storage state in memory and persisted', async function (t) {
const tmpDir = await t.tmp()
{
const storage = new HypercoreStorage(tmpDir)
const core = new Hypercore(storage)
await core.append(['a', 'b', 'c', 'd', 'e'])
await core.clear(2)
t.alike(getBitfields(core, 0, 5), [true, true, false, true, true])
await core.close()
}

{
const storage = new HypercoreStorage(tmpDir)
const core = new Hypercore(storage)
await core.ready()
t.alike(getBitfields(core, 0, 5), [true, true, false, true, true])
await core.close()
}
})

function getBitfields (hypercore, start = 0, end = null) {
if (!end) end = hypercore.length

const res = []
for (let i = start; i < end; i++) {
res.push(hypercore.core.bitfield.get(i))
}

return res
}

0 comments on commit a951200

Please sign in to comment.