Skip to content
This repository was archived by the owner on Dec 28, 2022. It is now read-only.

Commit a8701ec

Browse files
authored
Download available blocks on non-sparse core.update() (#111)
* Download available blocks on non-sparse `core.update()` * Data -> blocks * Download until `tree.length` * Await upgrade promise before reading `tree.length`
1 parent ffe3907 commit a8701ec

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,19 @@ module.exports = class Hypercore extends EventEmitter {
573573
const activeRequests = (opts && opts.activeRequests) || this.activeRequests
574574
const req = this.replicator.addUpgrade(activeRequests)
575575

576-
if (!this.snapshotted) return req.promise
577-
if (!(await req.promise)) return false
576+
const upgraded = await req.promise
578577

579-
return this._updateSnapshot()
578+
if (!this.sparse) {
579+
// Download all available blocks in non-sparse mode
580+
const start = this.length
581+
const end = this.core.tree.length
582+
583+
await this.download({ start, end, ifAvailable: true }).downloaded()
584+
}
585+
586+
if (!upgraded) return false
587+
if (this.snapshotted) return this._updateSnapshot()
588+
return true
580589
}
581590

582591
async seek (bytes, opts) {

test/replicate.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,3 +726,15 @@ test('download range resolves immediately if no peers', async function (t) {
726726

727727
t.pass('range resolved')
728728
})
729+
730+
test('download available blocks on non-sparse update', async function (t) {
731+
const a = await create()
732+
const b = await create(a.key, { sparse: false })
733+
734+
replicate(a, b, t)
735+
736+
await a.append(['a', 'b', 'c', 'd', 'e'])
737+
await b.update()
738+
739+
t.is(b.contiguousLength, 5)
740+
})

0 commit comments

Comments
 (0)