UNDER DEVELOPMENT - DRAFT
Skip to main content
ln.university
LN 100 Fundamentals Lesson 04 Lesson 4 of 7 · 7 min · no measured block
Lesson 04 Reading lesson revised

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.

Assumed before you start
After this lesson you can
  • 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
Anchor text · read alongside CC-BY-SA-4.0 · free online
Chapter 8 · anchor
Mastering the Lightning Network
Andreas M. Antonopoulos, Olaoluwa Osuntokun, René Pickhardt
"Routing on a Network of Payment Channels", the sections on hash time-locked contracts and decrementing timelocks. This lesson's §2 and §4 condense them.
Read the chapter →

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.

  1. The sender offers it with update_add_htlc, which carries the amount, the payment_hash, the cltv_expiry and an onion packet.
  2. The two nodes exchange commitment_signed and revoke_and_ack, until both commitment transactions contain the HTLC and the earlier ones are revoked. BOLT 2 calls this state irrevocably committed.
  3. The receiver ends the HTLC in one of two ways. It sends update_fulfill_htlc with the preimage, or it sends update_fail_htlc if it cannot forward the payment or the HTLC has timed out.
  4. A second round of commitment_signed and revoke_and_ack removes 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:

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.

HTLCExpiryCheck at the node that receives it
C offers to DH+18D requires at least its own height + 18, which is H+18 when D’s height is H
B offers to CH+98C: 98 - 80 = 18, which is at least 18
A offers to BH+138B: 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.

Adapted from Mastering the Lightning Network, ch. 8, Routing on a Network of Payment Channels · CC-BY-SA-4.0 · condensed, terminology aligned to this course

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 commitmentSpent with the preimage bySpent after the expiry by
An HTLC the publisher offeredThe peer, with the preimageThe publisher, through an HTLC-timeout transaction
An HTLC the publisher receivedThe publisher, through an HTLC-success transactionThe 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.

Check yourself · 3 questions no score, no record
  1. 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?
  2. 02 Why must the HTLC that a node receives expire later than the HTLC that it offers on?
  3. 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?

Notes

  1. BOLT 2 gives cltv_expiry_delta of 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
  2. 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.