mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
chore: address review
This commit is contained in:
parent
0bf0b7cf89
commit
5c72424e57
@ -242,7 +242,7 @@ class AutoRelay {
|
|||||||
try {
|
try {
|
||||||
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
||||||
for await (const provider of this._libp2p.contentRouting.findProviders(cid)) {
|
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
|
continue
|
||||||
}
|
}
|
||||||
const peerId = provider.id
|
const peerId = provider.id
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
const minute = 60 * 1000
|
const minute = 60 * 1000
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
ADVERTISE_BOOT_DELAY: 15 * minute,
|
ADVERTISE_BOOT_DELAY: 15 * minute, // Delay before HOP relay service is advertised on the network
|
||||||
ADVERTISE_TTL: 30 * minute,
|
ADVERTISE_TTL: 30 * minute, // Delay Between HOP relay service advertisements on the network
|
||||||
CIRCUIT_PROTO_CODE: 290,
|
CIRCUIT_PROTO_CODE: 290, // Multicodec code
|
||||||
HOP_METADATA_KEY: 'hop_relay',
|
HOP_METADATA_KEY: 'hop_relay', // PeerStore metadaBook key for HOP relay service
|
||||||
HOP_METADATA_VALUE: 'true',
|
HOP_METADATA_VALUE: 'true', // PeerStore metadaBook value for HOP relay service
|
||||||
RELAY_RENDEZVOUS_NS: '/libp2p/relay'
|
RELAY_RENDEZVOUS_NS: '/libp2p/relay' // Relay HOP relay service namespace for discovery
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,16 @@ class Relay {
|
|||||||
* @param {Libp2p} libp2p
|
* @param {Libp2p} libp2p
|
||||||
*/
|
*/
|
||||||
constructor (libp2p) {
|
constructor (libp2p) {
|
||||||
this._options = libp2p._config.relay
|
|
||||||
this._libp2p = libp2p
|
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
|
// Create autoRelay if enabled
|
||||||
this._autoRelay = this._options.autoRelay.enabled && new AutoRelay({ libp2p, ...this._options.autoRelay })
|
this._autoRelay = this._options.autoRelay.enabled && new AutoRelay({ libp2p, ...this._options.autoRelay })
|
||||||
@ -35,10 +43,10 @@ class Relay {
|
|||||||
// Advertise service if HOP enabled
|
// Advertise service if HOP enabled
|
||||||
const canHop = this._options.hop.enabled
|
const canHop = this._options.hop.enabled
|
||||||
|
|
||||||
if (canHop) {
|
if (canHop && this._options.advertise.enabled) {
|
||||||
this._timeout = setTimeout(() => {
|
this._timeout = setTimeout(() => {
|
||||||
this._advertiseService()
|
this._advertiseService()
|
||||||
}, this._options.advertise.bootDelay || ADVERTISE_BOOT_DELAY)
|
}, this._options.advertise.bootDelay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,12 +72,16 @@ class Relay {
|
|||||||
} else {
|
} else {
|
||||||
log.error(err)
|
log.error(err)
|
||||||
}
|
}
|
||||||
|
// Stop the advertise
|
||||||
|
this.stop()
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restart timeout
|
// Restart timeout
|
||||||
this._timeout = setTimeout(() => {
|
this._timeout = setTimeout(() => {
|
||||||
this._advertiseService()
|
this._advertiseService()
|
||||||
}, this._options.advertise.ttl || ADVERTISE_TTL)
|
}, this._options.advertise.ttl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,6 +63,7 @@ const DefaultConfig = {
|
|||||||
enabled: true,
|
enabled: true,
|
||||||
advertise: {
|
advertise: {
|
||||||
bootDelay: RelayConstants.ADVERTISE_BOOT_DELAY,
|
bootDelay: RelayConstants.ADVERTISE_BOOT_DELAY,
|
||||||
|
enabled: true,
|
||||||
ttl: RelayConstants.ADVERTISE_TTL
|
ttl: RelayConstants.ADVERTISE_TTL
|
||||||
},
|
},
|
||||||
hop: {
|
hop: {
|
||||||
|
@ -462,15 +462,15 @@ describe('auto-relay', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('discovery', () => {
|
describe('discovery', () => {
|
||||||
let libp2p
|
let local
|
||||||
let libp2p2
|
let remote
|
||||||
let relayLibp2p
|
let relayLibp2p
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const peerIds = await createPeerId({ number: 3 })
|
const peerIds = await createPeerId({ number: 3 })
|
||||||
|
|
||||||
// Create 2 nodes, and turn HOP on for the relay
|
// 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({
|
const delegate = new DelegatedContentRouter(peerId, ipfsHttpClient({
|
||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
protocol: 'http',
|
protocol: 'http',
|
||||||
@ -532,7 +532,7 @@ describe('auto-relay', () => {
|
|||||||
])
|
])
|
||||||
|
|
||||||
// Start each node
|
// 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
|
// Should provide on start
|
||||||
await pWaitFor(() => relayLibp2p.contentRouting.provide.callCount === 1)
|
await pWaitFor(() => relayLibp2p.contentRouting.provide.callCount === 1)
|
||||||
@ -552,32 +552,32 @@ describe('auto-relay', () => {
|
|||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
// Stop each node
|
// 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 () => {
|
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
|
// Spy add listen relay
|
||||||
sinon.spy(libp2p.relay._autoRelay, '_addListenRelay')
|
sinon.spy(local.relay._autoRelay, '_addListenRelay')
|
||||||
// Spy Find Providers
|
// Spy Find Providers
|
||||||
sinon.spy(libp2p.contentRouting, 'findProviders')
|
sinon.spy(local.contentRouting, 'findProviders')
|
||||||
|
|
||||||
// Try to listen on Available hop relays
|
// Try to listen on Available hop relays
|
||||||
await libp2p.relay._autoRelay._listenOnAvailableHopRelays()
|
await local.relay._autoRelay._listenOnAvailableHopRelays()
|
||||||
|
|
||||||
// Should try to find relay service providers
|
// 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
|
// Wait for peer added as listen relay
|
||||||
await pWaitFor(() => libp2p.relay._autoRelay._addListenRelay.callCount === 1)
|
await pWaitFor(() => local.relay._autoRelay._addListenRelay.callCount === 1)
|
||||||
expect(libp2p.relay._autoRelay._listenRelays.size).to.equal(1)
|
expect(local.relay._autoRelay._listenRelays.size).to.equal(1)
|
||||||
await pWaitFor(() => libp2p.multiaddrs.length === originalMultiaddrsLength + 1)
|
await pWaitFor(() => local.multiaddrs.length === originalMultiaddrsLength + 1)
|
||||||
|
|
||||||
const relayedAddr = libp2p.multiaddrs[libp2p.multiaddrs.length - 1]
|
const relayedAddr = local.multiaddrs[local.multiaddrs.length - 1]
|
||||||
libp2p2.peerStore.addressBook.set(libp2p2.peerId, [relayedAddr])
|
remote.peerStore.addressBook.set(local.peerId, [relayedAddr])
|
||||||
|
|
||||||
// Dial from peer 2 through the relayed address
|
// Dial from remote through the relayed address
|
||||||
const conn = await libp2p2.dial(libp2p2.peerId)
|
const conn = await remote.dial(local.peerId)
|
||||||
expect(conn).to.exist()
|
expect(conn).to.exist()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user