fix moving to a new btih

This commit is contained in:
Victor Shyba 2020-02-25 21:18:01 -03:00
parent 5a7312cbb8
commit 539655292a
3 changed files with 41 additions and 20 deletions

View file

@ -2,6 +2,7 @@ import asyncio
import binascii import binascii
import os import os
import logging import logging
import random
from hashlib import sha1 from hashlib import sha1
from tempfile import mkdtemp from tempfile import mkdtemp
from typing import Optional from typing import Optional
@ -179,10 +180,10 @@ class TorrentSession:
self._loop.create_task(self._handles[btih].status_loop()) self._loop.create_task(self._handles[btih].status_loop())
await self._handles[btih].metadata_completed.wait() 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: if btih in self._handles:
handle = self._handles[btih] 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) self._handles.pop(btih)
async def save_file(self, btih, download_directory): async def save_file(self, btih, download_directory):
@ -207,7 +208,7 @@ def _create_fake_torrent(tmpdir):
# beware, that's just for testing # beware, that's just for testing
path = os.path.join(tmpdir, 'tmp') path = os.path.join(tmpdir, 'tmp')
with open(path, 'wb') as myfile: 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 = libtorrent.file_storage()
file_storage.add_file('tmp', size) file_storage.add_file('tmp', size)
t = libtorrent.create_torrent(file_storage, 0, 4 * 1024 * 1024) t = libtorrent.create_torrent(file_storage, 0, 4 * 1024 * 1024)

View file

@ -111,6 +111,10 @@ class TorrentManager(SourceManager):
super().stop() super().stop()
log.info("finished stopping the torrent manager") 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, async def create(self, file_path: str, key: Optional[bytes] = None,
iv_generator: Optional[typing.Generator[bytes, None, None]] = None): iv_generator: Optional[typing.Generator[bytes, None, None]] = None):
raise NotImplementedError raise NotImplementedError

View file

@ -10,33 +10,49 @@ from lbry.wallet import Transaction
class FileCommands(CommandTestCase): class FileCommands(CommandTestCase):
async def initialize_torrent(self): async def initialize_torrent(self, tx_to_update=None):
self.seeder_session = TorrentSession(self.loop, None) if not hasattr(self, 'seeder_session'):
self.addCleanup(self.seeder_session.stop) self.seeder_session = TorrentSession(self.loop, None)
await self.seeder_session.bind(port=4040) self.addCleanup(self.seeder_session.stop)
self.btih = await self.seeder_session.add_fake_torrent() 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() address = await self.account.receiving.get_or_create_usable_address()
claim = Claim() if not tx_to_update:
claim.stream.update(bt_infohash=self.btih) claim = Claim()
tx = await Transaction.claim_create( claim.stream.update(bt_infohash=btih)
'torrent', claim, 1, address, [self.account], self.account) 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 tx.sign([self.account])
await self.broadcast(tx) await self.broadcast(tx)
await self.confirm_tx(tx.id) await self.confirm_tx(tx.id)
client_session = TorrentSession(self.loop, None) self.client_session = self.daemon.file_manager.source_managers['torrent'].torrent_session
self.daemon.file_manager.source_managers['torrent'] = TorrentManager( self.client_session._session.add_dht_node(('localhost', 4040))
self.loop, self.daemon.conf, client_session, self.daemon.storage, self.daemon.analytics_manager return tx, btih
)
await self.daemon.file_manager.source_managers['torrent'].start()
await client_session.bind(port=4041)
client_session._session.add_dht_node(('localhost', 4040))
async def test_download_torrent(self): 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.assertNotIn('error', await self.out(self.daemon.jsonrpc_get('torrent')))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1) 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.assertNotIn('error', await self.out(self.daemon.jsonrpc_get('torrent')))
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1) 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): async def create_streams_in_range(self, *args, **kwargs):
self.stream_claim_ids = [] self.stream_claim_ids = []