refactor: examples-delegated-routing

This commit is contained in:
Vasco Santos 2019-12-10 19:40:39 +01:00
parent 51da8874d8
commit f596ecdaa5
4 changed files with 43 additions and 55 deletions

View File

@ -10,7 +10,7 @@ especially useful when your libp2p node will have limited resources, making runn
also highly useful if your node is generating content, but can't reliably be on the network. You can use delegate nodes
to provide content on your behalf.
The starting [Libp2p Bundle](./src/libp2p-bundle.js) in this example starts by disabling the DHT and adding the Delegated Peer and Content Routers.
The starting [Libp2p Configuration](./src/libp2p-configuration.js) in this example starts by disabling the DHT and adding the Delegated Peer and Content Routers.
Once you've completed the example, you should try enabled the DHT and see what kind of results you get! You can also enable the
various Peer Discovery modules and see the impact it has on your Peer count.
@ -23,7 +23,9 @@ various Peer Discovery modules and see the impact it has on your Peer count.
2. Run the IPFS daemon: `ipfs daemon`
3. The daemon will output a line about its API address, like `API server listening on /ip4/127.0.0.1/tcp/8080`
4. In another window output the addresses of the node: `ipfs id`. Make note of the websocket address, it will contain `/ws/` in the address.
5. In `./src/libp2p-bundle.js` check if the host and port of your node are correct, according to the previous step. If they are different, replace them.
- If there is no websocket address, you will need to add it in the ipfs config file (`~/.ipfs/config`)
- Add to Swarm Addresses something like: `"/ip4/127.0.0.1/tcp/4010/ws"`
5. In `./src/libp2p-configuration.js` check if the host and port of your node are correct, according to the previous step. If they are different, replace them.
6. In `./src/App.js` replace `BootstrapNode` with your nodes Websocket address from step 4.
7. Start this example:

View File

@ -3,19 +3,18 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"ipfs": "~0.34.4",
"libp2p": "github:libp2p/js-libp2p#master",
"libp2p-delegated-content-routing": "~0.2.2",
"libp2p-delegated-peer-routing": "~0.2.2",
"libp2p-kad-dht": "~0.14.12",
"libp2p-mplex": "~0.8.5",
"libp2p-secio": "~0.11.1",
"libp2p-webrtc-star": "~0.15.8",
"libp2p-websocket-star": "~0.10.2",
"libp2p-websockets": "~0.12.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "2.1.8"
"ipfs": "~0.40.0",
"libp2p": "../..",
"libp2p-delegated-content-routing": "~0.4.1",
"libp2p-delegated-peer-routing": "~0.4.0",
"libp2p-kad-dht": "~0.18.2",
"libp2p-mplex": "~0.9.3",
"libp2p-secio": "~0.12.1",
"libp2p-webrtc-star": "~0.17.0",
"libp2p-websockets": "~0.13.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.3.0"
},
"scripts": {
"start": "react-scripts start"

View File

@ -3,12 +3,11 @@
import React from 'react'
import Ipfs from 'ipfs'
import libp2pBundle from './libp2p-bundle'
const Component = React.Component
import libp2pConfig from './libp2p-configuration'
const BootstrapNode = '/ip4/127.0.0.1/tcp/8081/ws/p2p/QmdoG8DpzYUZMVP5dGmgmigZwR1RE8Cf6SxMPg1SBXJAQ8'
const BootstrapNode = '/ip4/127.0.0.1/tcp/4004/ws/ipfs/QmPHafDaco9vynQ93MHv5cRSW6UCECycCGdTRafL8X5WEj'
class App extends Component {
class App extends React.Component {
constructor (props) {
super(props)
this.state = {
@ -38,34 +37,30 @@ class App extends Component {
})
}
handleHashSubmit (event) {
async handleHashSubmit (event) {
event.preventDefault()
this.setState({
isLoading: this.state.isLoading + 1
})
this.ipfs.cat(this.state.hash, (err, data) => {
if (err) console.log('Error', err)
const data = await this.ipfs.cat(this.state.hash)
this.setState({
response: data.toString(),
isLoading: this.state.isLoading - 1
})
this.setState({
response: data.toString(),
isLoading: this.state.isLoading - 1
})
}
handlePeerSubmit (event) {
async handlePeerSubmit (event) {
event.preventDefault()
this.setState({
isLoading: this.state.isLoading + 1
})
this.ipfs.dht.findpeer(this.state.peer, (err, results) => {
if (err) console.log('Error', err)
const results = await this.ipfs.dht.findpeer(this.state.peer)
this.setState({
response: JSON.stringify(results, null, 2),
isLoading: this.state.isLoading - 1
})
this.setState({
response: JSON.stringify(results, null, 2),
isLoading: this.state.isLoading - 1
})
}
@ -90,25 +85,22 @@ class App extends Component {
preload: {
enabled: false
},
libp2p: libp2pBundle
libp2p: libp2pConfig
})
this.ipfs.on('ready', () => {
this.ipfs.on('ready', async () => {
if (this.peerInterval) {
clearInterval(this.peerInterval)
}
this.ipfs.swarm.connect(BootstrapNode, (err) => {
if (err) {
console.log('Error connecting to the node', err)
}
console.log('Connected!')
})
this.peerInterval = setInterval(() => {
this.ipfs.swarm.peers((err, peers) => {
if (err) console.log(err)
if (peers) this.setState({peers: peers.length})
})
await this.ipfs.swarm.connect(BootstrapNode)
console.log('Connected!')
this.peerInterval = setInterval(async () => {
const peers = await this.ipfs.swarm.peers()
if (peers) this.setState({peers: peers.length})
}, 2500)
})
}

View File

@ -3,7 +3,6 @@
const Libp2p = require('libp2p')
const Websockets = require('libp2p-websockets')
const WebSocketStar = require('libp2p-websocket-star')
const WebRTCStar = require('libp2p-webrtc-star')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
@ -11,18 +10,16 @@ const KadDHT = require('libp2p-kad-dht')
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
const DelegatedContentRouter = require('libp2p-delegated-content-routing')
export default function Libp2pBundle ({peerInfo, peerBook}) {
export default function Libp2pConfiguration ({peerInfo}) {
const wrtcstar = new WebRTCStar({id: peerInfo.id})
const wsstar = new WebSocketStar({id: peerInfo.id})
const delegatedApiOptions = {
host: '0.0.0.0',
host: '127.0.0.1',
protocol: 'http',
port: '8080'
port: '5001'
}
return new Libp2p({
peerInfo,
peerBook,
// Lets limit the connection managers peers and have it check peer health less frequently
connectionManager: {
maxPeers: 10,
@ -36,12 +33,10 @@ export default function Libp2pBundle ({peerInfo, peerBook}) {
new DelegatedPeerRouter(delegatedApiOptions)
],
peerDiscovery: [
wrtcstar.discovery,
wsstar.discovery
wrtcstar.discovery
],
transport: [
wrtcstar,
wsstar,
Websockets
],
streamMuxer: [