Lesson Ready to Start
Foundational
How the Lightning Network Works
The technical building blocks of Lightning including payment channels, commitment transactions, HTLCs for multi-hop routing, onion encryption, invoices, and the safety mechanisms that prevent cheating.
Video length: 0 mins 1 sec
Transcript
Welcome back! In the previous lessons, we explored why Lightning is needed and how it evolved. Now it's time to look under the hood. In this lesson, we'll break down the technical building blocks that make the Lightning Network function: payment channels, commitment transactions, hashed time-locked contracts (HTLCs), and multi-hop routing.
Payment Channels and Commitments
At its core, Lightning is built on payment channels—two-party agreements that allow participants to exchange value off-chain. When you open a channel, you and your counterparty lock Bitcoin into a 2-of-2 multisignature address on the main blockchain. This funding transaction is the only on-chain action required to start transacting. Once the channel is open, both parties can exchange signed commitment transactions that reflect updated balances. These transactions are never broadcast unless the channel is closed. The result is instant transfers with no miner fees for each update, and final settlement guaranteed by Bitcoin's security.
Every time you send or receive sats through a channel, a new commitment transaction is created. This transaction represents the current state of the channel—how much belongs to you, and how much belongs to your counterparty. Both parties hold their own version of the commitment transaction, each spendable by themselves after a time delay or immediately by the other party with a revocation key. This asymmetry is crucial: if someone tries to broadcast an old, favorable state, the counterparty can use the revocation key to claim all funds as a penalty. This mechanism ensures that cheating is economically irrational.
Routing and Smart Contracts
To route payments beyond direct channel partners, Lightning uses HTLCs—Hashed Time-Locked Contracts. An HTLC is a conditional payment: "I'll pay you X sats if you can reveal a secret (preimage) that hashes to Y within Z blocks." Here's how it works: The recipient generates a random secret and shares its hash with the sender. The sender creates an HTLC offering payment contingent on revealing that secret. Each hop along the route creates a similar HTLC with slightly less time. When the recipient reveals the secret to claim payment, each preceding node can use it to claim their incoming HTLC. If the secret isn't revealed in time, the funds return to the sender. This atomic property means payments either complete fully or fail completely—no funds are ever stuck in limbo.
You don't need a direct channel with everyone you want to pay. Lightning forms a network graph where payments route through intermediaries. When Alice wants to pay Dave, but only has a channel with Bob, the payment can hop: Alice → Bob → Carol → Dave. Each intermediary earns a small routing fee for forwarding the payment. The sender's node calculates the route using pathfinding algorithms that consider channel capacity, fees, and reliability. Onion routing encrypts each hop's instructions so intermediaries only know their immediate predecessor and successor—not the full payment path.
To receive a Lightning payment, you generate an invoice—a string (often shown as a QR code) that contains the payment hash derived from your secret preimage, the amount requested, an expiry time, and routing hints if your node isn't well-connected. The payer's wallet decodes this invoice, finds a route, and initiates the HTLC chain. Once you reveal the preimage, the payment settles instantly across all hops.
Closing Channels and Network Safety
Channels can close in three ways. A cooperative close occurs when both parties agree on the final balance and broadcast a simple settlement transaction—this is the fastest and cheapest method. A unilateral close happens when one party broadcasts their latest commitment transaction; the broadcaster must wait through a time-lock period before accessing funds, while the counterparty can spend immediately. Finally, a penalty close occurs if someone broadcasts an old state—the counterparty uses the revocation key to claim all channel funds, punishing cheating and maintaining network integrity.
What if you're offline when your counterparty tries to cheat? Watchtowers are third-party services that monitor the blockchain on your behalf. If they detect an old commitment transaction, they can broadcast the penalty transaction for you, keeping your funds safe even when you're not watching.
Putting It All Together
The Lightning Network combines these components into a cohesive system. Funding transactions anchor channels to Bitcoin's security, while commitment transactions enable instant off-chain updates. HTLCs allow trustless multi-hop payments, and onion routing preserves privacy across the network. Revocation mechanisms and watchtowers work together to deter cheating.
The result is a payment network that inherits Bitcoin's finality while offering speed, privacy, and scalability that the base layer cannot achieve alone.
In this lesson, we've explored the technical machinery behind Lightning. Understanding these concepts — channels, commitments, HTLCs, and routing — gives you the foundation to use Lightning confidently and troubleshoot issues when they arise.
In our next lesson, we'll compare Lightning vs On-Chain Bitcoin, helping you understand when to use each and how they complement one another.
Payment Channels and Commitments
At its core, Lightning is built on payment channels—two-party agreements that allow participants to exchange value off-chain. When you open a channel, you and your counterparty lock Bitcoin into a 2-of-2 multisignature address on the main blockchain. This funding transaction is the only on-chain action required to start transacting. Once the channel is open, both parties can exchange signed commitment transactions that reflect updated balances. These transactions are never broadcast unless the channel is closed. The result is instant transfers with no miner fees for each update, and final settlement guaranteed by Bitcoin's security.
Every time you send or receive sats through a channel, a new commitment transaction is created. This transaction represents the current state of the channel—how much belongs to you, and how much belongs to your counterparty. Both parties hold their own version of the commitment transaction, each spendable by themselves after a time delay or immediately by the other party with a revocation key. This asymmetry is crucial: if someone tries to broadcast an old, favorable state, the counterparty can use the revocation key to claim all funds as a penalty. This mechanism ensures that cheating is economically irrational.
Routing and Smart Contracts
To route payments beyond direct channel partners, Lightning uses HTLCs—Hashed Time-Locked Contracts. An HTLC is a conditional payment: "I'll pay you X sats if you can reveal a secret (preimage) that hashes to Y within Z blocks." Here's how it works: The recipient generates a random secret and shares its hash with the sender. The sender creates an HTLC offering payment contingent on revealing that secret. Each hop along the route creates a similar HTLC with slightly less time. When the recipient reveals the secret to claim payment, each preceding node can use it to claim their incoming HTLC. If the secret isn't revealed in time, the funds return to the sender. This atomic property means payments either complete fully or fail completely—no funds are ever stuck in limbo.
You don't need a direct channel with everyone you want to pay. Lightning forms a network graph where payments route through intermediaries. When Alice wants to pay Dave, but only has a channel with Bob, the payment can hop: Alice → Bob → Carol → Dave. Each intermediary earns a small routing fee for forwarding the payment. The sender's node calculates the route using pathfinding algorithms that consider channel capacity, fees, and reliability. Onion routing encrypts each hop's instructions so intermediaries only know their immediate predecessor and successor—not the full payment path.
To receive a Lightning payment, you generate an invoice—a string (often shown as a QR code) that contains the payment hash derived from your secret preimage, the amount requested, an expiry time, and routing hints if your node isn't well-connected. The payer's wallet decodes this invoice, finds a route, and initiates the HTLC chain. Once you reveal the preimage, the payment settles instantly across all hops.
Closing Channels and Network Safety
Channels can close in three ways. A cooperative close occurs when both parties agree on the final balance and broadcast a simple settlement transaction—this is the fastest and cheapest method. A unilateral close happens when one party broadcasts their latest commitment transaction; the broadcaster must wait through a time-lock period before accessing funds, while the counterparty can spend immediately. Finally, a penalty close occurs if someone broadcasts an old state—the counterparty uses the revocation key to claim all channel funds, punishing cheating and maintaining network integrity.
What if you're offline when your counterparty tries to cheat? Watchtowers are third-party services that monitor the blockchain on your behalf. If they detect an old commitment transaction, they can broadcast the penalty transaction for you, keeping your funds safe even when you're not watching.
Putting It All Together
The Lightning Network combines these components into a cohesive system. Funding transactions anchor channels to Bitcoin's security, while commitment transactions enable instant off-chain updates. HTLCs allow trustless multi-hop payments, and onion routing preserves privacy across the network. Revocation mechanisms and watchtowers work together to deter cheating.
The result is a payment network that inherits Bitcoin's finality while offering speed, privacy, and scalability that the base layer cannot achieve alone.
In this lesson, we've explored the technical machinery behind Lightning. Understanding these concepts — channels, commitments, HTLCs, and routing — gives you the foundation to use Lightning confidently and troubleshoot issues when they arise.
In our next lesson, we'll compare Lightning vs On-Chain Bitcoin, helping you understand when to use each and how they complement one another.
Views:
20
Comment below with questions, suggestions and corrections.
Go to Comments