From 7ea2eae3318ac3b7e602200c958507c447b7d14c Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 24 Jul 2019 13:26:08 +0200 Subject: [PATCH] refactor: remove the need for swarm connect This removes all code that caused swarm connect calls from delegate to self. It was not needed: delegate is one of bootstrap nodes, so we are always connected to it. https://github.com/libp2p/js-libp2p-delegated-content-routing/issues/12#issuecomment-514591525 License: MIT Signed-off-by: Marcin Rataj --- src/index.js | 51 ++------------------------------------------------- 1 file changed, 2 insertions(+), 49 deletions(-) diff --git a/src/index.js b/src/index.js index c660197..973716d 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,6 @@ const dht = require('ipfs-http-client/src/dht') const swarm = require('ipfs-http-client/src/swarm') const refs = require('ipfs-http-client/src/files-regular/refs') const defaultConfig = require('ipfs-http-client/src/utils/default-config') -const multiaddr = require('multiaddr') const { default: PQueue } = require('p-queue') const pMemoize = require('p-memoize') const debug = require('debug') @@ -19,18 +18,6 @@ const DEFAULT_IPFS_API = { host: 'node0.delegate.ipfs.io' } -// assuming below nodes need have autorelay enabled -const DEFAULT_BOOSTRAP_NODES = [ - '/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd', - '/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3', - '/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM', - '/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu', - '/ipfs/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm', - '/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64', - '/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic', - '/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6' -] - const CONCURRENT_HTTP_REQUESTS = 4 const SWARM_CONNECT_MAX_AGE = 60e3 @@ -43,9 +30,8 @@ class DelegatedContentRouting { * * @param {PeerID} peerId - the id of the node that is using this routing. * @param {object} [api] - (Optional) the api endpoint of the delegated node to use. - * @param {Array} [bootstrappers] - (Optional) list of bootstrapper nodes we are connected to. */ - constructor (peerId, api, bootstrappers) { + constructor (peerId, api) { if (peerId == null) { throw new Error('missing self peerId') } @@ -58,11 +44,6 @@ class DelegatedContentRouting { this.refs = refs(this.api) this.peerId = peerId - bootstrappers = bootstrappers || DEFAULT_BOOSTRAP_NODES.map((addr) => multiaddr(addr)) - this.circuits = bootstrappers.map((addr) => { - return addr.encapsulate(`/p2p-circuit/ipfs/${this.peerId.toB58String()}`) - }) - // limit concurrency to avoid request flood in web browser // https://github.com/libp2p/js-libp2p-delegated-content-routing/issues/12 const concurrency = { concurrency: CONCURRENT_HTTP_REQUESTS } @@ -104,7 +85,7 @@ class DelegatedContentRouting { * Announce to the network that the delegated node can provide the given key. * * Currently this uses the following hack - * - call swarm.connect on the delegated node to us, to ensure we are connected + * - delegate is one of bootstrap nodes, so we are always connected to it * - call refs on the delegated node, so it fetches the content * * @param {CID} key @@ -114,34 +95,6 @@ class DelegatedContentRouting { async provide (key) { const keyString = key.toBaseEncodedString() log('provide starts: ' + keyString) - - let results - try { - // optimization: try the first addr - // (swarm.connect will return success if ANY connection to this.peerId already exists) - const addr = this.circuits.find(a => Boolean(a)) - const res = await this._httpQueue.add(() => this.swarm.connect(addr.toString())) - if (res && res.error) throw new Error() // trigger fallback - results = [res] - } catch (err) { - // fallback to trying all potential circuits - results = await Promise.all( - this.circuits.map((addr) => - this._httpQueue.add(() => - this.swarm.connect(addr.toString()).catch(() => {}) - ) - ) - ) - } - // only some need to succeed - const success = results.filter((res) => res && res.error == null) - - if (success.length === 0) { - throw new Error('unable to swarm.connect using p2p-circuit') - } - - // async preload of data to delegate node - // note: we call `provide` for every block, so it does not need to be recursive await this._httpQueueRefs.add(() => this.refs(keyString, { recursive: false }) )