URI: 
       tlnworker: rename 'invoices' to 'payments' when they can be in both directions - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 638de63f1378d280d0773540ca951cc326ca052c
   DIR parent 788d54f9a67d819a69e9e80186a45322b5559b55
  HTML Author: ThomasV <thomasv@electrum.org>
       Date:   Wed,  9 Oct 2019 20:16:11 +0200
       
       lnworker: rename 'invoices' to 'payments' when they can be in both directions
       
       Diffstat:
         M electrum/lnpeer.py                  |       6 +++---
         M electrum/lnworker.py                |      50 ++++++++++++++++----------------
         M electrum/tests/test_lnpeer.py       |      19 ++++++++-----------
         M electrum/wallet.py                  |       8 ++++----
       
       4 files changed, 40 insertions(+), 43 deletions(-)
       ---
   DIR diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py
       t@@ -1267,7 +1267,7 @@ class Peer(Logger):
                self.logger.info(f"on_update_fulfill_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}")
                chan.receive_htlc_settle(preimage, htlc_id)
                self.lnworker.save_preimage(payment_hash, preimage)
       -        self.lnworker.set_invoice_status(payment_hash, PR_PAID)
       +        self.lnworker.set_payment_status(payment_hash, PR_PAID)
                local_ctn = chan.get_latest_ctn(LOCAL)
                asyncio.ensure_future(self._on_update_fulfill_htlc(chan, htlc_id, preimage, local_ctn))
        
       t@@ -1406,7 +1406,7 @@ class Peer(Logger):
                await self.await_local(chan, local_ctn)
                await self.await_remote(chan, remote_ctn)
                try:
       -            info = self.lnworker.get_invoice_info(htlc.payment_hash)
       +            info = self.lnworker.get_payment_info(htlc.payment_hash)
                    preimage = self.lnworker.get_preimage(htlc.payment_hash)
                except UnknownPaymentHash:
                    reason = OnionRoutingFailureMessage(code=OnionFailureCode.INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS, data=b'')
       t@@ -1443,7 +1443,7 @@ class Peer(Logger):
                self.logger.info(f"_fulfill_htlc. chan {chan.short_channel_id}. htlc_id {htlc_id}")
                chan.settle_htlc(preimage, htlc_id)
                payment_hash = sha256(preimage)
       -        self.lnworker.set_invoice_status(payment_hash, PR_PAID)
       +        self.lnworker.set_payment_status(payment_hash, PR_PAID)
                remote_ctn = chan.get_latest_ctn(REMOTE)
                self.send_message("update_fulfill_htlc",
                                  channel_id=chan.channel_id,
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -85,7 +85,7 @@ encoder = ChannelJsonEncoder()
        
        from typing import NamedTuple
        
       -class InvoiceInfo(NamedTuple):
       +class PaymentInfo(NamedTuple):
            payment_hash: bytes
            amount: int
            direction: int
       t@@ -324,7 +324,7 @@ class LNWallet(LNWorker):
                LNWorker.__init__(self, xprv)
                self.ln_keystore = keystore.from_xprv(xprv)
                self.localfeatures |= LnLocalFeatures.OPTION_DATA_LOSS_PROTECT_REQ
       -        self.invoices = self.storage.get('lightning_invoices2', {})        # RHASH -> amount, direction, is_paid
       +        self.payments = self.storage.get('lightning_payments', {})        # RHASH -> amount, direction, is_paid
                self.preimages = self.storage.get('lightning_preimages', {})      # RHASH -> preimage
                self.sweep_address = wallet.get_receiving_address()
                self.lock = threading.RLock()
       t@@ -481,7 +481,7 @@ class LNWallet(LNWorker):
                        label = self.wallet.get_label(key)
                        if _direction == SENT:
                            try:
       -                        inv = self.get_invoice_info(bfh(key))
       +                        inv = self.get_payment_info(bfh(key))
                                fee_msat = inv.amount*1000 - amount_msat if inv.amount else None
                            except UnknownPaymentHash:
                                fee_msat = None
       t@@ -850,11 +850,11 @@ class LNWallet(LNWorker):
                lnaddr = lndecode(invoice, expected_hrp=constants.net.SEGWIT_HRP)
                key = bh2u(lnaddr.paymenthash)
                amount = int(lnaddr.amount * COIN) if lnaddr.amount else None
       -        status = self.get_invoice_status(lnaddr.paymenthash)
       +        status = self.get_payment_status(lnaddr.paymenthash)
                if status == PR_PAID:
                    raise PaymentFailure(_("This invoice has been paid already"))
       -        info = InvoiceInfo(lnaddr.paymenthash, amount, SENT, PR_UNPAID)
       -        self.save_invoice_info(info)
       +        info = PaymentInfo(lnaddr.paymenthash, amount, SENT, PR_UNPAID)
       +        self.save_payment_info(info)
                self._check_invoice(invoice, amount_sat)
                self.wallet.set_label(key, lnaddr.get_description())
                for i in range(attempts):
       t@@ -870,12 +870,12 @@ class LNWallet(LNWorker):
                if not chan:
                    raise Exception(f"PathFinder returned path with short_channel_id "
                                    f"{short_channel_id} that is not in channel list")
       -        self.set_invoice_status(lnaddr.paymenthash, PR_INFLIGHT)
       +        self.set_payment_status(lnaddr.paymenthash, PR_INFLIGHT)
                peer = self.peers[route[0].node_id]
                htlc = await peer.pay(route, chan, int(lnaddr.amount * COIN * 1000), lnaddr.paymenthash, lnaddr.get_min_final_cltv_expiry())
                self.network.trigger_callback('htlc_added', htlc, lnaddr, SENT)
                success = await self.pending_payments[(short_channel_id, htlc.htlc_id)]
       -        self.set_invoice_status(lnaddr.paymenthash, (PR_PAID if success else PR_UNPAID))
       +        self.set_payment_status(lnaddr.paymenthash, (PR_PAID if success else PR_UNPAID))
                return success
        
            @staticmethod
       t@@ -968,7 +968,7 @@ class LNWallet(LNWorker):
                                     "Other clients will likely not be able to send to us.")
                payment_preimage = os.urandom(32)
                payment_hash = sha256(payment_preimage)
       -        info = InvoiceInfo(payment_hash, amount_sat, RECEIVED, PR_UNPAID)
       +        info = PaymentInfo(payment_hash, amount_sat, RECEIVED, PR_UNPAID)
                amount_btc = amount_sat/Decimal(COIN) if amount_sat else None
                lnaddr = LnAddr(payment_hash, amount_btc,
                                tags=[('d', message),
       t@@ -988,7 +988,7 @@ class LNWallet(LNWorker):
                    'invoice': invoice
                }
                self.save_preimage(payment_hash, payment_preimage)
       -        self.save_invoice_info(info)
       +        self.save_payment_info(info)
                self.wallet.add_payment_request(req)
                self.wallet.set_label(key, message)
                return key
       t@@ -1002,36 +1002,36 @@ class LNWallet(LNWorker):
            def get_preimage(self, payment_hash: bytes) -> bytes:
                return bfh(self.preimages.get(bh2u(payment_hash)))
        
       -    def get_invoice_info(self, payment_hash: bytes) -> bytes:
       +    def get_payment_info(self, payment_hash: bytes) -> bytes:
                key = payment_hash.hex()
                with self.lock:
       -            if key not in self.invoices:
       +            if key not in self.payments:
                        raise UnknownPaymentHash(payment_hash)
       -            amount, direction, status = self.invoices[key]
       -            return InvoiceInfo(payment_hash, amount, direction, status)
       +            amount, direction, status = self.payments[key]
       +            return PaymentInfo(payment_hash, amount, direction, status)
        
       -    def save_invoice_info(self, info):
       +    def save_payment_info(self, info):
                key = info.payment_hash.hex()
                with self.lock:
       -            self.invoices[key] = info.amount, info.direction, info.status
       -        self.storage.put('lightning_invoices2', self.invoices)
       +            self.payments[key] = info.amount, info.direction, info.status
       +        self.storage.put('lightning_payments', self.payments)
                self.storage.write()
        
       -    def get_invoice_status(self, payment_hash):
       +    def get_payment_status(self, payment_hash):
                try:
       -            info = self.get_invoice_info(payment_hash)
       +            info = self.get_payment_info(payment_hash)
                    return info.status
                except UnknownPaymentHash:
                    return PR_UNKNOWN
        
       -    def set_invoice_status(self, payment_hash: bytes, status):
       +    def set_payment_status(self, payment_hash: bytes, status):
                try:
       -            info = self.get_invoice_info(payment_hash)
       +            info = self.get_payment_info(payment_hash)
                except UnknownPaymentHash:
                    # if we are forwarding
                    return
                info = info._replace(status=status)
       -        self.save_invoice_info(info)
       +        self.save_payment_info(info)
                if info.direction == RECEIVED and info.status == PR_PAID:
                    self.network.trigger_callback('payment_received', self.wallet, bh2u(payment_hash), PR_PAID)
        
       t@@ -1077,13 +1077,13 @@ class LNWallet(LNWorker):
                                                 cltv_expiry_delta)]))
                return routing_hints
        
       -    def delete_invoice(self, payment_hash_hex: str):
       +    def delete_payment(self, payment_hash_hex: str):
                try:
                    with self.lock:
       -                del self.invoices[payment_hash_hex]
       +                del self.payments[payment_hash_hex]
                except KeyError:
                    return
       -        self.storage.put('lightning_invoices', self.invoices)
       +        self.storage.put('lightning_payments', self.payments)
                self.storage.write()
        
            def get_balance(self):
   DIR diff --git a/electrum/tests/test_lnpeer.py b/electrum/tests/test_lnpeer.py
       t@@ -21,7 +21,7 @@ from electrum.channel_db import ChannelDB
        from electrum.lnworker import LNWallet
        from electrum.lnmsg import encode_msg, decode_msg
        from electrum.logging import console_stderr_handler
       -from electrum.lnworker import InvoiceInfo, RECEIVED, PR_UNPAID
       +from electrum.lnworker import PaymentInfo, RECEIVED, PR_UNPAID
        
        from .test_lnchannel import create_test_channels
        from . import ElectrumTestCase
       t@@ -88,8 +88,7 @@ class MockLNWallet:
                self.node_keypair = local_keypair
                self.network = MockNetwork(tx_queue)
                self.channels = {self.chan.channel_id: self.chan}
       -        self.invoices = {}
       -        self.inflight = {}
       +        self.payments = {}
                self.wallet = MockWallet()
                self.localfeatures = LnLocalFeatures(0)
                self.pending_payments = defaultdict(asyncio.Future)
       t@@ -120,13 +119,11 @@ class MockLNWallet:
            def on_channels_updated(self):
                pass
        
       -    def save_invoice(*args, is_paid=False):
       -        pass
       -
            preimages = {}
       -    get_invoice_info = LNWallet.get_invoice_info
       -    save_invoice_info = LNWallet.save_invoice_info
       -    set_invoice_status = LNWallet.set_invoice_status
       +    get_payment_info = LNWallet.get_payment_info
       +    save_payment_info = LNWallet.save_payment_info
       +    set_payment_status = LNWallet.set_payment_status
       +    get_payment_status = LNWallet.get_payment_status
            save_preimage = LNWallet.save_preimage
            get_preimage = LNWallet.get_preimage
            _create_route_from_invoice = LNWallet._create_route_from_invoice
       t@@ -216,9 +213,9 @@ class TestPeer(ElectrumTestCase):
                amount_btc = amount_sat/Decimal(COIN)
                payment_preimage = os.urandom(32)
                RHASH = sha256(payment_preimage)
       -        info = InvoiceInfo(RHASH, amount_sat, RECEIVED, PR_UNPAID)
       +        info = PaymentInfo(RHASH, amount_sat, RECEIVED, PR_UNPAID)
                w2.save_preimage(RHASH, payment_preimage)
       -        w2.save_invoice_info(info)
       +        w2.save_payment_info(info)
                lnaddr = LnAddr(
                            RHASH,
                            amount_btc,
   DIR diff --git a/electrum/wallet.py b/electrum/wallet.py
       t@@ -574,7 +574,7 @@ class Abstract_Wallet(AddressSynchronizer):
                if request_type == PR_TYPE_ONCHAIN:
                    item['status'] = PR_PAID if item.get('txid') is not None else PR_UNPAID
                elif self.lnworker and request_type == PR_TYPE_LN:
       -            item['status'] = self.lnworker.get_invoice_status(bfh(item['rhash']))
       +            item['status'] = self.lnworker.get_payment_status(bfh(item['rhash']))
                else:
                    return
                self.check_if_expired(item)
       t@@ -1367,7 +1367,7 @@ class Abstract_Wallet(AddressSynchronizer):
                    if conf is not None:
                        req['confirmations'] = conf
                elif self.lnworker and _type == PR_TYPE_LN:
       -            req['status'] = self.lnworker.get_invoice_status(bfh(key))
       +            req['status'] = self.lnworker.get_payment_status(bfh(key))
                else:
                    return
                self.check_if_expired(req)
       t@@ -1443,7 +1443,7 @@ class Abstract_Wallet(AddressSynchronizer):
                if key in self.receive_requests:
                    self.remove_payment_request(key)
                elif self.lnworker:
       -            self.lnworker.delete_invoice(key)
       +            self.lnworker.delete_payment(key)
        
            def delete_invoice(self, key):
                """ lightning or on-chain """
       t@@ -1451,7 +1451,7 @@ class Abstract_Wallet(AddressSynchronizer):
                    self.invoices.pop(key)
                    self.storage.put('invoices', self.invoices)
                elif self.lnworker:
       -            self.lnworker.delete_invoice(key)
       +            self.lnworker.delete_payment(key)
        
            def remove_payment_request(self, addr):
                if addr not in self.receive_requests: