1
0

lnsweep: factor out "maybe_reveal_preimage_for_htlc"

no functional changes
This commit is contained in:
SomberNight
2025-11-28 15:55:35 +00:00
parent 125b7ee0d7
commit ef8fd03e8f

View File

@@ -417,7 +417,8 @@ def sweep_our_ctx(
privkey=our_localdelayed_privkey.get_secret_bytes(), privkey=our_localdelayed_privkey.get_secret_bytes(),
is_revocation=False, is_revocation=False,
): ):
txs[actual_htlc_tx.txid() + f':{output_idx}'] = SweepInfo( prevout = actual_htlc_tx.txid() + f':{output_idx}'
txs[prevout] = SweepInfo(
name=f'second-stage-htlc:{output_idx}', name=f'second-stage-htlc:{output_idx}',
cltv_abs=0, cltv_abs=0,
txin=sweep_txin, txin=sweep_txin,
@@ -437,18 +438,16 @@ def sweep_our_ctx(
subject=LOCAL, subject=LOCAL,
ctn=ctn) ctn=ctn)
for (direction, htlc), (ctx_output_idx, htlc_relative_idx) in htlc_to_ctx_output_idx_map.items(): for (direction, htlc), (ctx_output_idx, htlc_relative_idx) in htlc_to_ctx_output_idx_map.items():
preimage = None
if direction == RECEIVED: if direction == RECEIVED:
if not chan.lnworker.is_complete_mpp(htlc.payment_hash): # note: it is the first stage (witness of htlc_tx) that reveals the preimage,
# do not redeem this, it might publish the preimage of an incomplete MPP # so if we are already in second stage, it is already revealed.
continue # However, here, we don't make a distinction.
preimage = chan.lnworker.get_preimage(htlc.payment_hash) preimage = _maybe_reveal_preimage_for_htlc(
chan=chan, htlc=htlc,
)
if not preimage: if not preimage:
# we might not have the preimage if this is a hold invoice
continue continue
if htlc.payment_hash in chan.lnworker.dont_settle_htlcs:
continue
else:
preimage = None
try: try:
txs_htlc( txs_htlc(
htlc=htlc, htlc=htlc,
@@ -461,6 +460,25 @@ def sweep_our_ctx(
return txs return txs
def _maybe_reveal_preimage_for_htlc(
*,
chan: 'AbstractChannel',
htlc: 'UpdateAddHtlc',
) -> Optional[bytes]:
"""Given a Remote-added-HTLC, return the preimage if it's okay to reveal it on-chain."""
if not chan.lnworker.is_complete_mpp(htlc.payment_hash):
# - do not redeem this, it might publish the preimage of an incomplete MPP
# - OTOH maybe this chan just got closed, and we are still receiving new htlcs
# for this MPP set. So the MPP set might still transition to complete!
# The MPP_TIMEOUT is only around 2 minutes, so this window is short.
# The default keep_watching logic in lnwatcher is sufficient to call us again.
return None
if htlc.payment_hash in chan.lnworker.dont_settle_htlcs:
return None
preimage = chan.lnworker.get_preimage(htlc.payment_hash)
return preimage
def extract_ctx_secrets(chan: 'Channel', ctx: Transaction): def extract_ctx_secrets(chan: 'Channel', ctx: Transaction):
# note: the remote sometimes has two valid non-revoked commitment transactions, # note: the remote sometimes has two valid non-revoked commitment transactions,
# either of which could be broadcast # either of which could be broadcast
@@ -739,19 +757,14 @@ def sweep_their_ctx(
subject=REMOTE, subject=REMOTE,
ctn=ctn) ctn=ctn)
for (direction, htlc), (ctx_output_idx, htlc_relative_idx) in htlc_to_ctx_output_idx_map.items(): for (direction, htlc), (ctx_output_idx, htlc_relative_idx) in htlc_to_ctx_output_idx_map.items():
preimage = None
is_received_htlc = direction == RECEIVED is_received_htlc = direction == RECEIVED
if not is_received_htlc and not is_revocation: if not is_received_htlc and not is_revocation:
if not chan.lnworker.is_complete_mpp(htlc.payment_hash): preimage = _maybe_reveal_preimage_for_htlc(
# do not redeem this, it might publish the preimage of an incomplete MPP chan=chan, htlc=htlc,
continue )
preimage = chan.lnworker.get_preimage(htlc.payment_hash)
if not preimage: if not preimage:
# we might not have the preimage if this is a hold invoice
continue continue
if htlc.payment_hash in chan.lnworker.dont_settle_htlcs:
continue
else:
preimage = None
tx_htlc( tx_htlc(
htlc=htlc, htlc=htlc,
is_received_htlc=is_received_htlc, is_received_htlc=is_received_htlc,