From 1b62186f26ae543c0674a0d33b8b4d58d517853b Mon Sep 17 00:00:00 2001 From: gbl Date: Tue, 10 Nov 2026 15:41:37 -0300 Subject: [PATCH 1/2] Tell a peer our new length even when we can't upgrade it yet A peer only asks us for blocks below the length it last heard from us. The sync went out only when canUpgrade flipped, so a relay that grew while still unable to upgrade a peer stayed silent, and a peer that caught up while the canUpgrade check was in flight never heard our length either. Send the sync whenever our length, fork or the length we ack is newer than what we last sent. --- lib/replicator.js | 38 ++++++++++++++++++++------- test/replicate.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 9 deletions(-) diff --git a/lib/replicator.js b/lib/replicator.js index 24bb15d9..45f3a7e6 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -548,6 +548,9 @@ class Peer { this.syncsProcessing = 0 this.lastUpgradableLength = 0 this.lastUpgradableFork = 0 + this.lengthSent = 0 + this.forkSent = 0 + this.remoteLengthSent = 0 this._remoteContiguousLength = 0 @@ -693,11 +696,14 @@ class Peer { } this.needsSync = false + this.lengthSent = this.core.state.length + this.forkSent = this.core.state.fork + this.remoteLengthSent = this._remoteLengthToAck() this.wireSync.send({ fork: this.core.state.fork, length: this.core.state.length, - remoteLength: this.core.state.fork === this.remoteFork ? this.remoteLength : 0, + remoteLength: this.remoteLengthSent, canUpgrade: this.canUpgrade, uploading: true, downloading: this.replicator.isDownloading(), @@ -839,7 +845,8 @@ class Peer { if ( this.needsSync === true || - (this.core.state.fork === this.remoteFork && this.core.state.length > this.remoteLength) + (this.core.state.fork === this.remoteFork && this.core.state.length > this.remoteLength) || + this._lengthSent() === false ) { this.signalUpgrade() } @@ -873,19 +880,32 @@ class Peer { if (remoteLength !== this.remoteLength || remoteFork !== this.remoteFork) { return } - if (canUpgrade === this.canUpgrade) { + if (canUpgrade !== this.canUpgrade) { + this.canUpgrade = canUpgrade + if (canUpgrade) { + this.lastUpgradableLength = this.remoteLength + this.lastUpgradableFork = this.remoteFork + } + } else if (this._syncSent()) { return } - this.canUpgrade = canUpgrade - if (canUpgrade) { - this.lastUpgradableLength = this.remoteLength - this.lastUpgradableFork = this.remoteFork - } - + // the sync carries our length and acks theirs, so a remote we cannot upgrade still needs it this.sendSync() } + _syncSent() { + return this._lengthSent() && this.remoteLengthSent === this._remoteLengthToAck() + } + + _lengthSent() { + return this.lengthSent === this.core.state.length && this.forkSent === this.core.state.fork + } + + _remoteLengthToAck() { + return this.core.state.fork === this.remoteFork ? this.remoteLength : 0 + } + // Safe to call in the background - never fails async _canUpgrade(remoteLength, remoteFork) { if (remoteFork !== this.core.state.fork) return false diff --git a/test/replicate.js b/test/replicate.js index c850b875..0f208cde 100644 --- a/test/replicate.js +++ b/test/replicate.js @@ -1303,6 +1303,71 @@ test('downloaded blocks are not unslabbed if bigger than half of slab size', asy ) }) +test('a relay that cannot upgrade a peer still tells it its new length', async function (t) { + const a = await create(t) + const relay = await create(t, a.key) + const b = await create(t, a.key) + + await a.append('a') + let s = replicate(a, relay, t, { teardown: false }) + await relay.get(0) + await unreplicate(s) + + // b holds only the roots of length 3, so neither it nor the relay can upgrade the other + await a.append(['b', 'c']) + s = replicate(a, b, t, { teardown: false }) + await b.update({ wait: true }) + await unreplicate(s) + + replicate(relay, b, t) + + await a.append(['d', 'e', 'f']) + s = replicate(a, relay, t, { teardown: false }) + t.alike(await relay.get(5), b4a.from('f')) + await unreplicate(s) + + s = replicate(a, b, t, { teardown: false }) + await b.update({ wait: true }) + await unreplicate(s) + + t.alike(await b.get(5, { timeout: 5000 }), b4a.from('f'), 'b gets the block from the relay') +}) + +test('a relay that catches up with a peer at once still tells it its new length', async function (t) { + let missed = 0 + + // the two catch-ups race, so one setup only sometimes lands the remote's sync mid-check + for (let i = 0; i < 10; i++) { + const a = await create(t) + const relay = await create(t, a.key) + const b = await create(t, a.key) + + await a.append('a') + let s = replicate(a, relay, t, { teardown: false }) + await relay.get(0) + await unreplicate(s) + + await a.append(['b', 'c']) + s = replicate(a, b, t, { teardown: false }) + await b.update({ wait: true }) + await unreplicate(s) + + const link = replicate(relay, b, t, { teardown: false }) + + await a.append(['d', 'e', 'f']) + const toRelay = replicate(a, relay, t, { teardown: false }) + const toB = replicate(a, b, t, { teardown: false }) + await Promise.all([relay.get(5), b.update({ wait: true })]) + await unreplicate(toRelay) + await unreplicate(toB) + + await b.get(5, { timeout: 1000 }).catch(() => missed++) + await unreplicate(link) + } + + t.is(missed, 0, 'b got every block from the relay') +}) + test('sparse replication without gossiping', async function (t) { t.plan(4) From f7c54d84a3e4d12931d2d1b2c3cc584a0e45d38c Mon Sep 17 00:00:00 2001 From: gbl Date: Wed, 11 Nov 2026 09:56:57 -0300 Subject: [PATCH 2/2] Cover the missed ack and keep a regression from crashing the run Test that a peer which cannot upgrade a shorter one still acks its length, so the shorter one's update({ wait: true }) returns. Use t.execution for the relay test, and race the update against a timer, so either one failing on a regression fails only its own test. Drop the comment in _updateCanUpgradeAndSync. --- lib/replicator.js | 1 - test/replicate.js | 28 +++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/replicator.js b/lib/replicator.js index 45f3a7e6..b2c15045 100644 --- a/lib/replicator.js +++ b/lib/replicator.js @@ -890,7 +890,6 @@ class Peer { return } - // the sync carries our length and acks theirs, so a remote we cannot upgrade still needs it this.sendSync() } diff --git a/test/replicate.js b/test/replicate.js index 0f208cde..01a54447 100644 --- a/test/replicate.js +++ b/test/replicate.js @@ -1330,7 +1330,7 @@ test('a relay that cannot upgrade a peer still tells it its new length', async f await b.update({ wait: true }) await unreplicate(s) - t.alike(await b.get(5, { timeout: 5000 }), b4a.from('f'), 'b gets the block from the relay') + await t.execution(b.get(5, { timeout: 5000 }), 'b gets the block from the relay') }) test('a relay that catches up with a peer at once still tells it its new length', async function (t) { @@ -1368,6 +1368,32 @@ test('a relay that catches up with a peer at once still tells it its new length' t.is(missed, 0, 'b got every block from the relay') }) +test('a peer that cannot upgrade a shorter one still acks its length', async function (t) { + const a = await create(t) + const relay = await create(t, a.key) + const b = await create(t, a.key) + + await a.append(['a', 'b', 'c']) + let s = replicate(a, b, t, { teardown: false }) + await b.update({ wait: true }) + await unreplicate(s) + + await a.append(['d', 'e', 'f']) + s = replicate(a, relay, t, { teardown: false }) + await relay.get(5) + await unreplicate(s) + + replicate(relay, b, t) + + // nothing else holds the loop open while the update waits + const timeout = new Promise((resolve) => { + const timer = setTimeout(resolve, 5000, 'timed out') + t.teardown(() => clearTimeout(timer)) + }) + const updated = await Promise.race([b.update({ wait: true }), timeout]) + t.is(updated, false, 'b stops waiting once the relay acks its length') +}) + test('sparse replication without gossiping', async function (t) { t.plan(4)