I am trying to understand lib2p2 Rust by looking at the rust libp2p examples in the rust libp2p github repo and substrate network module as well. My question is, If I am building a peer to peer network such as a blockchain using libp2p.

  1. If I am correct , do I use both kadamlia and mdns for peer discovery ? or I can use only mdns or only kadamlia since both can be used for peer discovery according to the libp2p documentation

  2. If I used kademlia for discovery and routing, does this mean my network will also connect with the ipfs network? I came across information mentioning that ipfs is coupled with the kademlia protocol. how do I use rust libp2p such that the peers in my protocol network (e.g blockhain) only connect with other peers running my own protocol such as a blockchain network? is there a code sample for this ?

  3. What libp2p behavior or module to I use for broadcasting information to other peers in the network ? such as broadcasting blocks as in a blockchain network ? is it gossipsub? I looked into the substrate network crate Cargo.toml but i saw that the gossipsub feature is not enabled

1 Answer 1


Kademlia and mDNS are different kinds of peer discovery mechanisms. For Kademlia you need at least one bootnode that you already know and from whom you can learn about other nodes of the network. mDNS doesn't require a bootnode but peer discovery is limited to local network.

Your blockchain won't connect to IPFS unless you use the same protocol name for you DHT. For Polkadot, we use <genesis hash>/kad as the protocol name which forces Kademlia to only connect to other Polkadot nodes. Example here

We don't use Gossipsub in Polkadot but a custom protocol which allows sending notifications to other peers. This protocol is used to implement block, transaction and other gossip protocols in Polkadot. Here is an example of how it's used in the transaction protocol

  • Thanks for the reply, so for doing peer discovery with Kademlia. I came across the information that I would need to perform a ramdom walk, is there a code sample for the random walk config and running the random walk as well
    – Neo
    Dec 16, 2023 at 18:33
  • I am facing a similar problem. Is it possible to connect my network to IPFS and then query the participating nodes in my network from IPFS using a protocol-id without providing any bootnodes?
    – Discardo
    Jan 17 at 5:16
  • If you want to use Kademlia for peer discovery, you must a priori knowledge of at least one node because you send that node the first FIND_NODE message which will then contain new 20 peers. Is there a particular issue you take with bootnodes? Jan 17 at 6:43
  • Yes, I want to connect my frontend application with the Substrate network (details here), and providing bootnodes might not be helpful in this scenario if those nodes go out of the network.
    – Discardo
    Jan 17 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.