Skip to content

Commit bf0ebc8

Browse files
authored
Merge pull request #10283 from f321x/fix_add_peer
cli: fix add_peer command
2 parents 5e8f43e + fca879d commit bf0ebc8

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

electrum/commands.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import base64
3434
import asyncio
3535
import inspect
36+
from asyncio import CancelledError
3637
from collections import defaultdict
3738
from functools import wraps
3839
from decimal import Decimal, InvalidOperation
@@ -44,6 +45,7 @@
4445

4546
from . import util
4647
from .lnmsg import OnionWireSerializer
48+
from .lnworker import LN_P2P_NETWORK_TIMEOUT
4749
from .logging import Logger
4850
from .onion_message import create_blinded_path, send_onion_message_to
4951
from .submarine_swaps import NostrTransport
@@ -1684,7 +1686,12 @@ async def add_peer(self, connection_string, timeout=20, gossip=False, wallet: Ab
16841686
arg:int:timeout:Timeout in seconds (default=20)
16851687
"""
16861688
lnworker = self.network.lngossip if gossip else wallet.lnworker
1687-
await lnworker.add_peer(connection_string)
1689+
peer = await lnworker.add_peer(connection_string)
1690+
try:
1691+
await util.wait_for2(peer.initialized, timeout=LN_P2P_NETWORK_TIMEOUT)
1692+
except (CancelledError, Exception) as e:
1693+
# FIXME often simply CancelledError and real cause (e.g. timeout) remains hidden
1694+
raise UserFacingException(f"Connection failed: {repr(e)}")
16881695
return True
16891696

16901697
@command('wnl')

tests/test_commands.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import binascii
23
import datetime
34
import os.path
@@ -740,3 +741,38 @@ async def test_export_lightning_preimage(self, *mock_args):
740741
assert await cmds.export_lightning_preimage(payment_hash=payment_hash.hex(), wallet=w) == preimage.hex()
741742
assert await cmds.export_lightning_preimage(payment_hash=os.urandom(32).hex(), wallet=w) is None
742743

744+
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
745+
@mock.patch('electrum.commands.LN_P2P_NETWORK_TIMEOUT', 0.001)
746+
async def test_add_peer(self, *mock_args):
747+
w = restore_wallet_from_text__for_unittest(
748+
'disagree rug lemon bean unaware square alone beach tennis exhibit fix mimic',
749+
path='if_this_exists_mocking_failed_648151893',
750+
config=self.config)['wallet']
751+
cmds = Commands(config=self.config)
752+
753+
# Mock the network and lnworker
754+
mock_lnworker = mock.Mock()
755+
w.lnworker = mock_lnworker
756+
mock_peer = mock.Mock()
757+
mock_peer.initialized = asyncio.Future()
758+
connection_string = "[email protected]:9735"
759+
called = False
760+
async def lnworker_add_peer(*args, **kwargs):
761+
assert args[0] == connection_string
762+
nonlocal called
763+
called += 1
764+
return mock_peer
765+
mock_lnworker.add_peer = lnworker_add_peer
766+
767+
# check if add_peer times out if peer doesn't initialize (LN_P2P_NETWORK_TIMEOUT is 0.001s)
768+
with self.assertRaises(UserFacingException):
769+
await cmds.add_peer(connection_string=connection_string, wallet=w)
770+
# check if add_peer called lnworker.add_peer
771+
assert called == 1
772+
773+
mock_peer.initialized = asyncio.Future()
774+
mock_peer.initialized.set_result(True)
775+
# check if add_peer returns True if peer is initialized
776+
result = await cmds.add_peer(connection_string=connection_string, wallet=w)
777+
assert called == 2
778+
self.assertTrue(result)

0 commit comments

Comments
 (0)