Skip to content

Commit e10f57d

Browse files
authored
Merge pull request #3642 from lbryio/libtorrent
use official libtorrent, fix tests, make it a normal dependency
2 parents 5c543cb + 8a033d5 commit e10f57d

7 files changed

Lines changed: 43 additions & 57 deletions

File tree

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
1919
restore-keys: ${{ runner.os }}-pip-
2020
- run: pip install --user --upgrade pip wheel
21-
- run: pip install -e .[torrent,lint]
21+
- run: pip install -e .[lint]
2222
- run: make lint
2323

2424
tests-unit:
@@ -50,7 +50,7 @@ jobs:
5050
string: ${{ runner.os }}
5151
- run: python -m pip install --user --upgrade pip wheel
5252
- if: startsWith(runner.os, 'linux')
53-
run: pip install -e .[torrent,test]
53+
run: pip install -e .[test]
5454
- if: startsWith(runner.os, 'linux')
5555
env:
5656
HOME: /tmp

lbry/extras/daemon/components.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@
2828
from lbry.wallet import WalletManager
2929
from lbry.wallet.usage_payment import WalletServerPayer
3030
from lbry.torrent.tracker import TrackerClient
31-
32-
try:
33-
from lbry.torrent.session import TorrentSession
34-
except ImportError:
35-
TorrentSession = None
31+
from lbry.torrent.session import TorrentSession
3632

3733
log = logging.getLogger(__name__)
3834

@@ -361,10 +357,6 @@ async def start(self):
361357
wallet = self.component_manager.get_component(WALLET_COMPONENT)
362358
node = self.component_manager.get_component(DHT_COMPONENT) \
363359
if self.component_manager.has_component(DHT_COMPONENT) else None
364-
try:
365-
torrent = self.component_manager.get_component(LIBTORRENT_COMPONENT) if TorrentSession else None
366-
except NameError:
367-
torrent = None
368360
log.info('Starting the file manager')
369361
loop = asyncio.get_event_loop()
370362
self.file_manager = FileManager(
@@ -373,7 +365,8 @@ async def start(self):
373365
self.file_manager.source_managers['stream'] = StreamManager(
374366
loop, self.conf, blob_manager, wallet, storage, node,
375367
)
376-
if TorrentSession and LIBTORRENT_COMPONENT not in self.conf.components_to_skip:
368+
if self.component_manager.has_component(LIBTORRENT_COMPONENT):
369+
torrent = self.component_manager.get_component(LIBTORRENT_COMPONENT)
377370
self.file_manager.source_managers['torrent'] = TorrentManager(
378371
loop, self.conf, torrent, storage, self.component_manager.analytics_manager
379372
)
@@ -502,9 +495,8 @@ async def get_status(self):
502495
}
503496

504497
async def start(self):
505-
if TorrentSession:
506-
self.torrent_session = TorrentSession(asyncio.get_event_loop(), None)
507-
await self.torrent_session.bind() # TODO: specify host/port
498+
self.torrent_session = TorrentSession(asyncio.get_event_loop(), None)
499+
await self.torrent_session.bind() # TODO: specify host/port
508500

509501
async def stop(self):
510502
if self.torrent_session:

lbry/torrent/session.py

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,13 @@
1010
import libtorrent
1111

1212

13-
NOTIFICATION_MASKS = [
14-
"error",
15-
"peer",
16-
"port_mapping",
17-
"storage",
18-
"tracker",
19-
"debug",
20-
"status",
21-
"progress",
22-
"ip_block",
23-
"dht",
24-
"stats",
25-
"session_log",
26-
"torrent_log",
27-
"peer_log",
28-
"incoming_request",
29-
"dht_log",
30-
"dht_operation",
31-
"port_mapping_log",
32-
"picker_log",
33-
"file_progress",
34-
"piece_progress",
35-
"upload",
36-
"block_progress"
37-
]
3813
log = logging.getLogger(__name__)
39-
40-
4114
DEFAULT_FLAGS = ( # fixme: somehow the logic here is inverted?
4215
libtorrent.add_torrent_params_flags_t.flag_auto_managed
4316
| libtorrent.add_torrent_params_flags_t.flag_update_subscribe
4417
)
4518

4619

47-
def get_notification_type(notification) -> str:
48-
for i, notification_type in enumerate(NOTIFICATION_MASKS):
49-
if (1 << i) & notification:
50-
return notification_type
51-
raise ValueError("unrecognized notification type")
52-
53-
5420
class TorrentHandle:
5521
def __init__(self, loop, executor, handle):
5622
self._loop = loop
@@ -156,10 +122,8 @@ async def add_fake_torrent(self):
156122
async def bind(self, interface: str = '0.0.0.0', port: int = 10889):
157123
settings = {
158124
'listen_interfaces': f"{interface}:{port}",
159-
'enable_outgoing_utp': True,
160-
'enable_incoming_utp': True,
161-
'enable_outgoing_tcp': False,
162-
'enable_incoming_tcp': False
125+
'enable_natpmp': False,
126+
'enable_upnp': False
163127
}
164128
self._session = await self._loop.run_in_executor(
165129
self._executor, libtorrent.session, settings # pylint: disable=c-extension-no-member

scripts/hook-libtorrent.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Hook for libtorrent.
3+
"""
4+
5+
import os
6+
import glob
7+
import os.path
8+
from PyInstaller.utils.hooks import get_module_file_attribute
9+
from PyInstaller import compat
10+
11+
12+
def get_binaries():
13+
if compat.is_win:
14+
files = ('c:/Windows/System32/libssl-1_1-x64.dll', 'c:/Windows/System32/libcrypto-1_1-x64.dll')
15+
for file in files:
16+
if not os.path.isfile(file):
17+
print(f"MISSING {file}")
18+
return [(file, '.') for file in files]
19+
return []
20+
21+
22+
binaries = get_binaries()
23+
for file in glob.glob(os.path.join(get_module_file_attribute('libtorrent'), 'libtorrent*pyd*')):
24+
binaries.append((file, 'libtorrent'))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
'coincurve==15.0.0',
4747
'pbkdf2==1.3',
4848
'filetype==1.0.9',
49+
'libtorrent==2.0.6',
4950
],
5051
extras_require={
51-
'torrent': ['lbry-libtorrent'],
5252
'lint': [
5353
'pylint==2.10.0'
5454
],

tests/integration/datanetwork/test_file_commands.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ def __init__(self, *a, **kw):
1818
super().__init__(*a, **kw)
1919
self.skip_libtorrent = False
2020

21+
async def add_forever(self):
22+
while True:
23+
for handle in self.client_session._handles.values():
24+
handle._handle.connect_peer(('127.0.0.1', 4040))
25+
await asyncio.sleep(.1)
26+
2127
async def initialize_torrent(self, tx_to_update=None):
2228
if not hasattr(self, 'seeder_session'):
2329
self.seeder_session = TorrentSession(self.loop, None)
2430
self.addCleanup(self.seeder_session.stop)
25-
await self.seeder_session.bind(port=4040)
31+
await self.seeder_session.bind('127.0.0.1', port=4040)
2632
btih = await self.seeder_session.add_fake_torrent()
2733
address = await self.account.receiving.get_or_create_usable_address()
2834
if not tx_to_update:
@@ -40,8 +46,9 @@ async def initialize_torrent(self, tx_to_update=None):
4046
await tx.sign([self.account])
4147
await self.broadcast_and_confirm(tx)
4248
self.client_session = self.daemon.file_manager.source_managers['torrent'].torrent_session
43-
self.client_session._session.add_dht_node(('localhost', 4040))
4449
self.client_session.wait_start = False # fixme: this is super slow on tests
50+
task = asyncio.create_task(self.add_forever())
51+
self.addCleanup(task.cancel)
4552
return tx, btih
4653

4754
@skipIf(TorrentSession is None, "libtorrent not installed")

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ deps =
55
extras =
66
test
77
hub
8-
torrent
98
changedir = {toxinidir}/tests
109
setenv =
1110
HOME=/tmp

0 commit comments

Comments
 (0)