Skip to content

Commit 818d2b2

Browse files
authoredMay 12, 2021
fix: store remote agent and protocol version during identify (#943)
1 parent d163ffd commit 818d2b2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
 

‎src/identify/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ class IdentifyService {
189189
const envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
190190
if (this.peerStore.addressBook.consumePeerRecord(envelope)) {
191191
this.peerStore.protoBook.set(id, protocols)
192+
this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
193+
this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
192194
return
193195
}
194196
} catch (err) {
@@ -204,6 +206,7 @@ class IdentifyService {
204206

205207
this.peerStore.protoBook.set(id, protocols)
206208
this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
209+
this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
207210

208211
// TODO: Add and score our observed addr
209212
log('received observed address of %s', cleanObservedAddr)

‎test/identify/index.spec.js

+33
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,39 @@ describe('Identify', () => {
430430
await connection.close()
431431
})
432432

433+
it('should store remote agent and protocol versions in metadataBook after connecting', async () => {
434+
libp2p = new Libp2p({
435+
...baseOptions,
436+
peerId
437+
})
438+
439+
await libp2p.start()
440+
441+
sinon.spy(libp2p.identifyService, 'identify')
442+
const peerStoreSpyConsumeRecord = sinon.spy(libp2p.peerStore.addressBook, 'consumePeerRecord')
443+
const peerStoreSpyAdd = sinon.spy(libp2p.peerStore.addressBook, 'add')
444+
445+
const connection = await libp2p.dialer.connectToPeer(remoteAddr)
446+
expect(connection).to.exist()
447+
448+
// Wait for peer store to be updated
449+
// Dialer._createDialTarget (add), Identify (consume)
450+
await pWaitFor(() => peerStoreSpyConsumeRecord.callCount === 1 && peerStoreSpyAdd.callCount === 1)
451+
expect(libp2p.identifyService.identify.callCount).to.equal(1)
452+
453+
// The connection should have no open streams
454+
await pWaitFor(() => connection.streams.length === 0)
455+
await connection.close()
456+
457+
const remotePeer = PeerId.createFromB58String(remoteAddr.getPeerId())
458+
459+
const storedAgentVersion = libp2p.peerStore.metadataBook.getValue(remotePeer, 'AgentVersion')
460+
const storedProtocolVersion = libp2p.peerStore.metadataBook.getValue(remotePeer, 'ProtocolVersion')
461+
462+
expect(storedAgentVersion).to.exist()
463+
expect(storedProtocolVersion).to.exist()
464+
})
465+
433466
it('should push protocol updates to an already connected peer', async () => {
434467
libp2p = new Libp2p({
435468
...baseOptions,

0 commit comments

Comments
 (0)
Please sign in to comment.