是的,那张图是正确的。这里有一些实现FIND_VALUE
您描述的算法的python代码:
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的摘录。