gpt4 book ai didi

p2p - 如何在 Bittorrent DHT 中找到具有精确 info_hash 的节点?

转载 作者:行者123 更新时间:2023-12-02 04:33:48 25 4
gpt4 key购买 nike

在bittorrent的DHT协议(protocol)的文档中,给出了get_peers方法用于查找具有给定info_hash的节点。它说如果响应包含“values”键,则查询的节点已返回有关包含准确info_hash的节点的信息.如果节点返回“nodes”键,它已经返回了最接近结果的 K 个节点。我们是否应该在返回的节点(最近的)上递归调用 get_peers 以到达精确的节点(具有相同的 info_hash)?

最佳答案

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

是也不是。如果你是 LISP 那种人,你可以使用递归函数。也就是说,一个 while 循环就可以完成这项工作。下面是一些实现 FIND_VALUE 算法的 python 代码,带有一些注释:

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])

此代码基于qadom's peer.py

关于p2p - 如何在 Bittorrent DHT 中找到具有精确 info_hash 的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45925560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com