Skip to content

Commit db6934a

Browse files
committed
add test case for restart, fix torrent file update
1 parent 9236854 commit db6934a

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

lbry/extras/daemon/storage.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import asyncio
66
import binascii
77
import time
8+
from operator import itemgetter
89
from typing import Optional
910
from lbry.wallet import SQLiteMixin
1011
from lbry.conf import Config
@@ -635,6 +636,15 @@ def update_db_removed(transaction: sqlite3.Connection, removed):
635636
def get_all_lbry_files(self) -> typing.Awaitable[typing.List[typing.Dict]]:
636637
return self.db.run(get_all_lbry_files)
637638

639+
async def get_all_torrent_files(self) -> typing.List[typing.Dict]:
640+
def _get_all_torrent_files(transaction):
641+
cursor = transaction.execute("select * from file join torrent on file.bt_infohash=torrent.bt_infohash")
642+
return [
643+
{field: value for field, value in zip(list(map(itemgetter(0), cursor.description)), row)}
644+
for row in cursor.fetchall()
645+
]
646+
return await self.db.run(_get_all_torrent_files)
647+
638648
def change_file_status(self, stream_hash: str, new_status: str):
639649
log.debug("update file status %s -> %s", stream_hash, new_status)
640650
return self.db.execute_fetchall("update file set status=? where stream_hash=?", (new_status, stream_hash))
@@ -907,7 +917,7 @@ async def get_content_claim(self, stream_hash: str, include_supports: typing.Opt
907917

908918
async def get_content_claim_for_torrent(self, bt_infohash):
909919
claims = await self.db.run(get_claims_from_torrent_info_hashes, [bt_infohash])
910-
return claims[bt_infohash].as_dict() if claims else None
920+
return claims[bt_infohash] if claims else None
911921

912922
# # # # # # # # # reflector functions # # # # # # # # #
913923

lbry/file/file_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ async def download_from_uri(self, uri, exchange_rate_manager: 'ExchangeRateManag
138138
existing[0].identifier, outpoint, existing[0].torrent_length, existing[0].torrent_name
139139
)
140140
claim_info = await self.storage.get_content_claim_for_torrent(existing[0].identifier)
141-
existing[0].set_claim(claim_info, claim)
141+
existing[0].set_claim(claim_info.as_dict() if claim_info else None, claim)
142142
else:
143143
await self.storage.save_content_claim(
144144
existing[0].stream_hash, outpoint
@@ -238,7 +238,7 @@ async def download_from_uri(self, uri, exchange_rate_manager: 'ExchangeRateManag
238238
stream.identifier, outpoint, stream.torrent_length, stream.torrent_name
239239
)
240240
claim_info = await self.storage.get_content_claim_for_torrent(stream.identifier)
241-
stream.set_claim(claim_info, claim)
241+
stream.set_claim(claim_info.as_dict() if claim_info else None, claim)
242242
if save_file:
243243
await asyncio.wait_for(stream.save_file(), timeout - (self.loop.time() - before_download),
244244
loop=self.loop)

lbry/file/source_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ async def create(self, file_path: str, key: Optional[bytes] = None,
8484
raise NotImplementedError()
8585

8686
async def delete(self, source: ManagedDownloadSource, delete_file: Optional[bool] = False):
87+
await self.storage.delete_torrent(source.identifier)
8788
self.remove(source)
8889
if delete_file and source.output_file_exists:
8990
os.remove(source.full_path)

lbry/torrent/torrent_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async def recover_streams(self, file_infos: typing.List[typing.Dict]):
161161
async def _load_stream(self, rowid: int, bt_infohash: str, file_name: Optional[str],
162162
download_directory: Optional[str], status: str,
163163
claim: Optional['StoredContentClaim'], content_fee: Optional['Transaction'],
164-
added_on: Optional[int]):
164+
added_on: Optional[int], **kwargs):
165165
stream = TorrentSource(
166166
self.loop, self.config, self.storage, identifier=bt_infohash, file_name=file_name,
167167
download_directory=download_directory, status=status, claim=claim, rowid=rowid,
@@ -171,7 +171,9 @@ async def _load_stream(self, rowid: int, bt_infohash: str, file_name: Optional[s
171171
self.add(stream)
172172

173173
async def initialize_from_database(self):
174-
pass
174+
for file in await self.storage.get_all_torrent_files():
175+
claim = await self.storage.get_content_claim_for_torrent(file['bt_infohash'])
176+
await self._load_stream(None, claim=claim, **file)
175177

176178
async def start(self):
177179
await super().start()

tests/integration/datanetwork/test_file_commands.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ async def test_download_torrent(self):
6666
# claim now points to another torrent, update to it
6767
self.assertNotIn('error', await self.out(self.daemon.jsonrpc_get('torrent')))
6868
self.assertEqual((await self.daemon.jsonrpc_file_list())['items'][0].identifier, new_btih)
69+
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
70+
71+
# restart and verify that only one updated stream was recovered
72+
self.daemon.file_manager.stop()
73+
await self.daemon.file_manager.start()
74+
self.assertEqual((await self.daemon.jsonrpc_file_list())['items'][0].identifier, new_btih)
75+
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)
76+
6977
self.assertIn(new_btih, self.client_session._handles)
7078
self.assertNotIn(btih, self.client_session._handles)
7179
self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1)

0 commit comments

Comments
 (0)