URI: 
       tmove lnpay attempts logic to lnworker.pay - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit dbe8b756595778ed5fb1ee251ac24e28b02e5e1e
   DIR parent 3349e941def1e5cadf686cd3ea0079cebcf23427
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Thu, 30 May 2019 21:04:35 +0200
       
       move lnpay attempts logic to lnworker.pay
       
       Diffstat:
         M electrum/commands.py                |       5 +++--
         M electrum/gui/qt/main_window.py      |      11 +++++------
         M electrum/lnworker.py                |      17 ++++++++++-------
       
       3 files changed, 18 insertions(+), 15 deletions(-)
       ---
   DIR diff --git a/electrum/commands.py b/electrum/commands.py
       t@@ -780,8 +780,8 @@ class Commands:
                self.lnworker.reestablish_channel()
        
            @command('wn')
       -    def lnpay(self, invoice, timeout=10):
       -        return self.lnworker.pay(invoice, timeout=timeout)
       +    def lnpay(self, invoice, attempts=1, timeout=10):
       +        return self.lnworker.pay(invoice, attempts=attempts, timeout=timeout)
        
            @command('wn')
            def addinvoice(self, requested_amount, message):
       t@@ -902,6 +902,7 @@ command_options = {
            'domain':      ("-D", "List of addresses"),
            'memo':        ("-m", "Description of the request"),
            'expiration':  (None, "Time in seconds"),
       +    'attempts':    (None, "Number of payment attempts"),
            'timeout':     (None, "Timeout in seconds"),
            'force':       (None, "Create new address beyond gap limit, if no more addresses are available."),
            'pending':     (None, "Show only pending requests."),
   DIR diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py
       t@@ -1678,6 +1678,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
                LN_NUM_PAYMENT_ATTEMPTS = 3
                def on_success(result):
                    self.logger.info(f'ln payment success. {result}')
       +            self.show_error(_('Payment succeeded'))
                    self.do_clear()
                def on_failure(exc_info):
                    type_, e, traceback = exc_info
       t@@ -1686,14 +1687,12 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger):
                    elif isinstance(e, InvoiceError):
                        self.show_error(_('InvoiceError: {}').format(e))
                    else:
       +                self.show_error(_('Error: {}').format(e))
                        raise e
                def task():
       -            for i in range(LN_NUM_PAYMENT_ATTEMPTS):
       -                success = self.wallet.lnworker.pay(invoice, amount_sat=amount, timeout=30)
       -                if success:
       -                    break
       -            else:
       -                raise PaymentFailure('Failed after {i} attempts')
       +            success = self.wallet.lnworker.pay(invoice, attempts=LN_NUM_PAYMENT_ATTEMPTS, amount_sat=amount, timeout=30)
       +            if not success:
       +                raise PaymentFailure('Failed after {LN_NUM_PAYMENT_ATTEMPTS} attempts')
        
                msg = _('Sending lightning payment...')
                WaitingDialog(self, msg, task, on_success, on_failure)
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -652,13 +652,13 @@ class LNWallet(LNWorker):
                chan = f.result(timeout)
                return chan.funding_outpoint.to_str()
        
       -    def pay(self, invoice, amount_sat=None, timeout=10):
       +    def pay(self, invoice, attempts=1, amount_sat=None, timeout=10):
                """
                Can be called from other threads
                Raises timeout exception if htlc is not fulfilled
                """
                fut = asyncio.run_coroutine_threadsafe(
       -            self._pay(invoice, amount_sat),
       +            self._pay(invoice, attempts, amount_sat),
                    self.network.asyncio_loop)
                return fut.result(timeout=timeout)
        
       t@@ -668,14 +668,17 @@ class LNWallet(LNWorker):
                        if chan.short_channel_id == short_channel_id:
                            return chan
        
       -    async def _pay(self, invoice, amount_sat=None):
       +    async def _pay(self, invoice, attempts=1, amount_sat=None):
                addr = self._check_invoice(invoice, amount_sat)
                self.save_invoice(addr.paymenthash, invoice, SENT, is_paid=False)
                self.wallet.set_label(bh2u(addr.paymenthash), addr.get_description())
       -        route = await self._create_route_from_invoice(decoded_invoice=addr)
       -        if not self.get_channel_by_short_id(route[0].short_channel_id):
       -            assert False, 'Found route with short channel ID we don\'t have: ' + repr(route[0].short_channel_id)
       -        return await self._pay_to_route(route, addr, invoice)
       +        for i in range(attempts):
       +            route = await self._create_route_from_invoice(decoded_invoice=addr)
       +            if not self.get_channel_by_short_id(route[0].short_channel_id):
       +                assert False, 'Found route with short channel ID we don\'t have: ' + repr(route[0].short_channel_id)
       +            if await self._pay_to_route(route, addr, invoice):
       +                return True
       +        return False
        
            async def _pay_to_route(self, route, addr, pay_req):
                short_channel_id = route[0].short_channel_id