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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,10 @@ Emitted when a new connection has been established with a peer.

Emitted when a peer's connection has been closed.

#### `core.on('peer-synchronize', peer)`

Emitted when a synchronize message from a peer has been processed, meaning the peer's remote state (`peer.remoteLength`, `peer.remoteFork`, etc) has been updated. The first emission for a peer means its handshake has completed: `peer-add` fires before the peer has sent anything, so this is the earliest point at which the peer's remote state can be read.

#### `core.on('upload', index, byteLength, peer)`

Emitted when a block is uploaded to a peer.
Expand Down
10 changes: 10 additions & 0 deletions lib/replicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,8 @@ class Peer {
this.remoteHasManifest = hasManifest
this.remoteAllowPush = allowPush

this.replicator._onpeersynchronize(this)

if (this.closeIfIdle()) return

this.lengthAcked = sameFork ? remoteLength : 0
Expand Down Expand Up @@ -3217,6 +3219,14 @@ module.exports = class Replicator {
}
}

_onpeersynchronize(peer) {
const sessions = this.core.monitors

for (let i = sessions.length - 1; i >= 0; i--) {
sessions[i].emit('peer-synchronize', peer)
}
}

_ondownload(index, byteLength, from, req) {
const sessions = this.core.monitors

Expand Down
19 changes: 19 additions & 0 deletions test/replicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2937,6 +2937,25 @@ test('delayed updateAll timer doesnt keep event loop alive', async function (t)
t.is(r._updateAllBump, null, 'timer reset to null')
})

test('peer-synchronize is emitted when a synchronize message is processed', async function (t) {
const a = await create(t)
await a.append(['a', 'b', 'c'])

const b = await create(t, a.key)

const aSynchronized = new Promise((resolve) => a.once('peer-synchronize', resolve))
const bSynchronized = new Promise((resolve) => b.once('peer-synchronize', resolve))

replicate(a, b, t)

const peerOnA = await aSynchronized
t.is(peerOnA.remoteSynced, true, 'peer remote state is set when emitted')

const peerOnB = await bSynchronized
t.is(peerOnB.remoteSynced, true, 'peer remote state is set when emitted')
t.is(peerOnB.remoteLength, 3, 'peer remote length is known when emitted')
})

async function createAndDownload(t, core) {
const b = await create(t, core.key)
replicate(core, b, t, { teardown: false })
Expand Down