Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 1aaaab9

Browse files
jacobheunAlan Shaw
authored and
Alan Shaw
committed
feat: add delegate routers to libp2p config (#2195)
* feat: add delegate routers to libp2p config * refactor: add delegates if they are configured * chore: fix linting * fix: delegate example link License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 80a36e3 commit 1aaaab9

File tree

6 files changed

+115
-2
lines changed

6 files changed

+115
-2
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,18 @@ Enable and configure experimental features.
322322
323323
Modify the default IPFS node config. This object will be *merged* with the default config; it will not replace it.
324324
325+
###### Configuring Delegate Routers
326+
327+
If you need to support Delegated Content and/or Peer Routing, you can enable it by specifying the multiaddrs of your delegate nodes in the config via `options.config.Addresses.Delegates`. If you need to run a delegate router we encourage you to run your own, with go-ipfs. You can see instructions for doing so in the [delegated routing example](https://github.com./libp2p/js-libp2p/tree/master/examples/delegated-routing).
328+
329+
If you are not able to run your own delegate router nodes, we currently have two nodes that support delegated routing. **Important**: As many people may be leveraging these nodes, performance may be affected, which is why we recommend running your own nodes in production.
330+
331+
Available delegate multiaddrs are:
332+
- `/dns4/node0.preload.ipfs.io/tcp/443/https`
333+
- `/dns4/node1.preload.ipfs.io/tcp/443/https`
334+
335+
**Note**: If more than 1 delegate multiaddr is specified, the actual delegate will be randomly selected on startup.
336+
325337
##### `options.ipld`
326338
327339
| Type | Default |

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@
122122
"libp2p": "~0.25.3",
123123
"libp2p-bootstrap": "~0.9.3",
124124
"libp2p-crypto": "~0.16.0",
125+
"libp2p-delegated-content-routing": "^0.2.2",
126+
"libp2p-delegated-peer-routing": "^0.2.2",
125127
"libp2p-kad-dht": "~0.15.1",
126128
"libp2p-keychain": "~0.4.1",
127129
"libp2p-mdns": "~0.12.0",

src/core/components/libp2p.js

+27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
const get = require('dlv')
44
const mergeOptions = require('merge-options')
55
const ipnsUtils = require('../ipns/routing/utils')
6+
const multiaddr = require('multiaddr')
7+
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
8+
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
69

710
module.exports = function libp2p (self, config) {
811
const options = self._options || {}
@@ -35,10 +38,34 @@ module.exports = function libp2p (self, config) {
3538
}
3639

3740
function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
41+
// Set up Delegate Routing based on the presence of Delegates in the config
42+
let contentRouting
43+
let peerRouting
44+
const delegateHosts = get(options, 'config.Addresses.Delegates',
45+
get(config, 'Addresses.Delegates', [])
46+
)
47+
if (delegateHosts.length > 0) {
48+
// Pick a random delegate host
49+
const delegateString = delegateHosts[Math.floor(Math.random() * delegateHosts.length)]
50+
const delegateAddr = multiaddr(delegateString).toOptions()
51+
const delegatedApiOptions = {
52+
host: delegateAddr.host,
53+
// port is a string atm, so we need to convert for the check
54+
protocol: parseInt(delegateAddr.port) === 443 ? 'https' : 'http',
55+
port: delegateAddr.port
56+
}
57+
contentRouting = [new DelegatedContentRouter(peerInfo.id, delegatedApiOptions)]
58+
peerRouting = [new DelegatedPeerRouter(delegatedApiOptions)]
59+
}
60+
3861
const libp2pDefaults = {
3962
datastore,
4063
peerInfo,
4164
peerBook,
65+
modules: {
66+
contentRouting,
67+
peerRouting
68+
},
4269
config: {
4370
peerDiscovery: {
4471
mdns: {

src/core/runtime/config-browser.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module.exports = () => ({
55
Swarm: [
66
],
77
API: '',
8-
Gateway: ''
8+
Gateway: '',
9+
Delegates: []
910
},
1011
Discovery: {
1112
MDNS: {

src/core/runtime/config-nodejs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = () => ({
77
'/ip4/127.0.0.1/tcp/4003/ws'
88
],
99
API: '/ip4/127.0.0.1/tcp/5002',
10-
Gateway: '/ip4/127.0.0.1/tcp/9090'
10+
Gateway: '/ip4/127.0.0.1/tcp/9090',
11+
Delegates: []
1112
},
1213
Discovery: {
1314
MDNS: {

test/core/libp2p.spec.js

+70
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,75 @@ describe('libp2p customization', function () {
222222
done()
223223
})
224224
})
225+
226+
it('should NOT create delegate routers if they are not defined', (done) => {
227+
const ipfs = {
228+
_repo: {
229+
datastore
230+
},
231+
_peerInfo: peerInfo,
232+
_peerBook: peerBook,
233+
// eslint-disable-next-line no-console
234+
_print: console.log,
235+
_options: {
236+
config: {
237+
Addresses: {
238+
Delegates: []
239+
}
240+
}
241+
}
242+
}
243+
244+
_libp2p = libp2pComponent(ipfs, testConfig)
245+
246+
_libp2p.start((err) => {
247+
expect(err).to.not.exist()
248+
249+
expect(_libp2p._modules.contentRouting).to.not.exist()
250+
expect(_libp2p._modules.peerRouting).to.not.exist()
251+
done()
252+
})
253+
})
254+
255+
it('should create delegate routers if they are defined', (done) => {
256+
const ipfs = {
257+
_repo: {
258+
datastore
259+
},
260+
_peerInfo: peerInfo,
261+
_peerBook: peerBook,
262+
// eslint-disable-next-line no-console
263+
_print: console.log,
264+
_options: {
265+
config: {
266+
Addresses: {
267+
Delegates: [
268+
'/dns4/node0.preload.ipfs.io/tcp/443/https'
269+
]
270+
}
271+
}
272+
}
273+
}
274+
275+
_libp2p = libp2pComponent(ipfs, testConfig)
276+
277+
_libp2p.start((err) => {
278+
expect(err).to.not.exist()
279+
280+
expect(_libp2p._modules.contentRouting).to.have.length(1)
281+
expect(_libp2p._modules.contentRouting[0].api).to.include({
282+
host: 'node0.preload.ipfs.io',
283+
port: '443',
284+
protocol: 'https'
285+
})
286+
expect(_libp2p._modules.peerRouting).to.have.length(1)
287+
expect(_libp2p._modules.peerRouting[0].api).to.include({
288+
host: 'node0.preload.ipfs.io',
289+
port: '443',
290+
protocol: 'https'
291+
})
292+
done()
293+
})
294+
})
225295
})
226296
})

0 commit comments

Comments
 (0)