0

js-libp2p 的新手,并试图通过四处探索来掌握它。我目前正在尝试了解对等发现的工作原理。

我正在运行以下代码:

'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()}`)
    })
})();

它同时使用 Bootstrap 和 KadDHT。

当我从终端运行它时,我看到它连接到引导节点,然后继续发现更多对等点并与它们连接。例如,控制台如下所示:

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

当我从同一台机器上的另一个终端运行脚本时,我也会得到类似的输出。但问题是,即使这两个节点正在连接到其他对等节点,它们也永远无法相互连接。

这是怎么回事?我需要做什么才能让他们发现彼此并建立联系?

我想最终我的问题是,我需要使用什么对等发现机制来让不在同一网络上的两个节点找到并相互连接?

我能够使用 mdns 让同一个 LAN 上的对等点找到并相互连接,但是对于假定对等点不在同一个 LAN 上的情况,我没有成功。

建议将不胜感激。

4

1 回答 1

0

在手动使用拨号命令拨号到该连接之前,您可以获取目标计算机的对等 ID 并使用 findPeer 找到相应的地址。 https://github.com/libp2p/js-libp2p/blob/master/doc/API.md#peerroutingfindpeer

请记住,如果您在对称 NAT 后面,您还需要在 libp2p 配置中启用自动中继:

https://en.m.wikipedia.org/wiki/NAT_traversal

于 2021-12-28T23:52:09.073 回答