tremove UnknownPaymentHash (exception used as value) - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit abe1bece2c1ecaa096b7bdf8f437580c50bb0eeb DIR parent a5a5048d53e8db4c5ded6b9b5525c242c32bd445 HTML Author: ThomasV <thomasv@electrum.org> Date: Sat, 2 May 2020 22:27:28 +0200 remove UnknownPaymentHash (exception used as value) Diffstat: M electrum/lnpeer.py | 9 ++++----- M electrum/lnsweep.py | 14 +++----------- M electrum/lnutil.py | 1 - M electrum/lnworker.py | 29 ++++++++++------------------- 4 files changed, 17 insertions(+), 36 deletions(-) --- DIR diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py t@@ -38,7 +38,7 @@ from .lnutil import (Outpoint, LocalConfig, RECEIVED, UpdateAddHtlc, funding_output_script, get_per_commitment_secret_from_seed, secret_to_pubkey, PaymentFailure, LnFeatures, LOCAL, REMOTE, HTLCOwner, generate_keypair, LnKeyFamily, - ln_compare_features, privkey_to_pubkey, UnknownPaymentHash, MIN_FINAL_CLTV_EXPIRY_ACCEPTED, + ln_compare_features, privkey_to_pubkey, MIN_FINAL_CLTV_EXPIRY_ACCEPTED, LightningPeerConnectionClosed, HandshakeFailed, NotFoundChanAnnouncementForUpdate, MINIMUM_MAX_HTLC_VALUE_IN_FLIGHT_ACCEPTED, MAXIMUM_HTLC_MINIMUM_MSAT_ACCEPTED, MAXIMUM_REMOTE_TO_SELF_DELAY_ACCEPTED, RemoteMisbehaving, DEFAULT_TO_SELF_DELAY, t@@ -1218,12 +1218,11 @@ class Peer(Logger): def maybe_fulfill_htlc(self, *, chan: Channel, htlc: UpdateAddHtlc, onion_packet: OnionPacket, processed_onion: ProcessedOnionPacket, ) -> Tuple[Optional[bytes], Optional[OnionRoutingFailureMessage]]: - try: - info = self.lnworker.get_payment_info(htlc.payment_hash) - preimage = self.lnworker.get_preimage(htlc.payment_hash) - except UnknownPaymentHash: + info = self.lnworker.get_payment_info(htlc.payment_hash) + if info is None: reason = OnionRoutingFailureMessage(code=OnionFailureCode.INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS, data=b'') return None, reason + preimage = self.lnworker.get_preimage(htlc.payment_hash) try: payment_secret_from_onion = processed_onion.hop_data.payload["payment_data"]["payment_secret"] except: DIR diff --git a/electrum/lnsweep.py b/electrum/lnsweep.py t@@ -11,7 +11,7 @@ from . import ecc from .lnutil import (make_commitment_output_to_remote_address, make_commitment_output_to_local_witness_script, derive_privkey, derive_pubkey, derive_blinded_pubkey, derive_blinded_privkey, make_htlc_tx_witness, make_htlc_tx_with_open_channel, UpdateAddHtlc, - LOCAL, REMOTE, make_htlc_output_witness_script, UnknownPaymentHash, + LOCAL, REMOTE, make_htlc_output_witness_script, get_ordered_channel_configs, privkey_to_pubkey, get_per_commitment_secret_from_seed, RevocationStore, extract_ctn_from_tx_and_chan, UnableToDeriveSecret, SENT, RECEIVED, map_htlcs_to_ctx_output_idxs, Direction) t@@ -232,11 +232,7 @@ def create_sweeptxs_for_our_ctx(*, chan: 'AbstractChannel', ctx: Transaction, def create_txns_for_htlc(*, htlc: 'UpdateAddHtlc', htlc_direction: Direction, ctx_output_idx: int, htlc_relative_idx: int): if htlc_direction == RECEIVED: - try: - preimage = chan.lnworker.get_preimage(htlc.payment_hash) - except UnknownPaymentHash as e: - _logger.info(f'trying to sweep htlc from our latest ctx but getting {repr(e)}') - return + preimage = chan.lnworker.get_preimage(htlc.payment_hash) else: preimage = None htlctx_witness_script, htlc_tx = create_htlctx_that_spends_from_our_ctx( t@@ -375,11 +371,7 @@ def create_sweeptxs_for_their_ctx(*, chan: 'Channel', ctx: Transaction, def create_sweeptx_for_htlc(htlc: 'UpdateAddHtlc', is_received_htlc: bool, ctx_output_idx: int) -> None: if not is_received_htlc and not is_revocation: - try: - preimage = chan.lnworker.get_preimage(htlc.payment_hash) - except UnknownPaymentHash as e: - _logger.info(f'trying to sweep htlc from their latest ctx but getting {repr(e)}') - return + preimage = chan.lnworker.get_preimage(htlc.payment_hash) else: preimage = None htlc_output_witness_script = make_htlc_output_witness_script( DIR diff --git a/electrum/lnutil.py b/electrum/lnutil.py t@@ -245,7 +245,6 @@ class LightningPeerConnectionClosed(LightningError): pass class UnableToDeriveSecret(LightningError): pass class HandshakeFailed(LightningError): pass class ConnStringFormatError(LightningError): pass -class UnknownPaymentHash(LightningError): pass class RemoteMisbehaving(LightningError): pass class NotFoundChanAnnouncementForUpdate(Exception): pass DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py t@@ -50,7 +50,7 @@ from .lnutil import (Outpoint, LNPeerAddr, get_compressed_pubkey_from_bech32, extract_nodeid, PaymentFailure, split_host_port, ConnStringFormatError, generate_keypair, LnKeyFamily, LOCAL, REMOTE, - UnknownPaymentHash, MIN_FINAL_CLTV_EXPIRY_FOR_INVOICE, + MIN_FINAL_CLTV_EXPIRY_FOR_INVOICE, NUM_MAX_EDGES_IN_PAYMENT_PATH, SENT, RECEIVED, HTLCOwner, UpdateAddHtlc, Direction, LnFeatures, ShortChannelID, PaymentAttemptLog, PaymentAttemptFailureDetails, t@@ -606,11 +606,8 @@ class LNWallet(LNWorker): timestamp = htlc.timestamp label = self.wallet.get_label(key) if _direction == SENT: - try: - inv = self.get_payment_info(bfh(key)) - fee_msat = - inv.amount*1000 - amount_msat if inv.amount else None - except UnknownPaymentHash: - fee_msat = None + info = self.get_payment_info(bfh(key)) + fee_msat = - info.amount*1000 - amount_msat if info and info.amount else None else: fee_msat = None else: t@@ -1114,10 +1111,9 @@ class LNWallet(LNWorker): def get_payment_info(self, payment_hash: bytes) -> PaymentInfo: key = payment_hash.hex() with self.lock: - if key not in self.payments: - raise UnknownPaymentHash(payment_hash) - amount, direction, status = self.payments[key] - return PaymentInfo(payment_hash, amount, direction, status) + if key in self.payments: + amount, direction, status = self.payments[key] + return PaymentInfo(payment_hash, amount, direction, status) def save_payment_info(self, info: PaymentInfo) -> None: key = info.payment_hash.hex() t@@ -1127,12 +1123,8 @@ class LNWallet(LNWorker): self.wallet.save_db() def get_payment_status(self, payment_hash): - try: - info = self.get_payment_info(payment_hash) - status = info.status - except UnknownPaymentHash: - status = PR_UNPAID - return status + info = self.get_payment_info(payment_hash) + return info.status if info else PR_UNPAID def get_invoice_status(self, key): log = self.logs[key] t@@ -1158,9 +1150,8 @@ class LNWallet(LNWorker): return payment_attempt def set_payment_status(self, payment_hash: bytes, status): - try: - info = self.get_payment_info(payment_hash) - except UnknownPaymentHash: + info = self.get_payment_info(payment_hash) + if info is None: # if we are forwarding return info = info._replace(status=status)