forked from LBRYCommunity/lbry-sdk
replace duplicated code
This commit is contained in:
parent
0146044b90
commit
838fd71538
2 changed files with 7 additions and 9 deletions
|
@ -6,6 +6,7 @@ import itertools
|
||||||
|
|
||||||
from prometheus_client import Gauge
|
from prometheus_client import Gauge
|
||||||
|
|
||||||
|
from lbry import utils
|
||||||
from lbry.dht import constants
|
from lbry.dht import constants
|
||||||
from lbry.dht.protocol.distance import Distance
|
from lbry.dht.protocol.distance import Distance
|
||||||
if typing.TYPE_CHECKING:
|
if typing.TYPE_CHECKING:
|
||||||
|
@ -70,9 +71,8 @@ class KBucket:
|
||||||
if len(self.peers) < constants.K:
|
if len(self.peers) < constants.K:
|
||||||
self.peers.append(peer)
|
self.peers.append(peer)
|
||||||
self.peer_in_routing_table_metric.labels("global").inc()
|
self.peer_in_routing_table_metric.labels("global").inc()
|
||||||
if peer.node_id[0] == self._node_id[0]:
|
bits_colliding = utils.get_colliding_prefix_bits(peer.node_id, self._node_id, 32)
|
||||||
bits_colliding = 8 - (peer.node_id[1] ^ self._node_id[1]).bit_length()
|
self.peer_with_x_bit_colliding_metric.labels(amount=bits_colliding).inc()
|
||||||
self.peer_with_x_bit_colliding_metric.labels(amount=(bits_colliding + 8)).inc()
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
@ -140,9 +140,8 @@ class KBucket:
|
||||||
def remove_peer(self, peer: 'KademliaPeer') -> None:
|
def remove_peer(self, peer: 'KademliaPeer') -> None:
|
||||||
self.peers.remove(peer)
|
self.peers.remove(peer)
|
||||||
self.peer_in_routing_table_metric.labels("global").dec()
|
self.peer_in_routing_table_metric.labels("global").dec()
|
||||||
if peer.node_id[0] == self._node_id[0]:
|
bits_colliding = utils.get_colliding_prefix_bits(peer.node_id, self._node_id, 32)
|
||||||
bits_colliding = 8 - (peer.node_id[1] ^ self._node_id[1]).bit_length()
|
self.peer_with_x_bit_colliding_metric.labels(amount=bits_colliding).dec()
|
||||||
self.peer_with_x_bit_colliding_metric.labels(amount=(bits_colliding + 8)).dec()
|
|
||||||
|
|
||||||
def key_in_range(self, key: bytes) -> bool:
|
def key_in_range(self, key: bytes) -> bool:
|
||||||
""" Tests whether the specified key (i.e. node ID) is in the range
|
""" Tests whether the specified key (i.e. node ID) is in the range
|
||||||
|
|
|
@ -417,12 +417,11 @@ class BackgroundDownloaderComponent(Component):
|
||||||
await asyncio.sleep(self.download_loop_delay_seconds)
|
await asyncio.sleep(self.download_loop_delay_seconds)
|
||||||
|
|
||||||
def _download_next_close_blob_hash(self):
|
def _download_next_close_blob_hash(self):
|
||||||
node_id_prefix = int.from_bytes(self.dht_node.protocol.node_id[:4], "big")
|
node_id = self.dht_node.protocol.node_id
|
||||||
for blob_hash in self.dht_node.stored_blob_hashes:
|
for blob_hash in self.dht_node.stored_blob_hashes:
|
||||||
colliding_bits = 32 - int(node_id_prefix ^ int.from_bytes(blob_hash[:4], "big")).bit_length()
|
|
||||||
if blob_hash.hex() in self.blob_manager.completed_blob_hashes:
|
if blob_hash.hex() in self.blob_manager.completed_blob_hashes:
|
||||||
continue
|
continue
|
||||||
if colliding_bits >= self.MIN_PREFIX_COLLIDING_BITS:
|
if utils.get_colliding_prefix_bits(node_id, blob_hash, 32) >= self.MIN_PREFIX_COLLIDING_BITS:
|
||||||
self.ongoing_download = asyncio.create_task(self.background_downloader.download_blobs(blob_hash.hex()))
|
self.ongoing_download = asyncio.create_task(self.background_downloader.download_blobs(blob_hash.hex()))
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
|
|
Loading…
Reference in a new issue