Skip to content

Commit

Permalink
chore: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Sep 1, 2020
1 parent 02dbdc3 commit 5b79b09
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/circuit/auto-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ class AutoRelay {
/**
* Creates an instance of AutoRelay
* @constructor
* @param {object} params
* @param {Libp2p} params.libp2p
* @param {number} params.maxListeners maximum number of relays to listen.
* @param {object} props
* @param {Libp2p} props.libp2p
* @param {number} props.maxListeners maximum number of relays to listen.
*/
constructor ({ libp2p, maxListeners }) {
this._libp2p = libp2p
this._peerId = libp2p.peerId
this._registrar = libp2p.registrar
this._peerStore = libp2p.peerStore
this._connectionManager = libp2p.connectionManager
this._transportManager = libp2p.transportManager
Expand All @@ -46,8 +45,8 @@ class AutoRelay {
}

/**
* Check if a new peer supports the relay protocol.
* If the protocol is not supported, check if it was supported before and remove listen relay.
* Check if a peer supports the relay protocol.
* If the protocol is not supported, check if it was supported before and remove it as a listen relay.
* If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
* inside the threshold.
* @param {Object} props
Expand Down Expand Up @@ -80,6 +79,7 @@ class AutoRelay {
}

await canHop({ connection })

this._peerStore.metadataBook.set(peerId, hopMetadataKey, uint8ArrayFromString(hopMetadataValue))
await this._addListenRelay(connection, id)
} catch (err) {
Expand Down
2 changes: 0 additions & 2 deletions src/transport-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ class TransportManager {
* @param {Array<Multiaddr>} addrs addresses to attempt to listen on
*/
async listen (addrs) {
// const addrs = this.libp2p.addressManager.getListenAddrs()

if (!addrs || addrs.length === 0) {
log('no addresses were provided for listening, this node is dial only')
return
Expand Down
111 changes: 99 additions & 12 deletions test/relay/auto-relay.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,82 @@ const baseOptions = require('../utils/base-options')
const listenAddr = '/ip4/0.0.0.0/tcp/0'

describe('auto-relay', () => {
describe('basics', () => {
let libp2p
let relayLibp2p
let autoRelay

beforeEach(async () => {
const peerIds = await createPeerId({ number: 2 })
// Create 2 nodes, and turn HOP on for the relay
;[libp2p, relayLibp2p] = peerIds.map((peerId, index) => {
const opts = baseOptions

opts.config.relay = {
...opts.config.relay,
hop: {
enabled: index !== 0
},
autoRelay: {
enabled: true,
maxListeners: 1
}
}

return new Libp2p({
...opts,
addresses: {
listen: [listenAddr]
},
connectionManager: {
autoDial: false
},
peerDiscovery: {
autoDial: false
},
peerId
})
})

autoRelay = libp2p.transportManager._transports.get('Circuit')._autoRelay

expect(autoRelay.maxListeners).to.eql(1)
})

beforeEach(() => {
// Start each node
return Promise.all([libp2p, relayLibp2p].map(libp2p => libp2p.start()))
})

afterEach(() => {
// Stop each node
return Promise.all([libp2p, relayLibp2p].map(libp2p => libp2p.stop()))
})

it('should ask if node supports hop on protocol change (relay protocol) and add to listen multiaddrs', async () => {
// Spy if a connected peer is being added as listen relay
sinon.spy(autoRelay, '_addListenRelay')

const originalMultiaddrsLength = relayLibp2p.multiaddrs.length

// Discover relay
libp2p.peerStore.addressBook.add(relayLibp2p.peerId, relayLibp2p.multiaddrs)
await libp2p.dial(relayLibp2p.peerId)

// Wait for peer added as listen relay
await pWaitFor(() => autoRelay._addListenRelay.callCount === 1)
expect(autoRelay._listenRelays.size).to.equal(1)

// Wait for listen multiaddr update
await pWaitFor(() => libp2p.multiaddrs.length === originalMultiaddrsLength + 1)
expect(libp2p.multiaddrs[2].getPeerId()).to.eql(relayLibp2p.peerId.toB58String())

// Peer has relay multicodec
const knownProtocols = libp2p.peerStore.protoBook.get(relayLibp2p.peerId)
expect(knownProtocols).to.include(relayMulticodec)
})
})

describe('flows with 1 listener max', () => {
let libp2p
let relayLibp2p1
Expand Down Expand Up @@ -81,8 +157,9 @@ describe('auto-relay', () => {

// Discover relay
relayLibp2p1.peerStore.addressBook.add(relayLibp2p2.peerId, relayLibp2p2.multiaddrs)
expect(relayLibp2p1.multiaddrs).to.have.lengthOf(2)
expect(relayLibp2p2.multiaddrs).to.have.lengthOf(2)

const originalMultiaddrs1Length = relayLibp2p1.multiaddrs.length
const originalMultiaddrs2Length = relayLibp2p2.multiaddrs.length

await relayLibp2p1.dial(relayLibp2p2.peerId)

Expand All @@ -92,8 +169,8 @@ describe('auto-relay', () => {

// Wait for listen multiaddr update
await Promise.all([
pWaitFor(() => relayLibp2p1.multiaddrs.length === 3),
pWaitFor(() => relayLibp2p2.multiaddrs.length === 3)
pWaitFor(() => relayLibp2p1.multiaddrs.length === originalMultiaddrs1Length + 1),
pWaitFor(() => relayLibp2p2.multiaddrs.length === originalMultiaddrs2Length + 1)
])
expect(relayLibp2p1.multiaddrs[2].getPeerId()).to.eql(relayLibp2p2.peerId.toB58String())

Expand All @@ -103,15 +180,18 @@ describe('auto-relay', () => {
})

it('should be able to dial a peer from its relayed address previously added', async () => {
const originalMultiaddrs1Length = relayLibp2p1.multiaddrs.length
const originalMultiaddrs2Length = relayLibp2p2.multiaddrs.length

// Discover relay
relayLibp2p1.peerStore.addressBook.add(relayLibp2p2.peerId, relayLibp2p2.multiaddrs)

await relayLibp2p1.dial(relayLibp2p2.peerId)

// Wait for listen multiaddr update
await Promise.all([
pWaitFor(() => relayLibp2p1.multiaddrs.length === 3),
pWaitFor(() => relayLibp2p2.multiaddrs.length === 3)
pWaitFor(() => relayLibp2p1.multiaddrs.length === originalMultiaddrs1Length + 1),
pWaitFor(() => relayLibp2p2.multiaddrs.length === originalMultiaddrs2Length + 1)
])
expect(relayLibp2p1.multiaddrs[2].getPeerId()).to.eql(relayLibp2p2.peerId.toB58String())

Expand All @@ -123,6 +203,9 @@ describe('auto-relay', () => {
})

it('should only add maxListeners relayed addresses', async () => {
const originalMultiaddrs1Length = relayLibp2p1.multiaddrs.length
const originalMultiaddrs2Length = relayLibp2p2.multiaddrs.length

// Spy if a connected peer is being added as listen relay
sinon.spy(autoRelay1, '_addListenRelay')
sinon.spy(autoRelay1._listenRelays, 'add')
Expand All @@ -139,8 +222,8 @@ describe('auto-relay', () => {

// Wait for listen multiaddr update
await Promise.all([
pWaitFor(() => relayLibp2p1.multiaddrs.length === 3),
pWaitFor(() => relayLibp2p2.multiaddrs.length === 3)
pWaitFor(() => relayLibp2p1.multiaddrs.length === originalMultiaddrs1Length + 1),
pWaitFor(() => relayLibp2p2.multiaddrs.length === originalMultiaddrs2Length + 1)
])
expect(relayLibp2p1.multiaddrs[2].getPeerId()).to.eql(relayLibp2p2.peerId.toB58String())

Expand All @@ -166,24 +249,28 @@ describe('auto-relay', () => {
})

it('should not listen on a relayed address if peer disconnects', async () => {
const originalMultiaddrs1Length = relayLibp2p1.multiaddrs.length

// Discover one relay and connect
relayLibp2p1.peerStore.addressBook.add(relayLibp2p2.peerId, relayLibp2p2.multiaddrs)
await relayLibp2p1.dial(relayLibp2p2.peerId)

// Wait for listenning on the relay
await pWaitFor(() => relayLibp2p1.multiaddrs.length === 3)
await pWaitFor(() => relayLibp2p1.multiaddrs.length === originalMultiaddrs1Length + 1)
expect(autoRelay1._listenRelays.size).to.equal(1)
expect(relayLibp2p1.multiaddrs[2].getPeerId()).to.eql(relayLibp2p2.peerId.toB58String())

// Disconnect from peer used for relay
await relayLibp2p1.hangUp(relayLibp2p2.peerId)

// Wait for removed listening on the relay
await pWaitFor(() => relayLibp2p1.multiaddrs.length === 2)
await pWaitFor(() => relayLibp2p1.multiaddrs.length === originalMultiaddrs1Length)
expect(autoRelay1._listenRelays.size).to.equal(0)
})

it('should try to listen on other relayed addresses if one used relay disconnects', async () => {
const originalMultiaddrs1Length = relayLibp2p1.multiaddrs.length

// Spy if a connected peer is being added as listen relay
sinon.spy(autoRelay1, '_addListenRelay')

Expand All @@ -201,7 +288,7 @@ describe('auto-relay', () => {
expect(relayLibp2p1.connectionManager.size).to.eql(2)

// Wait for listen multiaddr update
await pWaitFor(() => relayLibp2p1.multiaddrs.length === 3)
await pWaitFor(() => relayLibp2p1.multiaddrs.length === originalMultiaddrs1Length + 1)
expect(relayLibp2p1.multiaddrs[2].getPeerId()).to.eql(relayLibp2p2.peerId.toB58String())

// Spy if relay from listen map was removed
Expand All @@ -218,7 +305,7 @@ describe('auto-relay', () => {
expect(relayLibp2p1.connectionManager.size).to.eql(1)

// Wait for listen multiaddr update
await pWaitFor(() => relayLibp2p1.multiaddrs.length === 3)
await pWaitFor(() => relayLibp2p1.multiaddrs.length === originalMultiaddrs1Length + 1)
expect(relayLibp2p1.multiaddrs[2].getPeerId()).to.eql(relayLibp2p3.peerId.toB58String())
})
})
Expand Down

0 comments on commit 5b79b09

Please sign in to comment.