From 4bc7c813affb3bd73a7831a5e447a883aa422fbc Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Fri, 13 Nov 2020 09:24:01 +0100 Subject: [PATCH 1/2] feat: custom and store self protocol and agent version --- doc/API.md | 1 + src/config.js | 8 ++++ src/identify/index.js | 14 ++++++- test/identify/index.spec.js | 82 ++++++++++++++++++++++++++++++++----- 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/doc/API.md b/doc/API.md index 69e0ca4f78..eb868eeaaf 100644 --- a/doc/API.md +++ b/doc/API.md @@ -92,6 +92,7 @@ Creates an instance of Libp2p. | options.modules | [`Array`](./CONFIGURATION.md#modules) | libp2p [modules](./CONFIGURATION.md#modules) to use | | [options.addresses] | `{ listen: Array, announce: Array, noAnnounce: Array }` | Addresses for transport listening and to advertise to the network | | [options.config] | `object` | libp2p modules configuration and core configuration | +| [options.host] | `{agentVersion: string, protocolVersion: string}` | libp2p host options | | [options.connectionManager] | [`object`](./CONFIGURATION.md#configuring-connection-manager) | libp2p Connection Manager [configuration](./CONFIGURATION.md#configuring-connection-manager) | | [options.transportManager] | [`object`](./CONFIGURATION.md#configuring-transport-manager) | libp2p transport manager [configuration](./CONFIGURATION.md#configuring-transport-manager) | | [options.datastore] | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) | diff --git a/src/config.js b/src/config.js index 1cc0f097b4..2dca5c7a69 100644 --- a/src/config.js +++ b/src/config.js @@ -4,6 +4,10 @@ const mergeOptions = require('merge-options') const { dnsaddrResolver } = require('multiaddr/src/resolvers') const Constants = require('./constants') +const { + AGENT_VERSION, + PROTOCOL_VERSION +} = require('./identify/consts') const { FaultTolerance } = require('./transport-manager') @@ -27,6 +31,10 @@ const DefaultConfig = { dnsaddr: dnsaddrResolver } }, + host: { + agentVersion: AGENT_VERSION, + protocolVersion: PROTOCOL_VERSION + }, metrics: { enabled: false }, diff --git a/src/identify/index.js b/src/identify/index.js index f42a8b6f94..f39f95f03e 100644 --- a/src/identify/index.js +++ b/src/identify/index.js @@ -83,6 +83,16 @@ class IdentifyService { this._protocols = protocols this.handleMessage = this.handleMessage.bind(this) + + // Store self host metadata + this._host = { + agentVersion: AGENT_VERSION, + protocolVersion: PROTOCOL_VERSION, + ...libp2p._options.host + } + + this.peerStore.metadataBook.set(this.peerId, 'AgentVersion', uint8ArrayFromString(this._host.agentVersion)) + this.peerStore.metadataBook.set(this.peerId, 'ProtocolVersion', uint8ArrayFromString(this._host.protocolVersion)) } /** @@ -246,8 +256,8 @@ class IdentifyService { const signedPeerRecord = await this._getSelfPeerRecord() const message = Message.encode({ - protocolVersion: PROTOCOL_VERSION, - agentVersion: AGENT_VERSION, + protocolVersion: this._host.protocolVersion, + agentVersion: this._host.agentVersion, publicKey, listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes), signedPeerRecord, diff --git a/test/identify/index.spec.js b/test/identify/index.spec.js index 1ccbf67122..53a526a143 100644 --- a/test/identify/index.spec.js +++ b/test/identify/index.spec.js @@ -53,7 +53,8 @@ describe('Identify', () => { peerId: localPeer, connectionManager: new EventEmitter(), peerStore: new PeerStore({ peerId: localPeer }), - multiaddrs: listenMaddrs + multiaddrs: listenMaddrs, + _options: { host: {} } }, protocols }) @@ -63,7 +64,8 @@ describe('Identify', () => { peerId: remotePeer, connectionManager: new EventEmitter(), peerStore: new PeerStore({ peerId: remotePeer }), - multiaddrs: listenMaddrs + multiaddrs: listenMaddrs, + _options: { host: {} } }, protocols }) @@ -106,7 +108,8 @@ describe('Identify', () => { peerId: localPeer, connectionManager: new EventEmitter(), peerStore: new PeerStore({ peerId: localPeer }), - multiaddrs: listenMaddrs + multiaddrs: listenMaddrs, + _options: { host: {} } }, protocols }) @@ -116,7 +119,8 @@ describe('Identify', () => { peerId: remotePeer, connectionManager: new EventEmitter(), peerStore: new PeerStore({ peerId: remotePeer }), - multiaddrs: listenMaddrs + multiaddrs: listenMaddrs, + _options: { host: {} } }, protocols }) @@ -165,7 +169,8 @@ describe('Identify', () => { peerId: localPeer, connectionManager: new EventEmitter(), peerStore: new PeerStore({ peerId: localPeer }), - multiaddrs: [] + multiaddrs: [], + _options: { host: {} } }, protocols }) @@ -174,7 +179,8 @@ describe('Identify', () => { peerId: remotePeer, connectionManager: new EventEmitter(), peerStore: new PeerStore({ peerId: remotePeer }), - multiaddrs: [] + multiaddrs: [], + _options: { host: {} } }, protocols }) @@ -201,6 +207,38 @@ describe('Identify', () => { .and.to.have.property('code', Errors.ERR_INVALID_PEER) }) + it('should store host data into metadataBook', () => { + const agentVersion = 'js-project/1.0.0' + const protocolVersion = '1000' + const peerStore = new PeerStore({ peerId: localPeer }) + + sinon.spy(peerStore.metadataBook, 'set') + + new IdentifyService({ // eslint-disable-line no-new + libp2p: { + peerId: localPeer, + connectionManager: new EventEmitter(), + peerStore, + multiaddrs: listenMaddrs, + _options: { + host: { + agentVersion, + protocolVersion + } + } + }, + protocols + }) + + expect(peerStore.metadataBook.set.callCount).to.eql(2) + + const storedAgentVersion = peerStore.metadataBook.getValue(localPeer, 'AgentVersion') + const storedProtocolVersion = peerStore.metadataBook.getValue(localPeer, 'ProtocolVersion') + + expect(agentVersion).to.eql(unit8ArrayToString(storedAgentVersion)) + expect(protocolVersion).to.eql(unit8ArrayToString(storedProtocolVersion)) + }) + describe('push', () => { it('should be able to push identify updates to another peer', async () => { const connectionManager = new EventEmitter() @@ -211,7 +249,8 @@ describe('Identify', () => { peerId: localPeer, connectionManager: new EventEmitter(), peerStore: new PeerStore({ peerId: localPeer }), - multiaddrs: listenMaddrs + multiaddrs: listenMaddrs, + _options: { host: {} } }, protocols: new Map([ [multicodecs.IDENTIFY], @@ -224,7 +263,8 @@ describe('Identify', () => { peerId: remotePeer, connectionManager, peerStore: new PeerStore({ peerId: remotePeer }), - multiaddrs: [] + multiaddrs: [], + _options: { host: {} } } }) @@ -272,7 +312,8 @@ describe('Identify', () => { peerId: localPeer, connectionManager: new EventEmitter(), peerStore: new PeerStore({ peerId: localPeer }), - multiaddrs: listenMaddrs + multiaddrs: listenMaddrs, + _options: { host: {} } }, protocols: new Map([ [multicodecs.IDENTIFY], @@ -285,7 +326,8 @@ describe('Identify', () => { peerId: remotePeer, connectionManager, peerStore: new PeerStore({ peerId: remotePeer }), - multiaddrs: [] + multiaddrs: [], + _options: { host: {} } } }) @@ -404,5 +446,25 @@ describe('Identify', () => { // Verify the streams close await pWaitFor(() => connection.streams.length === 0) }) + + it('should store host data into metadataBook', () => { + const agentVersion = 'js-project/1.0.0' + const protocolVersion = '1000' + + libp2p = new Libp2p({ + ...baseOptions, + peerId, + host: { + agentVersion, + protocolVersion + } + }) + + const storedAgentVersion = libp2p.peerStore.metadataBook.getValue(localPeer, 'AgentVersion') + const storedProtocolVersion = libp2p.peerStore.metadataBook.getValue(localPeer, 'ProtocolVersion') + + expect(agentVersion).to.eql(unit8ArrayToString(storedAgentVersion)) + expect(protocolVersion).to.eql(unit8ArrayToString(storedProtocolVersion)) + }) }) }) From 93d804e49251279d4c175497e4017c8df8703530 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Mon, 16 Nov 2020 16:40:51 +0100 Subject: [PATCH 2/2] fix: do not enable custom protocolVersion --- doc/API.md | 2 +- src/config.js | 8 ++------ test/identify/index.spec.js | 16 ++++++---------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/doc/API.md b/doc/API.md index eb868eeaaf..5da8c30386 100644 --- a/doc/API.md +++ b/doc/API.md @@ -92,7 +92,7 @@ Creates an instance of Libp2p. | options.modules | [`Array`](./CONFIGURATION.md#modules) | libp2p [modules](./CONFIGURATION.md#modules) to use | | [options.addresses] | `{ listen: Array, announce: Array, noAnnounce: Array }` | Addresses for transport listening and to advertise to the network | | [options.config] | `object` | libp2p modules configuration and core configuration | -| [options.host] | `{agentVersion: string, protocolVersion: string}` | libp2p host options | +| [options.host] | `{ agentVersion: string }` | libp2p host options | | [options.connectionManager] | [`object`](./CONFIGURATION.md#configuring-connection-manager) | libp2p Connection Manager [configuration](./CONFIGURATION.md#configuring-connection-manager) | | [options.transportManager] | [`object`](./CONFIGURATION.md#configuring-transport-manager) | libp2p transport manager [configuration](./CONFIGURATION.md#configuring-transport-manager) | | [options.datastore] | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) | diff --git a/src/config.js b/src/config.js index 2dca5c7a69..6e0997938d 100644 --- a/src/config.js +++ b/src/config.js @@ -4,10 +4,7 @@ const mergeOptions = require('merge-options') const { dnsaddrResolver } = require('multiaddr/src/resolvers') const Constants = require('./constants') -const { - AGENT_VERSION, - PROTOCOL_VERSION -} = require('./identify/consts') +const { AGENT_VERSION } = require('./identify/consts') const { FaultTolerance } = require('./transport-manager') @@ -32,8 +29,7 @@ const DefaultConfig = { } }, host: { - agentVersion: AGENT_VERSION, - protocolVersion: PROTOCOL_VERSION + agentVersion: AGENT_VERSION }, metrics: { enabled: false diff --git a/test/identify/index.spec.js b/test/identify/index.spec.js index 53a526a143..74394dd158 100644 --- a/test/identify/index.spec.js +++ b/test/identify/index.spec.js @@ -207,9 +207,8 @@ describe('Identify', () => { .and.to.have.property('code', Errors.ERR_INVALID_PEER) }) - it('should store host data into metadataBook', () => { + it('should store host data and protocol version into metadataBook', () => { const agentVersion = 'js-project/1.0.0' - const protocolVersion = '1000' const peerStore = new PeerStore({ peerId: localPeer }) sinon.spy(peerStore.metadataBook, 'set') @@ -222,8 +221,7 @@ describe('Identify', () => { multiaddrs: listenMaddrs, _options: { host: { - agentVersion, - protocolVersion + agentVersion } } }, @@ -236,7 +234,7 @@ describe('Identify', () => { const storedProtocolVersion = peerStore.metadataBook.getValue(localPeer, 'ProtocolVersion') expect(agentVersion).to.eql(unit8ArrayToString(storedAgentVersion)) - expect(protocolVersion).to.eql(unit8ArrayToString(storedProtocolVersion)) + expect(storedProtocolVersion).to.exist() }) describe('push', () => { @@ -447,16 +445,14 @@ describe('Identify', () => { await pWaitFor(() => connection.streams.length === 0) }) - it('should store host data into metadataBook', () => { + it('should store host data and protocol version into metadataBook', () => { const agentVersion = 'js-project/1.0.0' - const protocolVersion = '1000' libp2p = new Libp2p({ ...baseOptions, peerId, host: { - agentVersion, - protocolVersion + agentVersion } }) @@ -464,7 +460,7 @@ describe('Identify', () => { const storedProtocolVersion = libp2p.peerStore.metadataBook.getValue(localPeer, 'ProtocolVersion') expect(agentVersion).to.eql(unit8ArrayToString(storedAgentVersion)) - expect(protocolVersion).to.eql(unit8ArrayToString(storedProtocolVersion)) + expect(storedProtocolVersion).to.exist() }) }) })