diff --git a/requirements-core.txt b/requirements-core.txt index 64b882f4ea7..3708a1a7f0c 100644 --- a/requirements-core.txt +++ b/requirements-core.txt @@ -23,5 +23,6 @@ yappi==1.3.3 yarl==1.7.2 # keep this dependency higher than 1.6.3. See: https://github.com/aio-libs/yarl/issues/517 Faker==9.8.2 sentry-sdk==1.5.0 +bitarray==2.5.1 pyipv8==2.8.0 libtorrent==1.2.15 diff --git a/src/tribler/core/components/libtorrent/download_manager/download.py b/src/tribler/core/components/libtorrent/download_manager/download.py index a5cbb37ac8c..72a7b8bad54 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download.py +++ b/src/tribler/core/components/libtorrent/download_manager/download.py @@ -9,8 +9,9 @@ from collections import defaultdict from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple +from bitarray import bitarray from ipv8.taskmanager import TaskManager, task -from ipv8.util import int2byte, succeed +from ipv8.util import succeed from tribler.core import notifications from tribler.core.components.libtorrent.download_manager.download_config import DownloadConfig @@ -225,14 +226,9 @@ def get_pieces_base64(self) -> bytes: """ Returns a base64 encoded bitmask of the pieces that we have. """ - bitstr = b"" - for bit in self.handle.status().pieces: - bitstr += b'1' if bit else b'0' - - encoded_str = b"" - for i in range(0, len(bitstr), 8): - encoded_str += int2byte(int(bitstr[i:i + 8].ljust(8, b'0'), 2)) - return base64.b64encode(encoded_str) + binary_gen = (int(boolean) for boolean in self.handle.status().pieces) + bits = bitarray(binary_gen) + return base64.b64encode(bits.tobytes()) def post_alert(self, alert_type: str, alert_dict: Optional[Dict] = None): alert_dict = alert_dict or {}