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
This commit is contained in:
Vasco Santos 2020-11-20 15:14:01 +01:00 committed by GitHub
parent 824a444f56
commit d0a9fada32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 12 deletions

View File

@ -92,6 +92,7 @@ Creates an instance of Libp2p.
| options.modules | [`Array<object>`](./CONFIGURATION.md#modules) | libp2p [modules](./CONFIGURATION.md#modules) to use | | options.modules | [`Array<object>`](./CONFIGURATION.md#modules) | libp2p [modules](./CONFIGURATION.md#modules) to use |
| [options.addresses] | `{ listen: Array<string>, announce: Array<string>, noAnnounce: Array<string> }` | Addresses for transport listening and to advertise to the network | | [options.addresses] | `{ listen: Array<string>, announce: Array<string>, noAnnounce: Array<string> }` | Addresses for transport listening and to advertise to the network |
| [options.config] | `object` | libp2p modules configuration and core configuration | | [options.config] | `object` | libp2p modules configuration and core configuration |
| [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.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.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) | | [options.datastore] | `object` | must implement [ipfs/interface-datastore](https://github.com/ipfs/interface-datastore) (in memory datastore will be used if not provided) |

View File

@ -4,6 +4,7 @@ const mergeOptions = require('merge-options')
const { dnsaddrResolver } = require('multiaddr/src/resolvers') const { dnsaddrResolver } = require('multiaddr/src/resolvers')
const Constants = require('./constants') const Constants = require('./constants')
const { AGENT_VERSION } = require('./identify/consts')
const { FaultTolerance } = require('./transport-manager') const { FaultTolerance } = require('./transport-manager')
@ -27,6 +28,9 @@ const DefaultConfig = {
dnsaddr: dnsaddrResolver dnsaddr: dnsaddrResolver
} }
}, },
host: {
agentVersion: AGENT_VERSION
},
metrics: { metrics: {
enabled: false enabled: false
}, },

View File

@ -83,6 +83,16 @@ class IdentifyService {
this._protocols = protocols this._protocols = protocols
this.handleMessage = this.handleMessage.bind(this) 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 signedPeerRecord = await this._getSelfPeerRecord()
const message = Message.encode({ const message = Message.encode({
protocolVersion: PROTOCOL_VERSION, protocolVersion: this._host.protocolVersion,
agentVersion: AGENT_VERSION, agentVersion: this._host.agentVersion,
publicKey, publicKey,
listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes), listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
signedPeerRecord, signedPeerRecord,

View File

@ -53,7 +53,8 @@ describe('Identify', () => {
peerId: localPeer, peerId: localPeer,
connectionManager: new EventEmitter(), connectionManager: new EventEmitter(),
peerStore: new PeerStore({ peerId: localPeer }), peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: listenMaddrs multiaddrs: listenMaddrs,
_options: { host: {} }
}, },
protocols protocols
}) })
@ -63,7 +64,8 @@ describe('Identify', () => {
peerId: remotePeer, peerId: remotePeer,
connectionManager: new EventEmitter(), connectionManager: new EventEmitter(),
peerStore: new PeerStore({ peerId: remotePeer }), peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: listenMaddrs multiaddrs: listenMaddrs,
_options: { host: {} }
}, },
protocols protocols
}) })
@ -106,7 +108,8 @@ describe('Identify', () => {
peerId: localPeer, peerId: localPeer,
connectionManager: new EventEmitter(), connectionManager: new EventEmitter(),
peerStore: new PeerStore({ peerId: localPeer }), peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: listenMaddrs multiaddrs: listenMaddrs,
_options: { host: {} }
}, },
protocols protocols
}) })
@ -116,7 +119,8 @@ describe('Identify', () => {
peerId: remotePeer, peerId: remotePeer,
connectionManager: new EventEmitter(), connectionManager: new EventEmitter(),
peerStore: new PeerStore({ peerId: remotePeer }), peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: listenMaddrs multiaddrs: listenMaddrs,
_options: { host: {} }
}, },
protocols protocols
}) })
@ -165,7 +169,8 @@ describe('Identify', () => {
peerId: localPeer, peerId: localPeer,
connectionManager: new EventEmitter(), connectionManager: new EventEmitter(),
peerStore: new PeerStore({ peerId: localPeer }), peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: [] multiaddrs: [],
_options: { host: {} }
}, },
protocols protocols
}) })
@ -174,7 +179,8 @@ describe('Identify', () => {
peerId: remotePeer, peerId: remotePeer,
connectionManager: new EventEmitter(), connectionManager: new EventEmitter(),
peerStore: new PeerStore({ peerId: remotePeer }), peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: [] multiaddrs: [],
_options: { host: {} }
}, },
protocols protocols
}) })
@ -201,6 +207,36 @@ describe('Identify', () => {
.and.to.have.property('code', Errors.ERR_INVALID_PEER) .and.to.have.property('code', Errors.ERR_INVALID_PEER)
}) })
it('should store host data and protocol version into metadataBook', () => {
const agentVersion = 'js-project/1.0.0'
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
}
}
},
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(storedProtocolVersion).to.exist()
})
describe('push', () => { describe('push', () => {
it('should be able to push identify updates to another peer', async () => { it('should be able to push identify updates to another peer', async () => {
const connectionManager = new EventEmitter() const connectionManager = new EventEmitter()
@ -211,7 +247,8 @@ describe('Identify', () => {
peerId: localPeer, peerId: localPeer,
connectionManager: new EventEmitter(), connectionManager: new EventEmitter(),
peerStore: new PeerStore({ peerId: localPeer }), peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: listenMaddrs multiaddrs: listenMaddrs,
_options: { host: {} }
}, },
protocols: new Map([ protocols: new Map([
[multicodecs.IDENTIFY], [multicodecs.IDENTIFY],
@ -224,7 +261,8 @@ describe('Identify', () => {
peerId: remotePeer, peerId: remotePeer,
connectionManager, connectionManager,
peerStore: new PeerStore({ peerId: remotePeer }), peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: [] multiaddrs: [],
_options: { host: {} }
} }
}) })
@ -272,7 +310,8 @@ describe('Identify', () => {
peerId: localPeer, peerId: localPeer,
connectionManager: new EventEmitter(), connectionManager: new EventEmitter(),
peerStore: new PeerStore({ peerId: localPeer }), peerStore: new PeerStore({ peerId: localPeer }),
multiaddrs: listenMaddrs multiaddrs: listenMaddrs,
_options: { host: {} }
}, },
protocols: new Map([ protocols: new Map([
[multicodecs.IDENTIFY], [multicodecs.IDENTIFY],
@ -285,7 +324,8 @@ describe('Identify', () => {
peerId: remotePeer, peerId: remotePeer,
connectionManager, connectionManager,
peerStore: new PeerStore({ peerId: remotePeer }), peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: [] multiaddrs: [],
_options: { host: {} }
} }
}) })
@ -404,5 +444,23 @@ describe('Identify', () => {
// Verify the streams close // Verify the streams close
await pWaitFor(() => connection.streams.length === 0) await pWaitFor(() => connection.streams.length === 0)
}) })
it('should store host data and protocol version into metadataBook', () => {
const agentVersion = 'js-project/1.0.0'
libp2p = new Libp2p({
...baseOptions,
peerId,
host: {
agentVersion
}
})
const storedAgentVersion = libp2p.peerStore.metadataBook.getValue(localPeer, 'AgentVersion')
const storedProtocolVersion = libp2p.peerStore.metadataBook.getValue(localPeer, 'ProtocolVersion')
expect(agentVersion).to.eql(unit8ArrayToString(storedAgentVersion))
expect(storedProtocolVersion).to.exist()
})
}) })
}) })