3

In the documentation of DHT protocol for bittorrent,it is given that get_peers method is used for finding nodes with given info_hash.It says that if response contains "values" key,the queried node has returned the information about nodes containing exact info_hash.If node returns "nodes" key,it has returned K nodes closest to the result.Should we recursively call get_peers on returned nodes(closest) in order to reach till exact nodes(with same info_hash)?

the8472
  • 40,999
  • 5
  • 70
  • 122
  • Possible duplicate of [How Kademlia tree of nodes relates to the infohash of a torrent file?](https://stackoverflow.com/questions/29958613/how-kademlia-tree-of-nodes-relates-to-the-infohash-of-a-torrent-file) – the8472 Aug 28 '17 at 22:53
  • Thank you for directing me there @the8472 .So we have to do recursive get_peers operation until we get "values" as key in response? and stop doing it if we get same nodes as return value with "nodes" as key in response?Please guide me with these. – Pratik Shinde Aug 30 '17 at 18:37
  • Have you tried? Have you encountered a particular problem? What is there to "guide", you're just asking for restatements of the same things that have already been formulated in multiple places. – the8472 Aug 30 '17 at 22:16
  • @PratikShinde did you look at my answer? – amirouche Jul 08 '19 at 19:34

1 Answers1

0

Should we recursively call get_peers on returned nodes(closest) in order to reach till exact nodes(with same info_hash)?

Yes and no. You could use a recursive function if you are the LISP kind. That said, a while loop will do the job. Here is some python code that implement the FIND_VALUE algorithm with some comments:

async def _get(self, key):
    """Fetch the value associated with KEY from the network"""
    uid = key
    queried = set()
    while True:
        # retrieve the k nearest peers and remove already queried peers
        peers = nearest(k, self.peers)
        # no more peer to query, the key is not found in the dht
        if not peers:
            raise KeyError(uid)
        # query selected peers
        responses = dict()
        for address in peers:
            response = self._protocol.rpc(address, "value", uid)
            responses[address] = response
        # check responses: look for the value or add peers more near KEY
        for (address, response) in responses.items():
            queried.add(address)
            if isinstance(response, Exception):
                continue
            elif response[0] == b"VALUE":
                # value is found, return it
                value = response[1]
                if hash(value) == unpack(uid):
                    # at last!
                    return value
                else:
                    log.warning(
                        "[%r] bad value returned from %r", self._uid, address
                    )
                    await self.blacklist(address)
                    continue
            elif response[0] == b"PEERS":
                # value not found but we got peers that are more near KEY
                await self._welcome_peers(response[1])

This code is based on qadom's peer.py

amirouche
  • 7,682
  • 6
  • 40
  • 94