From 5c72424e5744f93f9e5eb77db0eceaad37965ed4 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 23 Sep 2020 16:40:33 +0200 Subject: [PATCH] chore: address review --- src/circuit/auto-relay.js | 2 +- src/circuit/constants.js | 12 ++++++------ src/circuit/index.js | 20 ++++++++++++++++---- src/config.js | 1 + test/relay/auto-relay.node.js | 34 +++++++++++++++++----------------- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/circuit/auto-relay.js b/src/circuit/auto-relay.js index c9559ce9..4bffecc0 100644 --- a/src/circuit/auto-relay.js +++ b/src/circuit/auto-relay.js @@ -242,7 +242,7 @@ class AutoRelay { try { const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS) for await (const provider of this._libp2p.contentRouting.findProviders(cid)) { - if (!provider || !provider.id || !provider.multiaddrs || !provider.multiaddrs.length) { + if (!provider || !provider.multiaddrs.length) { continue } const peerId = provider.id diff --git a/src/circuit/constants.js b/src/circuit/constants.js index 53bd6505..b4de629c 100644 --- a/src/circuit/constants.js +++ b/src/circuit/constants.js @@ -3,10 +3,10 @@ const minute = 60 * 1000 module.exports = { - ADVERTISE_BOOT_DELAY: 15 * minute, - ADVERTISE_TTL: 30 * minute, - CIRCUIT_PROTO_CODE: 290, - HOP_METADATA_KEY: 'hop_relay', - HOP_METADATA_VALUE: 'true', - RELAY_RENDEZVOUS_NS: '/libp2p/relay' + ADVERTISE_BOOT_DELAY: 15 * minute, // Delay before HOP relay service is advertised on the network + ADVERTISE_TTL: 30 * minute, // Delay Between HOP relay service advertisements on the network + CIRCUIT_PROTO_CODE: 290, // Multicodec code + HOP_METADATA_KEY: 'hop_relay', // PeerStore metadaBook key for HOP relay service + HOP_METADATA_VALUE: 'true', // PeerStore metadaBook value for HOP relay service + RELAY_RENDEZVOUS_NS: '/libp2p/relay' // Relay HOP relay service namespace for discovery } diff --git a/src/circuit/index.js b/src/circuit/index.js index d0df9041..ae54ce18 100644 --- a/src/circuit/index.js +++ b/src/circuit/index.js @@ -20,8 +20,16 @@ class Relay { * @param {Libp2p} libp2p */ constructor (libp2p) { - this._options = libp2p._config.relay this._libp2p = libp2p + this._options = { + advertise: { + bootDelay: ADVERTISE_BOOT_DELAY, + enabled: true, + ttl: ADVERTISE_TTL, + ...libp2p._config.relay.advertise + }, + ...libp2p._config.relay + } // Create autoRelay if enabled this._autoRelay = this._options.autoRelay.enabled && new AutoRelay({ libp2p, ...this._options.autoRelay }) @@ -35,10 +43,10 @@ class Relay { // Advertise service if HOP enabled const canHop = this._options.hop.enabled - if (canHop) { + if (canHop && this._options.advertise.enabled) { this._timeout = setTimeout(() => { this._advertiseService() - }, this._options.advertise.bootDelay || ADVERTISE_BOOT_DELAY) + }, this._options.advertise.bootDelay) } } @@ -64,12 +72,16 @@ class Relay { } else { log.error(err) } + // Stop the advertise + this.stop() + + return } // Restart timeout this._timeout = setTimeout(() => { this._advertiseService() - }, this._options.advertise.ttl || ADVERTISE_TTL) + }, this._options.advertise.ttl) } } diff --git a/src/config.js b/src/config.js index bc1b5675..4f18f38c 100644 --- a/src/config.js +++ b/src/config.js @@ -63,6 +63,7 @@ const DefaultConfig = { enabled: true, advertise: { bootDelay: RelayConstants.ADVERTISE_BOOT_DELAY, + enabled: true, ttl: RelayConstants.ADVERTISE_TTL }, hop: { diff --git a/test/relay/auto-relay.node.js b/test/relay/auto-relay.node.js index cd0add37..43f42a6a 100644 --- a/test/relay/auto-relay.node.js +++ b/test/relay/auto-relay.node.js @@ -462,15 +462,15 @@ describe('auto-relay', () => { }) describe('discovery', () => { - let libp2p - let libp2p2 + let local + let remote let relayLibp2p beforeEach(async () => { const peerIds = await createPeerId({ number: 3 }) // Create 2 nodes, and turn HOP on for the relay - ;[libp2p, libp2p2, relayLibp2p] = peerIds.map((peerId, index) => { + ;[local, remote, relayLibp2p] = peerIds.map((peerId, index) => { const delegate = new DelegatedContentRouter(peerId, ipfsHttpClient({ host: '0.0.0.0', protocol: 'http', @@ -532,7 +532,7 @@ describe('auto-relay', () => { ]) // Start each node - await Promise.all([libp2p, libp2p2, relayLibp2p].map(libp2p => libp2p.start())) + await Promise.all([local, remote, relayLibp2p].map(libp2p => libp2p.start())) // Should provide on start await pWaitFor(() => relayLibp2p.contentRouting.provide.callCount === 1) @@ -552,32 +552,32 @@ describe('auto-relay', () => { afterEach(() => { // Stop each node - return Promise.all([libp2p, libp2p2, relayLibp2p].map(libp2p => libp2p.stop())) + return Promise.all([local, remote, relayLibp2p].map(libp2p => libp2p.stop())) }) it('should find providers for relay and add it as listen relay', async () => { - const originalMultiaddrsLength = libp2p.multiaddrs.length + const originalMultiaddrsLength = local.multiaddrs.length // Spy add listen relay - sinon.spy(libp2p.relay._autoRelay, '_addListenRelay') + sinon.spy(local.relay._autoRelay, '_addListenRelay') // Spy Find Providers - sinon.spy(libp2p.contentRouting, 'findProviders') + sinon.spy(local.contentRouting, 'findProviders') // Try to listen on Available hop relays - await libp2p.relay._autoRelay._listenOnAvailableHopRelays() + await local.relay._autoRelay._listenOnAvailableHopRelays() // Should try to find relay service providers - await pWaitFor(() => libp2p.contentRouting.findProviders.callCount === 1) + await pWaitFor(() => local.contentRouting.findProviders.callCount === 1) // Wait for peer added as listen relay - await pWaitFor(() => libp2p.relay._autoRelay._addListenRelay.callCount === 1) - expect(libp2p.relay._autoRelay._listenRelays.size).to.equal(1) - await pWaitFor(() => libp2p.multiaddrs.length === originalMultiaddrsLength + 1) + await pWaitFor(() => local.relay._autoRelay._addListenRelay.callCount === 1) + expect(local.relay._autoRelay._listenRelays.size).to.equal(1) + await pWaitFor(() => local.multiaddrs.length === originalMultiaddrsLength + 1) - const relayedAddr = libp2p.multiaddrs[libp2p.multiaddrs.length - 1] - libp2p2.peerStore.addressBook.set(libp2p2.peerId, [relayedAddr]) + const relayedAddr = local.multiaddrs[local.multiaddrs.length - 1] + remote.peerStore.addressBook.set(local.peerId, [relayedAddr]) - // Dial from peer 2 through the relayed address - const conn = await libp2p2.dial(libp2p2.peerId) + // Dial from remote through the relayed address + const conn = await remote.dial(local.peerId) expect(conn).to.exist() }) })