Skip to content

Commit 23b3ae8

Browse files
add assertion that checkout does not break prologue (#672)
1 parent f0114a2 commit 23b3ae8

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ class Hypercore extends EventEmitter {
370370
if (this.state && checkout !== -1) {
371371
if (!opts.name && !opts.atom) throw ASSERTION('Checkouts must be named or atomized')
372372
if (checkout > this.state.length) throw ASSERTION('Invalid checkout ' + checkout + ' for ' + opts.name + ', length is ' + this.state.length)
373+
if (this.state.prologue && checkout < this.state.prologue.length) {
374+
throw ASSERTION('Invalid checkout ' + checkout + ' for ' + opts.name + ', prologue length is ' + this.state.prologue.length)
375+
}
373376
if (checkout < this.state.length) await this.state.truncate(checkout, this.fork)
374377
}
375378

test/sessions.js

+41
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,44 @@ test('sessions - cannot set checkout if name not set', async function (t) {
117117

118118
await core.close()
119119
})
120+
121+
test('sessions - checkout breaks prologue', async function (t) {
122+
const storage = await createStorage(t)
123+
const storage2 = await createStorage(t)
124+
125+
const core = new Hypercore(storage)
126+
127+
for (let i = 0; i < 10; i++) await core.append(b4a.from([i]))
128+
129+
const prologued = new Hypercore(storage2, {
130+
manifest: {
131+
...core.manifest,
132+
prologue: {
133+
hash: await core.treeHash(),
134+
length: core.length
135+
}
136+
}
137+
})
138+
139+
await prologued.ready()
140+
await prologued.core.copyPrologue(core.state)
141+
142+
process.on('uncaughtException', noop)
143+
144+
let session
145+
try {
146+
session = prologued.session({ name: 'fail', checkout: 7 })
147+
await session.ready()
148+
t.fail()
149+
} catch (err) {
150+
t.pass()
151+
}
152+
153+
await session.close()
154+
await prologued.close()
155+
await core.close()
156+
157+
process.off('uncaughtException', noop)
158+
})
159+
160+
function noop () {}

0 commit comments

Comments
 (0)