An HTLC, in full
A hashed time-locked contract (HTLC) is the promise that lets a payment cross a channel between two parties who do not trust each other. This lesson follows one HTLC from offer to claim. It condenses the HTLC sections of chapter 8 of Mastering the Lightning Network.
- 01 Channels are not accounts
- — What a cryptographic hash function does
- — What an absolute block-height time lock is
- State the two conditions under which an HTLC pays out
- Check whether a forwarded HTLC has a valid expiry
- Say which transaction claims an HTLC output in each direction on a unilateral close
An HTLC is an offer from one side of a channel to the other. Before a block height, only the receiver can claim the money, and only by showing a secret. From that height on, the sender can take the money back, and the receiver can still claim until the transaction that takes the money back confirms. Because one secret unlocks every channel in a route and each node’s deadline falls before its upstream node’s, a payment releases or returns every hop, as long as each node can enforce its HTLC on-chain in time. Very small HTLCs, below a size set by the protocol, cannot be enforced on-chain, so a node cannot rely on them.
The hash lock
The recipient picks a secret, the payment preimage, and puts its hash in the invoice. BOLT 11 requires the writer of an invoice to set payment_hash to the SHA-256 hash of the payment_preimage that it will give in return for payment. Anyone can check the hash, and nobody who lacks the preimage can practically produce it. A node that receives update_fulfill_htlc must check that the preimage hashes to the HTLC’s payment_hash. If it does not, BOLT 2 tells the node to send a warning and close the connection, or to send an error and fail the channel.
The preimage is also the receipt. Whoever holds it can show that the HTLC with that hash was paid.
The time lock
Each HTLC carries a cltv_expiry, an absolute block height. BOLT 2 requires a sender to set it below 500 000 000, because a value from that number up would mean a time in seconds, and the protocol supports only block heights. An HTLC has timed out once the latest block’s height is equal to or greater than the cltv_expiry.
The two locks give two outcomes. Before the expiry, only the receiver can claim, with the preimage. From the expiry on, the sender can take the HTLC back, and the receiver can still claim with the preimage until the sender’s timeout transaction confirms. BOLT 2 says: “Once an HTLC has timed out, it can either be fulfilled or timed-out.” Neither outcome needs the other party to cooperate, because the bitcoin transactions that enforce them are already signed.
One hop, in order
On one channel, an HTLC passes through these states.
- The sender offers it with
update_add_htlc, which carries the amount, thepayment_hash, thecltv_expiryand an onion packet. - The two nodes exchange
commitment_signedandrevoke_and_ack, until both commitment transactions contain the HTLC and the earlier ones are revoked. BOLT 2 calls this state irrevocably committed. - The receiver ends the HTLC in one of two ways. It sends
update_fulfill_htlcwith the preimage, or it sendsupdate_fail_htlcif it cannot forward the payment or the HTLC has timed out. - A second round of
commitment_signedandrevoke_and_ackremoves the HTLC. After a fulfill, the amount has moved to the receiver’s side. After a fail, every balance is as it was.
A node can remove only an HTLC that the other node added. A node must not send a fulfill or a fail until the HTLC is irrevocably committed on both sides.
Across a route
The sender builds one onion. In the usual case, without a blinded path, each hop learns only what its layer of the onion holds: the next channel, amt_to_forward and outgoing_cltv_value. BOLT 2 sets the order of events at each forwarding node:
- The node must not offer the outgoing HTLC until the incoming HTLC is irrevocably committed.
- The node must not fail the incoming HTLC until the outgoing HTLC’s removal is irrevocably committed, or until the outgoing output has been spent on-chain by an HTLC-timeout transaction that is deep enough.
- On receiving the preimage, from a fulfill or from an on-chain spend, the node must fulfill the incoming HTLC.
BOLT 4 sets the rule for the expiry. A forwarding node returns an error if the incoming cltv_expiry, less its own cltv_expiry_delta for the outgoing channel, is below the outgoing_cltv_value in the onion. So the incoming expiry must be later than the outgoing expiry by at least the delta.
cltv_expiry - cltv_expiry_delta >= outgoing_cltv_value
The delta is the node’s own number, advertised in its channel_update for the outgoing channel (BOLT 7).1 At the last hop, BOLT 4 requires the incoming expiry to be at least the current block height plus min_final_cltv_expiry_delta, and BOLT 11 sets that to 18 when the invoice leaves it out. The next table is a worked example with round numbers. A pays D through B and C, and no real node is involved. D’s invoice uses the default of 18. C advertises a delta of 80 on its channel to D, and B advertises 40 on its channel to C. H is the block height when A builds the payment.
| HTLC | Expiry | Check at the node that receives it |
|---|---|---|
| C offers to D | H+18 | D requires at least its own height + 18, which is H+18 when D’s height is H |
| B offers to C | H+98 | C: 98 - 80 = 18, which is at least 18 |
| A offers to B | H+138 | B: 138 - 40 = 98, which is at least 98 |
The expiries fall by one delta per hop, from A to D. If D never claims, the HTLC to D expires first, then C’s, then B’s, so each node can take back the HTLC it offered before the node upstream of it can take back the HTLC that it offered.
The refund time lock differs on each HTLC along the path. To unwind a failed payment in order, each hop waits a little less for its refund than the hop before it. The gap between two hops is the
cltv_expiry_delta, which each node sets and advertises to the network.
The delta also gives each node room to act. BOLT 2 explains why. After the downstream peer claims, the node needs enough blocks to see the preimage and claim from its upstream peer, even if a peer is slow or the chain reorganizes. BOLT 2 works out the worst case as 3R+2G+2S blocks, for a reorganization depth R, a grace period G and a transaction delay S.
If a peer stops answering: the on-chain transactions
Each HTLC that is large enough to be worth an output also appears as an output on the commitment transactions, so either side can settle it without the other. When a node publishes its commitment transaction in a unilateral close, BOLT 3 gives each HTLC output, besides the revocation path, two exits: one with the preimage and one after the expiry. HTLCs below the dust limit, after the second-stage fee, are trimmed, and BOLT 2 says such HTLCs cannot be enforced on-chain.
| On the publisher’s commitment | Spent with the preimage by | Spent after the expiry by |
|---|---|---|
| An HTLC the publisher offered | The peer, with the preimage | The publisher, through an HTLC-timeout transaction |
| An HTLC the publisher received | The publisher, through an HTLC-success transaction | The peer |
The two second-stage transactions differ in one field. The HTLC-timeout transaction has locktime equal to cltv_expiry, so it is invalid before the expiry. The HTLC-success transaction has locktime 0 and needs the preimage. Both pay to an output that the publisher can spend only after to_self_delay blocks, which leaves the peer time to claim it if the commitment transaction was revoked.2
BOLT 5 links the chain back to the route. When a node’s HTLC-timeout transaction is deep enough, the node must fail the matching incoming HTLC. When the node sees a preimage in a transaction on the chain, it must extract it and fulfill the incoming HTLC. This is the case that the delta in the last section pays for: the on-chain steps take blocks, and the upstream expiry has to be far enough away to hold them.
What to take from this
Before the expiry, an HTLC pays only the receiver, for the preimage. From the expiry on, it can return to the sender, and the receiver can still claim until the timeout transaction confirms. A route works because one preimage unlocks every hop and the expiries fall hop by hop. The next lesson uses these amounts and expiries to show why a path of channels is a harder constraint than one channel.
- 01 Node C advertises a `cltv_expiry_delta` of 80 on its channel to D. An HTLC arrives at C with `cltv_expiry` of H+98, where H is the current block height at the sender. C is asked to forward it to D with an expiry of H+30. Does C accept the forward?
No. BOLT 4 requires `cltv_expiry - cltv_expiry_delta` to be at least `outgoing_cltv_value`. Here that is 98 - 80 = 18, and 18 is less than 30, so the forward fails. C answers with `incorrect_cltv_expiry`. The rule guards C, because with a smaller gap D could still claim at the last moment while C has too little time left to claim from its own upstream.
- 02 Why must the HTLC that a node receives expire later than the HTLC that it offers on?
After the downstream peer claims with the preimage, the node still needs time to learn the preimage and claim from its upstream peer. BOLT 2 derives the worst case as 3R+2G+2S blocks, where R is a reorganization depth, G a grace period and S the time for a transaction to be mined. If the incoming HTLC expired first, the upstream peer could take its money back while the node had already paid downstream.
- 03 A node offered an HTLC on a channel, and the HTLC has passed its expiry with no preimage. Which transaction can claim the output on that node's own commitment transaction, and what must hold before the claim?
The HTLC-timeout transaction. Its `locktime` is `cltv_expiry`, so it is valid only at or after the expiry height, and it needs the signatures of both nodes. Its output is then delayed by `to_self_delay` blocks. Before the expiry, only the peer can spend that output, and only with the preimage, apart from the penalty path for a revoked commitment.
Notes
- BOLT 2 gives
cltv_expiry_deltaof at least 34 as a reasonable figure "if in doubt", from R=2, G=2 and S=12. It is a worked suggestion, and each node picks its own value and advertises it. A sender may also add a random offset to every expiry on a route, and BOLT 7 recommends doing so, so real expiries are usually later than the minimum that this lesson computes.BOLT 2, cltv_expiry_delta Selection - The channel type changes the fee handling, not the roles. With anchor outputs, or with
zero_fee_commitments, the second-stage HTLC transactions carry no fee of their own and use a different signature type. BOLT 3 describes each channel type.BOLT 3, HTLC-timeout and HTLC-success transactions
Source & license
This page adapts Mastering the Lightning Network by Andreas M. Antonopoulos, Olaoluwa Osuntokun, René Pickhardt, licensed CC BY-SA 4.0. Adapted passages are marked in the text with a hairline rule and a source line. Changes: condensed, terminology aligned to this course's glossary, and the book's example replaced with one that uses the expiry rule in BOLT 4 and the advertised deltas in BOLT 7.
This page is therefore also CC BY-SA 4.0. Copy it, translate it, teach from it, republish it under the same license, with attribution to the works above and to ln.university.