2018-10-19 17:37:34 +02:00
|
|
|
/* eslint-disable no-console */
|
2017-07-20 14:19:36 -07:00
|
|
|
'use strict'
|
|
|
|
|
2019-12-18 02:37:36 +00:00
|
|
|
const Libp2p = require('../../')
|
2017-07-20 14:19:36 -07:00
|
|
|
const TCP = require('libp2p-tcp')
|
2018-02-19 09:46:11 +00:00
|
|
|
const Mplex = require('libp2p-mplex')
|
2017-07-20 14:19:36 -07:00
|
|
|
const SECIO = require('libp2p-secio')
|
|
|
|
const PeerInfo = require('peer-info')
|
|
|
|
const KadDHT = require('libp2p-kad-dht')
|
|
|
|
|
2019-12-18 02:37:36 +00:00
|
|
|
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
|
2018-06-28 10:06:25 +02:00
|
|
|
}
|
2017-07-20 14:19:36 -07:00
|
|
|
}
|
2019-12-18 02:37:36 +00:00
|
|
|
})
|
2018-06-28 10:06:25 +02:00
|
|
|
|
2019-12-18 02:37:36 +00:00
|
|
|
await node.start()
|
|
|
|
return node
|
2017-07-20 14:19:36 -07:00
|
|
|
}
|
|
|
|
|
2019-12-18 02:37:36 +00:00
|
|
|
;(async () => {
|
|
|
|
const [node1, node2, node3] = await Promise.all([
|
|
|
|
createNode(),
|
|
|
|
createNode(),
|
|
|
|
createNode()
|
|
|
|
])
|
2017-07-20 14:19:36 -07:00
|
|
|
|
2019-12-18 02:37:36 +00:00
|
|
|
await Promise.all([
|
|
|
|
node1.dial(node2.peerInfo),
|
|
|
|
node2.dial(node3.peerInfo)
|
|
|
|
])
|
2017-07-20 14:19:36 -07:00
|
|
|
|
2019-12-18 02:37:36 +00:00
|
|
|
// The DHT routing tables need a moment to populate
|
|
|
|
await delay(100)
|
2017-07-20 14:19:36 -07:00
|
|
|
|
2019-12-18 02:37:36 +00:00
|
|
|
const peer = await node1.peerRouting.findPeer(node3.peerInfo.id)
|
2017-07-20 14:19:36 -07:00
|
|
|
|
2019-12-18 02:37:36 +00:00
|
|
|
console.log('Found it, multiaddrs are:')
|
|
|
|
peer.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
|
|
|
})();
|