Skip to content

Commit

Permalink
extract min_prefix_colliding_bits to a contanst
Browse files Browse the repository at this point in the history
  • Loading branch information
shyba committed Feb 19, 2022
1 parent 8f11405 commit a6b7817
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lbry/extras/daemon/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ async def stop(self):


class BackgroundDownloaderComponent(Component):
MIN_PREFIX_COLLIDING_BITS = 8
component_name = BACKGROUND_DOWNLOADER_COMPONENT
depends_on = [DATABASE_COMPONENT, BLOB_COMPONENT, DISK_SPACE_COMPONENT]

Expand Down Expand Up @@ -412,11 +413,14 @@ async def download_blobs_in_background(self):
while True:
self.space_available = await self.space_manager.get_free_space_mb(True)
if not self.is_busy and self.space_available > 10:
blob_hash = next((key.hex() for key in self.dht_node.stored_blob_hashes if
key[0] == self.dht_node.protocol.node_id[0]
and key.hex() not in self.blob_manager.completed_blob_hashes), None)
if blob_hash:
self.ongoing_download = asyncio.create_task(self.background_downloader.download_blobs(blob_hash))
node_id_prefix = int.from_bytes(self.dht_node.protocol.node_id[:4], "big")
for hash in self.dht_node.stored_blob_hashes:
colliding_bits = 16 - int(node_id_prefix ^ int.from_bytes(hash[:4], "big")).bit_length()
if hash.hex() in self.blob_manager.completed_blob_hashes:
continue
if colliding_bits >= self.MIN_PREFIX_COLLIDING_BITS:
self.ongoing_download = asyncio.create_task(self.background_downloader.download_blobs(hash))
break
await asyncio.sleep(self.download_loop_delay_seconds)

async def start(self):
Expand Down

0 comments on commit a6b7817

Please sign in to comment.