Skip to content

Commit 4d2b789

Browse files
lookup process
1 parent 1d3f8d2 commit 4d2b789

File tree

1 file changed

+80
-13
lines changed

1 file changed

+80
-13
lines changed

src/routing/kad-dht.md

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ requirements.
2727

2828
## Introduction
2929

30-
FIXME:
30+
`FIXME:`
3131

3232
Distributed Key-Value Store
3333

3434
Goal of DHT is to find the closest peers to some key (in a specific geometry). Once this routing to the closest nodes is possible, nodes can interact with these nodes in various ways, including in asking them to store and serve data.
3535

36+
### DHT Operations
37+
38+
* Peer Routing
39+
* Value storage and retrieval
40+
* Content provider advertisement and dsicovery
41+
3642
### Relation to [libp2p kad-dht](https://github.com/libp2p/specs/tree/master/kad-dht)
3743

3844
The IPFS Kademlia DHT specification is a specialization of the libp2p Kademlia DHT.
@@ -170,9 +176,7 @@ the most stable peers are eventually retained in the Routing Table.
170176

171177
#### IP Diversity Filter
172178

173-
SHOULD implement
174-
175-
FIXME:
179+
`FIXME:` DHT Servers SHOULD implement an [IP Diversity Filter](https://github.com/libp2p/go-libp2p-kbucket/blob/ddb36fa029a18ea0fd5a2b61eeb7235913749615/peerdiversity/filter.go#L45).
176180

177181
### Routing Table Refresh
178182

@@ -198,32 +202,91 @@ information on its closest peers.
198202

199203
## Lookup Process
200204

201-
Iterative vs Recursive
205+
When performing a lookup for a Kademlia Identifier in the DHT, a node begins by
206+
sending requests to known DHT servers whose identifiers are close to the
207+
target. Each response provides information on peers that are even closer to the
208+
target identifier, and the process continues iteratively until the absolute
209+
closest peers are discovered.
210+
211+
### Iterative vs Recursive Lookup
212+
213+
In an iterative lookup, the querying node sends requests to several known DHT
214+
servers. Each server returns a list of peers that are closer to the target
215+
Kademlia Identifier, but does not continue the lookup process. The querying
216+
node then directly contacts these closer peers, repeating the process until the
217+
closest nodes are found.
218+
219+
In a recursive lookup, the querying node delegates the task to a peer that is
220+
closer to the target. That peer then queries its own closer peers on behalf of
221+
the original node, and this delegation continues recursively until the target
222+
is reached.
223+
224+
The IPFS Kademlia DHT uses an iterative lookup approach because recursive
225+
lookups can enable [amplification
226+
attacks](https://en.wikipedia.org/wiki/Denial-of-service_attack#Amplification)
227+
and make error handling more complex.
202228

203229
### Server behavior
204230

205-
In public DHT swarms, DHT Servers MUST never respond with private or loopback multiaddresses.
231+
Upon receiving a lookup request for a Kademlia Identifier, a DHT Server MUST
232+
return the Peer ID and multiaddresses of the `k` closest nodes to the requested
233+
Kademlia Identifier that are stored in its Routing Table. DHT Servers SHOULD
234+
NOT return any information about unresponsive nodes.
235+
236+
In public DHT swarms, DHT Servers MUST filter out private and loopback
237+
multiaddresses, and MUST NOT include peers whose only addresses are private or
238+
loopback.
206239

207-
Should Server tell Client about Server? And about Client?
240+
`FIXME:` Define whether DHT Server should return information about itself and
241+
about requester.
208242

209-
### Concurrency
243+
### Client behavior
210244

211-
Implementation specific. Recommendation is `10`
245+
When a client initiates a lookup for a Kademlia Identifier `kid` (DHT Servers
246+
can initiate lookups as clients), it starts by selecting the closest nodes to
247+
`kid` in XOR distance, and put them in a list/set. Then it sends requests for
248+
`kid` to the closest nodes (see [concurrency](#concurrency)) to `kid` from the
249+
list.
212250

213-
### Lookup termination
251+
Upon receiving a response, the client adds freshly received peers to the list
252+
of closest peers. It sends a request to the closest peer to `kid` that hasn't
253+
been queried yet. The client ignores timeouts and invalid responses.
214254

215-
This is hard
255+
When a client (or a DHT server acting as a client) initiates a lookup for a
256+
Kademlia Identifier `kid`, it begins by selecting the known nodes closest to
257+
`kid` in terms of XOR distance, and adds them to a candidate list. It then
258+
sends lookup requests to the closest nodes from that list.
216259

217-
#### Resiliency
260+
As responses are received, any newly discovered peers are added to the
261+
candidate list. The client proceeds by sending a request to the nearest peer to
262+
`kid` that has not yet been queried. Invalid responses and timeouts are simply
263+
discarded.
218264

219-
Implementation specific. Recommendation is `3`
265+
#### Termination
266+
267+
The lookup process continues until the `k` closest reachable peers to `kid`
268+
have been successfully queried. The process may also be terminated early if the
269+
request-specific success criteria are met. Additionally, if every candidate
270+
peer has been queried without discovering any new ones, the lookup will
271+
terminate.
272+
273+
#### Concurrency
274+
275+
A client MAY have multiple concurrent in-flight queries to distinct nodes for
276+
the same lookup. This behavior is specific to the client and does not affect
277+
how DHT servers operate.
278+
279+
It is recommended that the maximum number of in-flight requests (denoted by
280+
`α`) be set to `10`.
220281

221282
## Peer Routing
222283

223284
DHT Clients that want to be routable must make sure they are in the peerstore of the closest DHT servers to their own PeerID.
224285

225286
When performing a `FIND_NODE` lookup, the client will converge to the closest nodes in XOR distance to the requested PeerID. These nodes are expected to know the multiaddrs of the target peer. The
226287

288+
### `FIND_NODE` Termination
289+
227290
### Routing to non-DHT Servers
228291

229292
### Signed Peer Records
@@ -234,6 +297,10 @@ When performing a `FIND_NODE` lookup, the client will converge to the closest no
234297

235298
sha256
236299

300+
### Lookup Termination and Resiliency
301+
302+
Resiliency: Implementation specific. Recommendation is `3`
303+
237304
### Provider Records
238305

239306
#### Provide Validity

0 commit comments

Comments
 (0)