mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-01 13:32:14 +00:00
fix: revert new identify protocol versions
This commit is contained in:
parent
4dfa8094d8
commit
a798c65286
@ -3,9 +3,7 @@
|
|||||||
exports.messages = {
|
exports.messages = {
|
||||||
NOT_STARTED_YET: 'The libp2p node is not started yet',
|
NOT_STARTED_YET: 'The libp2p node is not started yet',
|
||||||
DHT_DISABLED: 'DHT is not available',
|
DHT_DISABLED: 'DHT is not available',
|
||||||
CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required',
|
CONN_ENCRYPTION_REQUIRED: 'At least one connection encryption module is required'
|
||||||
ERR_INVALID_ENVELOPE: 'Invalid envelope received',
|
|
||||||
ERR_INVALID_PEER_RECORD: 'Invalid peer record received'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.codes = {
|
exports.codes = {
|
||||||
@ -22,8 +20,6 @@ exports.codes = {
|
|||||||
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
|
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
|
||||||
ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED',
|
ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED',
|
||||||
ERR_HOP_REQUEST_FAILED: 'ERR_HOP_REQUEST_FAILED',
|
ERR_HOP_REQUEST_FAILED: 'ERR_HOP_REQUEST_FAILED',
|
||||||
ERR_INVALID_ENVELOPE: 'ERR_INVALID_ENVELOPE',
|
|
||||||
ERR_INVALID_PEER_RECORD: 'ERR_INVALID_PEER_RECORD',
|
|
||||||
ERR_INVALID_KEY: 'ERR_INVALID_KEY',
|
ERR_INVALID_KEY: 'ERR_INVALID_KEY',
|
||||||
ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE',
|
ERR_INVALID_MESSAGE: 'ERR_INVALID_MESSAGE',
|
||||||
ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS',
|
ERR_INVALID_PARAMETERS: 'ERR_INVALID_PARAMETERS',
|
||||||
|
@ -4,9 +4,5 @@ const libp2pVersion = require('../../package.json').version
|
|||||||
|
|
||||||
module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0'
|
module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0'
|
||||||
module.exports.AGENT_VERSION = `js-libp2p/${libp2pVersion}`
|
module.exports.AGENT_VERSION = `js-libp2p/${libp2pVersion}`
|
||||||
module.exports.MULTICODEC_IDENTIFY = '/p2p/id/1.1.0'
|
module.exports.MULTICODEC_IDENTIFY = '/ipfs/id/1.0.0'
|
||||||
module.exports.MULTICODEC_IDENTIFY_PUSH = '/p2p/id/push/1.1.0'
|
module.exports.MULTICODEC_IDENTIFY_PUSH = '/ipfs/id/push/1.0.0'
|
||||||
|
|
||||||
// Legacy
|
|
||||||
module.exports.MULTICODEC_IDENTIFY_1_0_0 = '/ipfs/id/1.0.0'
|
|
||||||
module.exports.MULTICODEC_IDENTIFY_PUSH_1_0_0 = '/ipfs/id/push/1.0.0'
|
|
||||||
|
@ -22,14 +22,12 @@ const PeerRecord = require('../record/peer-record')
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
MULTICODEC_IDENTIFY,
|
MULTICODEC_IDENTIFY,
|
||||||
MULTICODEC_IDENTIFY_1_0_0,
|
|
||||||
MULTICODEC_IDENTIFY_PUSH,
|
MULTICODEC_IDENTIFY_PUSH,
|
||||||
MULTICODEC_IDENTIFY_PUSH_1_0_0,
|
|
||||||
AGENT_VERSION,
|
AGENT_VERSION,
|
||||||
PROTOCOL_VERSION
|
PROTOCOL_VERSION
|
||||||
} = require('./consts')
|
} = require('./consts')
|
||||||
|
|
||||||
const { messages, codes } = require('../errors')
|
const { codes } = require('../errors')
|
||||||
|
|
||||||
class IdentifyService {
|
class IdentifyService {
|
||||||
/**
|
/**
|
||||||
@ -97,7 +95,7 @@ class IdentifyService {
|
|||||||
push (connections) {
|
push (connections) {
|
||||||
const pushes = connections.map(async connection => {
|
const pushes = connections.map(async connection => {
|
||||||
try {
|
try {
|
||||||
const { stream } = await connection.newStream([MULTICODEC_IDENTIFY_PUSH, MULTICODEC_IDENTIFY_PUSH_1_0_0])
|
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY_PUSH)
|
||||||
const signedPeerRecord = await this._getSelfPeerRecord()
|
const signedPeerRecord = await this._getSelfPeerRecord()
|
||||||
|
|
||||||
await pipe(
|
await pipe(
|
||||||
@ -145,7 +143,7 @@ class IdentifyService {
|
|||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
async identify (connection) {
|
async identify (connection) {
|
||||||
const { protocol, stream } = await connection.newStream([MULTICODEC_IDENTIFY, MULTICODEC_IDENTIFY_1_0_0])
|
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY)
|
||||||
const [data] = await pipe(
|
const [data] = await pipe(
|
||||||
[],
|
[],
|
||||||
stream,
|
stream,
|
||||||
@ -183,40 +181,26 @@ class IdentifyService {
|
|||||||
// Get the observedAddr if there is one
|
// Get the observedAddr if there is one
|
||||||
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
|
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
|
||||||
|
|
||||||
// LEGACY: differentiate message with SignedPeerRecord
|
let addresses
|
||||||
if (protocol === MULTICODEC_IDENTIFY_1_0_0) {
|
|
||||||
// Update peers data in PeerStore
|
|
||||||
this.peerStore.addressBook.set(id, listenAddrs.map((addr) => multiaddr(addr)))
|
|
||||||
this.peerStore.protoBook.set(id, protocols)
|
|
||||||
|
|
||||||
// TODO: Track our observed address so that we can score it
|
|
||||||
log('received observed address of %s', observedAddr)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open envelope and verify if is authenticated
|
|
||||||
let envelope
|
|
||||||
try {
|
try {
|
||||||
envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
|
const envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
|
||||||
} catch (err) {
|
const peerRecord = await PeerRecord.createFromProtobuf(envelope.payload)
|
||||||
log('received invalid envelope, discard it')
|
|
||||||
throw errCode(new Error(messages.ERR_INVALID_ENVELOPE), codes.ERR_INVALID_ENVELOPE)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode peer record
|
addresses = peerRecord.multiaddrs
|
||||||
let peerRecord
|
|
||||||
try {
|
|
||||||
peerRecord = await PeerRecord.createFromProtobuf(envelope.payload)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log('received invalid peer record, discard it')
|
log('received invalid envelope, discard it and fallback to listenAddrs is available')
|
||||||
throw errCode(new Error(messages.ERR_INVALID_PEER_RECORD), codes.ERR_INVALID_PEER_RECORD)
|
// Try Legacy
|
||||||
|
addresses = listenAddrs
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Store as certified record
|
|
||||||
|
|
||||||
// Update peers data in PeerStore
|
// Update peers data in PeerStore
|
||||||
this.peerStore.addressBook.set(id, peerRecord.multiaddrs.map((addr) => multiaddr(addr)))
|
try {
|
||||||
|
this.peerStore.addressBook.set(id, addresses.map((addr) => multiaddr(addr)))
|
||||||
|
} catch (err) {
|
||||||
|
log.error('received invalid addrs', err)
|
||||||
|
}
|
||||||
|
|
||||||
this.peerStore.protoBook.set(id, protocols)
|
this.peerStore.protoBook.set(id, protocols)
|
||||||
this.peerStore.metadataBook.set(id, 'AgentVersion', Buffer.from(message.agentVersion))
|
this.peerStore.metadataBook.set(id, 'AgentVersion', Buffer.from(message.agentVersion))
|
||||||
|
|
||||||
@ -236,10 +220,8 @@ class IdentifyService {
|
|||||||
handleMessage ({ connection, stream, protocol }) {
|
handleMessage ({ connection, stream, protocol }) {
|
||||||
switch (protocol) {
|
switch (protocol) {
|
||||||
case MULTICODEC_IDENTIFY:
|
case MULTICODEC_IDENTIFY:
|
||||||
case MULTICODEC_IDENTIFY_1_0_0:
|
|
||||||
return this._handleIdentify({ connection, stream })
|
return this._handleIdentify({ connection, stream })
|
||||||
case MULTICODEC_IDENTIFY_PUSH:
|
case MULTICODEC_IDENTIFY_PUSH:
|
||||||
case MULTICODEC_IDENTIFY_PUSH_1_0_0:
|
|
||||||
return this._handlePush({ connection, stream })
|
return this._handlePush({ connection, stream })
|
||||||
default:
|
default:
|
||||||
log.error('cannot handle unknown protocol %s', protocol)
|
log.error('cannot handle unknown protocol %s', protocol)
|
||||||
@ -309,45 +291,23 @@ class IdentifyService {
|
|||||||
|
|
||||||
const id = connection.remotePeer
|
const id = connection.remotePeer
|
||||||
|
|
||||||
// Legacy
|
let addresses
|
||||||
if (!message.signedPeerRecord) {
|
|
||||||
try {
|
|
||||||
this.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr)))
|
|
||||||
} catch (err) {
|
|
||||||
return log.error('received invalid listen addrs', err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the protocols
|
try {
|
||||||
this.peerStore.protoBook.set(id, message.protocols)
|
const envelope = await Envelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
|
||||||
|
const peerRecord = await PeerRecord.createFromProtobuf(envelope.payload)
|
||||||
|
|
||||||
return
|
addresses = peerRecord.multiaddrs
|
||||||
|
} catch (err) {
|
||||||
|
log('received invalid envelope, discard it and fallback to listenAddrs is available')
|
||||||
|
// Try Legacy
|
||||||
|
addresses = message.listenAddrs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open envelope and verify if is authenticated
|
|
||||||
let envelope
|
|
||||||
try {
|
try {
|
||||||
envelope = await Envelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
|
this.peerStore.addressBook.set(id, addresses.map((addr) => multiaddr(addr)))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log('received invalid envelope, discard it')
|
log.error('received invalid addrs', err)
|
||||||
throw errCode(new Error(messages.ERR_INVALID_ENVELOPE), codes.ERR_INVALID_ENVELOPE)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode peer record
|
|
||||||
let peerRecord
|
|
||||||
try {
|
|
||||||
peerRecord = await PeerRecord.createFromProtobuf(envelope.payload)
|
|
||||||
} catch (err) {
|
|
||||||
log('received invalid peer record, discard it')
|
|
||||||
throw errCode(new Error(messages.ERR_INVALID_PEER_RECORD), codes.ERR_INVALID_PEER_RECORD)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update peers data in PeerStore
|
|
||||||
try {
|
|
||||||
// TODO: Store as certified record
|
|
||||||
|
|
||||||
this.peerStore.addressBook.set(id, peerRecord.multiaddrs.map((addr) => multiaddr(addr)))
|
|
||||||
} catch (err) {
|
|
||||||
return log.error('received invalid listen addrs', err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the protocols
|
// Update the protocols
|
||||||
@ -359,20 +319,25 @@ class IdentifyService {
|
|||||||
* @return {Buffer}
|
* @return {Buffer}
|
||||||
*/
|
*/
|
||||||
async _getSelfPeerRecord () {
|
async _getSelfPeerRecord () {
|
||||||
// TODO: Verify if updated
|
// TODO: support invalidation when dynamic multiaddrs are supported
|
||||||
if (this._selfRecord) {
|
if (this._selfRecord) {
|
||||||
return this._selfRecord
|
return this._selfRecord
|
||||||
}
|
}
|
||||||
|
|
||||||
const peerRecord = new PeerRecord({
|
try {
|
||||||
peerId: this.peerId,
|
const peerRecord = new PeerRecord({
|
||||||
multiaddrs: this._libp2p.multiaddrs
|
peerId: this.peerId,
|
||||||
})
|
multiaddrs: this._libp2p.multiaddrs
|
||||||
const envelope = await Envelope.seal(peerRecord, this.peerId)
|
})
|
||||||
|
const envelope = await Envelope.seal(peerRecord, this.peerId)
|
||||||
|
|
||||||
this._selfRecord = envelope.marshal()
|
this._selfRecord = envelope.marshal()
|
||||||
|
|
||||||
return this._selfRecord
|
return this._selfRecord
|
||||||
|
} catch (err) {
|
||||||
|
log.error('failed to get self peer record')
|
||||||
|
}
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -383,8 +348,6 @@ module.exports.IdentifyService = IdentifyService
|
|||||||
*/
|
*/
|
||||||
module.exports.multicodecs = {
|
module.exports.multicodecs = {
|
||||||
IDENTIFY: MULTICODEC_IDENTIFY,
|
IDENTIFY: MULTICODEC_IDENTIFY,
|
||||||
IDENTIFY_1_0_0: MULTICODEC_IDENTIFY_1_0_0,
|
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH
|
||||||
IDENTIFY_PUSH: MULTICODEC_IDENTIFY_PUSH,
|
|
||||||
IDENTIFY_PUSH_1_0_0: MULTICODEC_IDENTIFY_PUSH_1_0_0
|
|
||||||
}
|
}
|
||||||
module.exports.Message = Message
|
module.exports.Message = Message
|
||||||
|
@ -18,6 +18,7 @@ const { codes: Errors } = require('../../src/errors')
|
|||||||
const { IdentifyService, multicodecs } = require('../../src/identify')
|
const { IdentifyService, multicodecs } = require('../../src/identify')
|
||||||
const Peers = require('../fixtures/peers')
|
const Peers = require('../fixtures/peers')
|
||||||
const Libp2p = require('../../src')
|
const Libp2p = require('../../src')
|
||||||
|
const Envelope = require('../../src/record/envelope')
|
||||||
const baseOptions = require('../utils/base-options.browser')
|
const baseOptions = require('../utils/base-options.browser')
|
||||||
const pkg = require('../../package.json')
|
const pkg = require('../../package.json')
|
||||||
|
|
||||||
@ -25,19 +26,13 @@ const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
|
|||||||
const remoteAddr = MULTIADDRS_WEBSOCKETS[0]
|
const remoteAddr = MULTIADDRS_WEBSOCKETS[0]
|
||||||
const listenMaddrs = [multiaddr('/ip4/127.0.0.1/tcp/15002/ws')]
|
const listenMaddrs = [multiaddr('/ip4/127.0.0.1/tcp/15002/ws')]
|
||||||
|
|
||||||
const protocols = new Map([
|
|
||||||
[multicodecs.IDENTIFY, () => { }],
|
|
||||||
[multicodecs.IDENTIFY_PUSH, () => { }]
|
|
||||||
])
|
|
||||||
|
|
||||||
const protocolsLegacy = new Map([
|
|
||||||
[multicodecs.IDENTIFY_1_0_0, () => { }],
|
|
||||||
[multicodecs.IDENTIFY_PUSH_1_0_0, () => { }]
|
|
||||||
])
|
|
||||||
|
|
||||||
describe('Identify', () => {
|
describe('Identify', () => {
|
||||||
let localPeer
|
let localPeer
|
||||||
let remotePeer
|
let remotePeer
|
||||||
|
const protocols = new Map([
|
||||||
|
[multicodecs.IDENTIFY, () => {}],
|
||||||
|
[multicodecs.IDENTIFY_PUSH, () => {}]
|
||||||
|
])
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
[localPeer, remotePeer] = (await Promise.all([
|
[localPeer, remotePeer] = (await Promise.all([
|
||||||
@ -50,7 +45,7 @@ describe('Identify', () => {
|
|||||||
sinon.restore()
|
sinon.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should be able to identify another peer with 1.0.0 legacy protocol', async () => {
|
it('should be able to identify another peer', async () => {
|
||||||
const localIdentify = new IdentifyService({
|
const localIdentify = new IdentifyService({
|
||||||
libp2p: {
|
libp2p: {
|
||||||
peerId: localPeer,
|
peerId: localPeer,
|
||||||
@ -65,7 +60,7 @@ describe('Identify', () => {
|
|||||||
},
|
},
|
||||||
multiaddrs: listenMaddrs
|
multiaddrs: listenMaddrs
|
||||||
},
|
},
|
||||||
protocols: protocolsLegacy
|
protocols
|
||||||
})
|
})
|
||||||
|
|
||||||
const remoteIdentify = new IdentifyService({
|
const remoteIdentify = new IdentifyService({
|
||||||
@ -74,15 +69,15 @@ describe('Identify', () => {
|
|||||||
connectionManager: new EventEmitter(),
|
connectionManager: new EventEmitter(),
|
||||||
multiaddrs: listenMaddrs
|
multiaddrs: listenMaddrs
|
||||||
},
|
},
|
||||||
protocols: protocolsLegacy
|
protocols
|
||||||
})
|
})
|
||||||
|
|
||||||
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
const observedAddr = multiaddr('/ip4/127.0.0.1/tcp/1234')
|
||||||
const localConnectionMock = { newStream: () => { }, remotePeer }
|
const localConnectionMock = { newStream: () => {}, remotePeer }
|
||||||
const remoteConnectionMock = { remoteAddr: observedAddr }
|
const remoteConnectionMock = { remoteAddr: observedAddr }
|
||||||
|
|
||||||
const [local, remote] = duplexPair()
|
const [local, remote] = duplexPair()
|
||||||
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_1_0_0 })
|
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })
|
||||||
|
|
||||||
sinon.spy(localIdentify.peerStore.addressBook, 'set')
|
sinon.spy(localIdentify.peerStore.addressBook, 'set')
|
||||||
sinon.spy(localIdentify.peerStore.protoBook, 'set')
|
sinon.spy(localIdentify.peerStore.protoBook, 'set')
|
||||||
@ -93,7 +88,7 @@ describe('Identify', () => {
|
|||||||
remoteIdentify.handleMessage({
|
remoteIdentify.handleMessage({
|
||||||
connection: remoteConnectionMock,
|
connection: remoteConnectionMock,
|
||||||
stream: remote,
|
stream: remote,
|
||||||
protocol: multicodecs.IDENTIFY_1_0_0
|
protocol: multicodecs.IDENTIFY
|
||||||
})
|
})
|
||||||
])
|
])
|
||||||
|
|
||||||
@ -108,14 +103,15 @@ describe('Identify', () => {
|
|||||||
expect(call.args[1][0].equals(listenMaddrs[0]))
|
expect(call.args[1][0].equals(listenMaddrs[0]))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should be able to identify another peer', async () => {
|
// LEGACY
|
||||||
|
it('should be able to identify another peer with no certified peer records support', async () => {
|
||||||
const localIdentify = new IdentifyService({
|
const localIdentify = new IdentifyService({
|
||||||
libp2p: {
|
libp2p: {
|
||||||
peerId: localPeer,
|
peerId: localPeer,
|
||||||
connectionManager: new EventEmitter(),
|
connectionManager: new EventEmitter(),
|
||||||
peerStore: {
|
peerStore: {
|
||||||
addressBook: {
|
addressBook: {
|
||||||
set: () => { }
|
set: () => {}
|
||||||
},
|
},
|
||||||
protoBook: {
|
protoBook: {
|
||||||
set: () => { }
|
set: () => { }
|
||||||
@ -144,6 +140,7 @@ describe('Identify', () => {
|
|||||||
|
|
||||||
const [local, remote] = duplexPair()
|
const [local, remote] = duplexPair()
|
||||||
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })
|
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })
|
||||||
|
sinon.stub(Envelope, 'openAndCertify').throws()
|
||||||
|
|
||||||
sinon.spy(localIdentify.peerStore.addressBook, 'set')
|
sinon.spy(localIdentify.peerStore.addressBook, 'set')
|
||||||
sinon.spy(localIdentify.peerStore.protoBook, 'set')
|
sinon.spy(localIdentify.peerStore.protoBook, 'set')
|
||||||
@ -224,69 +221,6 @@ describe('Identify', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('push', () => {
|
describe('push', () => {
|
||||||
it('should be able to push identify updates to another peer with 1.0.0 legacy protocols', async () => {
|
|
||||||
const connectionManager = new EventEmitter()
|
|
||||||
connectionManager.getConnection = () => {}
|
|
||||||
|
|
||||||
const localIdentify = new IdentifyService({
|
|
||||||
libp2p: {
|
|
||||||
peerId: localPeer,
|
|
||||||
connectionManager: new EventEmitter(),
|
|
||||||
multiaddrs: listenMaddrs
|
|
||||||
},
|
|
||||||
protocols: new Map([
|
|
||||||
[multicodecs.IDENTIFY_1_0_0],
|
|
||||||
[multicodecs.IDENTIFY_PUSH_1_0_0],
|
|
||||||
['/echo/1.0.0']
|
|
||||||
])
|
|
||||||
})
|
|
||||||
const remoteIdentify = new IdentifyService({
|
|
||||||
libp2p: {
|
|
||||||
peerId: remotePeer,
|
|
||||||
connectionManager,
|
|
||||||
peerStore: {
|
|
||||||
addressBook: {
|
|
||||||
set: () => { }
|
|
||||||
},
|
|
||||||
protoBook: {
|
|
||||||
set: () => { }
|
|
||||||
}
|
|
||||||
},
|
|
||||||
multiaddrs: []
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Setup peer protocols and multiaddrs
|
|
||||||
const localProtocols = new Set([multicodecs.IDENTIFY_1_0_0, multicodecs.IDENTIFY_PUSH_1_0_0, '/echo/1.0.0'])
|
|
||||||
const localConnectionMock = { newStream: () => {} }
|
|
||||||
const remoteConnectionMock = { remotePeer: localPeer }
|
|
||||||
|
|
||||||
const [local, remote] = duplexPair()
|
|
||||||
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_PUSH_1_0_0 })
|
|
||||||
|
|
||||||
sinon.spy(remoteIdentify.peerStore.addressBook, 'set')
|
|
||||||
sinon.spy(remoteIdentify.peerStore.protoBook, 'set')
|
|
||||||
|
|
||||||
// Run identify
|
|
||||||
await Promise.all([
|
|
||||||
localIdentify.push([localConnectionMock]),
|
|
||||||
remoteIdentify.handleMessage({
|
|
||||||
connection: remoteConnectionMock,
|
|
||||||
stream: remote,
|
|
||||||
protocol: multicodecs.IDENTIFY_PUSH_1_0_0
|
|
||||||
})
|
|
||||||
])
|
|
||||||
|
|
||||||
expect(remoteIdentify.peerStore.addressBook.set.callCount).to.equal(1)
|
|
||||||
expect(remoteIdentify.peerStore.protoBook.set.callCount).to.equal(1)
|
|
||||||
const [peerId, multiaddrs] = remoteIdentify.peerStore.addressBook.set.firstCall.args
|
|
||||||
expect(peerId.bytes).to.eql(localPeer.bytes)
|
|
||||||
expect(multiaddrs).to.eql(listenMaddrs)
|
|
||||||
const [peerId2, protocols] = remoteIdentify.peerStore.protoBook.set.firstCall.args
|
|
||||||
expect(peerId2.bytes).to.eql(localPeer.bytes)
|
|
||||||
expect(protocols).to.eql(Array.from(localProtocols))
|
|
||||||
})
|
|
||||||
|
|
||||||
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()
|
||||||
connectionManager.getConnection = () => { }
|
connectionManager.getConnection = () => { }
|
||||||
@ -349,6 +283,71 @@ describe('Identify', () => {
|
|||||||
expect(peerId2.bytes).to.eql(localPeer.bytes)
|
expect(peerId2.bytes).to.eql(localPeer.bytes)
|
||||||
expect(protocols).to.eql(Array.from(localProtocols))
|
expect(protocols).to.eql(Array.from(localProtocols))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// LEGACY
|
||||||
|
it('should be able to push identify updates to another peer with no certified peer records support', async () => {
|
||||||
|
const connectionManager = new EventEmitter()
|
||||||
|
connectionManager.getConnection = () => { }
|
||||||
|
|
||||||
|
const localIdentify = new IdentifyService({
|
||||||
|
libp2p: {
|
||||||
|
peerId: localPeer,
|
||||||
|
connectionManager: new EventEmitter(),
|
||||||
|
multiaddrs: listenMaddrs
|
||||||
|
},
|
||||||
|
protocols: new Map([
|
||||||
|
[multicodecs.IDENTIFY],
|
||||||
|
[multicodecs.IDENTIFY_PUSH],
|
||||||
|
['/echo/1.0.0']
|
||||||
|
])
|
||||||
|
})
|
||||||
|
const remoteIdentify = new IdentifyService({
|
||||||
|
libp2p: {
|
||||||
|
peerId: remotePeer,
|
||||||
|
connectionManager,
|
||||||
|
peerStore: {
|
||||||
|
addressBook: {
|
||||||
|
set: () => { }
|
||||||
|
},
|
||||||
|
protoBook: {
|
||||||
|
set: () => { }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
multiaddrs: []
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Setup peer protocols and multiaddrs
|
||||||
|
const localProtocols = new Set([multicodecs.IDENTIFY, multicodecs.IDENTIFY_PUSH, '/echo/1.0.0'])
|
||||||
|
const localConnectionMock = { newStream: () => {} }
|
||||||
|
const remoteConnectionMock = { remotePeer: localPeer }
|
||||||
|
|
||||||
|
const [local, remote] = duplexPair()
|
||||||
|
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY_PUSH })
|
||||||
|
sinon.stub(Envelope, 'openAndCertify').throws()
|
||||||
|
|
||||||
|
sinon.spy(remoteIdentify.peerStore.addressBook, 'set')
|
||||||
|
sinon.spy(remoteIdentify.peerStore.protoBook, 'set')
|
||||||
|
|
||||||
|
// Run identify
|
||||||
|
await Promise.all([
|
||||||
|
localIdentify.push([localConnectionMock]),
|
||||||
|
remoteIdentify.handleMessage({
|
||||||
|
connection: remoteConnectionMock,
|
||||||
|
stream: remote,
|
||||||
|
protocol: multicodecs.IDENTIFY_PUSH
|
||||||
|
})
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(remoteIdentify.peerStore.addressBook.set.callCount).to.equal(1)
|
||||||
|
expect(remoteIdentify.peerStore.protoBook.set.callCount).to.equal(1)
|
||||||
|
const [peerId, multiaddrs] = remoteIdentify.peerStore.addressBook.set.firstCall.args
|
||||||
|
expect(peerId.bytes).to.eql(localPeer.bytes)
|
||||||
|
expect(multiaddrs).to.eql(listenMaddrs)
|
||||||
|
const [peerId2, protocols] = remoteIdentify.peerStore.protoBook.set.firstCall.args
|
||||||
|
expect(peerId2.bytes).to.eql(localPeer.bytes)
|
||||||
|
expect(protocols).to.eql(Array.from(localProtocols))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('libp2p.dialer.identifyService', () => {
|
describe('libp2p.dialer.identifyService', () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user