configuration to discover peers using mdns

612 views Asked by At

I am trying to make this example work. It is supposed to create two nodes with mdns support. Mdns is supposed to announce the peer every second, each peer is setup to print a statement once a peer is found.

When running it, the console output is merely empty. It prints out some garbage error in my concern: (node:51841) ExperimentalWarning: Readable[Symbol.asyncIterator] is an experimental feature. This feature could change at any time

How do i enable debugging log so that i can try to understand what is going on under the hood ? I would like to verify the mdns announce packets are issued, and possibly received, or not.

Alternatively, i am trying to use the bootstrap module to begin over wan peers, though expect it to be much slower, thus i would prefer to use mdns.

I tried to add various configuration and modules without much success, it is not clear to me if i am required to use the gossip module if i only want to announce some data on the dht. stuff like that.

any help is appreciated.

const Libp2p = require('libp2p')
const MulticastDNS = require('libp2p-mdns')
const KadDHT = require('libp2p-kad-dht')
const Bootstrap = require('libp2p-bootstrap')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')
const GossipSub = require('libp2p-gossipsub')
const { FaultTolerance } = require('libp2p/src/transport-manager')

const CID = require('cids')
const all = require('it-all')
const delay = require('delay')

const bootstrapers = [
  '/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
  '/ip4/104.236.176.52/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
  '/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
  '/ip4/162.243.248.213/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
  '/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
  '/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
  '/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
  '/ip4/178.62.61.185/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
  '/ip4/104.236.151.122/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
]

const createNode = () => {
  return Libp2p.create({
    addresses: {
      listen: ['/ip4/0.0.0.0/tcp/0']
    },
    modules: {
      transport: [ TCP ],
      streamMuxer: [ Mplex ],
      connEncryption: [ NOISE ],
      // peerDiscovery: [ MulticastDNS ],
      // peerDiscovery: [ MulticastDNS ],
      peerDiscovery: [ Bootstrap, MulticastDNS ],
      dht: KadDHT,
      pubsub: GossipSub
    },
    transportManager: {
      faultTolerance: FaultTolerance.NO_FATAL
    },
    config: {
      peerDiscovery: {
        autoDial: true,
        [MulticastDNS.tag]: {
          broadcast: true,
          interval: 1000,
          enabled: true
        },
        [Bootstrap.tag]: {
          interval: 1000,
          enabled: true,
          list: bootstrapers
        },
        [GossipSub.tag]: {
          enabled: true,
          emitSelf: true,
          signMessages: true,
          strictSigning: true
        },
        mdns: {
          broadcast: true,
          interval: 1000,
          enabled: true
        },
        bootstrap: {
          interval: 1000,
          enabled: true,
          list: bootstrapers
        },
        pubsub: {
          enabled: true,
          emitSelf: true,
          signMessages: true,
          strictSigning: true
        },
      },
      dht: {
        enabled: true
      }
    }
  })
}

( async () => {

  const [node1, node2] = await Promise.all([
    createNode(),
    createNode()
  ])

  node1.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))
  node2.on('peer:discovery', (peer) => console.log('Discovered:', peer.id.toB58String()))

  await Promise.all([
    node1.start(),
    node2.start()
  ])

  // const cid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
  // await node1.contentRouting.provide(cid)
  // await delay(3000)
  // console.log("looking for providers...")
  // const providers = await all(node2.contentRouting.findProviders(cid, { timeout: 5000 }))
  // console.log('Found provider:', providers[0].id.toB58String())
})()

edit: found out i could use DEBUG environment variable to print debug statements, thouhg i still have problems with event system.

1

There are 1 answers

0
AudioBubble On BEST ANSWER
  1. Logging

To enable debugging, configure the DEBUG environment variable. Start with DEBUG=*, it prints log lines like:

  mss:select select: read "/noise" +1ms
  libp2p:upgrader encrypting outbound connection to {"id":"QmRiNVP5NSGJPHLo256vNMTYW9VzsTKYm4dRG3GoJj37ah"} +12ms
  libp2p:noise Stage 0 - Initiator starting to send first message. +14ms

Select stuff you want to print out using DEBUG=*noise ...

  1. events

The probleme in OP code is that the peer:discovery event has an argument of type PeerID, not Peer. Thus the statements console.log('Discovered:', peer.id.toB58String()) is incorrect and should be replaced with console.log('Discovered:', peer.toB58String())

But one may have noticed the OP did not provide any error messages related to that. That happens because those event handlers are handled with an error silencer. I am unsure what is going on, though, runnning below code will not trigger the exception with the "yo" message.

  node1.on('peer:discovery', (peerID) => {
    console.log(node1.peerId.toB58String(), "discovered:", peerID.toB58String())
    throw "yo"
  })

Related source code

const Libp2p = require('libp2p')
const MulticastDNS = require('libp2p-mdns')
const KadDHT = require('libp2p-kad-dht')
const Bootstrap = require('libp2p-bootstrap')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const { NOISE } = require('libp2p-noise')

const CID = require('cids')
const all = require('it-all')
const delay = require('delay')

const bootstrapers = [
  '/ip4/104.131.131.82/tcp/4001/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ',
  '/ip4/104.236.176.52/tcp/4001/p2p/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z',
  '/ip4/104.236.179.241/tcp/4001/p2p/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
  '/ip4/162.243.248.213/tcp/4001/p2p/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm',
  '/ip4/128.199.219.111/tcp/4001/p2p/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu',
  '/ip4/104.236.76.40/tcp/4001/p2p/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64',
  '/ip4/178.62.158.247/tcp/4001/p2p/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
  '/ip4/178.62.61.185/tcp/4001/p2p/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
  '/ip4/104.236.151.122/tcp/4001/p2p/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx'
]

const createNode = () => {
  return Libp2p.create({
    addresses: {
      listen: ['/ip4/0.0.0.0/tcp/0']
    },
    modules: {
      transport: [ TCP ],
      streamMuxer: [ Mplex ],
      connEncryption: [ NOISE ],
      // peerDiscovery: [ MulticastDNS ],
      peerDiscovery: [ MulticastDNS ],
      // peerDiscovery: [ Bootstrap, MulticastDNS ],
      dht: KadDHT,
      // pubsub: GossipSub
    },
    // transportManager: {
    //   faultTolerance: FaultTolerance.NO_FATAL
    // },
    config: {
      peerDiscovery: {
        autoDial: true,
        [MulticastDNS.tag]: {
          broadcast: true,
          interval: 1000,
          enabled: true
        },
        [Bootstrap.tag]: {
          interval: 1000,
          enabled: true,
          list: bootstrapers
        },
      },
      dht: {
        enabled: true
      }
    }
  })
}

( async () => {

  const [node1, node2] = await Promise.all([
    createNode(),
    createNode()
  ])

  node1.on('peer:discovery', (peerID) => {
    console.log(node1.peerId.toB58String(), "discovered:", peerID.toB58String())
  })
  node2.on('peer:discovery', (peerID) => {
    console.log(node2.peerId.toB58String(), "discovered:", peerID.toB58String())
  })

  node1.on('error', console.error)
  node2.on('error', console.error)

  await Promise.all([
    node1.start(),
    node2.start()
  ])

})()