2019-11-29 16:41:08 +01:00
|
|
|
'use strict'
|
|
|
|
/* eslint-env mocha */
|
|
|
|
|
2020-10-15 15:31:33 +01:00
|
|
|
const { expect } = require('aegir/utils/chai')
|
2020-04-18 23:26:46 +02:00
|
|
|
const sinon = require('sinon')
|
2019-11-29 16:41:08 +01:00
|
|
|
|
2021-04-15 09:40:02 +02:00
|
|
|
const { Multiaddr } = require('multiaddr')
|
2019-11-29 16:41:08 +01:00
|
|
|
const { collect } = require('streaming-iterables')
|
|
|
|
const pipe = require('it-pipe')
|
2019-12-03 10:28:52 +01:00
|
|
|
const AggregateError = require('aggregate-error')
|
2020-04-09 16:07:18 +02:00
|
|
|
const PeerId = require('peer-id')
|
2020-08-24 11:58:02 +01:00
|
|
|
const uint8ArrayFromString = require('uint8arrays/from-string')
|
2020-04-09 16:07:18 +02:00
|
|
|
|
2020-04-14 14:05:30 +02:00
|
|
|
const { createPeerId } = require('../utils/creators/peer')
|
2019-11-29 16:41:08 +01:00
|
|
|
const baseOptions = require('../utils/base-options')
|
|
|
|
const Libp2p = require('../../src')
|
|
|
|
const { codes: Errors } = require('../../src/errors')
|
|
|
|
|
2020-04-30 08:46:25 +02:00
|
|
|
const listenAddr = '/ip4/0.0.0.0/tcp/0'
|
2020-04-14 14:05:30 +02:00
|
|
|
|
2019-11-29 16:41:08 +01:00
|
|
|
describe('Dialing (via relay, TCP)', () => {
|
|
|
|
let srcLibp2p
|
|
|
|
let relayLibp2p
|
|
|
|
let dstLibp2p
|
|
|
|
|
2020-04-18 23:26:46 +02:00
|
|
|
beforeEach(async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
const peerIds = await createPeerId({ number: 3 })
|
2019-11-29 16:41:08 +01:00
|
|
|
// Create 3 nodes, and turn HOP on for the relay
|
2020-04-14 14:05:30 +02:00
|
|
|
;[srcLibp2p, relayLibp2p, dstLibp2p] = peerIds.map((peerId, index) => {
|
2019-11-29 16:41:08 +01:00
|
|
|
const opts = baseOptions
|
|
|
|
index === 1 && (opts.config.relay.hop.enabled = true)
|
|
|
|
return new Libp2p({
|
|
|
|
...opts,
|
2020-04-14 14:05:30 +02:00
|
|
|
addresses: {
|
|
|
|
listen: [listenAddr]
|
|
|
|
},
|
|
|
|
peerId
|
2019-11-29 16:41:08 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
dstLibp2p.handle('/echo/1.0.0', ({ stream }) => pipe(stream, stream))
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// Start each node
|
2020-04-14 14:05:30 +02:00
|
|
|
return Promise.all([srcLibp2p, relayLibp2p, dstLibp2p].map(libp2p => libp2p.start()))
|
2019-11-29 16:41:08 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
// Stop each node
|
2019-12-15 17:33:16 +01:00
|
|
|
return Promise.all([srcLibp2p, relayLibp2p, dstLibp2p].map(async libp2p => {
|
|
|
|
await libp2p.stop()
|
|
|
|
// Clear the peer stores
|
2020-04-09 16:07:18 +02:00
|
|
|
for (const peerIdStr of libp2p.peerStore.peers.keys()) {
|
|
|
|
const peerId = PeerId.createFromCID(peerIdStr)
|
|
|
|
libp2p.peerStore.delete(peerId)
|
2019-12-15 17:33:16 +01:00
|
|
|
}
|
|
|
|
}))
|
2019-11-29 16:41:08 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to connect to a peer over a relay with active connections', async () => {
|
|
|
|
const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
|
2020-04-14 14:05:30 +02:00
|
|
|
const relayIdString = relayLibp2p.peerId.toB58String()
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
const dialAddr = relayAddr
|
|
|
|
.encapsulate(`/p2p/${relayIdString}`)
|
2020-04-14 14:05:30 +02:00
|
|
|
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId.toB58String()}`)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
const tcpAddrs = dstLibp2p.transportManager.getAddrs()
|
2021-04-15 09:40:02 +02:00
|
|
|
sinon.stub(dstLibp2p.addressManager, 'listen').value([new Multiaddr(`/p2p-circuit${relayAddr}/p2p/${relayIdString}`)])
|
2020-04-18 23:26:46 +02:00
|
|
|
|
2020-09-16 16:43:09 +02:00
|
|
|
await dstLibp2p.transportManager.listen(dstLibp2p.addressManager.getListenAddrs())
|
2019-11-29 16:41:08 +01:00
|
|
|
expect(dstLibp2p.transportManager.getAddrs()).to.have.deep.members([...tcpAddrs, dialAddr.decapsulate('p2p')])
|
|
|
|
|
|
|
|
const connection = await srcLibp2p.dial(dialAddr)
|
|
|
|
expect(connection).to.exist()
|
2020-04-14 14:05:30 +02:00
|
|
|
expect(connection.remotePeer.toBytes()).to.eql(dstLibp2p.peerId.toBytes())
|
|
|
|
expect(connection.localPeer.toBytes()).to.eql(srcLibp2p.peerId.toBytes())
|
2019-11-29 16:41:08 +01:00
|
|
|
expect(connection.remoteAddr).to.eql(dialAddr)
|
|
|
|
expect(connection.localAddr).to.eql(
|
|
|
|
relayAddr // the relay address
|
|
|
|
.encapsulate(`/p2p/${relayIdString}`) // with its peer id
|
|
|
|
.encapsulate('/p2p-circuit') // the local peer is connected over the relay
|
2020-04-14 14:05:30 +02:00
|
|
|
.encapsulate(`/p2p/${srcLibp2p.peerId.toB58String()}`) // and the local peer id
|
2019-11-29 16:41:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const { stream: echoStream } = await connection.newStream('/echo/1.0.0')
|
2020-08-24 11:58:02 +01:00
|
|
|
const input = uint8ArrayFromString('hello')
|
2019-11-29 16:41:08 +01:00
|
|
|
const [output] = await pipe(
|
|
|
|
[input],
|
|
|
|
echoStream,
|
|
|
|
collect
|
|
|
|
)
|
2019-12-11 16:05:59 +01:00
|
|
|
|
2019-11-29 16:41:08 +01:00
|
|
|
expect(output.slice()).to.eql(input)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fail to connect to a peer over a relay with inactive connections', async () => {
|
|
|
|
const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
|
2020-04-14 14:05:30 +02:00
|
|
|
const relayIdString = relayLibp2p.peerId.toB58String()
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
const dialAddr = relayAddr
|
|
|
|
.encapsulate(`/p2p/${relayIdString}`)
|
2020-04-14 14:05:30 +02:00
|
|
|
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId.toB58String()}`)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
await expect(srcLibp2p.dial(dialAddr))
|
2019-12-03 10:28:52 +01:00
|
|
|
.to.eventually.be.rejectedWith(AggregateError)
|
|
|
|
.and.to.have.nested.property('._errors[0].code', Errors.ERR_HOP_REQUEST_FAILED)
|
2019-11-29 16:41:08 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not stay connected to a relay when not already connected and HOP fails', async () => {
|
|
|
|
const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
|
2020-04-14 14:05:30 +02:00
|
|
|
const relayIdString = relayLibp2p.peerId.toB58String()
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
const dialAddr = relayAddr
|
|
|
|
.encapsulate(`/p2p/${relayIdString}`)
|
2020-04-14 14:05:30 +02:00
|
|
|
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId.toB58String()}`)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
await expect(srcLibp2p.dial(dialAddr))
|
2019-12-03 10:28:52 +01:00
|
|
|
.to.eventually.be.rejectedWith(AggregateError)
|
|
|
|
.and.to.have.nested.property('._errors[0].code', Errors.ERR_HOP_REQUEST_FAILED)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
// We should not be connected to the relay, because we weren't before the dial
|
2020-04-18 17:06:56 +02:00
|
|
|
const srcToRelayConn = srcLibp2p.connectionManager.get(relayLibp2p.peerId)
|
2019-11-29 16:41:08 +01:00
|
|
|
expect(srcToRelayConn).to.not.exist()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('dialer should stay connected to an already connected relay on hop failure', async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
const relayIdString = relayLibp2p.peerId.toB58String()
|
2020-02-11 16:32:40 +01:00
|
|
|
const relayAddr = relayLibp2p.transportManager.getAddrs()[0].encapsulate(`/p2p/${relayIdString}`)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
const dialAddr = relayAddr
|
2020-04-14 14:05:30 +02:00
|
|
|
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId.toB58String()}`)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
await srcLibp2p.dial(relayAddr)
|
|
|
|
|
|
|
|
await expect(srcLibp2p.dial(dialAddr))
|
2019-12-03 10:28:52 +01:00
|
|
|
.to.eventually.be.rejectedWith(AggregateError)
|
|
|
|
.and.to.have.nested.property('._errors[0].code', Errors.ERR_HOP_REQUEST_FAILED)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
2020-04-18 17:06:56 +02:00
|
|
|
const srcToRelayConn = srcLibp2p.connectionManager.get(relayLibp2p.peerId)
|
2019-11-29 16:41:08 +01:00
|
|
|
expect(srcToRelayConn).to.exist()
|
|
|
|
expect(srcToRelayConn.stat.status).to.equal('open')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('destination peer should stay connected to an already connected relay on hop failure', async () => {
|
2020-04-14 14:05:30 +02:00
|
|
|
const relayIdString = relayLibp2p.peerId.toB58String()
|
2020-02-11 16:32:40 +01:00
|
|
|
const relayAddr = relayLibp2p.transportManager.getAddrs()[0].encapsulate(`/p2p/${relayIdString}`)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
const dialAddr = relayAddr
|
2020-04-14 14:05:30 +02:00
|
|
|
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId.toB58String()}`)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
// Connect the destination peer and the relay
|
|
|
|
const tcpAddrs = dstLibp2p.transportManager.getAddrs()
|
2021-04-15 09:40:02 +02:00
|
|
|
sinon.stub(dstLibp2p.addressManager, 'getListenAddrs').returns([new Multiaddr(`${relayAddr}/p2p-circuit`)])
|
2020-04-18 23:26:46 +02:00
|
|
|
|
2020-09-16 16:43:09 +02:00
|
|
|
await dstLibp2p.transportManager.listen(dstLibp2p.addressManager.getListenAddrs())
|
2019-11-29 16:41:08 +01:00
|
|
|
expect(dstLibp2p.transportManager.getAddrs()).to.have.deep.members([...tcpAddrs, dialAddr.decapsulate('p2p')])
|
|
|
|
|
|
|
|
// Tamper with the our multiaddrs for the circuit message
|
2020-04-29 16:08:52 +02:00
|
|
|
sinon.stub(srcLibp2p, 'multiaddrs').value([{
|
2020-08-24 11:58:02 +01:00
|
|
|
bytes: uint8ArrayFromString('an invalid multiaddr')
|
2020-04-18 23:26:46 +02:00
|
|
|
}])
|
2019-11-29 16:41:08 +01:00
|
|
|
|
|
|
|
await expect(srcLibp2p.dial(dialAddr))
|
2019-12-03 10:28:52 +01:00
|
|
|
.to.eventually.be.rejectedWith(AggregateError)
|
|
|
|
.and.to.have.nested.property('._errors[0].code', Errors.ERR_HOP_REQUEST_FAILED)
|
2019-11-29 16:41:08 +01:00
|
|
|
|
2020-04-18 17:06:56 +02:00
|
|
|
const dstToRelayConn = dstLibp2p.connectionManager.get(relayLibp2p.peerId)
|
2019-11-29 16:41:08 +01:00
|
|
|
expect(dstToRelayConn).to.exist()
|
|
|
|
expect(dstToRelayConn.stat.status).to.equal('open')
|
|
|
|
})
|
|
|
|
})
|