mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-31 00:12:00 +00:00
feat: connection gater (#1142)
Port of https://github.com/libp2p/go-libp2p-core/blob/master/connmgr/gater.go Adds a new configuration key `connectionGater` which allows denying the dialing of certain peers, individual multiaddrs and the creation of connections at certain points in the connection flow. Fixes: https://github.com/libp2p/js-libp2p/issues/175 Refs: https://github.com/libp2p/js-libp2p/issues/744 Refs: https://github.com/libp2p/js-libp2p/issues/769 Co-authored-by: mzdws <8580712+mzdws@user.noreply.gitee.com>
This commit is contained in:
@@ -7,12 +7,11 @@ const { CLOSED } = require('libp2p-interfaces/src/connection/status')
|
||||
|
||||
const delay = require('delay')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
const mockConnection = require('../utils/mockConnection')
|
||||
const baseOptions = require('../utils/base-options.browser')
|
||||
|
||||
const listenMultiaddr = '/ip4/127.0.0.1/tcp/15002/ws'
|
||||
const { codes } = require('../../src/errors')
|
||||
const { Multiaddr } = require('multiaddr')
|
||||
|
||||
describe('Connection Manager', () => {
|
||||
let libp2p
|
||||
@@ -27,7 +26,7 @@ describe('Connection Manager', () => {
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: [listenMultiaddr]
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules
|
||||
}
|
||||
@@ -305,4 +304,230 @@ describe('libp2p.connections', () => {
|
||||
await remoteLibp2p.stop()
|
||||
})
|
||||
})
|
||||
|
||||
describe('connection gater', () => {
|
||||
let libp2p
|
||||
let remoteLibp2p
|
||||
|
||||
beforeEach(async () => {
|
||||
[remoteLibp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[1],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
remoteLibp2p && await remoteLibp2p.stop()
|
||||
libp2p && await libp2p.stop()
|
||||
})
|
||||
|
||||
it('intercept peer dial', async () => {
|
||||
const denyDialPeer = sinon.stub().returns(true)
|
||||
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules,
|
||||
connectionGater: {
|
||||
denyDialPeer
|
||||
}
|
||||
}
|
||||
})
|
||||
await libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
|
||||
|
||||
await expect(libp2p.dial(remoteLibp2p.peerId))
|
||||
.to.eventually.be.rejected().with.property('code', codes.ERR_PEER_DIAL_INTERCEPTED)
|
||||
})
|
||||
|
||||
it('intercept addr dial', async () => {
|
||||
const denyDialMultiaddr = sinon.stub().returns(false)
|
||||
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules,
|
||||
connectionGater: {
|
||||
denyDialMultiaddr
|
||||
}
|
||||
}
|
||||
})
|
||||
await libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
|
||||
await libp2p.dialer.connectToPeer(remoteLibp2p.peerId)
|
||||
|
||||
const peerIdMultiaddr = new Multiaddr(`/p2p/${remoteLibp2p.peerId}`)
|
||||
|
||||
for (const multiaddr of remoteLibp2p.multiaddrs) {
|
||||
expect(denyDialMultiaddr.calledWith(remoteLibp2p.peerId, multiaddr.encapsulate(peerIdMultiaddr))).to.be.true()
|
||||
}
|
||||
})
|
||||
|
||||
it('intercept multiaddr store during multiaddr dial', async () => {
|
||||
const filterMultiaddrForPeer = sinon.stub().returns(true)
|
||||
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules,
|
||||
connectionGater: {
|
||||
filterMultiaddrForPeer
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const peerIdMultiaddr = new Multiaddr(`/p2p/${remoteLibp2p.peerId}`)
|
||||
const fullMultiaddr = remoteLibp2p.multiaddrs[0].encapsulate(peerIdMultiaddr)
|
||||
|
||||
await libp2p.dialer.connectToPeer(fullMultiaddr)
|
||||
|
||||
expect(filterMultiaddrForPeer.callCount).to.equal(2)
|
||||
|
||||
const args = filterMultiaddrForPeer.getCall(1).args
|
||||
expect(args[0].toString()).to.equal(remoteLibp2p.peerId.toString())
|
||||
expect(args[1].toString()).to.equal(fullMultiaddr.toString())
|
||||
})
|
||||
|
||||
it('intercept accept inbound connection', async () => {
|
||||
const denyInboundConnection = sinon.stub().returns(false)
|
||||
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules,
|
||||
connectionGater: {
|
||||
denyInboundConnection
|
||||
}
|
||||
}
|
||||
})
|
||||
await remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs)
|
||||
await remoteLibp2p.dial(libp2p.peerId)
|
||||
|
||||
expect(denyInboundConnection.called).to.be.true()
|
||||
})
|
||||
|
||||
it('intercept accept outbound connection', async () => {
|
||||
const denyOutboundConnection = sinon.stub().returns(false)
|
||||
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules,
|
||||
connectionGater: {
|
||||
denyOutboundConnection
|
||||
}
|
||||
}
|
||||
})
|
||||
await libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
|
||||
await libp2p.dial(remoteLibp2p.peerId)
|
||||
|
||||
expect(denyOutboundConnection.called).to.be.true()
|
||||
})
|
||||
|
||||
it('intercept inbound encrypted', async () => {
|
||||
const denyInboundEncryptedConnection = sinon.stub().returns(false)
|
||||
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules,
|
||||
connectionGater: {
|
||||
denyInboundEncryptedConnection
|
||||
}
|
||||
}
|
||||
})
|
||||
await remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs)
|
||||
await remoteLibp2p.dial(libp2p.peerId)
|
||||
|
||||
expect(denyInboundEncryptedConnection.called).to.be.true()
|
||||
expect(denyInboundEncryptedConnection.getCall(0)).to.have.nested.property('args[0].id').that.equalBytes(remoteLibp2p.peerId.id)
|
||||
})
|
||||
|
||||
it('intercept outbound encrypted', async () => {
|
||||
const denyOutboundEncryptedConnection = sinon.stub().returns(false)
|
||||
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules,
|
||||
connectionGater: {
|
||||
denyOutboundEncryptedConnection
|
||||
}
|
||||
}
|
||||
})
|
||||
await libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
|
||||
await libp2p.dial(remoteLibp2p.peerId)
|
||||
|
||||
expect(denyOutboundEncryptedConnection.called).to.be.true()
|
||||
expect(denyOutboundEncryptedConnection.getCall(0)).to.have.nested.property('args[0].id').that.equalBytes(remoteLibp2p.peerId.id)
|
||||
})
|
||||
|
||||
it('intercept inbound upgraded', async () => {
|
||||
const denyInboundUpgradedConnection = sinon.stub().returns(false)
|
||||
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules,
|
||||
connectionGater: {
|
||||
denyInboundUpgradedConnection
|
||||
}
|
||||
}
|
||||
})
|
||||
await remoteLibp2p.peerStore.addressBook.set(libp2p.peerId, libp2p.multiaddrs)
|
||||
await remoteLibp2p.dial(libp2p.peerId)
|
||||
|
||||
expect(denyInboundUpgradedConnection.called).to.be.true()
|
||||
expect(denyInboundUpgradedConnection.getCall(0)).to.have.nested.property('args[0].id').that.equalBytes(remoteLibp2p.peerId.id)
|
||||
})
|
||||
|
||||
it('intercept outbound upgraded', async () => {
|
||||
const denyOutboundUpgradedConnection = sinon.stub().returns(false)
|
||||
|
||||
;[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
peerId: peerIds[0],
|
||||
addresses: {
|
||||
listen: ['/ip4/127.0.0.1/tcp/0/ws']
|
||||
},
|
||||
modules: baseOptions.modules,
|
||||
connectionGater: {
|
||||
denyOutboundUpgradedConnection
|
||||
}
|
||||
}
|
||||
})
|
||||
await libp2p.peerStore.addressBook.set(remoteLibp2p.peerId, remoteLibp2p.multiaddrs)
|
||||
await libp2p.dial(remoteLibp2p.peerId)
|
||||
|
||||
expect(denyOutboundUpgradedConnection.called).to.be.true()
|
||||
expect(denyOutboundUpgradedConnection.getCall(0)).to.have.nested.property('args[0].id').that.equalBytes(remoteLibp2p.peerId.id)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@@ -27,7 +27,7 @@ const TransportManager = require('../../src/transport-manager')
|
||||
const { codes: ErrorCodes } = require('../../src/errors')
|
||||
const Protector = require('../../src/pnet')
|
||||
const swarmKeyBuffer = uint8ArrayFromString(require('../fixtures/swarm.key'))
|
||||
|
||||
const { mockConnectionGater } = require('../utils/mock-connection-gater')
|
||||
const mockUpgrader = require('../utils/mockUpgrader')
|
||||
const createMockConnection = require('../utils/mockConnection')
|
||||
const Peers = require('../fixtures/peers')
|
||||
@@ -37,6 +37,7 @@ const listenAddr = new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||
const unsupportedAddr = new Multiaddr('/ip4/127.0.0.1/tcp/9999/ws/p2p/QmckxVrJw1Yo8LqvmDJNUmdAsKtSbiKWmrXJFyKmUraBoN')
|
||||
|
||||
describe('Dialing (direct, TCP)', () => {
|
||||
const connectionGater = mockConnectionGater()
|
||||
let remoteTM
|
||||
let localTM
|
||||
let peerStore
|
||||
@@ -50,7 +51,8 @@ describe('Dialing (direct, TCP)', () => {
|
||||
|
||||
peerStore = new PeerStore({
|
||||
peerId: remotePeerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
remoteTM = new TransportManager({
|
||||
libp2p: {
|
||||
@@ -67,7 +69,8 @@ describe('Dialing (direct, TCP)', () => {
|
||||
peerId: localPeerId,
|
||||
peerStore: new PeerStore({
|
||||
peerId: localPeerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
},
|
||||
upgrader: mockUpgrader
|
||||
@@ -86,7 +89,11 @@ describe('Dialing (direct, TCP)', () => {
|
||||
})
|
||||
|
||||
it('should be able to connect to a remote node via its multiaddr', async () => {
|
||||
const dialer = new Dialer({ transportManager: localTM, peerStore })
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
|
||||
const connection = await dialer.connectToPeer(remoteAddr)
|
||||
expect(connection).to.exist()
|
||||
@@ -94,14 +101,22 @@ describe('Dialing (direct, TCP)', () => {
|
||||
})
|
||||
|
||||
it('should be able to connect to a remote node via its stringified multiaddr', async () => {
|
||||
const dialer = new Dialer({ transportManager: localTM, peerStore })
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
const connection = await dialer.connectToPeer(remoteAddr.toString())
|
||||
expect(connection).to.exist()
|
||||
await connection.close()
|
||||
})
|
||||
|
||||
it('should fail to connect to an unsupported multiaddr', async () => {
|
||||
const dialer = new Dialer({ transportManager: localTM, peerStore })
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
|
||||
await expect(dialer.connectToPeer(unsupportedAddr))
|
||||
.to.eventually.be.rejectedWith(Error)
|
||||
@@ -109,7 +124,11 @@ describe('Dialing (direct, TCP)', () => {
|
||||
})
|
||||
|
||||
it('should fail to connect if peer has no known addresses', async () => {
|
||||
const dialer = new Dialer({ transportManager: localTM, peerStore })
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
const peerId = await PeerId.createFromJSON(Peers[1])
|
||||
|
||||
await expect(dialer.connectToPeer(peerId))
|
||||
@@ -121,11 +140,13 @@ describe('Dialing (direct, TCP)', () => {
|
||||
const peerId = await PeerId.createFromJSON(Peers[0])
|
||||
const peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
|
||||
await peerStore.addressBook.set(peerId, remoteTM.getAddrs())
|
||||
@@ -143,7 +164,8 @@ describe('Dialing (direct, TCP)', () => {
|
||||
add: () => {},
|
||||
getMultiaddrsForPeer: () => [unsupportedAddr]
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
const peerId = await PeerId.createFromJSON(Peers[0])
|
||||
|
||||
@@ -161,7 +183,8 @@ describe('Dialing (direct, TCP)', () => {
|
||||
add: () => { },
|
||||
getMultiaddrsForPeer: () => [...remoteAddrs, unsupportedAddr]
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
const peerId = await PeerId.createFromJSON(Peers[0])
|
||||
|
||||
@@ -176,7 +199,8 @@ describe('Dialing (direct, TCP)', () => {
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
dialTimeout: 50
|
||||
dialTimeout: 50,
|
||||
connectionGater
|
||||
})
|
||||
sinon.stub(localTM, 'dial').callsFake(async (addr, options) => {
|
||||
expect(options.signal).to.exist()
|
||||
@@ -206,7 +230,8 @@ describe('Dialing (direct, TCP)', () => {
|
||||
add: () => {},
|
||||
getMultiaddrsForPeer: () => addrs
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
expect(dialer.tokens).to.have.length(2)
|
||||
|
@@ -21,6 +21,7 @@ const addressSort = require('libp2p-utils/src/address-sort')
|
||||
const PeerStore = require('../../src/peer-store')
|
||||
const TransportManager = require('../../src/transport-manager')
|
||||
const Libp2p = require('../../src')
|
||||
const { mockConnectionGater } = require('../utils/mock-connection-gater')
|
||||
|
||||
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
|
||||
const mockUpgrader = require('../utils/mockUpgrader')
|
||||
@@ -30,6 +31,7 @@ const unsupportedAddr = new Multiaddr('/ip4/127.0.0.1/tcp/9999/ws/p2p/QmckxVrJw1
|
||||
const remoteAddr = MULTIADDRS_WEBSOCKETS[0]
|
||||
|
||||
describe('Dialing (direct, WebSockets)', () => {
|
||||
const connectionGater = mockConnectionGater()
|
||||
let localTM
|
||||
let peerStore
|
||||
let peerId
|
||||
@@ -38,7 +40,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
[peerId] = await createPeerId()
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
localTM = new TransportManager({
|
||||
libp2p: {},
|
||||
@@ -54,13 +57,21 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should have appropriate defaults', () => {
|
||||
const dialer = new Dialer({ transportManager: localTM, peerStore })
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
expect(dialer.maxParallelDials).to.equal(Constants.MAX_PARALLEL_DIALS)
|
||||
expect(dialer.timeout).to.equal(Constants.DIAL_TIMEOUT)
|
||||
})
|
||||
|
||||
it('should limit the number of tokens it provides', () => {
|
||||
const dialer = new Dialer({ transportManager: localTM, peerStore })
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
const maxPerPeer = Constants.MAX_PER_PEER_DIALS
|
||||
expect(dialer.tokens).to.have.length(Constants.MAX_PARALLEL_DIALS)
|
||||
const tokens = dialer.getTokens(maxPerPeer + 1)
|
||||
@@ -69,14 +80,22 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should not return tokens if non are left', () => {
|
||||
const dialer = new Dialer({ transportManager: localTM, peerStore })
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
sinon.stub(dialer, 'tokens').value([])
|
||||
const tokens = dialer.getTokens(1)
|
||||
expect(tokens.length).to.equal(0)
|
||||
})
|
||||
|
||||
it('should NOT be able to return a token twice', () => {
|
||||
const dialer = new Dialer({ transportManager: localTM, peerStore })
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
const tokens = dialer.getTokens(1)
|
||||
expect(tokens).to.have.length(1)
|
||||
expect(dialer.tokens).to.have.length(Constants.MAX_PARALLEL_DIALS - 1)
|
||||
@@ -93,7 +112,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
add: () => {},
|
||||
getMultiaddrsForPeer: () => [remoteAddr]
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
const connection = await dialer.connectToPeer(remoteAddr)
|
||||
@@ -109,7 +129,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
add: () => {},
|
||||
getMultiaddrsForPeer: () => [remoteAddr]
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
const connection = await dialer.connectToPeer(remoteAddr.toString())
|
||||
@@ -118,7 +139,11 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
})
|
||||
|
||||
it('should fail to connect to an unsupported multiaddr', async () => {
|
||||
const dialer = new Dialer({ transportManager: localTM, peerStore })
|
||||
const dialer = new Dialer({
|
||||
transportManager: localTM,
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
|
||||
await expect(dialer.connectToPeer(unsupportedAddr))
|
||||
.to.eventually.be.rejectedWith(AggregateError)
|
||||
@@ -132,7 +157,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
add: () => {},
|
||||
getMultiaddrsForPeer: () => [remoteAddr]
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
const connection = await dialer.connectToPeer(peerId)
|
||||
@@ -148,7 +174,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
set: () => {},
|
||||
getMultiaddrsForPeer: () => [unsupportedAddr]
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
await expect(dialer.connectToPeer(peerId))
|
||||
@@ -164,7 +191,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
add: () => {},
|
||||
getMultiaddrsForPeer: () => [remoteAddr]
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
sinon.stub(localTM, 'dial').callsFake(async (addr, options) => {
|
||||
expect(options.signal).to.exist()
|
||||
@@ -191,7 +219,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
add: () => { },
|
||||
getMultiaddrsForPeer: () => Array.from({ length: 11 }, (_, i) => new Multiaddr(`/ip4/127.0.0.1/tcp/1500${i}/ws/p2p/12D3KooWHFKTMzwerBtsVmtz4ZZEQy2heafxzWw6wNn5PPYkBxJ5`))
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
await expect(dialer.connectToPeer(remoteAddr))
|
||||
@@ -214,7 +243,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
transportManager: localTM,
|
||||
addressSorter: addressSort.publicAddressesFirst,
|
||||
maxParallelDials: 3,
|
||||
peerStore
|
||||
peerStore,
|
||||
connectionGater
|
||||
})
|
||||
|
||||
// Inject data in the AddressBook
|
||||
@@ -240,7 +270,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
set: () => {},
|
||||
getMultiaddrsForPeer: () => [remoteAddr, remoteAddr, remoteAddr]
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
expect(dialer.tokens).to.have.length(2)
|
||||
@@ -278,7 +309,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
set: () => {},
|
||||
getMultiaddrsForPeer: () => [remoteAddr, remoteAddr, remoteAddr]
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
expect(dialer.tokens).to.have.length(2)
|
||||
@@ -320,7 +352,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
addressBook: {
|
||||
set: () => { }
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
sinon.stub(dialer, '_createDialTarget').callsFake(() => {
|
||||
@@ -365,7 +398,8 @@ describe('Dialing (direct, WebSockets)', () => {
|
||||
filter: filters.all
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
connectionGater
|
||||
})
|
||||
|
||||
expect(libp2p.dialer).to.exist()
|
||||
|
@@ -23,10 +23,12 @@ const pkg = require('../../package.json')
|
||||
const AddressManager = require('../../src/address-manager')
|
||||
const { MemoryDatastore } = require('datastore-core/memory')
|
||||
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
|
||||
const { mockConnectionGater } = require('../utils/mock-connection-gater')
|
||||
const remoteAddr = MULTIADDRS_WEBSOCKETS[0]
|
||||
const listenMaddrs = [new Multiaddr('/ip4/127.0.0.1/tcp/15002/ws')]
|
||||
|
||||
describe('Identify', () => {
|
||||
const connectionGater = mockConnectionGater()
|
||||
let localPeer, localPeerStore, localAddressManager
|
||||
let remotePeer, remotePeerStore, remoteAddressManager
|
||||
const protocols = [multicodecs.IDENTIFY, multicodecs.IDENTIFY_PUSH]
|
||||
@@ -39,13 +41,15 @@ describe('Identify', () => {
|
||||
|
||||
localPeerStore = new PeerStore({
|
||||
peerId: localPeer,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
await localPeerStore.protoBook.set(localPeer, protocols)
|
||||
|
||||
remotePeerStore = new PeerStore({
|
||||
peerId: remotePeer,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
await remotePeerStore.protoBook.set(remotePeer, protocols)
|
||||
|
||||
@@ -230,7 +234,8 @@ describe('Identify', () => {
|
||||
const agentVersion = 'js-project/1.0.0'
|
||||
const peerStore = new PeerStore({
|
||||
peerId: localPeer,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
|
||||
sinon.spy(peerStore.metadataBook, 'setValue')
|
||||
@@ -272,7 +277,8 @@ describe('Identify', () => {
|
||||
|
||||
const localPeerStore = new PeerStore({
|
||||
peerId: localPeer,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
await localPeerStore.protoBook.set(localPeer, storedProtocols)
|
||||
|
||||
@@ -290,7 +296,8 @@ describe('Identify', () => {
|
||||
|
||||
const remotePeerStore = new PeerStore({
|
||||
peerId: remotePeer,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
await remotePeerStore.protoBook.set(remotePeer, storedProtocols)
|
||||
|
||||
@@ -352,7 +359,8 @@ describe('Identify', () => {
|
||||
|
||||
const localPeerStore = new PeerStore({
|
||||
peerId: localPeer,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
await localPeerStore.protoBook.set(localPeer, storedProtocols)
|
||||
|
||||
@@ -370,7 +378,8 @@ describe('Identify', () => {
|
||||
|
||||
const remotePeerStore = new PeerStore({
|
||||
peerId: remotePeer,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
await remotePeerStore.protoBook.set(remotePeer, storedProtocols)
|
||||
|
||||
|
@@ -13,7 +13,7 @@ const { MemoryDatastore } = require('datastore-core/memory')
|
||||
const PeerStore = require('../../src/peer-store')
|
||||
const Envelope = require('../../src/record/envelope')
|
||||
const PeerRecord = require('../../src/record/peer-record')
|
||||
|
||||
const { mockConnectionGater } = require('../utils/mock-connection-gater')
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
const {
|
||||
codes: { ERR_INVALID_PARAMETERS }
|
||||
@@ -29,6 +29,7 @@ const addr2 = new Multiaddr('/ip4/20.0.0.1/tcp/8001')
|
||||
const addr3 = new Multiaddr('/ip4/127.0.0.1/tcp/8002')
|
||||
|
||||
describe('addressBook', () => {
|
||||
const connectionGater = mockConnectionGater()
|
||||
let peerId
|
||||
|
||||
before(async () => {
|
||||
@@ -44,7 +45,8 @@ describe('addressBook', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
ab = peerStore.addressBook
|
||||
})
|
||||
@@ -164,7 +166,8 @@ describe('addressBook', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
ab = peerStore.addressBook
|
||||
})
|
||||
@@ -323,7 +326,8 @@ describe('addressBook', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
ab = peerStore.addressBook
|
||||
})
|
||||
@@ -364,7 +368,8 @@ describe('addressBook', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
ab = peerStore.addressBook
|
||||
})
|
||||
@@ -418,7 +423,8 @@ describe('addressBook', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
ab = peerStore.addressBook
|
||||
})
|
||||
@@ -478,7 +484,8 @@ describe('addressBook', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
ab = peerStore.addressBook
|
||||
})
|
||||
@@ -670,7 +677,8 @@ describe('addressBook', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
ab = peerStore.addressBook
|
||||
})
|
||||
|
@@ -8,6 +8,7 @@ const { Multiaddr } = require('multiaddr')
|
||||
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
|
||||
const { MemoryDatastore } = require('datastore-core/memory')
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
const { mockConnectionGater } = require('../utils/mock-connection-gater')
|
||||
|
||||
const addr1 = new Multiaddr('/ip4/127.0.0.1/tcp/8000')
|
||||
const addr2 = new Multiaddr('/ip4/127.0.0.1/tcp/8001')
|
||||
@@ -23,6 +24,7 @@ const proto3 = '/protocol3'
|
||||
*/
|
||||
|
||||
describe('peer-store', () => {
|
||||
const connectionGater = mockConnectionGater()
|
||||
let peerIds
|
||||
before(async () => {
|
||||
peerIds = await peerUtils.createPeerId({
|
||||
@@ -37,7 +39,8 @@ describe('peer-store', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId: peerIds[4],
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
})
|
||||
|
||||
@@ -66,7 +69,8 @@ describe('peer-store', () => {
|
||||
beforeEach(async () => {
|
||||
peerStore = new PeerStore({
|
||||
peerId: peerIds[4],
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
|
||||
// Add peer0 with { addr1, addr2 } and { proto1 }
|
||||
@@ -170,7 +174,8 @@ describe('peer-store', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId: peerIds[4],
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
})
|
||||
|
||||
|
@@ -9,7 +9,7 @@ const { MemoryDatastore } = require('datastore-core/memory')
|
||||
const Topology = require('libp2p-interfaces/src/topology/multicodec-topology')
|
||||
const PeerStore = require('../../src/peer-store')
|
||||
const Registrar = require('../../src/registrar')
|
||||
|
||||
const { mockConnectionGater } = require('../utils/mock-connection-gater')
|
||||
const createMockConnection = require('../utils/mockConnection')
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
const baseOptions = require('../utils/base-options.browser')
|
||||
@@ -17,6 +17,7 @@ const baseOptions = require('../utils/base-options.browser')
|
||||
const multicodec = '/test/1.0.0'
|
||||
|
||||
describe('registrar', () => {
|
||||
const connectionGater = mockConnectionGater()
|
||||
let peerStore
|
||||
let registrar
|
||||
let peerId
|
||||
@@ -29,7 +30,8 @@ describe('registrar', () => {
|
||||
beforeEach(() => {
|
||||
peerStore = new PeerStore({
|
||||
peerId,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
registrar = new Registrar({ peerStore, connectionManager: new EventEmitter() })
|
||||
})
|
||||
|
@@ -14,12 +14,14 @@ const mockUpgrader = require('../utils/mockUpgrader')
|
||||
const sinon = require('sinon')
|
||||
const Peers = require('../fixtures/peers')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const { mockConnectionGater } = require('../utils/mock-connection-gater')
|
||||
const addrs = [
|
||||
new Multiaddr('/ip4/127.0.0.1/tcp/0'),
|
||||
new Multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||
]
|
||||
|
||||
describe('Transport Manager (TCP)', () => {
|
||||
const connectionGater = mockConnectionGater()
|
||||
let tm
|
||||
let localPeer
|
||||
|
||||
@@ -35,7 +37,8 @@ describe('Transport Manager (TCP)', () => {
|
||||
addressManager: new AddressManager({ listen: addrs }),
|
||||
peerStore: new PeerStore({
|
||||
peerId: localPeer,
|
||||
datastore: new MemoryDatastore()
|
||||
datastore: new MemoryDatastore(),
|
||||
addressFilter: connectionGater.filterMultiaddrForPeer
|
||||
})
|
||||
},
|
||||
upgrader: mockUpgrader,
|
||||
|
@@ -18,7 +18,7 @@ const swarmKeyBuffer = uint8ArrayFromString(require('../fixtures/swarm.key'))
|
||||
const Libp2p = require('../../src')
|
||||
const Upgrader = require('../../src/upgrader')
|
||||
const { codes } = require('../../src/errors')
|
||||
|
||||
const { mockConnectionGater } = require('../utils/mock-connection-gater')
|
||||
const mockMultiaddrConnPair = require('../utils/mockMultiaddrConn')
|
||||
const Peers = require('../fixtures/peers')
|
||||
const addrs = [
|
||||
@@ -31,6 +31,17 @@ describe('Upgrader', () => {
|
||||
let remoteUpgrader
|
||||
let localPeer
|
||||
let remotePeer
|
||||
const connectionGater = mockConnectionGater()
|
||||
|
||||
const mockConnectionManager = {
|
||||
gater: {
|
||||
allowDialPeer: async () => true,
|
||||
allowDialMultiaddr: async () => true,
|
||||
acceptConnection: async () => true,
|
||||
acceptEncryptedConnection: async () => true,
|
||||
acceptUpgradedConnection: async () => true
|
||||
}
|
||||
}
|
||||
|
||||
before(async () => {
|
||||
([
|
||||
@@ -42,10 +53,14 @@ describe('Upgrader', () => {
|
||||
]))
|
||||
|
||||
localUpgrader = new Upgrader({
|
||||
localPeer
|
||||
connectionManager: mockConnectionManager,
|
||||
localPeer,
|
||||
connectionGater
|
||||
})
|
||||
remoteUpgrader = new Upgrader({
|
||||
localPeer: remotePeer
|
||||
connectionManager: mockConnectionManager,
|
||||
localPeer: remotePeer,
|
||||
connectionGater
|
||||
})
|
||||
|
||||
localUpgrader.protocols.set('/echo/1.0.0', ({ stream }) => pipe(stream, stream))
|
||||
@@ -321,6 +336,7 @@ describe('Upgrader', () => {
|
||||
describe('libp2p.upgrader', () => {
|
||||
let peers
|
||||
let libp2p
|
||||
const connectionGater = mockConnectionGater()
|
||||
|
||||
before(async () => {
|
||||
peers = await Promise.all([
|
||||
@@ -392,8 +408,10 @@ describe('libp2p.upgrader', () => {
|
||||
|
||||
const remoteUpgrader = new Upgrader({
|
||||
localPeer: remotePeer,
|
||||
connectionManager: libp2p.connectionManager,
|
||||
muxers: new Map([[Muxer.multicodec, Muxer]]),
|
||||
cryptos: new Map([[Crypto.protocol, Crypto]])
|
||||
cryptos: new Map([[Crypto.protocol, Crypto]]),
|
||||
connectionGater
|
||||
})
|
||||
remoteUpgrader.protocols.set('/echo/1.0.0', echoHandler)
|
||||
|
||||
@@ -424,8 +442,10 @@ describe('libp2p.upgrader', () => {
|
||||
|
||||
const remoteUpgrader = new Upgrader({
|
||||
localPeer: remotePeer,
|
||||
connectionManager: libp2p.connectionManager,
|
||||
muxers: new Map([[Muxer.multicodec, Muxer]]),
|
||||
cryptos: new Map([[Crypto.protocol, Crypto]])
|
||||
cryptos: new Map([[Crypto.protocol, Crypto]]),
|
||||
connectionGater
|
||||
})
|
||||
|
||||
const { inbound, outbound } = mockMultiaddrConnPair({ addrs, remotePeer })
|
||||
|
19
test/utils/mock-connection-gater.js
Normal file
19
test/utils/mock-connection-gater.js
Normal file
@@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
|
||||
function mockConnectionGater () {
|
||||
return {
|
||||
denyDialPeer: async () => Promise.resolve(false),
|
||||
denyDialMultiaddr: async () => Promise.resolve(false),
|
||||
denyInboundConnection: async () => Promise.resolve(false),
|
||||
denyOutboundConnection: async () => Promise.resolve(false),
|
||||
denyInboundEncryptedConnection: async () => Promise.resolve(false),
|
||||
denyOutboundEncryptedConnection: async () => Promise.resolve(false),
|
||||
denyInboundUpgradedConnection: async () => Promise.resolve(false),
|
||||
denyOutboundUpgradedConnection: async () => Promise.resolve(false),
|
||||
filterMultiaddrForPeer: async () => Promise.resolve(true)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mockConnectionGater
|
||||
}
|
Reference in New Issue
Block a user