From 8829c5cfada46c3e94b4e362e8b567c321a7f259 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Sun, 2 Jul 2023 23:38:41 -0300 Subject: [PATCH 1/7] Add peer.remoteContiguousLength --- lib/replicator.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/replicator.js b/lib/replicator.js index 68de631b2..70feb9eda 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -308,6 +308,7 @@ class Peer { this.remoteFork = 0 this.remoteLength = 0 + this.remoteContiguousLength = 0 this.remoteCanUpgrade = false this.remoteUploading = true this.remoteDownloading = true @@ -683,6 +684,11 @@ class Peer { } onrange ({ drop, start, length }) { + // console.log(this.stream.publicKey.slice(0, 3).toString('hex'), this.stream.remotePublicKey.slice(0, 3).toString('hex'), this.$id, 'onrange', { drop, start, length }) + + if (start === 0) this.remoteContiguousLength = length + if (drop) this.remoteContiguousLength = Math.min(this.remoteContiguousLength, start) + const has = drop === false if (length === 1) { From 88776037076c9269275c5e72ba3ffbebb70e1f14 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Wed, 5 Jul 2023 14:00:29 -0300 Subject: [PATCH 2/7] Feedback fix, and align things --- index.js | 8 +++++++- lib/replicator.js | 2 -- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f80b9bfdd..c253515db 100644 --- a/index.js +++ b/index.js @@ -607,7 +607,13 @@ module.exports = class Hypercore extends EventEmitter { } if (bitfield) { - this.replicator.onhave(bitfield.start, bitfield.length, bitfield.drop) + const contig = this.core.header.contiguousLength + + if (bitfield.start < contig) { + this.replicator.onhave(0, contig, bitfield.drop) + } else { + this.replicator.onhave(bitfield.start, bitfield.length, bitfield.drop) + } } if (value) { diff --git a/lib/replicator.js b/lib/replicator.js index 70feb9eda..8e0c0fe74 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -684,8 +684,6 @@ class Peer { } onrange ({ drop, start, length }) { - // console.log(this.stream.publicKey.slice(0, 3).toString('hex'), this.stream.remotePublicKey.slice(0, 3).toString('hex'), this.$id, 'onrange', { drop, start, length }) - if (start === 0) this.remoteContiguousLength = length if (drop) this.remoteContiguousLength = Math.min(this.remoteContiguousLength, start) From 9df074bcb213b977dcfe2ce28a4f59fd02cb80c3 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Thu, 6 Jul 2023 15:39:17 -0300 Subject: [PATCH 3/7] Kasper's feedback --- lib/replicator.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/replicator.js b/lib/replicator.js index 8e0c0fe74..16deb0125 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -684,9 +684,6 @@ class Peer { } onrange ({ drop, start, length }) { - if (start === 0) this.remoteContiguousLength = length - if (drop) this.remoteContiguousLength = Math.min(this.remoteContiguousLength, start) - const has = drop === false if (length === 1) { From 7e7f807ce6ea96ea5f48f71d3f5e7746b5fc2e5f Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Thu, 6 Jul 2023 18:43:16 -0300 Subject: [PATCH 4/7] Mafintosh's feedback --- lib/replicator.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/replicator.js b/lib/replicator.js index 16deb0125..691525dc9 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -685,6 +685,8 @@ class Peer { onrange ({ drop, start, length }) { const has = drop === false + const rangeStart = this.remoteBitfield.findFirst(!has, start) + const rangeLength = length - (rangeStart - start) if (length === 1) { this.remoteBitfield.setRange(start, length, has) From 78144e6d1bc193c9b5d6d64362ec9790f5472cf4 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Thu, 6 Jul 2023 18:45:49 -0300 Subject: [PATCH 5/7] Mafintosh's feedback x2 --- lib/replicator.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/replicator.js b/lib/replicator.js index 691525dc9..49b413de8 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -308,7 +308,6 @@ class Peer { this.remoteFork = 0 this.remoteLength = 0 - this.remoteContiguousLength = 0 this.remoteCanUpgrade = false this.remoteUploading = true this.remoteDownloading = true From 4d08a3967bc539d2ed60ad1a19421570d88bb546 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Thu, 6 Jul 2023 19:03:02 -0300 Subject: [PATCH 6/7] Guard against drop 0, contig --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c253515db..81e07af4c 100644 --- a/index.js +++ b/index.js @@ -609,8 +609,8 @@ module.exports = class Hypercore extends EventEmitter { if (bitfield) { const contig = this.core.header.contiguousLength - if (bitfield.start < contig) { - this.replicator.onhave(0, contig, bitfield.drop) + if (bitfield.start < contig && !bitfield.drop) { + this.replicator.onhave(0, contig, false) } else { this.replicator.onhave(bitfield.start, bitfield.length, bitfield.drop) } From d303f3b06ee53bc586d39dd9aaf50ce7b94c0bd1 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Thu, 6 Jul 2023 19:11:46 -0300 Subject: [PATCH 7/7] Feedback: optimize _maybeWant with rcl --- lib/replicator.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/replicator.js b/lib/replicator.js index 49b413de8..68de631b2 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -684,8 +684,6 @@ class Peer { onrange ({ drop, start, length }) { const has = drop === false - const rangeStart = this.remoteBitfield.findFirst(!has, start) - const rangeLength = length - (rangeStart - start) if (length === 1) { this.remoteBitfield.setRange(start, length, has)