0

我正在努力使这个例子工作。它应该创建两个支持 mdns 的节点。Mdns 应该每秒宣布一次对等点,每个对等点都设置为在找到对等点后打印一条语句。

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

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

或者,我正在尝试使用引导模块从 wan 对等方开始,尽管预计它会慢得多,因此我更喜欢使用 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 环境变量来打印调试语句,尽管我仍然有事件系统的问题。

4

1 回答 1

0
  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

选择要打印的内容DEBUG=*noise ...

  1. 事件

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

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

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

})()
于 2020-10-18T10:57:56.437 回答