使用 mdns 发现对等点的配置

configuration to discover peers using mdns

我正在尝试使这个示例起作用。它应该创建两个具有 mdns 支持的节点。 Mdns 应该每秒宣布一次对等点,每个对等点都设置为在找到对等点后打印一条语句。

当运行它时,控制台输出只是空的。它打印出我关心的一些垃圾错误:(node:51841) ExperimentalWarning: Readable[Symbol.asyncIterator] is an experimental feature. This feature could change at any time

如何启用调试日志以便我可以尝试了解引擎盖下发生的事情? 我想验证 mdns announce 数据包是否已发出,是否可能收到。

或者,我正在尝试使用 bootstrap 模块从 wan peers 开始,尽管预计它会慢得多,因此我更愿意使用 mdns。

我尝试添加各种配置和模块,但没有成功,我不清楚如果我只想在 dht 上发布一些数据,是否需要使用 gossip 模块。诸如此类。

感谢任何帮助。

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())
})()

编辑:发现我可以使用 DEBUG 环境变量来打印调试语句,尽管我仍然遇到事件系统问题。

  1. 日志记录

要启用调试,请配置 DEBUG 环境变量。以 DEBUG=* 开头,它会打印如下日志行:

  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 你想用 DEBUG=*noise ...

打印出来的东西
  1. 事件

OP 代码中的问题是 peer:discovery 事件有一个 PeerID 类型的参数,而不是 Peer。因此,语句 console.log('Discovered:', peer.id.toB58String()) 是不正确的,应替换为 console.log('Discovered:', peer.toB58String())

但是您可能已经注意到 OP 没有提供任何与此相关的错误消息。发生这种情况是因为这些事件处理程序是用错误消音器处理的。 不过,我不确定发生了什么,运行下面的代码不会触发带有“哟”消息的异常。

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

相关源码

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()
  ])

})()