mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
refactor: examples/peer-and-content-routing (#500)
* refactor: examples-peer-and-content-routing * chore: address review * chore: review suggestions Co-Authored-By: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
parent
835a689965
commit
7b326cc525
@ -1,77 +1,55 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const libp2p = require('../../')
|
const Libp2p = require('../../')
|
||||||
const TCP = require('libp2p-tcp')
|
const TCP = require('libp2p-tcp')
|
||||||
const Mplex = require('libp2p-mplex')
|
const Mplex = require('libp2p-mplex')
|
||||||
const SECIO = require('libp2p-secio')
|
const SECIO = require('libp2p-secio')
|
||||||
const PeerInfo = require('peer-info')
|
const PeerInfo = require('peer-info')
|
||||||
const KadDHT = require('libp2p-kad-dht')
|
const KadDHT = require('libp2p-kad-dht')
|
||||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
|
||||||
const waterfall = require('async/waterfall')
|
|
||||||
const parallel = require('async/parallel')
|
|
||||||
|
|
||||||
class MyBundle extends libp2p {
|
const delay = require('delay')
|
||||||
constructor (_options) {
|
|
||||||
const defaults = {
|
const createNode = async () => {
|
||||||
modules: {
|
const peerInfo = await PeerInfo.create()
|
||||||
transport: [ TCP ],
|
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||||
streamMuxer: [ Mplex ],
|
|
||||||
connEncryption: [ SECIO ],
|
const node = await Libp2p.create({
|
||||||
// we add the DHT module that will enable Peer and Content Routing
|
peerInfo,
|
||||||
dht: KadDHT
|
modules: {
|
||||||
},
|
transport: [TCP],
|
||||||
config: {
|
streamMuxer: [Mplex],
|
||||||
dht: {
|
connEncryption: [SECIO],
|
||||||
enabled: true,
|
dht: KadDHT
|
||||||
kBucketSize: 20
|
},
|
||||||
}
|
config: {
|
||||||
|
dht: {
|
||||||
|
enabled: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
super(defaultsDeep(_options, defaults))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createNode (callback) {
|
|
||||||
let node
|
|
||||||
|
|
||||||
waterfall([
|
|
||||||
(cb) => PeerInfo.create(cb),
|
|
||||||
(peerInfo, cb) => {
|
|
||||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
|
||||||
node = new MyBundle({
|
|
||||||
peerInfo
|
|
||||||
})
|
|
||||||
node.start(cb)
|
|
||||||
}
|
|
||||||
], (err) => callback(err, node))
|
|
||||||
}
|
|
||||||
|
|
||||||
parallel([
|
|
||||||
(cb) => createNode(cb),
|
|
||||||
(cb) => createNode(cb),
|
|
||||||
(cb) => createNode(cb)
|
|
||||||
], (err, nodes) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
const node1 = nodes[0]
|
|
||||||
const node2 = nodes[1]
|
|
||||||
const node3 = nodes[2]
|
|
||||||
|
|
||||||
parallel([
|
|
||||||
(cb) => node1.dial(node2.peerInfo, cb),
|
|
||||||
(cb) => node2.dial(node3.peerInfo, cb),
|
|
||||||
// Set up of the cons might take time
|
|
||||||
(cb) => setTimeout(cb, 300)
|
|
||||||
], (err) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
node1.peerRouting.findPeer(node3.peerInfo.id, (err, peer) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
console.log('Found it, multiaddrs are:')
|
|
||||||
peer.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
await node.start()
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
;(async () => {
|
||||||
|
const [node1, node2, node3] = await Promise.all([
|
||||||
|
createNode(),
|
||||||
|
createNode(),
|
||||||
|
createNode()
|
||||||
|
])
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
node1.dial(node2.peerInfo),
|
||||||
|
node2.dial(node3.peerInfo)
|
||||||
|
])
|
||||||
|
|
||||||
|
// The DHT routing tables need a moment to populate
|
||||||
|
await delay(100)
|
||||||
|
|
||||||
|
const peer = await node1.peerRouting.findPeer(node3.peerInfo.id)
|
||||||
|
|
||||||
|
console.log('Found it, multiaddrs are:')
|
||||||
|
peer.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||||
|
})();
|
||||||
|
@ -1,85 +1,61 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const libp2p = require('../../')
|
const Libp2p = require('../../')
|
||||||
const TCP = require('libp2p-tcp')
|
const TCP = require('libp2p-tcp')
|
||||||
const Mplex = require('libp2p-mplex')
|
const Mplex = require('libp2p-mplex')
|
||||||
const SECIO = require('libp2p-secio')
|
const SECIO = require('libp2p-secio')
|
||||||
const PeerInfo = require('peer-info')
|
const PeerInfo = require('peer-info')
|
||||||
const CID = require('cids')
|
const CID = require('cids')
|
||||||
const KadDHT = require('libp2p-kad-dht')
|
const KadDHT = require('libp2p-kad-dht')
|
||||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
|
||||||
const waterfall = require('async/waterfall')
|
|
||||||
const parallel = require('async/parallel')
|
|
||||||
|
|
||||||
class MyBundle extends libp2p {
|
const all = require('it-all')
|
||||||
constructor (_options) {
|
const delay = require('delay')
|
||||||
const defaults = {
|
|
||||||
modules: {
|
const createNode = async () => {
|
||||||
transport: [ TCP ],
|
const peerInfo = await PeerInfo.create()
|
||||||
streamMuxer: [ Mplex ],
|
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||||
connEncryption: [ SECIO ],
|
|
||||||
// we add the DHT module that will enable Peer and Content Routing
|
const node = await Libp2p.create({
|
||||||
dht: KadDHT
|
peerInfo,
|
||||||
},
|
modules: {
|
||||||
config: {
|
transport: [TCP],
|
||||||
dht: {
|
streamMuxer: [Mplex],
|
||||||
enabled: true,
|
connEncryption: [SECIO],
|
||||||
kBucketSize: 20
|
dht: KadDHT
|
||||||
}
|
},
|
||||||
|
config: {
|
||||||
|
dht: {
|
||||||
|
enabled: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
super(defaultsDeep(_options, defaults))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createNode (callback) {
|
|
||||||
let node
|
|
||||||
|
|
||||||
waterfall([
|
|
||||||
(cb) => PeerInfo.create(cb),
|
|
||||||
(peerInfo, cb) => {
|
|
||||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
|
||||||
node = new MyBundle({
|
|
||||||
peerInfo
|
|
||||||
})
|
|
||||||
node.start(cb)
|
|
||||||
}
|
|
||||||
], (err) => callback(err, node))
|
|
||||||
}
|
|
||||||
|
|
||||||
parallel([
|
|
||||||
(cb) => createNode(cb),
|
|
||||||
(cb) => createNode(cb),
|
|
||||||
(cb) => createNode(cb)
|
|
||||||
], (err, nodes) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
const node1 = nodes[0]
|
|
||||||
const node2 = nodes[1]
|
|
||||||
const node3 = nodes[2]
|
|
||||||
|
|
||||||
parallel([
|
|
||||||
(cb) => node1.dial(node2.peerInfo, cb),
|
|
||||||
(cb) => node2.dial(node3.peerInfo, cb),
|
|
||||||
// Set up of the cons might take time
|
|
||||||
(cb) => setTimeout(cb, 300)
|
|
||||||
], (err) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
|
|
||||||
|
|
||||||
node1.contentRouting.provide(cid, (err) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
console.log('Node %s is providing %s', node1.peerInfo.id.toB58String(), cid.toBaseEncodedString())
|
|
||||||
|
|
||||||
node3.contentRouting.findProviders(cid, 5000, (err, providers) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
console.log('Found provider:', providers[0].id.toB58String())
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
await node.start()
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
;(async () => {
|
||||||
|
const [node1, node2, node3] = await Promise.all([
|
||||||
|
createNode(),
|
||||||
|
createNode(),
|
||||||
|
createNode()
|
||||||
|
])
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
node1.dial(node2.peerInfo),
|
||||||
|
node2.dial(node3.peerInfo)
|
||||||
|
])
|
||||||
|
|
||||||
|
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
|
||||||
|
await node1.contentRouting.provide(cid)
|
||||||
|
|
||||||
|
console.log('Node %s is providing %s', node1.peerInfo.id.toB58String(), cid.toBaseEncodedString())
|
||||||
|
|
||||||
|
// wait for propagation
|
||||||
|
await delay(300)
|
||||||
|
|
||||||
|
const providers = await all(node3.contentRouting.findProviders(cid, { timeout: 3000 }))
|
||||||
|
|
||||||
|
console.log('Found provider:', providers[0].id.toB58String())
|
||||||
|
})();
|
||||||
|
@ -10,31 +10,27 @@ Content Routing is the category of modules that offer a way to find where conten
|
|||||||
|
|
||||||
This example builds on top of the [Protocol and Stream Muxing](../protocol-and-stream-muxing). We need to install `libp2p-kad-dht`, go ahead and `npm install libp2p-kad-dht`. If you want to see the final version, open [1.js](./1.js).
|
This example builds on top of the [Protocol and Stream Muxing](../protocol-and-stream-muxing). We need to install `libp2p-kad-dht`, go ahead and `npm install libp2p-kad-dht`. If you want to see the final version, open [1.js](./1.js).
|
||||||
|
|
||||||
First, let's update our bundle to support Peer Routing and Content Routing.
|
First, let's update our config to support Peer Routing and Content Routing.
|
||||||
|
|
||||||
```JavaScript
|
```JavaScript
|
||||||
class MyBundle extends libp2p {
|
const Libp2p = require('libp2p')
|
||||||
constructor (_options) {
|
const KadDHT = require('libp2p-kad-dht')
|
||||||
const defaults = {
|
|
||||||
modules: {
|
|
||||||
transport: [ TCP ],
|
|
||||||
streamMuxer: [ Mplex ],
|
|
||||||
connEncryption: [ SECIO ],
|
|
||||||
// we add the DHT module that will enable Peer and Content Routing
|
|
||||||
dht: KadDHT
|
|
||||||
},
|
|
||||||
config: {
|
|
||||||
dht: {
|
|
||||||
// dht must be enabled
|
|
||||||
enabled: true,
|
|
||||||
kBucketSize: 20
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
super(defaultsDeep(_options, defaults))
|
const node = await Libp2p.create({
|
||||||
|
modules: {
|
||||||
|
transport: [ TCP ],
|
||||||
|
streamMuxer: [ Mplex ],
|
||||||
|
connEncryption: [ SECIO ],
|
||||||
|
// we add the DHT module that will enable Peer and Content Routing
|
||||||
|
dht: KadDHT
|
||||||
|
},
|
||||||
|
config: {
|
||||||
|
dht: {
|
||||||
|
// dht must be enabled
|
||||||
|
enabled: true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
Once that is done, we can use the createNode function we developed in the previous example to create 3 nodes. Connect node 1 to node 2 and node 2 to node 3. We will use node 2 as a way to find the whereabouts of node 3
|
Once that is done, we can use the createNode function we developed in the previous example to create 3 nodes. Connect node 1 to node 2 and node 2 to node 3. We will use node 2 as a way to find the whereabouts of node 3
|
||||||
@ -44,22 +40,18 @@ const node1 = nodes[0]
|
|||||||
const node2 = nodes[1]
|
const node2 = nodes[1]
|
||||||
const node3 = nodes[2]
|
const node3 = nodes[2]
|
||||||
|
|
||||||
parallel([
|
await Promise.all([
|
||||||
(cb) => node1.dial(node2.peerInfo, cb),
|
node1.dial(node2.peerInfo),
|
||||||
(cb) => node2.dial(node3.peerInfo, cb),
|
node2.dial(node3.peerInfo)
|
||||||
// Set up of the cons might take time
|
])
|
||||||
(cb) => setTimeout(cb, 100)
|
|
||||||
], (err) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
//
|
// Set up of the cons might take time
|
||||||
node1.peerRouting.findPeer(node3.peerInfo.id, (err, peer) => {
|
await delay(100)
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
console.log('Found it, multiaddrs are:')
|
const peer = await node1.peerRouting.findPeer(node3.peerInfo.id)
|
||||||
peer.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
|
||||||
})
|
console.log('Found it, multiaddrs are:')
|
||||||
})
|
peer.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||||
```
|
```
|
||||||
|
|
||||||
You should see the output being something like:
|
You should see the output being something like:
|
||||||
@ -82,17 +74,12 @@ You can find this example completed in [2.js](./2.js), however as you will see i
|
|||||||
Instead of calling `peerRouting.findPeer`, we will use `contentRouting.provide` and `contentRouting.findProviders`.
|
Instead of calling `peerRouting.findPeer`, we will use `contentRouting.provide` and `contentRouting.findProviders`.
|
||||||
|
|
||||||
```JavaScript
|
```JavaScript
|
||||||
node1.contentRouting.provide(cid, (err) => {
|
await node1.contentRouting.provide(cid)
|
||||||
if (err) { throw err }
|
console.log('Node %s is providing %s', node1.peerInfo.id.toB58String(), cid.toBaseEncodedString())
|
||||||
|
|
||||||
console.log('Node %s is providing %s', node1.peerInfo.id.toB58String(), cid.toBaseEncodedString())
|
const provs = await all(node3.contentRouting.findProviders(cid, { timeout: 5000 }))
|
||||||
|
|
||||||
node3.contentRouting.findProviders(cid, 5000, (err, providers) => {
|
console.log('Found provider:', providers[0].id.toB58String())
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
console.log('Found provider:', providers[0].id.toB58String())
|
|
||||||
})
|
|
||||||
})
|
|
||||||
```
|
```
|
||||||
|
|
||||||
The output of your program should look like:
|
The output of your program should look like:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user