Js-ipfs: dht functionality

Created on 31 Jan 2019  路  5Comments  路  Source: ipfs/js-ipfs

  • Version:
    feat/dht-part-ii branch (b0acc71447e874a987408cfedf6ecec089519725)
  • Platform:
    mac/linux
  • Subsystem:
    dht

I just used browserify directly on a little example while having the feat/dhs-part-ii branch checked out.

So now my index.js looks like

'use strict'

const IPFS = require('ipfs')
const Protector = require('libp2p-pnet')

const swarmKey = Buffer.from("/key/swarm/psk/1.0.0/\n/base16/\nTHEKEY")

const node = new IPFS({
    repo: String(Math.random() + Date.now()),
    relay: {
        enabled: true,
        hop: {enabled: true}
    }, 
    preload: {enabled: false},
    libp2p2: {
        modules: {
            connProtector: new Protector(swarmKey)
        }
    },
    config: {
        Bootstrap: ["/ip4/IP_TO_A_GOLANG_NODE/tcp/9443/ws/ipfs/THE_PEER_ID"]
    }
})

node.once('ready', function() {
    console.log('IPFS node is ready')

    window.node = node

    node.dht.findPeer('THE_PEER_ID')
})

However I always get Error: Peer lookup failed from that last line.
Also even trying to do a node.get('THE_PEER_FILE_HASH') just hangs, even though that peers is in the node.swarm.peers() (only one since its the bootstrap).
If I take out the swarm key and connect to public network, the findPeer doesn't fail but always returns nothing, same with findProvs etc.

P1 dihard kinbug

Most helpful comment

Hello @gaillard

I did a small test:

  const node = new IPFS({
      repo: String(Math.random() + Date.now()),
      libp2p: {
        config: {
          dht: {
            enabled: true
          }
        }
      }
    })

    node.once('ready', async () => {
      let peers
      peers = await node.swarm.peers() // empty peers (still connecting)

      setTimeout(async () => {
        peers = await node.swarm.peers() // several peers (connected now)

        if (peers.length) {
          const b58peerId = peers[0].peer._idB58String
          const peerId = await node.dht.findPeer(b58peerId)

          console.log('peerId', peerId)
        }
      }, 5000)
    })

Basically, when the node is ready, it starts connecting to the peers. That way, if you immediatly try to find the peer and you are not connected with it yet, you will not be able to find it.

In my example, just a small test with a timeout, I wait a few seconds and then try to find a peer that is connected, and it worked fine in a React app.

Let me know if this answer helped you. If not, please provide me an example failing ready to run.

All 5 comments

Is the peer you鈥檙e trying to get the file from listening on an address that your browser node can connect to?

@alanshaw I'm already connected as shown by node.swarm.peers().

@vasco-santos can we resolve this before https://github.com/ipfs/js-ipfs/pull/856 is merged?

I will have a look in this

Hello @gaillard

I did a small test:

  const node = new IPFS({
      repo: String(Math.random() + Date.now()),
      libp2p: {
        config: {
          dht: {
            enabled: true
          }
        }
      }
    })

    node.once('ready', async () => {
      let peers
      peers = await node.swarm.peers() // empty peers (still connecting)

      setTimeout(async () => {
        peers = await node.swarm.peers() // several peers (connected now)

        if (peers.length) {
          const b58peerId = peers[0].peer._idB58String
          const peerId = await node.dht.findPeer(b58peerId)

          console.log('peerId', peerId)
        }
      }, 5000)
    })

Basically, when the node is ready, it starts connecting to the peers. That way, if you immediatly try to find the peer and you are not connected with it yet, you will not be able to find it.

In my example, just a small test with a timeout, I wait a few seconds and then try to find a peer that is connected, and it worked fine in a React app.

Let me know if this answer helped you. If not, please provide me an example failing ready to run.

Was this page helpful?
0 / 5 - 0 ratings