Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Fixed most ruff violations in the libtorrent folder #26

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/tribler/core/libtorrent/download_manager/dht_health_manager.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from binascii import hexlify
from __future__ import annotations

import libtorrent as lt
import math
from asyncio import Future
from binascii import hexlify
from typing import Awaitable

import libtorrent as lt
from ipv8.taskmanager import TaskManager

from tribler.core.torrent_checker.dataclasses import HealthInfo
Expand All @@ -15,9 +16,10 @@ class DHTHealthManager(TaskManager):
This class manages BEP33 health requests to the libtorrent DHT.
"""

def __init__(self, lt_session):
def __init__(self, lt_session: lt.session) -> None:
"""
Initialize the DHT health manager.
:param lt_session: The session used to perform health lookups.
"""
TaskManager.__init__(self)
Expand All @@ -27,9 +29,10 @@ def __init__(self, lt_session):
self.outstanding = {} # Map from transaction_id to infohash
self.lt_session = lt_session

def get_health(self, infohash, timeout=15) -> Awaitable[HealthInfo]:
def get_health(self, infohash: bytes, timeout: float = 15) -> Awaitable[HealthInfo]:
"""
Lookup the health of a given infohash.
:param infohash: The 20-byte infohash to lookup.
:param timeout: The timeout of the lookup.
"""
Expand All @@ -48,9 +51,10 @@ def get_health(self, infohash, timeout=15) -> Awaitable[HealthInfo]:

return lookup_future

def finalize_lookup(self, infohash):
def finalize_lookup(self, infohash: bytes) -> None:
"""
Finalize the lookup of the provided infohash and invoke the appropriate deferred.
:param infohash: The infohash of the lookup we finialize.
"""
for transaction_id in [key for key, value in self.outstanding.items() if value == infohash]:
Expand All @@ -71,9 +75,10 @@ def finalize_lookup(self, infohash):
self.lookup_futures.pop(infohash, None)

@staticmethod
def combine_bloomfilters(bf1, bf2):
def combine_bloomfilters(bf1: bytearray, bf2: bytearray) -> bytearray:
"""
Combine two given bloom filters by ORing the bits.
:param bf1: The first bloom filter to combine.
:param bf2: The second bloom filter to combine.
:return: A bytearray with the combined bloomfilter.
Expand All @@ -85,19 +90,20 @@ def combine_bloomfilters(bf1, bf2):
return final_bf

@staticmethod
def get_size_from_bloomfilter(bf):
def get_size_from_bloomfilter(bf: bytearray) -> int:
"""
Return the estimated number of items in the bloom filter.
:param bf: The bloom filter of which we estimate the size.
:return: A rounded integer, approximating the number of items in the filter.
"""

def tobits(s):
def tobits(s: bytes) -> list[int]:
result = []
for c in s:
num = ord(c) if isinstance(c, str) else c
bits = bin(num)[2:]
bits = '00000000'[len(bits):] + bits
bits = "00000000"[len(bits):] + bits
result.extend([int(b) for b in bits])
return result

Expand All @@ -114,10 +120,11 @@ def tobits(s):
c = min(m - 1, total_zeros)
return int(math.log(c / float(m)) / (2 * math.log(1 - 1 / float(m))))

def requesting_bloomfilters(self, transaction_id, infohash):
def requesting_bloomfilters(self, transaction_id: str, infohash: bytes) -> None:
"""
Tne libtorrent DHT has sent a get_peers query for an infohash we may be interested in.
If so, keep track of the transaction and node IDs.
:param transaction_id: The ID of the query
:param infohash: The infohash for which the query was sent.
"""
Expand All @@ -127,9 +134,11 @@ def requesting_bloomfilters(self, transaction_id, infohash):
# Libtorrent is reusing the transaction_id, and is now using it for a infohash that we're not interested in.
self.outstanding.pop(transaction_id, None)

def received_bloomfilters(self, transaction_id, bf_seeds=bytearray(256), bf_peers=bytearray(256)):
def received_bloomfilters(self, transaction_id: str, bf_seeds: bytearray = bytearray(256), # noqa: B008
bf_peers: bytearray = bytearray(256)) -> None: # noqa: B008
"""
We have received bloom filters from the libtorrent DHT. Register the bloom filters and process them.
:param transaction_id: The ID of the query for which we are receiving the bloom filter.
:param bf_seeds: The bloom filter indicating the IP addresses of the seeders.
:param bf_peers: The bloom filter indicating the IP addresses of the peers (leechers).
Expand Down
Loading
Loading