I am really trying to wrap my head around the discovery mechanism in Libp2p. I am trying to combine Bootstrap and having DHT random work to dynamically discover and connect to peers. But it seems I am not getting it wrong or it is not just possible.
The code I am using is:
const Libp2p = require("libp2p")
const Tcp = require('libp2p-tcp')
const { NOISE } = require('libp2p-noise')
const MPLEX = require('libp2p-mplex')
const Bootstrap = require('libp2p-bootstrap')
const KadDHT = require('libp2p-kad-dht')
async function main() {
const bootstrapers = const bootstrapers = [
'/dnsaddr/sjc-1.bootstrap.libp2p.io/tcp/4001/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN',
'/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'
]
const node = await Libp2p.create({
modules: {
transport: [Tcp],
connEncryption: [NOISE],
streamMuxer: [MPLEX],
peerDiscovery: [ Bootstrap ],
dht: KadDHT
},
addresses: {
listen: [
'/ip4/0.0.0.0/tcp/7770',
]
},
config: {
peerDiscovery: {
autoDial: true,
bootstrap: {
interval: 60e3,
enabled: true,
list: bootstrapers
}
},
dht: {
enabled: true,
randomWalk: {
enabled: true
}
}
}
})
await node.start()
node.multiaddrs.forEach(addr => {
console.log(`listening on addresses: ${addr.toString()}/p2p/${node.peerId.toB58String()}`)
})
node.on('peer:discovery', (peer) => {
console.log(`Discovered ${peer}`)
})
node.connectionManager.on('peer:connect', (connection) => {
console.log(`Connection established ${connection.remotePeer.toB58String()}`)
})
setInterval(() => {
console.log("size of peers", node.peerStore.peers.size)
}, 5000);
}
main()
When I run this code, I see I connect to the peers listed under bootstrap and nothing more. I was expecting with the DHT random walk condfiured, this will lead to more peers being discovered. But this does not seem to be happening. What could I be doing wrong?