# Notes on `go-libp2p-discovery` ## How does discovery work? - libp2p has 3 types of `Routing` interface - `ContentRouting` - `Provide` - `FindProviderAsync` - `PeerRouting` - `FindPeer` - `ValueStore` - `PutValue` - `GetValue` - See https://github.com/libp2p/go-libp2p-core/blob/master/routing/routing.go#L71 - `go-libp2p-kad-dht` implements every method in `routing.go` file - See https://github.com/libp2p/go-libp2p-kad-dht/blob/master/routing.go - __The filename corresponds to the interface implementaion.__ - Kademlia can be the instance of any of the routing interface - `Discovery` is a more abstract and higher level interface - `Advitiser` - `Advertise` - `Discoverer` - `FindPeers` - See https://github.com/libp2p/go-libp2p-core/blob/master/discovery/discovery.go#L24 - `RoutingDiscovery` struct implemets `Discovery` interface - it is defined in the package `libp2p-dht-discovery` - `RoutingDiscovery` struct wraps a `ContentRouting` interface - `RoutingDiscovery` defines `Advertise` and `FindPeers` - Inside `Advertise`, `Provide` is called - Inside `FindPeers`, `FindPeerAsync` is called - See https://github.com/libp2p/go-libp2p-discovery/blob/master/routing.go#L22 - How does `Provide` work in Kademlia? - Provider abstraction for indirect stores. - Some DHTs store values directly, while __an indirect store stores pointers to locations of the value__, similarly to Coral and Mainline DHT. - __Provide makes this node announce that it can provide a value for the given key__ - Can providers provide the same cid? - handler `handleAddProvider` - See https://github.com/libp2p/go-libp2p-kad-dht/blob/master/handlers.go#L354 - Kademlia has a `ProviderManager` that manages a provider set for a given key - How does `FindPeersAsync` work in Kademlia? - Check local `Providers` - Query `k` nearest peers to the advertised key for their `Providers` - Store these peers into the local `Peerstore` - How to use package `Discovery`? - See https://github.com/libp2p/go-libp2p-examples/tree/master/chat-with-rendezvous - See https://github.com/libp2p/go-libp2p-examples/tree/master/chat-with-mdns ###### tags: `note` `libp2p`