discv5 icon indicating copy to clipboard operation
discv5 copied to clipboard

Add example in readme

Open wemeetagain opened this issue 2 years ago • 3 comments

we should have better usage docs for this library

cc @holgerd77

wemeetagain avatar Jan 23 '23 10:01 wemeetagain

I do see an example: https://github.com/ChainSafe/discv5#example

import { Discv5Discovery, ENR } from "@chainsafe/discv5";
import Libp2p from "libp2p";
import PeerId from "peer-id";

const myPeerId: PeerId = ...;

const bootstrapEnrs: ENR[] = [...];

const libp2p = new Libp2p({
  peerId: myPeerId,
  modules: {
    peerDiscovery: [Discv5Discovery],
  },
  config: {
    discv5: {
      enr: ENR.createFromPeerId(myPeerInfo.id),
      bindAddr: "/ip4/0.0.0.0/udp/9000",
      bootstrapEnrs: bootstrapEnrs,
      searchInterval: 30000, // wait 30s between searches
    },
  },
});

However, if we compare with an example from the Bootstrap discovery mechanism from the libp2p website itself

import { createLibp2p } from 'libp2p'
import { bootstrap } from '@libp2p/bootstrap'
import { tcp } from 'libp2p/tcp'
import { noise } from '@libp2p/noise'
import { mplex } from '@libp2p/mplex'

let options = {
  transports: [
    tcp()
  ],
  streamMuxers: [
    mplex()
  ],
  connectionEncryption: [
    noise()
  ],
  peerDiscovery: [
    bootstrap({
      list: [ // a list of bootstrap peer multiaddrs to connect to on node startup
        "/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
        "/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
        "/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa"
      ],
      timeout: 1000, // in ms,
      tagName: 'bootstrap',
      tagValue: 50,
      tagTTL: 120000 // in ms
    })
  ]
}

const libp2p = await createLibp2p(options)

libp2p.on('peer:discovery', function (peerId) {
  console.log('found peer: ', peerId.toB58String())
})

These examples seem radically different. The libp2p example uses await createLibp2p(..) where the example for Discv5 uses new Libp2p(..). Also the options objects that are passed look very different, with the example from this repo having a modules key that I don't see in the example from libp2p.

Could it be that the current example is for an older version of libp2p?

Nowadays I tend to make tests in my project containing the example code that I use in the docs, so that when something changes the test breaks and I get a reminder that de example code in the docs needs to be updated.

Download avatar Nov 16 '23 11:11 Download

Could it be that the current example is for an older version of libp2p?

Yeah, thats what happened

Nowadays I tend to make tests in my project containing the example code that I use in the docs, so that when something changes the test breaks and I get a reminder that de example code in the docs needs to be updated.

Good idea, we have a much better example that's an e2e test, better to use that

wemeetagain avatar Nov 16 '23 13:11 wemeetagain