New to js-libp2p and trying to get a hang of it by poking around. I am currently trying to understand how peer discovery works.
I have the following code that I am running:
'use strict'
// bootstrap with DHT
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const { NOISE } = require('@chainsafe/libp2p-noise')
const Bootstrap = require('libp2p-bootstrap')
const KadDHT = require('libp2p-kad-dht')
const bootstrapers = [
'/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa',
'/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt'
]
;(async () => {
const node = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [NOISE],
peerDiscovery: [Bootstrap],
dht:KadDHT
},
config: {
dht: {
enabled: true,
randomWalk: {
enabled: true
}
},
peerDiscovery: {
bootstrap: {
interval: 60e3,
enabled: true,
list: bootstrapers
}
}
}
})
node.connectionManager.on('peer:connect', (connection) => {
console.log('Connection established to:', connection.remotePeer.toB58String()) // Emitted when a peer has been found
})
node.on('peer:discovery', (peerId) => {
// No need to dial, autoDial is on
console.log('Discovered:', peerId.toB58String())
})
await node.start()
node.multiaddrs.forEach(addr => {
console.log(`listening on addresses: ${addr.toString()}/p2p/${node.peerId.toB58String()}`)
})
})();
Which uses both Bootstrap
and KadDHT
.
When I run it from a terminal, I see it connects to the bootstrap node, and then continues to discover more peers and connect with them. For example, the console looks like this:
node src/2.js
Discovered: QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
Discovered: QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN
Discovered: QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb
Discovered: QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp
Discovered: QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa
Discovered: QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt
listening on addresses: /ip4/127.0.0.1/tcp/61082/p2p/QmddCd9ZxKgBH5rFdL54gNGybq2KAF6CfQ1aTTUrrY9Pbq
listening on addresses: /ip4/192.168.178.73/tcp/61082/p2p/QmddCd9ZxKgBH5rFdL54gNGybq2KAF6CfQ1aTTUrrY9Pbq
Connection established to: QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb
Connection established to: QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
Connection established to: QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa
Connection established to: QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp
Connection established to: QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN
Connection established to: QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt
-- end of discovering and connecting to the bootstrap peers ---
Discovered: QmYBEXLhD7CDEBx4uW1QBTZRgUS3xnntgCigv9ahjhehXZ
Discovered: QmWS845wqn5M59NsL3ozin7Rm25npcATfhGdko9YVxMi9R
Discovered: QmNpNwPFPbS2MTgPDMtVFdXMxPPYFTyVrrpANZnCKGUBjX
Discovered: 12D3KooWSV3XfUPDr2XTvdzyGe8PtY9uF3K9cZgm7gPLPv1QbGaQ
Discovered: QmRXzQWs9NwSwkmAMA6qqo2arVDduPQbs8Q6PxsW9SrPTH
Discovered: 12D3KooWGx5pFFG7W2EG8N6FFwRLh34nHcCLMzoBSMSSpHcJYN7G
Discovered: 12D3KooWSV63NK2vo5cvEmT4R6AkgMoVKxQtQ4V2oVk3BnK1F9YX
Discovered: QmRjLSisUCHVpFa5ELVvX3qVPfdxajxWJEHs9kN3EcxAW6
Discovered: QmcUQy2PRq6USbB164s9GCsgNYPxwJPE7KjfVffgbHvM1P
Discovered: QmNrW6qRFC2tExR7o33XSakfuZXuewv3A77FYEAUCWsSbS
Discovered: QmVaNqiRtJ8A1VB9fmZNr2E2HSEtYd9RE5RqbKmMtANxj6
Discovered: 12D3KooWPDBkwVUwrfqDFfGsv8hwMJo4yTikvAa9wcjLrVLQKZDY
Discovered: 12D3KooWNGdEk6m4U12SfzfKeqMAfV96UyZSoXSkxAMp6ySqQBPL
Discovered: 12D3KooWKs1v1BsX9aqg5BEGcxtPb1r9aUcjrAzTzhwVBdgrUKLx
Discovered: 12D3KooWLAfK874fo1izzh1crxewdZAcbNKXm4Vp6L9mFf1iQxk3
Discovered: 12D3KooWDLYiAdzUdM7iJHhWu5KjmCN62aWd7brQEQGRWbv8QcVb
Discovered: 12D3KooWQrTh45PUpGhRsDPWjLndAXgDLUQv1riSMgo6QgF34Nfy
Discovered: QmSaXJRZVyKJNE1tN6hjFtKWSkmfzWWCEzbbnWmu6LJM7h
Discovered: QmbEjWRSFtwRDzno8iSjZe2ntAM72ujcrhtRZvpNRffVxh
Discovered: 12D3KooWAXdGM897vFF7VdBTDWqpNEMZt68ftYG6izCoCZiJqZDE
Connection established to: 12D3KooWNGdEk6m4U12SfzfKeqMAfV96UyZSoXSkxAMp6ySqQBPL
Connection established to: QmNpNwPFPbS2MTgPDMtVFdXMxPPYFTyVrrpANZnCKGUBjX
Connection established to: QmWS845wqn5M59NsL3ozin7Rm25npcATfhGdko9YVxMi9R
Connection established to: 12D3KooWSV3XfUPDr2XTvdzyGe8PtY9uF3K9cZgm7gPLPv1QbGaQ
Connection established to: 12D3KooWLAfK874fo1izzh1crxewdZAcbNKXm4Vp6L9mFf1iQxk3
Connection established to: 12D3KooWKs1v1BsX9aqg5BEGcxtPb1r9aUcjrAzTzhwVBdgrUKLx
When I run the script from another terminal on the same machine, I also get similar output. The problem though is, even these two nodes are connecting to other peers, they never get to connect to each other.
How is this so? What do I need to do to make them discover each other and connect?
I guess ultimately my question is, what are the peer discovery mechanism I need to use to have two nodes that are not on the same network find and connect to each other?
I was able to use mdns to have peers on the same LAN find and connect to each other, but I have not being succuessful with scenatiors where the peers are not assumed to be on the same LAN.
Suggestions will be appreciated.