From 6350a187c7c207086e42436ccbcabd59af6f5e3d Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 9 Dec 2020 16:13:25 +0100 Subject: [PATCH] fix: dial self (#826) --- src/errors.js | 1 + src/index.js | 5 +++++ test/connection-manager/index.node.js | 23 ++++++++++++++++++----- test/dialing/direct.spec.js | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/errors.js b/src/errors.js index 18e600c6..cbff9aa8 100644 --- a/src/errors.js +++ b/src/errors.js @@ -16,6 +16,7 @@ exports.codes = { ERR_NODE_NOT_STARTED: 'ERR_NODE_NOT_STARTED', ERR_ALREADY_ABORTED: 'ERR_ALREADY_ABORTED', ERR_NO_VALID_ADDRESSES: 'ERR_NO_VALID_ADDRESSES', + ERR_DIALED_SELF: 'ERR_DIALED_SELF', ERR_DISCOVERED_SELF: 'ERR_DISCOVERED_SELF', ERR_DUPLICATE_TRANSPORT: 'ERR_DUPLICATE_TRANSPORT', ERR_ENCRYPTION_FAILED: 'ERR_ENCRYPTION_FAILED', diff --git a/src/index.js b/src/index.js index 33ace0c0..1af237b9 100644 --- a/src/index.js +++ b/src/index.js @@ -335,6 +335,11 @@ class Libp2p extends EventEmitter { */ async dialProtocol (peer, protocols, options) { 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) if (!connection) { diff --git a/test/connection-manager/index.node.js b/test/connection-manager/index.node.js index f7820e88..c1cc4ebc 100644 --- a/test/connection-manager/index.node.js +++ b/test/connection-manager/index.node.js @@ -18,10 +18,16 @@ const listenMultiaddr = '/ip4/127.0.0.1/tcp/15002/ws' describe('Connection Manager', () => { let libp2p + let peerIds + + before(async () => { + peerIds = await peerUtils.createPeerId({ number: 2 }) + }) beforeEach(async () => { [libp2p] = await peerUtils.createPeer({ config: { + peerId: peerIds[0], addresses: { listen: [listenMultiaddr] }, @@ -33,12 +39,10 @@ describe('Connection Manager', () => { afterEach(() => libp2p.stop()) 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 conn2 = await mockConnection({ localPeer, remotePeer }) - - const id = remotePeer.toB58String() + const id = peerIds[1].toB58String() // Add connection to the connectionManager libp2p.connectionManager.onConnect(conn1) @@ -57,6 +61,7 @@ describe('Connection Manager', () => { it('should add connection on dial and remove on node stop', async () => { const [remoteLibp2p] = await peerUtils.createPeer({ config: { + peerId: peerIds[1], addresses: { listen: ['/ip4/127.0.0.1/tcp/15003/ws'] }, @@ -89,9 +94,16 @@ describe('Connection Manager', () => { }) describe('libp2p.connections', () => { + let peerIds + + before(async () => { + peerIds = await peerUtils.createPeerId({ number: 2 }) + }) + it('libp2p.connections gets the connectionManager conns', async () => { const [libp2p] = await peerUtils.createPeer({ config: { + peerId: peerIds[0], addresses: { listen: ['/ip4/127.0.0.1/tcp/15003/ws'] }, @@ -100,6 +112,7 @@ describe('libp2p.connections', () => { }) const [remoteLibp2p] = await peerUtils.createPeer({ config: { + peerId: peerIds[1], addresses: { listen: ['/ip4/127.0.0.1/tcp/15004/ws'] }, diff --git a/test/dialing/direct.spec.js b/test/dialing/direct.spec.js index 540f528b..f7aa6d99 100644 --- a/test/dialing/direct.spec.js +++ b/test/dialing/direct.spec.js @@ -409,5 +409,20 @@ describe('Dialing (direct, WebSockets)', () => { 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) + }) }) })