Skip to content

Commit

Permalink
refactor: remove the need for swarm connect
Browse files Browse the repository at this point in the history
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.

libp2p#12 (comment)

License: MIT
Signed-off-by: Marcin Rataj <[email protected]>
  • Loading branch information
lidel committed Jul 24, 2019
1 parent 735c70f commit 7ea2eae
Showing 1 changed file with 2 additions and 49 deletions.
51 changes: 2 additions & 49 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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

Expand All @@ -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<Multiaddr>} [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')
}
Expand All @@ -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 }
Expand Down Expand Up @@ -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
Expand All @@ -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 })
)
Expand Down

0 comments on commit 7ea2eae

Please sign in to comment.