Home > Specs > BitTorrent > DHT
Peer Database
The peer database is a list of announced torrent hash IDs with a list of peer IPs that announced them. It serves the same purpose as a conventional tracker.
The database is queried using a
Get Peers message. A peer can be added to the database using an
Announce message.
The database will drop a peer 25 minutes after the announcement. Peers should refresh their announcement every 20 minutes.
Peers may also be dropped if there are too many under a particular hash ID. A reasonable limit is 100. More recently announced peers should be preferred.
To find peers in a specific torrent, a series of
Get Peers and
Find Node messages is used.
Start with 8 nodes from the local table that are closest to the target ID. Send them all a Get Peers message. If there are any peers in the DB, they will be returned in the
values
key, and a follow-up Find Node message should be used to see if there are any closer nodes. If there are not any peers in the DB, the Get Peers reply will contain a
nodes
key with closer nodes.
Any peer address/port pairs received are immediately forwarded to the local torrent object where they can be used for new peer connections.
Any nodes received that are closer than the 8 closest ones we have already queried should themselves also be queried. In this way, we will query most of the same nodes that any other peer in the same torrent is likely to announce to.
After completing a search, select the 8 nodes closest to the target ID that we successfully searched. Every search response will have contained a token that we can use for the announce.
While the search is ongoing, it is also a good idea to announce to some nodes immediately after the search query is responded to, regardless of the closeness of the node's ID to the target ID. Approximately 10%, chosen randomly, of responding nodes should be announced to immediately. This gives the DHT network additional robustness.