Да, эта диаграмма верна.Вот код Python, который реализует алгоритм FIND_VALUE
, который вы описываете:
async def _get(self, key):
"""Fetch the value associated with KEY from the network"""
uid = pack(key)
queried = set()
while True:
# retrieve the k nearest peers and remove already queried peers
peers = await self.peers((None, None), uid)
peers = [address for address in peers if address not in queried]
# no more peer to query, the key is not found in the dht
if not peers:
raise KeyError(unpack(uid))
# query selected peers
queries = dict()
for address in peers:
query = self._protocol.rpc(address, "value", uid)
queries[address] = query
responses = await gather(queries, return_exceptions=True)
for (address, response) in responses.items():
queried.add(address)
if isinstance(response, Exception):
continue
elif response[0] == b"VALUE":
value = response[1]
if hash(value) == unpack(uid):
# store it
@h.transactional
def add(tr, key, value):
tr.add("QADOM:MAPPING", key, "value", value)
await self._run(add, self._hoply, key, value)
# 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":
await self._welcome_peers(response[1])
else:
await self.blacklist(address)
log.warning(
"[%r] unknown response %r from %r",
self._uid,
response[0],
address,
Это выдержка из проекта qadom peer.py .