协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
lightning-htlcs【开发与工程】HTLC (Hash Time-Locked Contract) lifecycle: offered/received, fulfill via preimage, fail/timeout, 2nd-stage txs. Modern variant PTLCs (Point Time-Locked Contracts) with adaptor signatures. USE WHEN: implementing payment forwarding, debugging stuck HTLCs, reasoning about CLTV deltas in routes.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
The atomicity primitive that makes Lightning routing work. An HTLC
is "claimable with preimage r such that hash(r) = H, OR refundable
after CLTV expires".
Alice → Bob: update_add_htlc(amount, hash, cltv, onion)
The onion contains routing data for Bob to forward.
Bob receives, peels onion, sees instructions to forward to Carol:
Bob → Carol: update_add_htlc(amount - bob_fee, hash, cltv - bob_delta, onion')
Continues until destination Dave (final hop, onion is empty payload).
Dave reveals preimage r:
Dave → Carol: update_fulfill_htlc(channel_id, htlc_id, r)
Carol → Bob: update_fulfill_htlc(...) (verifies hash(r) = H, propagates)
Bob → Alice: update_fulfill_htlc(...)
Each hop earns its fee (held in the HTLC amount).
Carol → Bob: update_fail_htlc(channel_id, htlc_id, encrypted_failure)
Bob → Alice: update_fail_htlc(...)
Failure encryption uses ChaCha20 with onion shared secrets, hop-by-hop (only Alice can decrypt the original failure).
If a peer is unresponsive past timeout → force-close that channel, HTLC resolved on chain.
The HTLC has CLTV expiry. As the payment travels:
cltv_expiry_delta parameter.This ordering ensures:
Standard cltv_expiry_delta: 40-100 blocks per hop typically.
For an HTLC the channel partner offered to you:
hash160(payment_hash) OP_EQUALVERIFY <local_htlc_pk> OP_CHECKSIG
OR
<cltv> OP_CHECKLOCKTIMEVERIFY OP_DROP <remote_htlc_pk> OP_CHECKSIG
OR
<revocation_pubkey> OP_CHECKSIG
Three paths: success (preimage), timeout (CLTV), revocation (cheating detection).
After force-close, HTLC outputs aren't directly spendable — they go through pre-signed 2nd-stage txs:
to_local-style with CSV delay before claimable by you.This 2-stage structure is required for legacy commitment because sigs are pre-committed at channel open (no flexibility post-broadcast).
option_anchors_zero_fee_htlc_tx: HTLC 2nd-stage txs are 0-fee at
sign time, fee-bumped via the commitment's anchors when broadcast.
Modern Schnorr/Taproot-based replacement for HTLCs:
hash(r) = H, use r * G = T (Schnorr point).T (derived per-hop), so
observers can't correlate hops by hash value.Status: BOLT update in flight; implementations partial.
Channel-level limits (advertised in accept_channel):
max_accepted_htlcs (default ~483).max_htlc_value_in_flight_msat.dust_limit_satoshis.Excess HTLCs cause update_add_htlc to fail.
Causes:
Recovery:
HTLC-timeout txs are pre-signed at channel open. An attacker could historically "pin" these via low-fee descendants → honest party can't broadcast → CLTV passes upstream → loss.
Post TRUC v3 + ephemeral anchors: pinning surface removed.