How to log peer messages in IPFS / libp2p in the browser?

639 views Asked by At

I did a test to see what would happen if I try getting a CID that doesn't exist, to see if I could continuously ask every peer in IPFS. It doesn't seem to work. I "connect" to around 10 peers, then it gives up. I'm not sure I'm actually "connecting" though. I'm not really sure what going on. I don't know that I'm actually connecting to any peer and asking them for a CID. I seem to connect to a star server, and see a list a peer, but not sure I ever connect to any of those peers.

Are there other events I should be listening to to debug what's going on? How can I listen to the messages I'm sending to peers?

My assumption from how Bittorrent DHT works is that, if a CID doesn't exist, I should eventually be asking every single peer on the network if they have this CID. Is there only 10 peers in the entire network? Or only 10 peers on that star server? Is there no star server discovery? How do I find a "busy" star server?

<script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script>

<script>
    (async () => {
        window.node = await Ipfs.create()
        window.node.libp2p.on('peer:discovery', (peer) => console.log('peer:discovery', peer))
        window.node.libp2p.on('peer:connect', peerInfo => console.log('peer:connect', peerInfo))
        window.node.libp2p.on('peer:disconnect', peerInfo => console.log('peer:disconnect', peerInfo))
        window.node.libp2p.peerStore.on('peer', (peerId) => console.log('peer', peerId))
        window.node.libp2p.peerStore.on('change:multiaddrs', ({ peerId, multiaddrs}) => {
            const addresses = []
            for (const multiaddr of multiaddrs) {
                addresses.push(multiaddr.buffer.toString())
            }
            console.log('change:multiaddrs', {peerId, multiaddrs, addresses})
        })
        window.node.libp2p.peerStore.on('change:protocols', ({ peerId, protocols}) => console.log('change:protocols', {peerId, protocols}))
        window.node.libp2p.on('error', (err) => console.log('error', err))
        window.node.libp2p.connectionManager.on('peer:connect', (connection) => {
            console.log('connectionManager:peer:connect', {connection, remoteAddr: connection.remoteAddr.buffer.toString()})
        })
        window.node.libp2p.connectionManager.on('peer:disconnect', (connection) => console.log('connectionManager:peer:disconnect', connection))

        // fake CID does not exist
        results = window.node.get("QmZbj5ruYneZb8FuR9wnLuJCpCXMQudhSdWhdhp5U1oPWJ")
        for await (res of results) {
          for await (content of res.content) {
             window.content = content
              console.log(content.toString())
            }
        }
    })()
</script>
1

There are 1 answers

0
vasco.santos On

js-ipfs does not have the DHT enabled by default at this moment. You need to explicitly enable it through the create options. That would result in the IPFS bitswap protocol to query the network for the content you are looking for. During the DHT query, as you also described, the peer will connect to other peers on the network according to the DHT algorithm, in order to find the content.

So, why do we have the DHT disabled by default? The JS DHT implementation was not scaling well for several reasons, some related to the DHT implementation itself and others like undiable nodes in the network and a not efficient connection manager. GO DHT was really improved a few months ago and the JS implementation will be completely refactored according to the recent changes in the GO implementation. The connection manager improvements are also on the way.

While the DHT is not refactored and stable, it is recommended to leverage delegate nodes. A delegate node is a node that will make content and peer routing queries on behalf of others. [email protected] was released with some public delegated nodes configured by default. The delegate nodes are GO nodes that will do the DHT queries on behalf of the JS nodes, this way, it will be the go node behind the scenes that will establish the connections with other nodes.

Let me know if I could answer your issue