From ce1eabaed6cc3aabf35bed6af6a3df9f7a060273 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Tue, 25 Feb 2020 21:18:01 -0300 Subject: [PATCH] fix moving to a new btih --- lbry/torrent/session.py | 7 +-- lbry/torrent/torrent_manager.py | 4 ++ .../datanetwork/test_file_commands.py | 50 ++++++++++++------- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/lbry/torrent/session.py b/lbry/torrent/session.py index f3bbd2c53..d714bdbfb 100644 --- a/lbry/torrent/session.py +++ b/lbry/torrent/session.py @@ -2,6 +2,7 @@ import asyncio import binascii import os import logging +import random from hashlib import sha1 from tempfile import mkdtemp from typing import Optional @@ -179,10 +180,10 @@ class TorrentSession: self._loop.create_task(self._handles[btih].status_loop()) await self._handles[btih].metadata_completed.wait() - async def remove_torrent(self, btih, remove_files=False): + def remove_torrent(self, btih, remove_files=False): if btih in self._handles: handle = self._handles[btih] - self._session.remove_torrent(handle, 1 if remove_files else 0) + self._session.remove_torrent(handle._handle, 1 if remove_files else 0) self._handles.pop(btih) async def save_file(self, btih, download_directory): @@ -207,7 +208,7 @@ def _create_fake_torrent(tmpdir): # beware, that's just for testing path = os.path.join(tmpdir, 'tmp') with open(path, 'wb') as myfile: - size = myfile.write(b'0' * 40 * 1024 * 1024) + size = myfile.write(bytes([random.randint(0, 255) for _ in range(40)]) * 1024) file_storage = libtorrent.file_storage() file_storage.add_file('tmp', size) t = libtorrent.create_torrent(file_storage, 0, 4 * 1024 * 1024) diff --git a/lbry/torrent/torrent_manager.py b/lbry/torrent/torrent_manager.py index 9d47a55e9..c524599c7 100644 --- a/lbry/torrent/torrent_manager.py +++ b/lbry/torrent/torrent_manager.py @@ -111,6 +111,10 @@ class TorrentManager(SourceManager): super().stop() log.info("finished stopping the torrent manager") + async def delete(self, source: ManagedDownloadSource, delete_file: Optional[bool] = False): + await super().delete(source, delete_file) + self.torrent_session.remove_torrent(source.identifier, delete_file) + async def create(self, file_path: str, key: Optional[bytes] = None, iv_generator: Optional[typing.Generator[bytes, None, None]] = None): raise NotImplementedError diff --git a/tests/integration/datanetwork/test_file_commands.py b/tests/integration/datanetwork/test_file_commands.py index 15783f5f6..095d70765 100644 --- a/tests/integration/datanetwork/test_file_commands.py +++ b/tests/integration/datanetwork/test_file_commands.py @@ -10,33 +10,49 @@ from lbry.wallet import Transaction class FileCommands(CommandTestCase): - async def initialize_torrent(self): - self.seeder_session = TorrentSession(self.loop, None) - self.addCleanup(self.seeder_session.stop) - await self.seeder_session.bind(port=4040) - self.btih = await self.seeder_session.add_fake_torrent() + async def initialize_torrent(self, tx_to_update=None): + if not hasattr(self, 'seeder_session'): + self.seeder_session = TorrentSession(self.loop, None) + self.addCleanup(self.seeder_session.stop) + await self.seeder_session.bind(port=4040) + btih = await self.seeder_session.add_fake_torrent() address = await self.account.receiving.get_or_create_usable_address() - claim = Claim() - claim.stream.update(bt_infohash=self.btih) - tx = await Transaction.claim_create( - 'torrent', claim, 1, address, [self.account], self.account) + if not tx_to_update: + claim = Claim() + claim.stream.update(bt_infohash=btih) + tx = await Transaction.claim_create( + 'torrent', claim, 1, address, [self.account], self.account + ) + else: + claim = tx_to_update.outputs[0].claim + claim.stream.update(bt_infohash=btih) + tx = await Transaction.claim_update( + tx_to_update.outputs[0], claim, 1, address, [self.account], self.account + ) await tx.sign([self.account]) await self.broadcast(tx) await self.confirm_tx(tx.id) - client_session = TorrentSession(self.loop, None) - self.daemon.file_manager.source_managers['torrent'] = TorrentManager( - self.loop, self.daemon.conf, client_session, self.daemon.storage, self.daemon.analytics_manager - ) - await self.daemon.file_manager.source_managers['torrent'].start() - await client_session.bind(port=4041) - client_session._session.add_dht_node(('localhost', 4040)) + self.client_session = self.daemon.file_manager.source_managers['torrent'].torrent_session + self.client_session._session.add_dht_node(('localhost', 4040)) + return tx, btih async def test_download_torrent(self): - await self.initialize_torrent() + tx, btih = await self.initialize_torrent() self.assertNotIn('error', await self.out(self.daemon.jsonrpc_get('torrent'))) self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1) + # second call, see its there and move on self.assertNotIn('error', await self.out(self.daemon.jsonrpc_get('torrent'))) self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1) + self.assertEqual((await self.daemon.jsonrpc_file_list())['items'][0].identifier, btih) + self.assertIn(btih, self.client_session._handles) + tx, new_btih = await self.initialize_torrent(tx) + self.assertNotEqual(btih, new_btih) + # claim now points to another torrent, update to it + self.assertNotIn('error', await self.out(self.daemon.jsonrpc_get('torrent'))) + self.assertEqual((await self.daemon.jsonrpc_file_list())['items'][0].identifier, new_btih) + self.assertIn(new_btih, self.client_session._handles) + self.assertNotIn(btih, self.client_session._handles) + self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1) async def create_streams_in_range(self, *args, **kwargs): self.stream_claim_ids = []