diff --git a/lib/replicator.js b/lib/replicator.js index 24bb15d9..b2c15045 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,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() + } + + _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..01a54447 100644 --- a/test/replicate.js +++ b/test/replicate.js @@ -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)