Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example in readme #236

Open
wemeetagain opened this issue Jan 23, 2023 · 3 comments
Open

Add example in readme #236

wemeetagain opened this issue Jan 23, 2023 · 3 comments
Labels
good first issue Issues that are suitable for first-time contributors. help wanted The author indicates that additional help is wanted. prio-low scope-documentation

Comments

@wemeetagain
Copy link
Member

we should have better usage docs for this library

cc @holgerd77

@Download
Copy link

Download commented Nov 16, 2023

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
Copy link
Member Author

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

@philknows
Copy link
Member

Related #8

@philknows philknows added good first issue Issues that are suitable for first-time contributors. help wanted The author indicates that additional help is wanted. and removed meta-good-first-issue labels Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issues that are suitable for first-time contributors. help wanted The author indicates that additional help is wanted. prio-low scope-documentation
Projects
None yet
Development

No branches or pull requests

3 participants