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.
What this skill does
# HTLCs (Hash Time-Locked Contracts) 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". ## Lifecycle ### Sender → first hop ``` Alice → Bob: update_add_htlc(amount, hash, cltv, onion) ``` The onion contains routing data for Bob to forward. ### Forward chain 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). ### Settlement (cooperative) 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). ### Settlement (failed) ``` 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). ### Settlement (force-close) If a peer is unresponsive past timeout → force-close that channel, HTLC resolved on chain. ## CLTV deltas The HTLC has CLTV expiry. As the payment travels: - Sender's HTLC has the highest CLTV. - Each forwarder reduces CLTV by their `cltv_expiry_delta` parameter. - Receiver's HTLC has the smallest CLTV. This ordering ensures: - Receiver claims at low CLTV. - Forwarders have time to claim from upstream. - If anything goes wrong, refunds happen in safe order. Standard `cltv_expiry_delta`: 40-100 blocks per hop typically. ## HTLC scripts on chain (post force-close) 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). ## 2nd-stage HTLC txs After force-close, HTLC outputs aren't directly spendable — they go through pre-signed 2nd-stage txs: - **HTLC-success-tx**: spends HTLC output revealing preimage; output is `to_local`-style with CSV delay before claimable by you. - **HTLC-timeout-tx**: spends HTLC output after CLTV; same CSV-delayed output. This 2-stage structure is required for legacy commitment because sigs are pre-committed at channel open (no flexibility post-broadcast). ## Anchor zero-fee variant `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. ## PTLCs (Point Time-Locked Contracts) Modern Schnorr/Taproot-based replacement for HTLCs: - Instead of `hash(r) = H`, use `r * G = T` (Schnorr point). - Use **adaptor signatures**: each hop holds a pre-signature conditional on revealing the secret. - Privacy gain: each hop sees a different `T` (derived per-hop), so observers can't correlate hops by hash value. Status: BOLT update in flight; implementations partial. ## In-flight HTLC limits 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. ## "Stuck" HTLCs Causes: - Downstream node unresponsive (route stuck mid-forward). - Mempool fee spike preventing on-chain resolution. - Channel partner offline at force-close time. Recovery: - Watchtower or self-monitoring + force-close to reclaim funds. - Standard fix: wait for CLTV expiry, force-close the upstream channel, recover via 2nd-stage timeout tx. ## Replacement cycling impact 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. ## Common bugs - CLTV delta too small → upstream HTLC times out before downstream resolves → forced refund and lost fees. - Race condition: peer reveals preimage just before CLTV → both parties try to claim, possible loss. - Misencoded onion failure → hop can't decrypt; routing data lost. - Custom records (BOLT 4 TLV) incorrectly formatted → some impls reject HTLCs. ## See also - [channels/SKILL.md](../channels/SKILL.md) - [routing/SKILL.md](../routing/SKILL.md) - [onion/SKILL.md](../onion/SKILL.md) - [replacement-cycling/SKILL.md](../replacement-cycling/SKILL.md) - [../../cryptography/adaptor-sigs/SKILL.md](../../cryptography/adaptor-sigs/SKILL.md)
Related in Sales & CRM
process-mapper
IncludedUse when a BizOps lead, COO, or process-improvement owner needs to document an end-to-end business process (procurement, employee onboarding, incident handoff, customer-onboarding, claims adjudication) in BPMN-style notation, measure cycle times by stage, surface where work spends most of its time waiting vs. being worked, and quantify the gap between processing time and total elapsed time. Pairs Lean / Six Sigma / Theory-of-Constraints canon with deterministic stdlib-only Python tools to produce a process map, a ranked bottleneck list (with severity + root-cause hypothesis), and a cycle-time analysis (P50, P90, value-add ratio, Little's-Law throughput). Distinct from sales-pipeline, system-reliability (SLO), and strategic-OKR work — this is tactical process documentation for internal operations.
payment-integration
IncludedIntegrate payments with SePay (VietQR), Polar, Stripe, Paddle (MoR subscriptions), Creem.io (licensing). Checkout, webhooks, subscriptions, QR codes, multi-provider orders.
customer-success-manager
IncludedMonitors customer health, predicts churn risk, and identifies expansion opportunities using weighted scoring models for SaaS customer success
sales-engineer
IncludedAnalyzes RFP/RFI responses for coverage gaps, builds competitive feature comparison matrices, and plans proof-of-concept (POC) engagements for pre-sales engineering. Use when responding to RFPs, bids, or proposal requests; comparing product features against competitors; planning or scoring a customer POC or sales demo; preparing a technical proposal; or performing win/loss competitor analysis. Handles tasks described as 'RFP response', 'bid response', 'proposal response', 'competitor comparison', 'feature matrix', 'POC planning', 'sales demo prep', or 'pre-sales engineering'.
customer-success-manager
IncludedMonitors customer health, predicts churn risk, and identifies expansion opportunities using weighted scoring models for SaaS customer success
sales-engineer
IncludedAnalyzes RFP/RFI responses for coverage gaps, builds competitive feature comparison matrices, and plans proof-of-concept (POC) engagements for pre-sales engineering. Use when responding to RFPs, bids, or proposal requests; comparing product features against competitors; planning or scoring a customer POC or sales demo; preparing a technical proposal; or performing win/loss competitor analysis. Handles tasks described as 'RFP response', 'bid response', 'proposal response', 'competitor comparison', 'feature matrix', 'POC planning', 'sales demo prep', or 'pre-sales engineering'.