Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to oversend syncs in response. So far I can't figure out a way to reduce it only to the race conditions (in the second 10x try test). So it might be a matter of whether the extra syncs are worth catching that edgecase.

@marcus-pousette-hp marcus-pousette-hp Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Analysed this too. When measuring it locally, this only fires in the race, and each extra sync is correctly needed. It is about +20% syncs at zero latency (tests), 1% at 1 ms, and no difference at at 20-40 ms. And so for the low latency scenarios we can in the future add a 20 ms debounce/delay on this signal path and I think we would get back to mains behavior.

@marcus-pousette-hp marcus-pousette-hp Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now looked at slow tree reads. With a simulated 5–15 ms read in _canUpgrade, the race also shows up at 20–40 ms. In this scenario main drops 2–6% of length announcements and this PR adds 3–7% syncs but does the right thing, so my analysis above was partial here

) {
this.signalUpgrade()
}
Expand Down Expand Up @@ -873,19 +880,31 @@ 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
}

this.sendSync()
}

_syncSent() {
return this._lengthSent() && this.remoteLengthSent === this._remoteLengthToAck()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test covers remoteLengthSent. It also seem to fix a second hang: on main, when two peers can’t upgrade each other, update({ wait: true }) on the shorter one waits forever for the longer one to ack its length. Could you add a test for that?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in f7c54d8: "a peer that cannot upgrade a shorter one still acks its length". It hangs on main and passes here.

}

_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
Expand Down
91 changes: 91 additions & 0 deletions test/replicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,97 @@ 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)

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) {
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('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)

Expand Down