ChainSafe / discv5

A Typescript implementation of the Discv5 protocol
Apache License 2.0
28 stars 15 forks source link

Add example in readme #236

Open wemeetagain opened 1 year ago

wemeetagain commented 1 year ago

we should have better usage docs for this library

cc @holgerd77

Download commented 9 months ago

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.

wemeetagain commented 9 months ago

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