Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Nov 8, 2023
1 parent c48da3d commit 9c3cbe7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/bitfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BitfieldPage {
let i = Math.floor(start / 128)
const n = i + Math.ceil(length / 128)

while (i < n) this.tree.update(this.offset * 8 + i++ * 128)
while (i <= n) this.tree.update(this.offset * 8 + i++ * 128)
}

findFirst (val, position) {
Expand Down
10 changes: 7 additions & 3 deletions lib/remote-bitfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RemoteBitfieldPage {
let i = Math.floor(start / 128)
const n = i + Math.ceil(length / 128)

while (i < n) this.tree.update(this.offset * 8 + i++ * 128)
while (i <= n) this.tree.update(this.offset * 8 + i++ * 128)
}

findFirst (val, position) {
Expand All @@ -65,6 +65,7 @@ class RemoteBitfieldSegment {
this.offset = index * BYTES_PER_SEGMENT
this.tree = quickbit.Index.from([], BYTES_PER_SEGMENT)
this.pages = new Array(PAGES_PER_SEGMENT)
this.pagesLength = 0
}

get chunks () {
Expand All @@ -76,7 +77,10 @@ class RemoteBitfieldSegment {
}

add (page) {
this.pages[page.index - this.index * PAGES_PER_SEGMENT] = page
const pageIndex = page.index - this.index * PAGES_PER_SEGMENT
if (pageIndex >= this.pagesLength) this.pagesLength = pageIndex + 1

this.pages[pageIndex] = page

const chunk = { field: page.bitfield, offset: page.offset }

Expand All @@ -98,7 +102,7 @@ class RemoteBitfieldSegment {

if (i >= PAGES_PER_SEGMENT) return -1

while (i < this.pages.length) {
while (i < this.pagesLength) {
const p = this.pages[i]

let index = -1
Expand Down
34 changes: 22 additions & 12 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,20 +743,23 @@ class Peer {

_clearLocalRange (start, length) {
if (length === 1) {
this.missingBlocks.set(start, false)
this.missingBlocks.set(start, this.remoteBitfield.get(start) && !this.core.bitfield.get(start))
return
}

const contig = Math.min(this.core.tree.length, this.core.header.hints.contiguousLength)

if (start < contig) {
if (start + length < contig) {
const delta = contig - start
this.missingBlocks.setRange(start, delta)
start = contig
length -= delta
this.missingBlocks.setRange(start, delta, false)
return
}

if ((start & 31) > 0) start -= (start & 31)
const rem = start & 32767
if (rem > 0) {
start -= rem
length += rem
}

const end = start + Math.min(length, this.core.tree.length)
while (start < end) {
Expand All @@ -772,11 +775,17 @@ class Peer {

_unclearLocalRange (start, length) {
if (length === 1) {
this.missingBlocks.set(start, this.remoteBitfield.get(start))
this.missingBlocks.set(start, this.remoteBitfield.get(start) && !this.core.bitfield.get(start))
return
}

if ((start & 31) > 0) start -= (start & 31)
const rem = start & 2097151
if (rem > 0) {
start -= rem
length += rem
}

const fixedStart = start

const end = start + Math.min(length, this.remoteLength)
while (start < end) {
Expand All @@ -788,14 +797,14 @@ class Peer {
start += 2097152
}

this._clearLocalRange(start, length)
this._clearLocalRange(fixedStart, length)
}

onrange ({ drop, start, length }) {
const has = drop === false

if (length === 1) {
this.remoteBitfield.setRange(start, length, has)
this.remoteBitfield.set(start, has)
this.missingBlocks.set(start, has && !this.core.bitfield.get(start))
} else {
const rangeStart = this.remoteBitfield.findFirst(!has, start)
Expand Down Expand Up @@ -1027,12 +1036,14 @@ class Peer {
if (r.blocks) {
let min = -1
let max = -1

for (let i = r.start; i < r.end; i++) {
const index = r.blocks[i]
if (min === -1 || index < min) min = index
if (max === -1 || index > max) max = index
if (this.missingBlocks.get(index) === true && this._requestRangeBlock(index, length)) return true
}

if (min > -1) this._maybeWant(min, max - min)
return false
}
Expand All @@ -1047,7 +1058,6 @@ class Peer {

while (true) {
i = this.missingBlocks.findFirst(true, i)

if (i === -1 || i >= end) break

if (this._requestRangeBlock(i, length)) return true
Expand All @@ -1061,7 +1071,7 @@ class Peer {

if (i === -1 || i >= off) break

if (this.core.bitfield.get(i) === false && this._hasTreeParent(i) && this._requestRangeBlock(i, length)) return true
if (this._requestRangeBlock(i, length)) return true
i++
}

Expand Down

0 comments on commit 9c3cbe7

Please sign in to comment.