From 034876697a04494436db8030afa5b6e50d6c226f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kr=C3=BCger?= Date: Sat, 12 Jan 2019 14:10:37 +0100 Subject: [PATCH] feat: add stardust behind flag "EXPERIMENTAL.stardust" This adds support for the stardust protocol behind the flag EXPERIMENTAL.stardust This pr is part of the endeavour to replace ws-star with stardust for the time being until a better solution is found. See the discussion at https://github.com/libp2p/js-libp2p-websocket-star/issues/70#issuecomment-453601740 for more information --- package.json | 1 + src/core/components/libp2p.js | 7 ++++++- src/core/runtime/config-browser.js | 1 + src/core/runtime/libp2p-browser.js | 13 +++++++++++-- src/core/runtime/libp2p-nodejs.js | 16 ++++++++++++++-- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 7a4c204881..1e2f9633d8 100644 --- a/package.json +++ b/package.json @@ -137,6 +137,7 @@ "libp2p-mplex": "~0.8.4", "libp2p-record": "~0.6.1", "libp2p-secio": "~0.10.1", + "libp2p-stardust": "0.0.2", "libp2p-tcp": "~0.13.0", "libp2p-webrtc-star": "~0.15.5", "libp2p-websocket-star": "~0.10.0", diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index c5e3a40b1c..ad5b5c14fd 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -26,6 +26,10 @@ module.exports = function libp2p (self) { enabled: get(opts.options, 'config.Discovery.MDNS.Enabled', get(opts.config, 'Discovery.MDNS.Enabled', true)) }, + stardust: { + enabled: get(opts.options, 'config.Discovery.Stardust.Enabled', + get(opts.config, 'Discovery.Stardust.Enabled', true)) + }, webRTCStar: { enabled: get(opts.options, 'config.Discovery.webRTCStar.Enabled', get(opts.config, 'Discovery.webRTCStar.Enabled', true)) @@ -55,7 +59,8 @@ module.exports = function libp2p (self) { }, EXPERIMENTAL: { dht: get(opts.options, 'EXPERIMENTAL.dht', false), - pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false) + pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false), + stardust: get(opts.options, 'EXPERIMENTAL.stardust', false) } }, connectionManager: get(opts.options, 'connectionManager', diff --git a/src/core/runtime/config-browser.js b/src/core/runtime/config-browser.js index ca8c99e153..5556362b3b 100644 --- a/src/core/runtime/config-browser.js +++ b/src/core/runtime/config-browser.js @@ -3,6 +3,7 @@ module.exports = () => ({ Addresses: { Swarm: [ + '/dns4/stardust.mkg20001.io/tcp/443/wss/p2p-stardust' // until it's actually stable it would be good to have this enabled here so `EXPERIMENTAL.stardust` actually does something ], API: '', Gateway: '' diff --git a/src/core/runtime/libp2p-browser.js b/src/core/runtime/libp2p-browser.js index fec12fa9a3..e3d101ee55 100644 --- a/src/core/runtime/libp2p-browser.js +++ b/src/core/runtime/libp2p-browser.js @@ -4,6 +4,7 @@ const WS = require('libp2p-websockets') const WebRTCStar = require('libp2p-webrtc-star') const WebSocketStar = require('libp2p-websocket-star') const Multiplex = require('libp2p-mplex') +const Stardust = require('libp2p-stardust') const SECIO = require('libp2p-secio') const Bootstrap = require('libp2p-bootstrap') const libp2p = require('libp2p') @@ -14,12 +15,18 @@ class Node extends libp2p { const wrtcstar = new WebRTCStar({ id: _options.peerInfo.id }) const wsstar = new WebSocketStar({ id: _options.peerInfo.id }) + let stardust + + if (_options.config.EXPERIMENTAL.stardust) { + stardust = new Stardust({ id: _options.peerInfo.id }) + } + const defaults = { modules: { transport: [ WS, wrtcstar, - wsstar + stardust || wsstar ], streamMuxer: [ Multiplex @@ -29,7 +36,7 @@ class Node extends libp2p { ], peerDiscovery: [ wrtcstar.discovery, - wsstar.discovery, + stardust ? stardust.discovery : wsstar.discovery, Bootstrap ] }, @@ -52,6 +59,8 @@ class Node extends libp2p { } } + delete _options.config.EXPERIMENTAL.stardust + super(defaultsDeep(_options, defaults)) } } diff --git a/src/core/runtime/libp2p-nodejs.js b/src/core/runtime/libp2p-nodejs.js index 1b59c435c7..14b5f1d4ab 100644 --- a/src/core/runtime/libp2p-nodejs.js +++ b/src/core/runtime/libp2p-nodejs.js @@ -7,6 +7,7 @@ const WebSocketStar = require('libp2p-websocket-star') const Bootstrap = require('libp2p-bootstrap') const KadDHT = require('libp2p-kad-dht') const Multiplex = require('libp2p-mplex') +const Stardust = require('libp2p-stardust') const SECIO = require('libp2p-secio') const libp2p = require('libp2p') const defaultsDeep = require('@nodeutils/defaults-deep') @@ -15,12 +16,18 @@ class Node extends libp2p { constructor (_options) { const wsstar = new WebSocketStar({ id: _options.peerInfo.id }) + let stardust + + if (_options.config.EXPERIMENTAL.stardust) { + stardust = new Stardust({ id: _options.peerInfo.id }) + } + const defaults = { modules: { transport: [ TCP, WS, - wsstar + stardust || wsstar ], streamMuxer: [ Multiplex @@ -31,7 +38,7 @@ class Node extends libp2p { peerDiscovery: [ MulticastDNS, Bootstrap, - wsstar.discovery + stardust ? stardust.discovery : wsstar.discovery ], dht: KadDHT }, @@ -40,6 +47,9 @@ class Node extends libp2p { mdns: { enabled: true }, + stardust: { + enabled: true + }, bootstrap: { enabled: true }, @@ -57,6 +67,8 @@ class Node extends libp2p { } } + delete _options.config.EXPERIMENTAL.stardust + super(defaultsDeep(_options, defaults)) } }