Lightning for Builders Foundations

How the Lightning Network works

Open a channel yourself, confirm it on-chain, and pay through it, to see the three moves every channel makes: open, update, close.

45 minBeginner3 / 17

Draft: this lesson is still waiting for review.

What you'll learn

  • Explain how a funding transaction creates a channel
  • Open and confirm a channel from Alice to Bob
  • Pay over the new channel and explain what changed on each side

Key terms

channel (chaneli)
A payment link between two Lightning nodes, locked by a Bitcoin transaction.
multisig
A setup where several signatures are needed to spend bitcoin, for example 2 of 3.
commitment transaction (muamala wa ahadi)
A signed transaction holding a channel's current balances; every payment makes a new one.
confirmation (uthibitisho)
A transaction gets one confirmation when it enters a block; each later block adds another.
liquidity (ukwasi)
How much can be sent (outbound) or received (inbound) over a channel.

Every channel lives through three moves.

  1. Open. One side sends bitcoin to a 2-of-2 multisig output that needs both signatures to spend. This funding transaction is an ordinary Bitcoin transaction.
  2. Update. For each payment, both sides sign a new commitment transaction that splits the funds differently. Nobody broadcasts it: it just sits ready, and the old one is revoked.
  3. Close. When they are done, the latest split goes on-chain, either together (a cooperative close) or by one side alone.

Only moves 1 and 3 touch the blockchain. Everything in between is just messages.

Step 1: Alice opens a channel to Bob

Alice needs Bob's node id (his public key) and his address on the network. Then she funds 500,000 sats:

Alice opens a channel to Bob

These exercises need a laptop with the lab running. You can read the lesson on your phone.

The output includes a funding_txid. The channel exists, but it is pending: the funding transaction is in the mempool, not yet in a block.

Step 2: Confirm it

On regtest you are the miner. Six blocks give the funding transaction six confirmations, and the channel becomes active:

Confirm the channel by mining blocks

These exercises need a laptop with the lab running. You can read the lesson on your phone.

Step 3: Bob gets paid

Bob runs c-lightning, so his invoice command is different, and c-lightning counts in millisatoshis. Make the invoice on Bob, then pay it from Alice:

Bob requests a payment, Alice pays

These exercises need a laptop with the lab running. You can read the lesson on your phone.

Alice opened the channel with all 500,000 sats on her side. After paying 20,000 sats, Bob has 20,000 on his side and can now pay her back. That balance on each side is the channel's liquidity.

Reflect

Write your answer down, or talk it over with a friend or your chama.

  1. Why does a channel need confirmations before it can be used?
  2. After Alice paid Bob, how could Bob ever pay Alice back over the same channel?

Check yourself

Question 1 of 4What kind of output does a funding transaction pay to?

Further reading

Adapted from Mastering the Lightning Network. Andreas M. Antonopoulos, Olaoluwa Osuntokun and René Pickhardt. Licence: CC BY-SA 4.0.