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

feat: replace websocket-star with stardust #2600

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1003,22 +1003,25 @@ The code above assumes you are running a local `signaling server` on port `9090`

#### Is there a more stable alternative to webrtc-star that offers a similar functionality?

Yes, websocket-star! A WebSockets based transport that uses a Relay to route the messages. To enable it, just do:
Yes, stardust! A WebSockets based transport that uses a Relay to route the messages. To enable it, just do:

```JavaScript
const node = await IPFS.create({
libp2p: require('stardust4ipfs'),
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
'/dns4/stardust.mkg20001.io/tcp/443/wss/p2p-stardust'
]
}
}
})

// your instance with websocket-star is ready
// your instance with stardust is ready
```

(You may need to install the stardust4ipfs module as well, with `npm install stardust4ipfs`)

#### I see some slowness when hopping between tabs Chrome with IPFS nodes, is there a reason why?

Yes, unfortunately, due to [Chrome aggressive resource throttling policy](https://github.com/ipfs/js-ipfs/issues/611), it cuts freezes the execution of any background tab, turning an IPFS node that was running on that webpage into a vegetable state.
Expand Down
2 changes: 1 addition & 1 deletion examples/circuit-relaying/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ In order to enable the relay functionality in `go-ipfs` we need to edit it's con

The two options we're looking for are `DisableRelay` and `EnableRelayHop`. We want the former (`DisableRelay`) set to `false` and the latter (`EnableRelayHop`) to `true`, just like in the example above. That should set our go node as a relay.

We also need to make sure our go node can be dialed from the browser. For that, we need to enable a transport that both the browser and the go node can communicate over. We will use the web sockets transport, although there are others that can be used, such as `webrtc-star` and `websocket-star`. To enable the transport and set the interface and port we need to edit the `~/.ipfs/config` one more time. Let's find the `Swarm` array and add our desired address there. I picked `/ip4/0.0.0.0/tcp/4004/ws` because it is a port I know is not being used by anything on my machine, but we can also use port `0` so that the OS chooses a random available port for us — either one should work.
We also need to make sure our go node can be dialed from the browser. For that, we need to enable a transport that both the browser and the go node can communicate over. We will use the web sockets transport, although there are others that can be used, such as `webrtc-star` and `stardust`. To enable the transport and set the interface and port we need to edit the `~/.ipfs/config` one more time. Let's find the `Swarm` array and add our desired address there. I picked `/ip4/0.0.0.0/tcp/4004/ws` because it is a port I know is not being used by anything on my machine, but we can also use port `0` so that the OS chooses a random available port for us — either one should work.

```json
"Swarm": [
Expand Down
10 changes: 5 additions & 5 deletions examples/custom-libp2p/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Libp2p = require('libp2p')
const IPFS = require('ipfs')
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WebSocketStar = require('libp2p-websocket-star')
const Stardust = require('libp2p-stardust')
const Bootstrap = require('libp2p-bootstrap')
const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
Expand Down Expand Up @@ -32,8 +32,8 @@ const libp2pBundle = (opts) => {
const peerBook = opts.peerBook
const bootstrapList = opts.config.Bootstrap

// Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node
const wsstar = new WebSocketStar({
// Create our Stardust transport and give it our PeerId, straight from the ipfs node
const stardust = new Stardust({
id: peerInfo.id
})

Expand All @@ -50,7 +50,7 @@ const libp2pBundle = (opts) => {
modules: {
transport: [
TCP,
wsstar
stardust
],
streamMuxer: [
MPLEX,
Expand All @@ -62,7 +62,7 @@ const libp2pBundle = (opts) => {
peerDiscovery: [
MulticastDNS,
Bootstrap,
wsstar.discovery
stardust.discovery
],
dht: KadDHT
},
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-libp2p/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"libp2p-secio": "~0.11.1",
"libp2p-spdy": "~0.13.3",
"libp2p-tcp": "~0.13.0",
"libp2p-websocket-star": "~0.10.2",
"libp2p-stardust": "~0.1.2",
"pull-mplex": "~0.1.0"
}
}
4 changes: 3 additions & 1 deletion examples/exchange-files-in-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"license": "MIT",
"devDependencies": {
"browserify": "^16.2.3",
"http-server": "~0.11.1"
"http-server": "~0.11.1",
"stardust4ipfs": "~0.2.0"
},
"dependencies": {
"ipfs": "file:../../"
Expand All @@ -18,3 +19,4 @@
"ipfs": "ipfs/dist"
}
}

7 changes: 4 additions & 3 deletions examples/exchange-files-in-browser/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ async function start () {
if (!node) {
const options = {
repo: 'ipfs-' + Math.random(),
libp2p: require('stardust4ipfs'),
config: {
Addresses: {
Swarm: ['/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star']
Swarm: ['/dns4/stardust.mkg20001.io/tcp/443/wss/p2p-stardust']
}
}
}
Expand Down Expand Up @@ -154,7 +155,7 @@ const publishHash = (hash) => {
const sendFileList = () => Promise.all(FILES.map(publishHash))

const updateProgress = (bytesLoaded) => {
let percent = 100 - ((bytesLoaded / fileSize) * 100)
const percent = 100 - ((bytesLoaded / fileSize) * 100)

$progressBar.style.transform = `translateX(${-percent}%)`
}
Expand Down Expand Up @@ -261,7 +262,7 @@ async function connectToPeer (event) {

await node.swarm.connect(multiaddr)

onSuccess(`Successfully connected to peer.`)
onSuccess('Successfully connected to peer.')
$multiaddrInput.value = ''
}

Expand Down
2 changes: 1 addition & 1 deletion package-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
["libp2p/js-libp2p-bootstrap", "libp2p-bootstrap"],
["libp2p/js-libp2p-secio", "libp2p-secio"],
["libp2p/js-libp2p-tcp", "libp2p-tcp"],
["libp2p/js-libp2p-stardust", "libp2p-stardust"],
["libp2p/js-libp2p-webrtc-star", "libp2p-webrtc-star"],
["libp2p/js-libp2p-websocket-star", "libp2p-websocket-star"],
["libp2p/js-libp2p-websockets", "libp2p-websockets"],
["libp2p/pull-mplex", "pull-mplex"],

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
"libp2p-secio": "~0.11.0",
"libp2p-tcp": "^0.13.0",
"libp2p-webrtc-star": "~0.16.0",
"libp2p-websocket-star-multi": "~0.4.3",
"libp2p-websockets": "~0.12.3",
"lodash": "^4.17.15",
"lodash.flatten": "^4.4.0",
Expand Down Expand Up @@ -207,12 +206,13 @@
"interface-ipfs-core": "^0.118.0",
"ipfs-interop": "^0.1.1",
"ipfsd-ctl": "^0.47.2",
"libp2p-websocket-star": "~0.10.2",
"libp2p-stardust": "~0.1.2",
"ncp": "^2.0.0",
"p-event": "^4.1.0",
"qs": "^6.5.2",
"rimraf": "^3.0.0",
"sinon": "^7.4.2",
"stardust4ipfs": "^0.2.0",
"stream-to-promise": "^2.2.0",
"temp-write": "^4.0.0"
},
Expand Down Expand Up @@ -358,3 +358,4 @@
"Максим Ильин <negamaxi@gmail.com>"
]
}

15 changes: 1 addition & 14 deletions src/core/runtime/libp2p-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@

const WS = require('libp2p-websockets')
const WebRTCStar = require('libp2p-webrtc-star')
const WebSocketStarMulti = require('libp2p-websocket-star-multi')
const Multiplex = require('pull-mplex')
const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap')
const KadDHT = require('libp2p-kad-dht')
const GossipSub = require('libp2p-gossipsub')
const multiaddr = require('multiaddr')

module.exports = ({ peerInfo, options }) => {
const wrtcstar = new WebRTCStar({ id: peerInfo.id })

// this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41
// const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
const wsstarServers = peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star'))
peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers
const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: peerInfo.id, ignore_no_online: !wsstarServers.length || options.wsStarIgnoreErrors })

return {
switch: {
denyTTL: 2 * 60 * 1e3, // 2 minute base
Expand All @@ -30,8 +22,7 @@ module.exports = ({ peerInfo, options }) => {
modules: {
transport: [
WS,
wrtcstar,
wsstar
wrtcstar
],
streamMuxer: [
Multiplex
Expand All @@ -41,7 +32,6 @@ module.exports = ({ peerInfo, options }) => {
],
peerDiscovery: [
wrtcstar.discovery,
wsstar.discovery,
Bootstrap
],
dht: KadDHT,
Expand All @@ -55,9 +45,6 @@ module.exports = ({ peerInfo, options }) => {
},
webRTCStar: {
enabled: true
},
websocketStar: {
enabled: true
}
},
dht: {
Expand Down
17 changes: 2 additions & 15 deletions src/core/runtime/libp2p-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,13 @@
const TCP = require('libp2p-tcp')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const WebSocketStarMulti = require('libp2p-websocket-star-multi')
const Bootstrap = require('libp2p-bootstrap')
const KadDHT = require('libp2p-kad-dht')
const GossipSub = require('libp2p-gossipsub')
const Multiplex = require('pull-mplex')
const SECIO = require('libp2p-secio')
const multiaddr = require('multiaddr')

module.exports = ({ peerInfo, options }) => {
// this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41
// const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
const wsstarServers = peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star'))
peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers
const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: peerInfo.id, ignore_no_online: !wsstarServers.length || options.wsStarIgnoreErrors })

return {
switch: {
denyTTL: 2 * 60 * 1e3, // 2 minute base
Expand All @@ -29,8 +21,7 @@ module.exports = ({ peerInfo, options }) => {
modules: {
transport: [
TCP,
WS,
wsstar
WS
],
streamMuxer: [
Multiplex
Expand All @@ -40,8 +31,7 @@ module.exports = ({ peerInfo, options }) => {
],
peerDiscovery: [
MulticastDNS,
Bootstrap,
wsstar.discovery
Bootstrap
],
dht: KadDHT,
pubsub: GossipSub
Expand All @@ -54,9 +44,6 @@ module.exports = ({ peerInfo, options }) => {
},
bootstrap: {
enabled: true
},
websocketStar: {
enabled: true
}
},
dht: {
Expand Down
22 changes: 8 additions & 14 deletions test/core/libp2p.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha')
const MemoryStore = require('interface-datastore').MemoryDatastore
const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
const WebSocketStar = require('libp2p-websocket-star')
const Stardust = require('libp2p-stardust')
const Multiplex = require('pull-mplex')
const SECIO = require('libp2p-secio')
const KadDHT = require('libp2p-kad-dht')
Expand Down Expand Up @@ -71,14 +71,14 @@ describe('libp2p customization', function () {
_print: console.log,
_options: {
libp2p: (opts) => {
const wsstar = new WebSocketStar({ id: opts.peerInfo.id })
const stardust = new Stardust({ id: opts.peerInfo.id })

return new Libp2p({
peerInfo: opts.peerInfo,
peerBook: opts.peerBook,
modules: {
transport: [
wsstar
stardust
],
streamMuxer: [
Multiplex
Expand All @@ -87,7 +87,7 @@ describe('libp2p customization', function () {
SECIO
],
peerDiscovery: [
wsstar.discovery
stardust.discovery
],
dht: KadDHT
}
Expand Down Expand Up @@ -170,9 +170,6 @@ describe('libp2p customization', function () {
},
webRTCStar: {
enabled: false
},
websocketStar: {
enabled: true
}
},
pubsub: {
Expand All @@ -182,13 +179,13 @@ describe('libp2p customization', function () {
strictSigning: true
}
})
expect(_libp2p._transport).to.have.length(3)
expect(_libp2p._transport).to.have.length(2)
done()
})
})

it('should allow for overriding via options', (done) => {
const wsstar = new WebSocketStar({ id: peerInfo.id })
const stardust = new Stardust({ id: peerInfo.id })

const ipfs = {
_repo: {
Expand All @@ -212,10 +209,10 @@ describe('libp2p customization', function () {
libp2p: {
modules: {
transport: [
wsstar
stardust
],
peerDiscovery: [
wsstar.discovery
stardust.discovery
]
}
}
Expand All @@ -238,9 +235,6 @@ describe('libp2p customization', function () {
},
webRTCStar: {
enabled: false
},
websocketStar: {
enabled: true
}
}
})
Expand Down