Skip to content

Commit

Permalink
chore: do not listen on a relayed conn
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Sep 1, 2020
1 parent 1dccb7c commit 02dbdc3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/circuit/auto-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const multiaddr = require('multiaddr')
const { relay: multicodec } = require('./multicodec')
const { canHop } = require('./circuit/hop')

const circuitProtoCode = 290
const hopMetadataKey = 'hop_relay'
const hopMetadataValue = 'true'

Expand Down Expand Up @@ -72,6 +73,12 @@ class AutoRelay {
try {
const connection = this._connectionManager.get(peerId)

// Do not hop on a relayed connection
if (connection.remoteAddr.protoCodes().includes(circuitProtoCode)) {
log(`relayed connection to ${id} will not be used to hop on`)
return
}

await canHop({ connection })
this._peerStore.metadataBook.set(peerId, hopMetadataKey, uint8ArrayFromString(hopMetadataValue))
await this._addListenRelay(connection, id)
Expand Down Expand Up @@ -180,6 +187,4 @@ class AutoRelay {
}
}

// TODO: be careful about relay connect to relay peer that might create a double relayed conn

module.exports = AutoRelay
10 changes: 8 additions & 2 deletions test/relay/auto-relay.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ describe('auto-relay', () => {
})
})

// TODO: do not allow listen on a relayed connection
describe.skip('flows with 2 max listeners', () => {
describe('flows with 2 max listeners', () => {
let relayLibp2p1
let relayLibp2p2
let relayLibp2p3
Expand Down Expand Up @@ -301,6 +300,13 @@ describe('auto-relay', () => {
expect(autoRelay2._listenRelays.size).to.equal(1)

// Relay 1 discovers Relay 2 relayed multiaddr via Relay 3
const ma2RelayedBy3 = relayLibp2p2.multiaddrs[2]
relayLibp2p1.peerStore.addressBook.add(relayLibp2p2.peerId, [ma2RelayedBy3])
await relayLibp2p1.dial(relayLibp2p2.peerId)

// Peer not added as listen relay
expect(autoRelay1._addListenRelay.callCount).to.equal(1)
expect(autoRelay1._listenRelays.size).to.equal(1)
})
})
})

0 comments on commit 02dbdc3

Please sign in to comment.