-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
835a689
commit 7b326cc
Showing
3 changed files
with
110 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,55 @@ | ||
/* eslint-disable no-console */ | ||
'use strict' | ||
|
||
const libp2p = require('../../') | ||
const Libp2p = require('../../') | ||
const TCP = require('libp2p-tcp') | ||
const Mplex = require('libp2p-mplex') | ||
const SECIO = require('libp2p-secio') | ||
const PeerInfo = require('peer-info') | ||
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 { | ||
constructor (_options) { | ||
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: { | ||
enabled: true, | ||
kBucketSize: 20 | ||
} | ||
const delay = require('delay') | ||
|
||
const createNode = async () => { | ||
const peerInfo = await PeerInfo.create() | ||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') | ||
|
||
const node = await Libp2p.create({ | ||
peerInfo, | ||
modules: { | ||
transport: [TCP], | ||
streamMuxer: [Mplex], | ||
connEncryption: [SECIO], | ||
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)) | ||
await node.start() | ||
return node | ||
} | ||
|
||
parallel([ | ||
(cb) => createNode(cb), | ||
(cb) => createNode(cb), | ||
(cb) => createNode(cb) | ||
], (err, nodes) => { | ||
if (err) { throw err } | ||
;(async () => { | ||
const [node1, node2, node3] = await Promise.all([ | ||
createNode(), | ||
createNode(), | ||
createNode() | ||
]) | ||
|
||
const node1 = nodes[0] | ||
const node2 = nodes[1] | ||
const node3 = nodes[2] | ||
await Promise.all([ | ||
node1.dial(node2.peerInfo), | ||
node2.dial(node3.peerInfo) | ||
]) | ||
|
||
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 } | ||
// The DHT routing tables need a moment to populate | ||
await delay(100) | ||
|
||
node1.peerRouting.findPeer(node3.peerInfo.id, (err, peer) => { | ||
if (err) { throw err } | ||
const peer = await node1.peerRouting.findPeer(node3.peerInfo.id) | ||
|
||
console.log('Found it, multiaddrs are:') | ||
peer.multiaddrs.forEach((ma) => console.log(ma.toString())) | ||
}) | ||
}) | ||
}) | ||
console.log('Found it, multiaddrs are:') | ||
peer.multiaddrs.forEach((ma) => console.log(ma.toString())) | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,61 @@ | ||
/* eslint-disable no-console */ | ||
'use strict' | ||
|
||
const libp2p = require('../../') | ||
const Libp2p = require('../../') | ||
const TCP = require('libp2p-tcp') | ||
const Mplex = require('libp2p-mplex') | ||
const SECIO = require('libp2p-secio') | ||
const PeerInfo = require('peer-info') | ||
const CID = require('cids') | ||
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 { | ||
constructor (_options) { | ||
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: { | ||
enabled: true, | ||
kBucketSize: 20 | ||
} | ||
const all = require('it-all') | ||
const delay = require('delay') | ||
|
||
const createNode = async () => { | ||
const peerInfo = await PeerInfo.create() | ||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') | ||
|
||
const node = await Libp2p.create({ | ||
peerInfo, | ||
modules: { | ||
transport: [TCP], | ||
streamMuxer: [Mplex], | ||
connEncryption: [SECIO], | ||
dht: KadDHT | ||
}, | ||
config: { | ||
dht: { | ||
enabled: true | ||
} | ||
} | ||
}) | ||
|
||
super(defaultsDeep(_options, defaults)) | ||
} | ||
await node.start() | ||
return node | ||
} | ||
|
||
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)) | ||
} | ||
;(async () => { | ||
const [node1, node2, node3] = await Promise.all([ | ||
createNode(), | ||
createNode(), | ||
createNode() | ||
]) | ||
|
||
parallel([ | ||
(cb) => createNode(cb), | ||
(cb) => createNode(cb), | ||
(cb) => createNode(cb) | ||
], (err, nodes) => { | ||
if (err) { throw err } | ||
await Promise.all([ | ||
node1.dial(node2.peerInfo), | ||
node2.dial(node3.peerInfo) | ||
]) | ||
|
||
const node1 = nodes[0] | ||
const node2 = nodes[1] | ||
const node3 = nodes[2] | ||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL') | ||
await node1.contentRouting.provide(cid) | ||
|
||
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 } | ||
console.log('Node %s is providing %s', node1.peerInfo.id.toB58String(), cid.toBaseEncodedString()) | ||
|
||
const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL') | ||
// wait for propagation | ||
await delay(300) | ||
|
||
node1.contentRouting.provide(cid, (err) => { | ||
if (err) { throw err } | ||
const providers = await all(node3.contentRouting.findProviders(cid, { timeout: 3000 })) | ||
|
||
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()) | ||
}) | ||
}) | ||
}) | ||
}) | ||
console.log('Found provider:', providers[0].id.toB58String()) | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters