In my new weekend project I decided to write an bittorrent client from scratch, no ready to use libraries at all. After two days looking for documentation I'm already about to give up :smile:. I know there are the BEPs, but they are far from enough to understand all the specification. After reading a lot more I think the tracker and peer protocols seems to be old and easy to understand/implement (yes, I know, to write a good code with balance, peer selection, optimizations, this is not easy as I just said, but all I want to is to do the basics to learn, not to compete with tens of good clients out there.)
So, I have decided to start by the DHT which seems to the the more complex part and also the less documented. When you stop looking for bittorrent DHT or mainline DHT and start looking for kademlia DHT you have a lot more information but it not so obvious how to put it all together.
Here is what I understand so far (and there are gaps which I hope to fill in):
- I start with my DHT tree empty
 - use 
find_nodeson my bootstrap node - add the received nodes to my own tree, so I can then select the ones closer to my own ID
 - start issuing 
find_nodesto the selected ones and add their responses to my tree - go back to 3 until I stop receiving unknown/new nodes
 - if I receive an 
announce_peerwith aninfo_hashthan I should save its information on a local DB (the info_hash and ip/port of the sender) - if a node uses 
get_peerswith aninfo_hashI have in my DB then I send the information otherwise I should send a list of closer nodes I have in my own tree (closest to that info_hash) - when I use 
get_peerson other nodes I will receive peers or nodes, in the later case I think the nodes are closer to theinfo_hashand not to my ownnodeIdso, should I add these nodes to my tree or start a new tree based on them? - when I want to announce I am interested on an 
info_hashshould I useannounce_peereverywhere or just to the nodes withnodeIdcloser to the targetinfo_hash? How much is closer enough? 
At this point I have a lot of nodes which IDs are closer to my own ID, and informations about info_hash'es I am not really interested.
I am afraid that I have a giant stupid question: why I did that?
I mean: my selfish reason to do all this work is to locate peers to the info_hash I'm interested in. I understand that the information of one info_hash is likely to be saved on a node which ID is closer to that info_hash. So my chances to find its information is bigger if I create a tree of nodes closer to the info_hash and not closer to my own ID (at this point, if you know the subject, you already noticed how lost I am).
Should I create multiples trees? One for me (to be there to save the information of info_hashes closer to my nodeID people send me), and other tree closer to each one of my target info_hashes so I can retrieve their information?
Should I create a single tree closer to my node ID and hope for the best when querying this tree for the info_hashes I need?
Should I give up since I have completely misunderstood the idea behind DHT at all?
Well, any real documentation, flowcharts, any thing will be welcome!