mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
fix: dial self (#826)
This commit is contained in:
parent
8e3bb09279
commit
6350a187c7
@ -16,6 +16,7 @@ exports.codes = {
|
|||||||
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
|
ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED',
|
||||||
ERR_ALREADY_ABORTED: 'ERR_ALREADY_ABORTED',
|
ERR_ALREADY_ABORTED: 'ERR_ALREADY_ABORTED',
|
||||||
ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES',
|
ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES',
|
||||||
|
ERR_DIALED_SELF: 'ERR_DIALED_SELF',
|
||||||
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF',
|
ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF',
|
||||||
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
|
ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT',
|
||||||
ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED',
|
ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED',
|
||||||
|
@ -335,6 +335,11 @@ class Libp2p extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
async dialProtocol (peer, protocols, options) {
|
async dialProtocol (peer, protocols, options) {
|
||||||
const { id, multiaddrs } = getPeer(peer)
|
const { id, multiaddrs } = getPeer(peer)
|
||||||
|
|
||||||
|
if (id.equals(this.peerId)) {
|
||||||
|
throw errCode(new Error('Cannot dial self'), codes.ERR_DIALED_SELF)
|
||||||
|
}
|
||||||
|
|
||||||
let connection = this.connectionManager.get(id)
|
let connection = this.connectionManager.get(id)
|
||||||
|
|
||||||
if (!connection) {
|
if (!connection) {
|
||||||
|
@ -18,10 +18,16 @@ const listenMultiaddr = '/ip4/127.0.0.1/tcp/15002/ws'
|
|||||||
|
|
||||||
describe('Connection Manager', () => {
|
describe('Connection Manager', () => {
|
||||||
let libp2p
|
let libp2p
|
||||||
|
let peerIds
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
peerIds = await peerUtils.createPeerId({ number: 2 })
|
||||||
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
[libp2p] = await peerUtils.createPeer({
|
[libp2p] = await peerUtils.createPeer({
|
||||||
config: {
|
config: {
|
||||||
|
peerId: peerIds[0],
|
||||||
addresses: {
|
addresses: {
|
||||||
listen: [listenMultiaddr]
|
listen: [listenMultiaddr]
|
||||||
},
|
},
|
||||||
@ -33,12 +39,10 @@ describe('Connection Manager', () => {
|
|||||||
afterEach(() => libp2p.stop())
|
afterEach(() => libp2p.stop())
|
||||||
|
|
||||||
it('should filter connections on disconnect, removing the closed one', async () => {
|
it('should filter connections on disconnect, removing the closed one', async () => {
|
||||||
const [localPeer, remotePeer] = await peerUtils.createPeerId({ number: 2 })
|
const conn1 = await mockConnection({ localPeer: peerIds[0], remotePeer: peerIds[1] })
|
||||||
|
const conn2 = await mockConnection({ localPeer: peerIds[0], remotePeer: peerIds[1] })
|
||||||
|
|
||||||
const conn1 = await mockConnection({ localPeer, remotePeer })
|
const id = peerIds[1].toB58String()
|
||||||
const conn2 = await mockConnection({ localPeer, remotePeer })
|
|
||||||
|
|
||||||
const id = remotePeer.toB58String()
|
|
||||||
|
|
||||||
// Add connection to the connectionManager
|
// Add connection to the connectionManager
|
||||||
libp2p.connectionManager.onConnect(conn1)
|
libp2p.connectionManager.onConnect(conn1)
|
||||||
@ -57,6 +61,7 @@ describe('Connection Manager', () => {
|
|||||||
it('should add connection on dial and remove on node stop', async () => {
|
it('should add connection on dial and remove on node stop', async () => {
|
||||||
const [remoteLibp2p] = await peerUtils.createPeer({
|
const [remoteLibp2p] = await peerUtils.createPeer({
|
||||||
config: {
|
config: {
|
||||||
|
peerId: peerIds[1],
|
||||||
addresses: {
|
addresses: {
|
||||||
listen: ['/ip4/127.0.0.1/tcp/15003/ws']
|
listen: ['/ip4/127.0.0.1/tcp/15003/ws']
|
||||||
},
|
},
|
||||||
@ -89,9 +94,16 @@ describe('Connection Manager', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('libp2p.connections', () => {
|
describe('libp2p.connections', () => {
|
||||||
|
let peerIds
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
peerIds = await peerUtils.createPeerId({ number: 2 })
|
||||||
|
})
|
||||||
|
|
||||||
it('libp2p.connections gets the connectionManager conns', async () => {
|
it('libp2p.connections gets the connectionManager conns', async () => {
|
||||||
const [libp2p] = await peerUtils.createPeer({
|
const [libp2p] = await peerUtils.createPeer({
|
||||||
config: {
|
config: {
|
||||||
|
peerId: peerIds[0],
|
||||||
addresses: {
|
addresses: {
|
||||||
listen: ['/ip4/127.0.0.1/tcp/15003/ws']
|
listen: ['/ip4/127.0.0.1/tcp/15003/ws']
|
||||||
},
|
},
|
||||||
@ -100,6 +112,7 @@ describe('libp2p.connections', () => {
|
|||||||
})
|
})
|
||||||
const [remoteLibp2p] = await peerUtils.createPeer({
|
const [remoteLibp2p] = await peerUtils.createPeer({
|
||||||
config: {
|
config: {
|
||||||
|
peerId: peerIds[1],
|
||||||
addresses: {
|
addresses: {
|
||||||
listen: ['/ip4/127.0.0.1/tcp/15004/ws']
|
listen: ['/ip4/127.0.0.1/tcp/15004/ws']
|
||||||
},
|
},
|
||||||
|
@ -409,5 +409,20 @@ describe('Dialing (direct, WebSockets)', () => {
|
|||||||
|
|
||||||
expect(libp2p.dialer.destroy).to.have.property('callCount', 1)
|
expect(libp2p.dialer.destroy).to.have.property('callCount', 1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should fail to dial self', async () => {
|
||||||
|
libp2p = new Libp2p({
|
||||||
|
peerId,
|
||||||
|
modules: {
|
||||||
|
transport: [Transport],
|
||||||
|
streamMuxer: [Muxer],
|
||||||
|
connEncryption: [Crypto]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await expect(libp2p.dial(peerId))
|
||||||
|
.to.eventually.be.rejected()
|
||||||
|
.and.to.have.property('code', ErrorCodes.ERR_DIALED_SELF)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user