@@ -426,7 +426,8 @@ def txs_htlc(
426426 privkey = our_localdelayed_privkey .get_secret_bytes (),
427427 is_revocation = False ,
428428 ):
429- txs [actual_htlc_tx .txid () + f':{ output_idx } ' ] = SweepInfo (
429+ prevout = actual_htlc_tx .txid () + f':{ output_idx } '
430+ txs [prevout ] = SweepInfo (
430431 name = f'second-stage-htlc:{ output_idx } ' ,
431432 cltv_abs = 0 ,
432433 txin = sweep_txin ,
@@ -451,23 +452,12 @@ def txs_htlc(
451452 # note: it is the first stage (witness of htlc_tx) that reveals the preimage,
452453 # so if we are already in second stage, it is already revealed.
453454 # However, here, we don't make a distinction.
454- if not chan .lnworker .is_complete_mpp (htlc .payment_hash ):
455- # - do not redeem this, it might publish the preimage of an incomplete MPP
456- # - OTOH maybe this chan just got closed, and we are still receiving new htlcs
457- # for this MPP set. So the MPP set might still transition to complete!
458- # The MPP_TIMEOUT is only around 2 minutes, so this window is short.
459- # The default keep_watching logic in lnwatcher is sufficient to call us again.
460- continue
461- if htlc .payment_hash in chan .lnworker .dont_settle_htlcs :
462- prevout = ctx .txid () + ':%d' % ctx_output_idx
463- txs [prevout ] = KeepWatchingTXO (
464- name = f"our_ctx_htlc_{ ctx_output_idx } _for_hold_invoice" ,
465- until_height = htlc .cltv_abs ,
466- )
467- continue
468- preimage = chan .lnworker .get_preimage (htlc .payment_hash )
455+ preimage = _maybe_reveal_preimage_for_htlc (
456+ chan = chan , htlc = htlc , txs = txs ,
457+ prevout = ctx .txid () + ':%d' % ctx_output_idx ,
458+ sweep_info_name = f"our_ctx_htlc_{ ctx_output_idx } " ,
459+ )
469460 if not preimage :
470- # we might not have the preimage if this is a hold invoice
471461 continue
472462 try :
473463 txs_htlc (
@@ -481,6 +471,32 @@ def txs_htlc(
481471 return txs
482472
483473
474+ def _maybe_reveal_preimage_for_htlc (
475+ * ,
476+ chan : 'AbstractChannel' ,
477+ htlc : 'UpdateAddHtlc' ,
478+ txs : Dict [str , MaybeSweepInfo ], # mutated in-place!
479+ prevout : str , # commitment txid + output_idx (so always for first stage)
480+ sweep_info_name : str ,
481+ ) -> Optional [bytes ]:
482+ """Given a Remote-added-HTLC, return the preimage if it's okay to reveal it on-chain."""
483+ if not chan .lnworker .is_complete_mpp (htlc .payment_hash ):
484+ # - do not redeem this, it might publish the preimage of an incomplete MPP
485+ # - OTOH maybe this chan just got closed, and we are still receiving new htlcs
486+ # for this MPP set. So the MPP set might still transition to complete!
487+ # The MPP_TIMEOUT is only around 2 minutes, so this window is short.
488+ # The default keep_watching logic in lnwatcher is sufficient to call us again.
489+ return None
490+ if htlc .payment_hash in chan .lnworker .dont_settle_htlcs :
491+ txs [prevout ] = KeepWatchingTXO (
492+ name = sweep_info_name + "_for_hold_invoice" ,
493+ until_height = htlc .cltv_abs ,
494+ )
495+ return None
496+ preimage = chan .lnworker .get_preimage (htlc .payment_hash )
497+ return preimage
498+
499+
484500def extract_ctx_secrets (chan : 'Channel' , ctx : Transaction ):
485501 # note: the remote sometimes has two valid non-revoked commitment transactions,
486502 # either of which could be broadcast
@@ -762,23 +778,12 @@ def tx_htlc(
762778 preimage = None
763779 is_received_htlc = direction == RECEIVED
764780 if not is_received_htlc and not is_revocation :
765- if not chan .lnworker .is_complete_mpp (htlc .payment_hash ):
766- # - do not redeem this, it might publish the preimage of an incomplete MPP
767- # - OTOH maybe this chan just got closed, and we are still receiving new htlcs
768- # for this MPP set. So the MPP set might still transition to complete!
769- # The MPP_TIMEOUT is only around 2 minutes, so this window is short.
770- # The default keep_watching logic in lnwatcher is sufficient to call us again.
771- continue
772- if htlc .payment_hash in chan .lnworker .dont_settle_htlcs :
773- prevout = ctx .txid () + ':%d' % ctx_output_idx
774- txs [prevout ] = KeepWatchingTXO (
775- name = f"their_ctx_htlc_{ ctx_output_idx } _for_hold_invoice" ,
776- until_height = htlc .cltv_abs ,
777- )
778- continue
779- preimage = chan .lnworker .get_preimage (htlc .payment_hash )
781+ preimage = _maybe_reveal_preimage_for_htlc (
782+ chan = chan , htlc = htlc , txs = txs ,
783+ prevout = ctx .txid () + ':%d' % ctx_output_idx ,
784+ sweep_info_name = f"their_ctx_htlc_{ ctx_output_idx } " ,
785+ )
780786 if not preimage :
781- # we might not have the preimage if this is a hold invoice
782787 continue
783788 tx_htlc (
784789 htlc = htlc ,
0 commit comments