Chapter 9. Channel Operation and Payment Forwarding

In this chapter we will bring together payment channels and hash time-locked contracts (HTLCs). In Chapter 7, we explained the way Alice and Bob construct a payment channel between their two nodes. We also looked at the commitment and penalty mechanisms that secure the payment channel. In Chapter 8, we looked at HTLCs and how these can be used to route a payment across a path made of multiple payment channels. In this chapter we bring the two concepts together by looking at how HTLCs are managed on each payment channel, how the HTLCs are committed to the channel state, and how they are settled to update the channel balances.

Specifically, we will be discussing “Adding, settling, failing HTLCs” and the “Channel state machine” that form the overlap between the peer-to-peer layer and the routing layer, as highlighted by an outline in Figure 9-1.

Channel operation and payment forwarding in the Lightning protocol suite

Figure 9-1. Channel operation and payment forwarding in the Lightning protocol suite

Local (Single Channel) Versus Routed (Multiple Channels)

Even though it is possible to send payments across a payment channel simply by updating the channel balances and creating new commitment transactions, the Lightning protocol uses HTLCs even for “local” payments across a payment channel. The reason for this is to maintain the same protocol design regardless of whether a payment is only one hop (across a single payment channel) or several hops (routed across multiple payment channels).

By maintaining the same abstraction for both local and remote, we not only simplify the protocol design but also improve privacy. For the recipient of a payment there is no discernible difference between a payment made directly by their channel partner and a payment forwarded by their channel partner on behalf of someone else.

Forwarding Payments and Updating Commitments with HTLCs

We will revisit our example from Chapter 8 to demonstrate how the HTLCs from Alice to Dina get committed to each payment channel. As you recall in our example, Alice is paying Dina 50,000 satoshis by routing an HTLC via Bob and Chan. The network is shown in Figure 9-2.

Alice pays Dina with an HTLC routed via Bob and Chan

Figure 9-2. Alice pays Dina with an HTLC routed via Bob and Chan

We will focus on the payment channel between Alice and Bob and review the messages and transactions that they use to process this HTLC.

HTLC and Commitment Message Flow

The message flow between Alice and Bob (and also between any pair of channel partners) is shown in Figure 9-3.

The message flow for HTLC commitment between channel partners

Figure 9-3. The message flow for HTLC commitment between channel partners

We’ve already seen the commitment_signed and revoke_and_ack in Chapter 7. Now we will see how HTLCs fit into the commitment scheme. The two new messages are update_add_htlc, which Alice uses to ask Bob to add an HTLC, and update_fulfill_htlc, which Bob uses to redeem the HTLC once he has received the payment secret (Dina’s secret).

Forwarding Payments with HTLCs

Alice and Bob start with a payment channel that has a 70,000 satoshi balance on each side.

As we saw in Chapter 7, this means that Alice and Bob have negotiated and each hold commitment transactions. These commitment transactions are asymmetric, delayed, and revocable, and look like the example in Figure 9-4.

Alice and Bob's initial commitment transactions

Figure 9-4. Alice and Bob’s initial commitment transactions

Adding an HTLC

Alice wants Bob to accept an HTLC worth 50,200 satoshis to forward to Dina. To do so, Alice must send the details of this HTLC, including the payment hash and amount, to Bob. Bob will also need to know where to forward it, which is something we discuss in detail in Chapter 10.

To add the HTLC, Alice starts the flow we saw in Figure 9-3 by sending the update_add_htlc message to Bob.

The update_add_HTLC Message

Alice sends the update_add_HTLC Lightning message to Bob. This message is defined in BOLT #2: Peer Protocol, update_add_HTLC, and is shown in Example 9-1.

Example 9-1. The update_add_HTLC message








This is the channel that Alice has with Bob where she wants to add the HTLC. Remember that Alice and Bob may have multiple channels with each other.


This is an HTLC counter and starts at 0 for the first HTLC offered to Bob by Alice and is incremented for each subsequent offered HTLC.


This is the amount (value) of the HTLC in millisatoshis. In our example this is 50,200,000 millisatoshis (i.e., 50,200 satoshis).


This is the payment hash calculated from Dina’s invoice. It is H = RIPEMD160(SHA-256(R)), where R is Dina’s secret that is known only by Dina and will be revealed if Dina is paid.


This is the expiry time for this HTLC, which will be encoded as a time-locked refund in case the HTLC fails to reach Dina in this time.


This is an onion-encrypted route that tells Bob where to forward this HTLC next (to Chan). Onion routing is covered in detail in Chapter 10.


As a reminder, accounting within the Lightning Network is in units of millisatoshis (thousandths of a satoshi), whereas Bitcoin accounting is in satoshis. Any amounts in HTLCs are millisatoshis, which are then rounded to the nearest satoshi in the Bitcoin commitment transactions.

HTLC in Commitment Transactions

The received information is enough for Bob to create a new commitment transaction. The new commitment transaction has the same two outputs to_self and to_remote for Alice and Bob’s balance, and a new output representing the HTLC offered by Alice.

We’ve already seen the basic structure of an HTLC in Chapter 8. The complete script of an offered HTLC is defined in BOLT #3: Transactions, Offered HTLC Output and is shown in Example 9-2.

Example 9-2. Offered HTLC output script

1 # Revocation 1







8 # Redemption 2




12 # Refund 3






The first clause of the OP_IF conditional is redeemable by Alice with a revocation key. If this commitment is later revoked, Alice will have a revocation key to claim this output in a penalty transaction, taking the whole channel balance.


The second clause is redeemable by the preimage (payment secret, or in our example, Dina’s secret) if it is revealed. This allows Bob to claim this output if he has the secret from Dina, meaning he has successfully delivered the payment to Dina.


The third and final clause is a refund of the HTLC to Alice if the HTLC expires without reaching Dina. It is time-locked with the expiration cltv_expiry. This ensures that Alice’s balance is not “stuck” in an HTLC that can’t be routed to Dina.

There are three ways to claim this output. Try to read the script and see if you can figure it out (remember, it is a stack-based language, so things appear “backward”).

New Commitment with HTLC Output

Bob now has the necessary information to add this HTLC script as an additional output and create a new commitment transaction. Bob’s new commitment will have 50,200 satoshis in the HTLC output. That amount will come from Alice’s channel balance, so Alice’s new balance will be 19,800 satoshis (70,000 – 50,200 = 19,800). Bob constructs this commitment as a tentative “Commitment #3,” shown in Figure 9-5.

Bob's new commitment with an HTLC output

Figure 9-5. Bob’s new commitment with an HTLC output

Alice Commits

Shortly after sending the update_add_htlc message, she will commit to the new state of the channel, so that the HTLC can be safely added by Bob. Bob has the HTLC information and has constructed a new commitment but does not yet have this new commitment signed by Alice.

Alice sends commitment_signed to Bob, with the signature for the new commitment and for the HTLC within. We saw the commitment_signed message in Chapter 7, but now we can understand the rest of the fields. As a reminder, it is shown in Example 9-3.

Example 9-3. The commitment_signed message





The fields num_htlcs and htlc_signature now make more sense:


This is the number of HTLCs that are outstanding in the commitment transaction. In our example, just one HTLC, the one Alice offered.


This is an array of signatures (num_htlcs in length), containing signatures for the HTLC outputs.

Alice can send these signatures without hesitation: she can always get a refund if the HTLC expires without being routed to Dina.

Now, Bob has a new signed commitment transaction, as shown in Figure 9-6.

Bob has a new signed commitment

Figure 9-6. Bob has a new signed commitment

Bob Acknowledges New Commitment and Revokes Old One

Now that Bob has a new signed commitment, he needs to acknowledge it and revoke the old commitment. He does so by sending the revoke_and_ack message, as we saw in Chapter 7 previously. As a reminder, that message is shown in Example 9-4.

Example 9-4. The revoke_and_ack message




Bob sends the per_commitment_secret that allows Alice to construct a revocation key to build a penalty transaction spending Bob’s old commitment. Once Bob has sent this, he can never publish “Commitment #2” without risking a penalty transaction and losing all his money. So, the old commitment is effectively revoked.

Bob has effectively moved the channel state forward, as shown in Figure 9-7.

Bob has revoked the old commitment

Figure 9-7. Bob has revoked the old commitment

Despite the fact that Bob has a new (signed) commitment transaction and an HTLC output inside, he cannot consider his HTLC as being set up successfully.

He first needs to have Alice revoke her old commitment, because otherwise, Alice can roll back her balance to 70,000 satoshis. Bob needs to make sure that Alice also has a commitment transaction containing the HTLC and has revoked the old commitment.

That is why, if Bob is not the final recipient of the HTLC funds, he should not forward the HTLC yet by offering an HTLC on the next channel with Chan.

Alice has constructed a mirror-image new commitment transaction containing the new HTLC, but it is yet to be signed by Bob. We can see it in Figure 9-8.

Alice's new commitment with an HTLC output

Figure 9-8. Alice’s new commitment with an HTLC output

As we described in Chapter 7, Alice’s commitment is the mirror image of Bob’s, as it contains the asymmetric, delayed, revocable construct for revocation and penalty enforcement of old commitments. Alice’s 19,800 satoshi balance (after deducting the HTLC value), is delayed and revocable. Bob’s 70,000 satoshi balance is immediately redeemable.

Next, the message flow for commitment_signed and revoke_and_ack is now repeated, but in the opposite direction. Bob sends commitment_signed to sign Alice’s new commitment, and Alice responds by revoking her old commitment.

For completeness sake, let’s quickly review the commitment transactions as this round of commitment/revocation happens.

Bob Commits

Bob now sends a commitment_signed back to Alice, with his signatures for Alice’s new commitment transaction, including the HTLC output she has added.

Now Alice has the signature for the new commitment transaction. The state of the channel is shown in Figure 9-9.

Alice has a new signed commitment

Figure 9-9. Alice has a new signed commitment

Alice can now acknowledge the new commitment by revoking the old one. Alice sends the revoke_and_ack message containing the necessary per_commitment_point that will allow Bob to construct a revocation key and penalty transaction. Thus, Alice revokes her old commitment.

The channel state is shown in Figure 9-10.

Alice has revoked the old commitment

Figure 9-10. Alice has revoked the old commitment

Multiple HTLCs

At any point in time, Alice and Bob may have dozens or even hundreds of HTLCs across a single channel. Each HTLC is offered and added to the commitment transaction as an additional output. A commitment transaction therefore always has two outputs for the channel partner balances and any number of HTLC outputs, one per HTLC.

As we saw in the commitment_signed message, there is an array for HTLC signatures so that multiple HTLC commitments can be transmitted at the same time.

The current maximum number of HTLCs allowed on a channel is 483 HTLCs to account for the maximum Bitcoin transaction size and ensure that the commitment transactions continue to be valid Bitcoin transactions.

As we will see in the next section, the maximum is only for pending HTLCs because, once an HTLC is fulfilled (or fails due to timeout/error), it is removed from the commitment transaction.

HTLC Fulfillment

Now Bob and Alice both have a new commitment transaction with an additional HTLC output, and we have achieved a major step toward updating a payment channel.

The new balance of Alice and Bob does not reflect yet that Alice has successfully sent 50,200 satoshis to Bob.

However, the HTLCs are now set up in a way that secure settlement in exchange for the proof of payment will be possible.

HTLC Propagation

Let’s assume that Bob continues the chain and sets up an HTLC with Chan for 50,100 satoshis. The process will be exactly the same as we just saw between Alice and Bob. Bob will send update_add_htlc to Chan, then they will exchange commitment_signed and revoke_and_ack messages in two rounds, progressing their channel to the next state.

Next, Chan will do the same with Dina: offer a 50,000 satoshi HTLC, commit, and revoke, etc. However, Dina is the final recipient of the HTLC. Dina is the only one that knows the payment secret (the preimage of the payment hash). Therefore, Dina can fulfill the HTLC with Chan immediately!

Dina Fulfills the HTLC with Chan

Dina can settle the HTLC by sending an update_ful⁠fill_htlc message to Chan. The update_fulfill_htlc message is defined in BOLT #2: Peer Protocol, update_fulfill_htlc and is shown here:




It’s a really simple message:


The channel ID on which the HTLC is committed.


The ID of the HTLC (we started with 0 and incremented for each HTLC on the channel).


The secret that proves payment was made and redeems the HTLC. This is the R value that was hashed by Dina to produce the payment hash in the invoice to Alice.

When Chan receives this message, he will immediately check if the payment_preimage (let’s call it R) produces the payment hash (let’s call it H) in the HTLC that he offered to Dina. He hashes it like this:

· H = RIPEMD160(SHA-256 (R))

If the result H matches the payment hash in the HTLC, Chan can do a little dance of celebration. This long-awaited secret can be used to redeem the HTLC, and will be passed back along the chain of payment channels all the way to Alice, resolving every HTLC that was part of this payment to Dina.

Let’s go back to Alice and Bob’s channel and watch them unwind the HTLC. To get there, let’s assume Dina sent the update_fulfill_htlc to Chan, Chan sent update_fulfill_htlc to Bob, and Bob sent update_fulfill_htlc to Alice. The payment preimage has propagated all the way back to Alice.

Bob Settles the HTLC with Alice

When Bob sends the update_fulfill_htlc to Alice, it will contain the same payment_preimage that Dina selected for her invoice. That payment_preimage has traveled backward along the payment path. At each step, the channel_id will be different and id (HTLC ID) may be different. But the preimage is the same!

Alice will also validate the payment_preimage received from Bob. She will compare its hash to the HTLC payment hash in the HTLC she offered Bob. She will also find this preimage matches the hash in Dina’s invoice. This is proof that Dina was paid.

The message flow between Alice and Bob is shown in Figure 9-11.

The HTLC fulfillment message flow

Figure 9-11. The HTLC fulfillment message flow

Both Alice and Bob can now remove the HTLC from the commitment transactions and update their channel balances.

They create new commitments (Commitment #4), as shown in Figure 9-12.

The HTLC is removed and balances are updated in new commitments

Figure 9-12. The HTLC is removed and balances are updated in new commitments

Next, they complete two rounds of commitment and revocation. First, Alice sends commitment_signed to sign Bob’s new commitment transaction. Bob responds with revoke_and_ack to revoke his old commitment. Once Bob has moved the state of the channel forward, the commitments look like we see in Figure 9-13.

Alice signs Bob's new commitment and Bob revoked the old one

Figure 9-13. Alice signs Bob’s new commitment and Bob revoked the old one

Finally, Bob signs Alice’s commitment by sending Alice a commitment_signed message. Then Alice acknowledges and revokes her old commitment by sending revoke_and_ack to Bob. The end result is that both Alice and Bob have moved their channel state to Commitment #4, have removed the HTLC, and have updated their balances. Their current channel state is represented by the commitment transactions that are shown in Figure 9-14.

Alice and Bob settle the HTLC and update balances

Figure 9-14. Alice and Bob settle the HTLC and update balances

Removing an HTLC Due to Error or Expiry

If an HTLC cannot be fulfilled, it can be removed from the channel commitment using the same process of commitment and revocation.

Instead of update_fulfill_htlc, Bob would send an update_fail_htlc or update_fail_malformed_htlc. These two messages are defined in BOLT #2: Peer Protocol, Removing an HTLC.

The update_fail_htlc message is shown in the following:





It’s pretty self-explanatory. The multibyte reason field is defined in BOLT #4: Onion Routing, which we will describe in Chapter 10.

If Alice received an update_fail_htlc from Bob, the process would unfold in much the same way: the two channel partners would remove the HTLC, create updated commitment transactions, and go through two rounds of commitment/revocation to move the channel state forward to the next commitment. The only difference: the end balances would revert back to what they were without the HTLC, essentially refunding Alice for the HTLC value.

Making a Local Payment

At this point, you will easily understand why HTLCs are used for both remote and local payments. When Alice pays Bob for a coffee, she doesn’t just update the channel balance and commit to a new state. Instead, the payment is made with an HTLC, in the same way Alice paid Dina. The fact that there’s only one channel hop makes no difference. It would work like this:

1. Alice orders a coffee from Bob’s shop page.

2. Bob’s shop sends an invoice with a payment hash.

3. Alice constructs an HTLC from that payment hash.

4. Alice offers the HTLC to Bob with update_add_htlc.

5. Alice and Bob exchange commitments and revocations adding the HTLC to their commitment transactions.

6. Bob sends update_fulfill_htlc to Alice with the payment preimage.

7. Alice and Bob exchange commitments and revocations removing the HTLC and updating the channel balances.

Whether an HTLC is forwarded across many channels or just fulfilled in a single channel “hop,” the process is exactly the same


In this chapter we saw how commitment transactions (from Chapter 7) and HTLCs (from Chapter 8) work together. We saw how an HTLC is added to a commitment transaction, and how it is fulfilled. We saw how the asymmetric, delayed, revocable system for enforcing channel state is extended to HTLCs.

We also saw how a local payment and a multihop routed payment are handled identically: using HTLCs.

In the next chapter we will look at the encrypted message routing system called onion routing.

If you find an error or have any questions, please email us at Thank you!