From e3485de496e275e85fa15c68a6aa5a1744acaa93 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sun, 1 Jan 2023 23:43:25 +0000 Subject: [PATCH] qt gui: handle swap server unreachable note: testnet swap server is offline atm closes https://github.com/spesmilo/electrum/issues/8107 --- electrum/gui/qt/main_window.py | 7 ++++++- electrum/submarine_swaps.py | 21 +++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py index 3019968a6..4a18eab6e 100644 --- a/electrum/gui/qt/main_window.py +++ b/electrum/gui/qt/main_window.py @@ -74,6 +74,7 @@ from electrum.simple_config import SimpleConfig from electrum.logging import Logger from electrum.lnutil import ln_dummy_address, extract_nodeid, ConnStringFormatError from electrum.lnaddr import lndecode +from electrum.submarine_swaps import SwapServerError from .exception_window import Exception_Hook from .amountedit import BTCAmountEdit @@ -1131,7 +1132,11 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger, QtEventListener): return def get_pairs_thread(): self.network.run_from_another_thread(self.wallet.lnworker.swap_manager.get_pairs()) - BlockingWaitingDialog(self, _('Please wait...'), get_pairs_thread) + try: + BlockingWaitingDialog(self, _('Please wait...'), get_pairs_thread) + except SwapServerError as e: + self.show_error(str(e)) + return d = SwapDialog(self, is_reverse=is_reverse, recv_amount_sat=recv_amount_sat, channels=channels) return d.run() diff --git a/electrum/submarine_swaps.py b/electrum/submarine_swaps.py index df35d6afc..7779c8988 100644 --- a/electrum/submarine_swaps.py +++ b/electrum/submarine_swaps.py @@ -6,6 +6,7 @@ from decimal import Decimal import math import attr +import aiohttp from .crypto import sha256, hash_160 from .ecc import ECPrivkey @@ -21,6 +22,7 @@ from .lnutil import hex_to_bytes from .json_db import StoredObject from . import constants from .address_synchronizer import TX_HEIGHT_LOCAL +from .i18n import _ if TYPE_CHECKING: from .network import Network @@ -79,6 +81,11 @@ WITNESS_TEMPLATE_REVERSE_SWAP = [ ] +class SwapServerError(Exception): + def __str__(self): + return _("The swap server errored or is unreachable.") + + @attr.s class SwapData(StoredObject): is_reverse = attr.ib(type=bool) @@ -472,11 +479,17 @@ class SwapManager(Logger): self._swaps_by_lockup_address[swap.lockup_address] = swap async def get_pairs(self) -> None: + """Might raise SwapServerError.""" from .network import Network - response = await Network.async_send_http_on_proxy( - 'get', - self.api_url + '/getpairs', - timeout=30) + try: + response = await Network.async_send_http_on_proxy( + 'get', + self.api_url + '/getpairs', + timeout=30) + except aiohttp.ClientError as e: + self.logger.error(f"Swap server errored: {e!r}") + raise SwapServerError() from e + # we assume server response is well-formed; otherwise let an exception propagate to the crash reporter pairs = json.loads(response) fees = pairs['pairs']['BTC/BTC']['fees'] self.percentage = fees['percentage']