Skip to content

Commit f5374a1

Browse files
Specify sourceLength when calling Core.copyFrom (#487)
* add copyFrom test with additional from past * copyFrom start option used to place additional block * rename start to sourceLength * fix standard --------- Co-authored-by: Mathias Buus <[email protected]>
1 parent 54fea3c commit f5374a1

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/core.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ module.exports = class Core {
226226
return false
227227
}
228228

229-
async copyFrom (src, signature, { length = src.tree.length, fork = src.tree.fork, additional = [] } = {}) {
229+
async copyFrom (src, signature, { length = src.tree.length, sourceLength = src.tree.length, fork = src.tree.fork, additional = [] } = {}) {
230230
await this._mutex.lock()
231231

232232
try {
@@ -288,8 +288,9 @@ module.exports = class Core {
288288
// add additional blocks
289289
if (length > src.tree.length) {
290290
const missing = length - src.tree.length
291+
const offset = src.tree.length - sourceLength
291292

292-
if (additional.length < missing) {
293+
if (additional.length < missing + offset) {
293294
throw INVALID_OPERATION('Insufficient additional nodes were passed')
294295
}
295296

@@ -299,7 +300,7 @@ module.exports = class Core {
299300
batch.length = source.length
300301
batch.byteLength = source.byteLength
301302

302-
const blocks = additional.length === missing ? additional : additional.slice(0, missing)
303+
const blocks = additional.length === missing ? additional : additional.slice(offset, offset + missing)
303304

304305
await this.blocks.putBatch(source.length, blocks, source.byteLength)
305306
this.bitfield.setRange(source.length, missing, true)

test/core.js

+39
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,45 @@ test('core - clone with different fork', async function (t) {
608608
t.alike(await clone2.blocks.get(2), b4a.from('c'))
609609
})
610610

611+
test('core - copyFrom with partially out of date additional', async function (t) {
612+
const { core } = await create()
613+
const { core: copy } = await create({ keyPair: core.header.keyPair })
614+
const { core: clone } = await create({ keyPair: { publicKey: core.header.keyPair.publicKey } })
615+
616+
await core.append([b4a.from('a'), b4a.from('b')])
617+
await copy.copyFrom(core, core.tree.signature)
618+
619+
t.is(copy.header.keyPair.publicKey, core.header.keyPair.publicKey)
620+
t.is(copy.header.keyPair.publicKey, clone.header.keyPair.publicKey)
621+
622+
await core.append([b4a.from('c')])
623+
await core.append([b4a.from('d')])
624+
625+
// copy is independent
626+
await copy.append([b4a.from('c')])
627+
628+
await clone.copyFrom(copy, core.tree.signature, {
629+
length: 4,
630+
sourceLength: 2,
631+
additional: [
632+
b4a.from('c'),
633+
b4a.from('d')
634+
]
635+
})
636+
637+
t.is(clone.header.tree.length, 4)
638+
t.alike(clone.header.tree.signature, core.header.tree.signature)
639+
640+
t.is(clone.tree.length, core.tree.length)
641+
t.is(clone.tree.byteLength, core.tree.byteLength)
642+
t.alike(clone.roots, core.roots)
643+
644+
t.alike(await clone.blocks.get(0), b4a.from('a'))
645+
t.alike(await clone.blocks.get(1), b4a.from('b'))
646+
t.alike(await clone.blocks.get(2), b4a.from('c'))
647+
t.alike(await clone.blocks.get(3), b4a.from('d'))
648+
})
649+
611650
async function create (opts) {
612651
const storage = new Map()
613652

0 commit comments

Comments
 (0)