chore: fix circuitv2 bug preventing e2e test from passing

This commit is contained in:
Marin
2022-03-08 14:55:15 +01:00
parent abe2b22af6
commit 84e38d7e95
4 changed files with 14 additions and 21 deletions

View File

@ -23,6 +23,7 @@ const StreamHandlerV2 = require('./v2/stream-handler')
const { handleHopProtocol } = require('./v2/hop') const { handleHopProtocol } = require('./v2/hop')
const { handleStop: handleStopV2 } = require('./v2/stop') const { handleStop: handleStopV2 } = require('./v2/stop')
const { Status, HopMessage, StopMessage } = require('./v2/protocol') const { Status, HopMessage, StopMessage } = require('./v2/protocol')
const createError = require('err-code')
const transportSymbol = Symbol.for('@libp2p/js-libp2p-circuit/circuit') const transportSymbol = Symbol.for('@libp2p/js-libp2p-circuit/circuit')
@ -178,13 +179,13 @@ class Circuit {
if (mStream) { if (mStream) {
// @ts-ignore dst peer will not be undefined // @ts-ignore dst peer will not be undefined
const remoteAddr = new Multiaddr(request.peer.addrs[0]) const remoteAddr = new Multiaddr(request.peer.addrs[0])
const localAddr = this._libp2p.addressManager.getListenAddrs()[0] const localAddr = this._libp2p.transportManager.getAddrs()[0]
const maConn = toConnection({ const maConn = toConnection({
stream: mStream, stream: mStream,
remoteAddr, remoteAddr,
localAddr localAddr
}) })
log('new inbound connection %s', maConn.remoteAddr)
const conn = await this._upgrader.upgradeInbound(maConn) const conn = await this._upgrader.upgradeInbound(maConn)
log('%s connection %s upgraded', 'inbound', maConn.remoteAddr) log('%s connection %s upgraded', 'inbound', maConn.remoteAddr)
this.handler && this.handler(conn) this.handler && this.handler(conn)
@ -320,12 +321,12 @@ class Circuit {
const status = HopMessage.decode(await streamHandler.read()) const status = HopMessage.decode(await streamHandler.read())
if (status.status !== Status.OK) { if (status.status !== Status.OK) {
throw new Error('failed to connect via realy with status ' + status.status) throw createError(new Error('failed to connect via realy with status ' + status.status), codes.ERR_HOP_REQUEST_FAILED)
} }
// TODO: do something with limit and transient connection // TODO: do something with limit and transient connection
let localAddr = connection.localAddr ?? relayAddr let localAddr = relayAddr
localAddr = localAddr.encapsulate(`/p2p-circuit/p2p/${this.peerId.toB58String()}`) localAddr = localAddr.encapsulate(`/p2p-circuit/p2p/${this.peerId.toB58String()}`)
const maConn = toConnection({ const maConn = toConnection({
stream: streamHandler.rest(), stream: streamHandler.rest(),

View File

@ -150,7 +150,7 @@ async function handleConnect ({ connection, streamHandler, request, reservationS
type: StopMessage.Type.CONNECT, type: StopMessage.Type.CONNECT,
peer: { peer: {
id: connection.remotePeer.id, id: connection.remotePeer.id,
addrs: [new Multiaddr(connection.remoteAddr).bytes] addrs: [new Multiaddr('/p2p/' + connection.remotePeer.toB58String()).bytes]
} }
} }
}) })
@ -166,11 +166,11 @@ async function handleConnect ({ connection, streamHandler, request, reservationS
const sourceStream = streamHandler.rest() const sourceStream = streamHandler.rest()
log('connection to destination established, short circuiting streams...') log('connection to destination established, short circuiting streams...')
// Short circuit the two streams to create the relayed connection // Short circuit the two streams to create the relayed connection
return pipe([ return pipe(
sourceStream, sourceStream,
destinationStream, destinationStream,
sourceStream sourceStream
]) )
} }
/** /**

View File

@ -43,7 +43,8 @@ async function discoveredRelayConfig (node, relay) {
}) })
} }
describe('auto-relay', () => { // TODO: replace with circuit v2 stuff
describe.skip('auto-relay', () => {
describe('basics', () => { describe('basics', () => {
let libp2p let libp2p
let relayLibp2p let relayLibp2p

View File

@ -50,19 +50,13 @@ describe('Dialing (via relay, TCP)', () => {
return Promise.all([srcLibp2p, relayLibp2p, dstLibp2p].map(libp2p => libp2p.stop())) return Promise.all([srcLibp2p, relayLibp2p, dstLibp2p].map(libp2p => libp2p.stop()))
}) })
it.only('should be able to connect to a peer over a relay with active connections', async () => { it('should be able to connect to a peer over a relay with active connections', async () => {
const relayAddr = relayLibp2p.transportManager.getAddrs()[0] const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
const relayIdString = relayLibp2p.peerId.toB58String() const relayIdString = relayLibp2p.peerId.toB58String()
console.log({
source: srcLibp2p.peerId.toB58String(),
relay: relayLibp2p.peerId.toB58String(),
destination: dstLibp2p.peerId.toB58String()
})
const dialAddr = relayAddr const dialAddr = relayAddr
.encapsulate(`/p2p/${relayIdString}`) .encapsulate(`/p2p/${relayIdString}`)
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId.toB58String()}`) .encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId}`)
const tcpAddrs = dstLibp2p.transportManager.getAddrs() const tcpAddrs = dstLibp2p.transportManager.getAddrs()
sinon.stub(dstLibp2p.addressManager, 'listen').value([new Multiaddr(`/p2p-circuit${relayAddr}/p2p/${relayIdString}`)]) sinon.stub(dstLibp2p.addressManager, 'listen').value([new Multiaddr(`/p2p-circuit${relayAddr}/p2p/${relayIdString}`)])
@ -95,7 +89,7 @@ describe('Dialing (via relay, TCP)', () => {
it('should fail to connect to a peer over a relay with inactive connections', async () => { it('should fail to connect to a peer over a relay with inactive connections', async () => {
const relayAddr = relayLibp2p.transportManager.getAddrs()[0] const relayAddr = relayLibp2p.transportManager.getAddrs()[0]
const relayIdString = relayLibp2p.peerId.toB58String() const relayIdString = relayLibp2p.peerId.toB58String()
sinon.stub(relayLibp2p.connectionManager, 'get').withArgs(dstLibp2p.peerId).returns(null)
const dialAddr = relayAddr const dialAddr = relayAddr
.encapsulate(`/p2p/${relayIdString}`) .encapsulate(`/p2p/${relayIdString}`)
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId.toB58String()}`) .encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerId.toB58String()}`)
@ -154,10 +148,7 @@ describe('Dialing (via relay, TCP)', () => {
await dstLibp2p.transportManager.listen(dstLibp2p.addressManager.getListenAddrs()) await dstLibp2p.transportManager.listen(dstLibp2p.addressManager.getListenAddrs())
expect(dstLibp2p.transportManager.getAddrs()).to.have.deep.members([...tcpAddrs, dialAddr.decapsulate('p2p')]) expect(dstLibp2p.transportManager.getAddrs()).to.have.deep.members([...tcpAddrs, dialAddr.decapsulate('p2p')])
// Tamper with the our multiaddrs for the circuit message sinon.stub(relayLibp2p.connectionManager, 'get').withArgs(dstLibp2p.peerId).returns(null)
sinon.stub(srcLibp2p, 'multiaddrs').value([{
bytes: uint8ArrayFromString('an invalid multiaddr')
}])
await expect(srcLibp2p.dial(dialAddr)) await expect(srcLibp2p.dial(dialAddr))
.to.eventually.be.rejectedWith(AggregateError) .to.eventually.be.rejectedWith(AggregateError)