Skip to content

Commit d0a9fad

Browse files
authored
feat: custom and store self agent version + store self protocol version (#800)
* feat: custom and store self protocol and agent version * fix: do not enable custom protocolVersion
1 parent 824a444 commit d0a9fad

File tree

4 files changed

+85
-12
lines changed

4 files changed

+85
-12
lines changed

doc/API.md

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Creates an instance of Libp2p.
9292
| options.modules | [`Array<object>`](./CONFIGURATION.md#modules) | libp2p [modules](./CONFIGURATION.md#modules) to use |
9393
| [options.addresses] | `{ listen: Array<string>, announce: Array<string>, noAnnounce: Array<string> }` | Addresses for transport listening and to advertise to the network |
9494
| [options.config] | `object` | libp2p modules configuration and core configuration |
95+
| [options.host] | `{ agentVersion: string }` | libp2p host options |
9596
| [options.connectionManager] | [`object`](./CONFIGURATION.md#configuring-connection-manager) | libp2p Connection Manager [configuration](./CONFIGURATION.md#configuring-connection-manager) |
9697
| [options.transportManager] | [`object`](./CONFIGURATION.md#configuring-transport-manager) | libp2p transport manager [configuration](./CONFIGURATION.md#configuring-transport-manager) |
9798
| [options.datastore] | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |

src/config.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const mergeOptions = require('merge-options')
44
const { dnsaddrResolver } = require('multiaddr/src/resolvers')
55

66
const Constants = require('./constants')
7+
const { AGENT_VERSION } = require('./identify/consts')
78

89
const { FaultTolerance } = require('./transport-manager')
910

@@ -27,6 +28,9 @@ const DefaultConfig = {
2728
dnsaddr: dnsaddrResolver
2829
}
2930
},
31+
host: {
32+
agentVersion: AGENT_VERSION
33+
},
3034
metrics: {
3135
enabled: false
3236
},

src/identify/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ class IdentifyService {
8383
this._protocols = protocols
8484

8585
this.handleMessage = this.handleMessage.bind(this)
86+
87+
// Store self host metadata
88+
this._host = {
89+
agentVersion: AGENT_VERSION,
90+
protocolVersion: PROTOCOL_VERSION,
91+
...libp2p._options.host
92+
}
93+
94+
this.peerStore.metadataBook.set(this.peerId, 'AgentVersion', uint8ArrayFromString(this._host.agentVersion))
95+
this.peerStore.metadataBook.set(this.peerId, 'ProtocolVersion', uint8ArrayFromString(this._host.protocolVersion))
8696
}
8797

8898
/**
@@ -246,8 +256,8 @@ class IdentifyService {
246256
const signedPeerRecord = await this._getSelfPeerRecord()
247257

248258
const message = Message.encode({
249-
protocolVersion: PROTOCOL_VERSION,
250-
agentVersion: AGENT_VERSION,
259+
protocolVersion: this._host.protocolVersion,
260+
agentVersion: this._host.agentVersion,
251261
publicKey,
252262
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
253263
signedPeerRecord,

test/identify/index.spec.js

+68-10
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ describe('Identify', () => {
5353
peerId: localPeer,
5454
connectionManager: new EventEmitter(),
5555
peerStore: new PeerStore({ peerId: localPeer }),
56-
multiaddrs: listenMaddrs
56+
multiaddrs: listenMaddrs,
57+
_options: { host: {} }
5758
},
5859
protocols
5960
})
@@ -63,7 +64,8 @@ describe('Identify', () => {
6364
peerId: remotePeer,
6465
connectionManager: new EventEmitter(),
6566
peerStore: new PeerStore({ peerId: remotePeer }),
66-
multiaddrs: listenMaddrs
67+
multiaddrs: listenMaddrs,
68+
_options: { host: {} }
6769
},
6870
protocols
6971
})
@@ -106,7 +108,8 @@ describe('Identify', () => {
106108
peerId: localPeer,
107109
connectionManager: new EventEmitter(),
108110
peerStore: new PeerStore({ peerId: localPeer }),
109-
multiaddrs: listenMaddrs
111+
multiaddrs: listenMaddrs,
112+
_options: { host: {} }
110113
},
111114
protocols
112115
})
@@ -116,7 +119,8 @@ describe('Identify', () => {
116119
peerId: remotePeer,
117120
connectionManager: new EventEmitter(),
118121
peerStore: new PeerStore({ peerId: remotePeer }),
119-
multiaddrs: listenMaddrs
122+
multiaddrs: listenMaddrs,
123+
_options: { host: {} }
120124
},
121125
protocols
122126
})
@@ -165,7 +169,8 @@ describe('Identify', () => {
165169
peerId: localPeer,
166170
connectionManager: new EventEmitter(),
167171
peerStore: new PeerStore({ peerId: localPeer }),
168-
multiaddrs: []
172+
multiaddrs: [],
173+
_options: { host: {} }
169174
},
170175
protocols
171176
})
@@ -174,7 +179,8 @@ describe('Identify', () => {
174179
peerId: remotePeer,
175180
connectionManager: new EventEmitter(),
176181
peerStore: new PeerStore({ peerId: remotePeer }),
177-
multiaddrs: []
182+
multiaddrs: [],
183+
_options: { host: {} }
178184
},
179185
protocols
180186
})
@@ -201,6 +207,36 @@ describe('Identify', () => {
201207
.and.to.have.property('code', Errors.ERR_INVALID_PEER)
202208
})
203209

210+
it('should store host data and protocol version into metadataBook', () => {
211+
const agentVersion = 'js-project/1.0.0'
212+
const peerStore = new PeerStore({ peerId: localPeer })
213+
214+
sinon.spy(peerStore.metadataBook, 'set')
215+
216+
new IdentifyService({ // eslint-disable-line no-new
217+
libp2p: {
218+
peerId: localPeer,
219+
connectionManager: new EventEmitter(),
220+
peerStore,
221+
multiaddrs: listenMaddrs,
222+
_options: {
223+
host: {
224+
agentVersion
225+
}
226+
}
227+
},
228+
protocols
229+
})
230+
231+
expect(peerStore.metadataBook.set.callCount).to.eql(2)
232+
233+
const storedAgentVersion = peerStore.metadataBook.getValue(localPeer, 'AgentVersion')
234+
const storedProtocolVersion = peerStore.metadataBook.getValue(localPeer, 'ProtocolVersion')
235+
236+
expect(agentVersion).to.eql(unit8ArrayToString(storedAgentVersion))
237+
expect(storedProtocolVersion).to.exist()
238+
})
239+
204240
describe('push', () => {
205241
it('should be able to push identify updates to another peer', async () => {
206242
const connectionManager = new EventEmitter()
@@ -211,7 +247,8 @@ describe('Identify', () => {
211247
peerId: localPeer,
212248
connectionManager: new EventEmitter(),
213249
peerStore: new PeerStore({ peerId: localPeer }),
214-
multiaddrs: listenMaddrs
250+
multiaddrs: listenMaddrs,
251+
_options: { host: {} }
215252
},
216253
protocols: new Map([
217254
[multicodecs.IDENTIFY],
@@ -224,7 +261,8 @@ describe('Identify', () => {
224261
peerId: remotePeer,
225262
connectionManager,
226263
peerStore: new PeerStore({ peerId: remotePeer }),
227-
multiaddrs: []
264+
multiaddrs: [],
265+
_options: { host: {} }
228266
}
229267
})
230268

@@ -272,7 +310,8 @@ describe('Identify', () => {
272310
peerId: localPeer,
273311
connectionManager: new EventEmitter(),
274312
peerStore: new PeerStore({ peerId: localPeer }),
275-
multiaddrs: listenMaddrs
313+
multiaddrs: listenMaddrs,
314+
_options: { host: {} }
276315
},
277316
protocols: new Map([
278317
[multicodecs.IDENTIFY],
@@ -285,7 +324,8 @@ describe('Identify', () => {
285324
peerId: remotePeer,
286325
connectionManager,
287326
peerStore: new PeerStore({ peerId: remotePeer }),
288-
multiaddrs: []
327+
multiaddrs: [],
328+
_options: { host: {} }
289329
}
290330
})
291331

@@ -404,5 +444,23 @@ describe('Identify', () => {
404444
// Verify the streams close
405445
await pWaitFor(() => connection.streams.length === 0)
406446
})
447+
448+
it('should store host data and protocol version into metadataBook', () => {
449+
const agentVersion = 'js-project/1.0.0'
450+
451+
libp2p = new Libp2p({
452+
...baseOptions,
453+
peerId,
454+
host: {
455+
agentVersion
456+
}
457+
})
458+
459+
const storedAgentVersion = libp2p.peerStore.metadataBook.getValue(localPeer, 'AgentVersion')
460+
const storedProtocolVersion = libp2p.peerStore.metadataBook.getValue(localPeer, 'ProtocolVersion')
461+
462+
expect(agentVersion).to.eql(unit8ArrayToString(storedAgentVersion))
463+
expect(storedProtocolVersion).to.exist()
464+
})
407465
})
408466
})

0 commit comments

Comments
 (0)