Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: store remote agent and protocol version during identify #943

Merged
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
3 changes: 3 additions & 0 deletions src/identify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ class IdentifyService {
const envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
if (this.peerStore.addressBook.consumePeerRecord(envelope)) {
this.peerStore.protoBook.set(id, protocols)
this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
return
}
} catch (err) {
Expand All @@ -204,6 +206,7 @@ class IdentifyService {

this.peerStore.protoBook.set(id, protocols)
this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))

// TODO: Add and score our observed addr
log('received observed address of %s', cleanObservedAddr)
Expand Down
33 changes: 33 additions & 0 deletions test/identify/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,39 @@ describe('Identify', () => {
await connection.close()
})

it('should store remote agent and protocol versions in metadataBook after connecting', async () => {
libp2p = new Libp2p({
...baseOptions,
peerId
})

await libp2p.start()

sinon.spy(libp2p.identifyService, 'identify')
const peerStoreSpyConsumeRecord = sinon.spy(libp2p.peerStore.addressBook, 'consumePeerRecord')
const peerStoreSpyAdd = sinon.spy(libp2p.peerStore.addressBook, 'add')

const connection = await libp2p.dialer.connectToPeer(remoteAddr)
expect(connection).to.exist()

// Wait for peer store to be updated
// Dialer._createDialTarget (add), Identify (consume)
await pWaitFor(() => peerStoreSpyConsumeRecord.callCount === 1 && peerStoreSpyAdd.callCount === 1)
expect(libp2p.identifyService.identify.callCount).to.equal(1)

// The connection should have no open streams
await pWaitFor(() => connection.streams.length === 0)
await connection.close()

const remotePeer = PeerId.createFromB58String(remoteAddr.getPeerId())

const storedAgentVersion = libp2p.peerStore.metadataBook.getValue(remotePeer, 'AgentVersion')
const storedProtocolVersion = libp2p.peerStore.metadataBook.getValue(remotePeer, 'ProtocolVersion')

expect(storedAgentVersion).to.exist()
expect(storedProtocolVersion).to.exist()
})

it('should push protocol updates to an already connected peer', async () => {
libp2p = new Libp2p({
...baseOptions,
Expand Down