Skip to content

Commit

Permalink
Merge pull request #6897 from drew2a/fix/6875
Browse files Browse the repository at this point in the history
Simplify base64 conversion in `get_pieces_base64`
  • Loading branch information
drew2a authored May 13, 2022
2 parents 3961a22 + 14e37a3 commit c8ecb65
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
1 change: 1 addition & 0 deletions requirements-core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {}
Expand Down
25 changes: 16 additions & 9 deletions src/tribler/core/components/libtorrent/tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
from pathlib import Path
from unittest.mock import Mock

from ipv8.util import succeed

import libtorrent as lt
from libtorrent import bencode

import pytest
from ipv8.util import succeed
from libtorrent import bencode

from tribler.core.exceptions import SaveResumeDataError
from tribler.core.components.libtorrent.download_manager.download_config import DownloadConfig
from tribler.core.components.libtorrent.utils.torrent_utils import get_info_from_handle
from tribler.core.exceptions import SaveResumeDataError
from tribler.core.tests.tools.base_test import MockObject
from tribler.core.tests.tools.common import TESTS_DATA_DIR
from tribler.core.components.libtorrent.utils.torrent_utils import get_info_from_handle
from tribler.core.utilities.unicode import hexlify
from tribler.core.utilities.utilities import bdecode_compat

Expand Down Expand Up @@ -86,6 +84,7 @@ def test_selected_files(mock_handle, test_download):
"""
Test whether the selected files are set correctly
"""

def mocked_set_file_prios(_):
mocked_set_file_prios.called = True

Expand Down Expand Up @@ -113,6 +112,7 @@ def test_selected_files_no_files(mock_handle, test_download):
"""
Test that no files are selected if torrent info is not available.
"""

def mocked_set_file_prios(_):
mocked_set_file_prios.called = True

Expand Down Expand Up @@ -147,6 +147,7 @@ async def test_set_share_mode(mock_handle, test_download):
"""
Test whether we set the right share mode in Download
"""

def mocked_set_share_mode(val):
assert val
mocked_set_share_mode.called = True
Expand All @@ -161,11 +162,12 @@ def test_get_num_connected_seeds_peers(mock_handle, test_download):
"""
Test whether connected peers and seeds are correctly returned
"""

def get_peer_info(seeders, leechers):
peer_info = []
for _ in range(seeders):
seeder = MockObject()
seeder.flags = 140347 # some value where seed flag(1024) is true
seeder.flags = 140347 # some value where seed flag(1024) is true
seeder.seed = 1024
peer_info.append(seeder)
for _ in range(leechers):
Expand All @@ -188,6 +190,7 @@ async def test_set_priority(mock_handle, test_download):
"""
Test whether setting the priority calls the right methods in Download
"""

def mocked_set_priority(prio):
assert prio == 1234
mocked_set_priority.called = True
Expand All @@ -202,6 +205,7 @@ def test_add_trackers(mock_handle, test_download):
"""
Testing whether trackers are added to the libtorrent handler in Download
"""

def mocked_add_trackers(tracker_info):
assert isinstance(tracker_info, dict)
assert tracker_info['url'] == 'http://google.com'
Expand Down Expand Up @@ -278,6 +282,7 @@ def test_metadata_received_invalid_info(mock_handle, test_download):
"""
Testing whether the right operations happen when we receive metadata but the torrent info is invalid
"""

def mocked_checkpoint():
raise RuntimeError("This code should not be reached!")

Expand All @@ -291,6 +296,7 @@ def test_metadata_received_invalid_torrent_with_value_error(mock_handle, test_do
Testing whether the right operations happen when we receive metadata but the torrent info is invalid and throws
Value Error
"""

def mocked_checkpoint():
raise RuntimeError("This code should not be reached!")

Expand All @@ -312,6 +318,7 @@ def test_torrent_checked_alert(mock_handle, test_download):
"""
Testing whether the right operations happen after a torrent checked alert is received
"""

def mocked_pause_checkpoint():
mocked_pause_checkpoint.called = True
return succeed(None)
Expand Down Expand Up @@ -352,8 +359,8 @@ def test_get_pieces_bitmask(mock_handle, test_download):
test_download.handle.status().pieces = [True, False, True, False, False]
assert test_download.get_pieces_base64() == b"oA=="

test_download.handle.status().pieces = [True * 16]
assert test_download.get_pieces_base64() == b"gA=="
test_download.handle.status().pieces = [True] * 16
assert test_download.get_pieces_base64() == b"//8="


async def test_resume_data_failed(test_download):
Expand Down

0 comments on commit c8ecb65

Please sign in to comment.