URI: 
       tLNWorker: do not save PR_INFLIGHT status, detect it on startup instead - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit c0bf9b4509cfdeace824a9054d9c90449ef46ad6
   DIR parent 51455c9d9a999628a0d36d61c50e5931ceda912a
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Thu,  4 Feb 2021 15:48:00 +0100
       
       LNWorker: do not save PR_INFLIGHT status, detect it on startup instead
       
       Diffstat:
         M electrum/gui/qt/channel_details.py  |       6 +++++-
         M electrum/lnchannel.py               |      24 ++++++++----------------
         M electrum/lnworker.py                |      26 +++++++++++++++-----------
       
       3 files changed, 28 insertions(+), 28 deletions(-)
       ---
   DIR diff --git a/electrum/gui/qt/channel_details.py b/electrum/gui/qt/channel_details.py
       t@@ -195,7 +195,11 @@ class ChannelDetailsDialog(QtWidgets.QDialog):
                vbox.addWidget(QLabel(_('Payments (HTLCs):')))
                w = QtWidgets.QTreeView(self)
                htlc_dict = chan.get_payments()
       -        w.setModel(self.make_model(htlc_dict))
       +        htlc_list = []
       +        for rhash, _list in htlc_dict.items():
       +            for _tuple in _list:
       +                htlc_list.append((rhash.hex(),) + _tuple)
       +        w.setModel(self.make_model(htlc_list))
                w.header().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
                vbox.addWidget(w)
                vbox.addLayout(Buttons(CloseButton(self)))
   DIR diff --git a/electrum/lnchannel.py b/electrum/lnchannel.py
       t@@ -670,27 +670,19 @@ class Channel(AbstractChannel):
            def get_next_feerate(self, subject: HTLCOwner) -> int:
                return self.hm.get_feerate_in_next_ctx(subject)
        
       -    def get_payments(self):
       -        out = []
       +    def get_payments(self, status=None):
       +        out = defaultdict(list)
                for direction, htlc in self.hm.all_htlcs_ever():
                    htlc_proposer = LOCAL if direction is SENT else REMOTE
                    if self.hm.was_htlc_failed(htlc_id=htlc.htlc_id, htlc_proposer=htlc_proposer):
       -                status = 'failed'
       +                _status = 'failed'
                    elif self.hm.was_htlc_preimage_released(htlc_id=htlc.htlc_id, htlc_proposer=htlc_proposer):
       -                status = 'settled'
       +                _status = 'settled'
                    else:
       -                status = 'inflight'
       -            rhash = htlc.payment_hash.hex()
       -            out.append((rhash, self.channel_id, htlc, direction, status))
       -        return out
       -
       -    def get_settled_payments(self):
       -        out = defaultdict(list)
       -        for direction, htlc in self.hm.all_htlcs_ever():
       -            htlc_proposer = LOCAL if direction is SENT else REMOTE
       -            if self.hm.was_htlc_preimage_released(htlc_id=htlc.htlc_id, htlc_proposer=htlc_proposer):
       -                rhash = htlc.payment_hash.hex()
       -                out[rhash].append((self.channel_id, htlc, direction))
       +                _status = 'inflight'
       +            if status and status != _status:
       +                continue
       +            out[htlc.payment_hash].append((self.channel_id, htlc, direction, _status))
                return out
        
            def open_with_first_pcp(self, remote_pcp: bytes, remote_sig: bytes) -> None:
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -82,7 +82,7 @@ if TYPE_CHECKING:
            from .simple_config import SimpleConfig
        
        
       -SAVED_PR_STATUS = [PR_PAID, PR_UNPAID, PR_INFLIGHT] # status that are persisted
       +SAVED_PR_STATUS = [PR_PAID, PR_UNPAID] # status that are persisted
        
        
        NUM_PEERS_TARGET = 4
       t@@ -583,6 +583,9 @@ class LNWallet(LNWorker):
                self.pending_payments = defaultdict(asyncio.Future)  # type: Dict[bytes, asyncio.Future[BarePaymentAttemptLog]]
        
                self.swap_manager = SwapManager(wallet=self.wallet, lnworker=self)
       +        # detect inflight payments
       +        for payment_hash in self.get_payments(status='inflight').keys():
       +            self.set_invoice_status(payment_hash.hex(), PR_INFLIGHT)
        
            @property
            def channels(self) -> Mapping[bytes, Channel]:
       t@@ -672,12 +675,12 @@ class LNWallet(LNWorker):
                    util.trigger_callback('channel', self.wallet, chan)
                super().peer_closed(peer)
        
       -    def get_settled_payments(self):
       +    def get_payments(self, *, status=None):
                # return one item per payment_hash
                # note: with AMP we will have several channels per payment
                out = defaultdict(list)
                for chan in self.channels.values():
       -            d = chan.get_settled_payments()
       +            d = chan.get_payments(status=status)
                    for k, v in d.items():
                        out[k] += v
                return out
       t@@ -685,19 +688,19 @@ class LNWallet(LNWorker):
            def get_payment_value(self, info: Optional['PaymentInfo'], plist):
                amount_msat = 0
                fee_msat = None
       -        for chan_id, htlc, _direction in plist:
       +        for chan_id, htlc, _direction, _status in plist:
                    amount_msat += int(_direction) * htlc.amount_msat
                    if _direction == SENT and info and info.amount_msat:
                        fee_msat = (fee_msat or 0) - info.amount_msat - amount_msat
       -        timestamp = min([htlc.timestamp for chan_id, htlc, _direction in plist])
       +        timestamp = min([htlc.timestamp for chan_id, htlc, _direction, _status in plist])
                return amount_msat, fee_msat, timestamp
        
            def get_lightning_history(self):
                out = {}
       -        for key, plist in self.get_settled_payments().items():
       +        for payment_hash, plist in self.get_payments(status='settled').items():
                    if len(plist) == 0:
                        continue
       -            payment_hash = bytes.fromhex(key)
       +            key = payment_hash.hex()
                    info = self.get_payment_info(payment_hash)
                    amount_msat, fee_msat, timestamp = self.get_payment_value(info, plist)
                    if info is not None:
       t@@ -766,15 +769,16 @@ class LNWallet(LNWorker):
                    }
                    out[closing_txid] = item
                # add info about submarine swaps
       -        settled_payments = self.get_settled_payments()
       +        settled_payments = self.get_payments(status='settled')
                current_height = self.wallet.get_local_height()
                for payment_hash_hex, swap in self.swap_manager.swaps.items():
                    txid = swap.spending_txid if swap.is_reverse else swap.funding_txid
                    if txid is None:
                        continue
       -            if payment_hash_hex in settled_payments:
       -                plist = settled_payments[payment_hash_hex]
       -                info = self.get_payment_info(bytes.fromhex(payment_hash_hex))
       +            payment_hash = bytes.fromhex(payment_hash_hex)
       +            if payment_hash in settled_payments:
       +                plist = settled_payments[payment_hash]
       +                info = self.get_payment_info(payment_hash)
                        amount_msat, fee_msat, timestamp = self.get_payment_value(info, plist)
                    else:
                        amount_msat = 0