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

When to close a channel

Closing a channel moves your balance back to the bitcoin blockchain. It costs an on-chain fee, and the kind of close decides how long you wait for your funds and who pays. This lesson follows BOLT 2, 3 and 5 through a cooperative close, a force close and a penalty close, and condenses the closing sections of Mastering the Lightning Network. It ends with a test for whether to keep a channel or close it.

Assumed before you start
After this lesson you can
  • Say which close is possible when the other node does not answer, and how long each one makes you wait
  • Say who pays the fee in each kind of close, and when a node needs its own on-chain funds
  • Run the keep-or-close test with numbers from your own node
Anchor text · read alongside CC-BY-SA-4.0 · free online
Chapter 7 · anchor
Mastering the Lightning Network
Andreas M. Antonopoulos, Olaoluwa Osuntokun, René Pickhardt
"Payment Channels", the sections on delayed spending, cheating and penalty, and the cooperative close. This lesson's §2, §3 and §6 condense them.
Read the chapter →
Chapter 5 · anchor
Mastering the Lightning Network
Andreas M. Antonopoulos, Olaoluwa Osuntokun, René Pickhardt
"Operating a Lightning Network Node", the sections on node availability and closing channels. They give the reasons to close and the cost of closing. This lesson uses them in §3, §6, §7, §8 and §9.
Read the chapter →

A channel closes when a closing or commitment transaction that spends its funding output confirms on the blockchain. The specification BOLT 5 names three ways a channel can end: a mutual close, a unilateral close, and a revoked transaction close.1 The kind of close decides how long you wait for your funds, who pays the fee, and what happens to payments still in flight. The first six sections of this lesson take each close and its parts in turn, and the last three ask when closing is worth its cost.

Three ways a channel ends

In a mutual close, both nodes sign one closing transaction that pays each side its balance. This course also calls it a cooperative close. In a unilateral close, one node publishes its latest commitment transaction without the other’s help. This course also calls it a force close. In a revoked transaction close, one node publishes an old commitment transaction, and the other node takes the channel balance as a penalty.

Cooperative closeForce closeRevoked transaction close
What starts itEither node sends shutdown, and the other answersOne node publishes its latest commitment transactionOne node publishes an old, revoked commitment transaction
Needs the other nodeYesNoNo
When the publisher can spendWhen the closing transaction confirmsAfter the delay that the other node setUsually never: the other node claims the publisher’s balance
Who pays the on-chain feeDepends on the negotiation, see §2The funder, through the commitment fee (none with zero_fee_commitments), plus any node that adds fee with an anchorEach node pays for the transactions it publishes

A cooperative close needs both nodes and has no delay

BOLT 2 says why a cooperative close is the preferred way: it lets the nodes “access their funds immediately” and “can be negotiated with lower fees”. Either node starts it with a shutdown message that names the script it wants to be paid to. After shutdown, the channel accepts no new HTLCs. The HTLCs already in the channel must settle or fail first. When none remain, the nodes negotiate the closing transaction.

The fee rule depends on the negotiation that both nodes support.2 In the legacy negotiation, the funder of the channel pays the fee, and the two nodes exchange closing_signed messages until they agree on one fee. In the newer simple close, each node can build its own closing transaction and pay its own fee from its own output, and the other node only signs it. A node can repeat that step to raise the fee or to change its payout script.

A closing transaction has no HTLC outputs and no time lock on the payouts. BOLT 5 says a node needs to do nothing more after a mutual close, because each side is paid to the script it named. You can spend your output when the closing transaction confirms.

A force close makes the publisher wait

A force close publishes the latest commitment transaction that your node holds. On that transaction, your own balance is the to_local output, and your peer’s balance is the to_remote output. BOLT 5 explains the asymmetry. The publisher must wait to claim its own funds, and the other node gets its funds without that wait.

The wait is the to_self_delay. Each node sets it, in blocks, for the other node’s outputs when the channel opens. So the delay on your to_local output is the number that your peer chose. After the commitment transaction confirms, your output stays locked for that many blocks.3 The book uses 432 blocks in its example, about three days. It also says the delay grows with the channel’s capacity. That is an implementation choice, not a BOLT rule: BOLT 2 asks only for a delay long enough to punish a cheat, and lnd, for example, scales it from 144 to 2016 blocks.

The to_remote output pays the other node without that delay. On option_anchors channels, BOLT 3 adds a one-block lock to to_remote, so the other node can spend it one block after the commitment transaction confirms.

If Alice closes the channel with the commitment transaction that she holds, she cannot spend her balance until the delay passes, but Bob can claim his balance at once. The delay is there for one reason: to give the other party time to use the penalty option if an old, revoked commitment is broadcast.

Adapted from Mastering the Lightning Network, ch. 7, Payment Channels · CC-BY-SA-4.0 · condensed from two paragraphs, terminology aligned to this course

A node can force close without anyone asking. BOLT 2 requires a node to fail the channel when an HTLC that it offered stays on a commitment transaction past its timeout deadline, shortly after the HTLC’s cltv_expiry.4 It also requires failing the channel when an HTLC that your node has fulfilled is still pending close to its expiry, because otherwise the upstream node could time it out.4 Failing a channel means a unilateral close, unless a mutual close with a sufficient fee is already possible. So a peer that stops answering while a payment crosses the channel can cost you a force close even if you never asked for one.

The book gives three examples of when a force close is necessary. The partner is offline. The partner is online but does not answer a request to close. Or the two nodes start to negotiate a close and cannot finish.

Payments in flight settle on-chain, each on its own clock

Every HTLC that is large enough becomes its own output on the commitment transaction. BOLT 3 leaves out an HTLC whose amount, minus the fee of its second-stage transaction, is below the dust limit. That amount goes to the miners as fee, or with zero_fee_commitments partly to the shared anchor, so a node cannot recover a trimmed HTLC on-chain. An HTLC, in full describes the HTLC outputs in detail. This section gives the rules that matter when a channel closes.

On the publisher’s commitmentThe publisher claims itThe other node claims it
An HTLC the publisher offeredAfter the cltv_expiry, with an HTLC-timeout transactionWith the preimage, before the HTLC-timeout transaction confirms
An HTLC the publisher receivedWith the preimage, through an HTLC-success transactionAfter the cltv_expiry

The publisher’s claims take two steps. The HTLC-timeout or HTLC-success transaction pays to an output with the same to_self_delay as to_local. So the publisher waits for the cltv_expiry or for the preimage, and then for the delay as well. The other node claims directly from the commitment transaction.

A forwarding node must link the chain back to the route. BOLT 5 says that when its HTLC-timeout transaction is deep enough, the node must fail the matching incoming HTLC. When a preimage appears in a transaction on the chain, the node must take it from the witness and fulfill the incoming HTLC. Until these steps finish, the funds in a pending HTLC stay locked, sometimes until the HTLC expires.

Anchor outputs let a node add the fee later

A commitment transaction is signed before anyone knows the fee rate on the day it is published. In the legacy format, the commitment fee is fixed when the nodes sign, the funder pays it, and BOLT 2 says the publishing node has “no general method” to add fee by child-pays-for-parent. If on-chain fees rise, a commitment transaction signed when fees were lower can wait a long time to confirm.

Anchor outputs fix this. With option_anchors, a commitment transaction usually carries two small anchor outputs, one for each node.5 A node that publishes the commitment transaction can spend its anchor in a child transaction that pays a higher fee, so miners take the parent and the child together. BOLT 5 says a node that force closes must spend its anchor with enough fee to get the commitment transaction mined. If the fee is still too low, the node should replace the child with one that pays more.

Anchors change who pays and from where. The base commitment fee and the anchor amounts still come out of the funder’s balance. The fee in the child transaction comes from inputs that the node adds from its own on-chain wallet. The same holds for the second-stage HTLC transactions. On anchor channels, they are signed with zero fee, and BOLT 5 says the node must add inputs that pay a sufficient fee. This lesson draws one conclusion: a node with anchor channels should keep some on-chain funds outside its channels, to bump a commitment transaction when fees rise and to pay for its HTLC transactions.

The newest channel type, zero_fee_commitments, goes further. The commitment transaction does not need to pay its own fee, and one shared anchor output, which anyone can spend, lets a node attach the fee in a child transaction.

Publishing an old state loses the channel balance

Each payment revokes the previous commitment transaction. BOLT 5 says that if a node publishes a revoked commitment, the other node can use its revocation private key to claim the funds. It can spend the cheater’s to_local output at once, without waiting for the delay. It can also claim the HTLC outputs, and any HTLC transactions that the cheater publishes from that commitment.

The delay is what makes the penalty possible. The honest node must get its penalty transaction confirmed before the cheater’s to_local delay ends. After that, the cheater can spend the output. The book’s example shows the result. Bob takes both outputs, and Alice loses her whole balance.6

This works only if a node watches the chain. The book says a node can afford short interruptions of an hour up to one or two days. The real limit is the to_self_delay that your node set for each peer, minus a safety margin. Some implementations set it as low as 144 blocks, about one day. If your node can be offline longer than the shortest of those delays, use a watchtower. A watchtower is a service that watches for revoked commitments on your behalf while your node is offline.

The same rule is the main risk in a force close of your own. BOLT 5 says a node that knows or assumes its channel state is outdated must not broadcast its last commitment transaction. BOLT 2 adds that such a node should send an error to ask the peer to fail the channel. When the peer publishes its own commitment, BOLT 5 says the node can still recover its main output, but it cannot recover the HTLCs.

CAUTION: Do not force close a channel if your node data can be older than the latest channel state. This can occur after you restore a node from a backup copy. A force close from old data can publish a revoked state, and your peer can then take all your funds in the channel. Ask the peer to close the channel. Use your node software’s static channel backup recovery, which asks each peer to close. Do not run the node from an old copy of its channel database. If the peer never comes back online, the funds in that channel stay locked.

What closing costs and what keeping costs

Closing has a price, and so does keeping. The book states the first one in a sentence.

Closing channels will incur an on-chain fee and will reduce your Lightning node’s capacity and connectivity.

Adapted from Mastering the Lightning Network, ch. 5, Operating a Lightning Network Node · CC-BY-SA-4.0 · shortened to one sentence

The fees fall on different nodes in each kind of close. This lesson gives no fee figures, because they depend on the fee rate on the day you close.

TransactionWho pays its fee
Cooperative close, legacy negotiationThe funder
Cooperative close, simple closeThe node that built that closing transaction, from its own output
Commitment transactionThe funder, at the fee rate agreed when it was signed (legacy and option_anchors). With zero_fee_commitments it pays no fee of its own: the node that spends the shared anchor pays, from its own on-chain wallet.
HTLC-timeout or HTLC-success on a legacy channelTaken from the HTLC amount, at the fee rate signed in advance
Child transaction that spends an anchorThe node that publishes it, from its own on-chain wallet
HTLC-timeout or HTLC-success on an anchor channelThe node that publishes it, from inputs it adds
Sweep of a delayed output after the waitThe node that owns the output
Penalty transactionThe node that claims the penalty, from the funds it claims

If you also want the capacity back, you pay again: the on-chain fee to open a new channel, and then a rebalance or a lease to put the balance on the side where you need it. What a rebalance actually costs and what inbound liquidity is worth price those two.

Keeping has costs too. The capital in the channel could earn elsewhere, and what capital costs, in bitcoin measures the baseline for that. A channel with its balance on the wrong side needs a rebalance before it is useful. And if your node is often offline, the peer can close the channel itself. If you funded the channel, you still pay its commitment fee. The book says such a partner “may consider the bitcoin locked up in their channels with you to be underutilized capacity, and may close those channels”.

A test you can run

Pick the period for which you plan to hold the funds, for example the next 90 days. Add up two lists for that period.

Keep.

  1. The forwarding fees the channel will earn. Your node’s own forwarding history is the best guide.
  2. The cost to buy the same liquidity again by a rebalance or a lease, if you will need it.

Close.

  1. The return on the freed capital, from the baseline or from another channel.
  2. Minus the on-chain fees to close, from the table above, and minus the fee to open a replacement if you want one.

If the Close total is higher than the Keep total, closing is probably worth it. If it is lower, keep the channel. A cost that you paid in the past appears on neither list, because you paid it either way. The test cannot price one thing: a closed channel ends your path to that peer, and the book counts that loss of connectivity as a cost.

Which close fits your situation

SituationClose availableWhat to weigh
The peer is online and answersCooperative closeThe test above
The peer is offlineForce close onlyThe to_self_delay before you can spend, the commitment fee, the on-chain funds you need for anchors, and whether the peer will return
The peer is online but does not answerForce close onlyThe same four
Your node data can be out of dateNeither from your sideAsk the peer to close, see the caution above
You need the funds on-chainCooperative close if the peer answersEach close costs a fee, and the book points to channels with large local balances
The channel earns little but worksCooperative closeThe test above

The book gives one more reason to close. A node that holds too much in its Lightning channels can move funds on-chain by closing channels. A node with plenty of inbound capacity can close the channels with large local balances, which the book calls “bundling” its funds for that move.

Check yourself · 3 questions no score, no record
  1. 01 Your channel partner has been offline for several days, and you want your balance back on-chain. You funded the channel, and the other node set a `to_self_delay` of 432 blocks. Which close can you do, and when can you spend your funds?
  2. 02 Your node forwards a payment. The downstream peer goes offline, and the HTLC that your node offered to it passes its `cltv_expiry`. You did not ask for a close. Why does your node close the channel anyway?
  3. 03 You restored your node from a copy of its data made last month, and a channel looks stuck. Is it safe to force close from that copy?

Notes

  1. BOLT 5 names the three endings "the good way", "the bad way" and "the ugly way", and says no funds are at risk in any of them, "provided that the situation is properly handled".BOLT 5, On-chain Transaction Handling
  2. The legacy negotiation uses closing_signed, and the simple close uses closing_complete and closing_sig when both nodes negotiate option_simple_close. BOLT 3 gives the fee rule for each closing transaction.BOLT 2, Channel Close
  3. The delay is a relative time lock, enforced with OP_CHECKSEQUENCEVERIFY in the to_local output script. The same script has a second branch that the other node can spend at once, but only after this state is revoked, because only then does it know the revocation private key. That branch is the penalty path in §6.BOLT 3, to_local Output
  4. BOLT 2 suggests a grace period G of 1 or 2 blocks after the HTLC's cltv_expiry for an HTLC the node offered. For an HTLC the node has fulfilled, the deadline falls before the cltv_expiry, and BOLT 2 calls 18 blocks reasonable.BOLT 2, cltv_expiry_delta Selection
  5. BOLT 3 fixes each anchor at 330 sats and locks it to that party's funding key. After 16 blocks, anyone can spend an unspent anchor, so that the small outputs do not stay unspent forever. With zero_fee_commitments, one shared pay-to-anchor output of at most 240 sats replaces the two.BOLT 3, anchor outputs
  6. On anchor channels, the cheating node can delay a penalty transaction that claims all revoked outputs at once. So BOLT 5 says that if such a transaction has not confirmed by a safety margin before the delay ends, the node should claim each revoked output in its own transaction. BOLT 5 recommends a margin of 18 blocks.BOLT 5, Revoked Transaction Close Handling

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 from two chapters, re-sequenced into a decision lesson, terminology aligned to this course's glossary, and brought up to the current BOLT 2, 3 and 5 text, which adds the simple close, anchor outputs and zero-fee commitments that the book does not cover.

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.