Start investing in Bitcoin with Pix, in a few minutesSponsoredOpen free account
Bitcoin

Lightning Network inside: channels, HTLC, routes and liquidity

The technical and didactic guide of how Bitcoin Layer 2 moves payments without recording each of them on the blockchain.

Lightning live and route rate calculator

Public numbers of the Mempool.Space direct network. Below, estimate how much a payment paid in transfer fees.

By Boletim Bitcoin newsroom

. 10 min reading.

In short: The Lightning Network is a network of payment channels built over Bitcoin. Two people crash Bitcoin in a multisig 2 out of 2 transaction and start exchanging balances outside the blockchain, signing new states with each payment. Payments between those who don't have a direct channel travel by routes from several nodes, protected by HTLC contracts and by layered routing, such as an onion. This guide shows how it works from within, with live data from the network.

If you've already read our Lightning Network for Beginners Guide, knows that it serves for quick and cheap payments on Satoshis. Here we go one level below: What happens when one channel opens, why no one can steal the balance of the other, as a payment crosses us who don't trust each other and where the fees come from.

The problem that Lightning solves

The Bitcoin blockchain processes one block every ten minutes on average and each block has limited space. When many people want to transact at the same time, the dispute for space raises the network fees. This is great for security, because every transaction is checked by thousands of us, but it's bad to pay for a coffee. The idea of Lightning, described by Joseph Poon and Thaddeus Dryja in the article The Bitcoin Lightning Network: Scalable Off-Chain Instant Payments, is to use blockchain just to open and close relationships, and do everything else out of it.

She's a layer 2: It has no currency of its own, has no token, and what circulates is real Bitcoin, locked in transactions that can be taken to blockchain at any time.

Lightning Network inside: channels, HTLC, routes and liquidity
Image: Pexels (free license)

How does a payment channel work?

A channel starts with a Financing Transaction (Funding). Ana and Bruno together create a multisig address 2 of 2: To spend Dali, the two signatures are necessary. Ana deposits, for example, 1,000,000 of SATs. Before transmitting this deposit, the two sign a commitment transaction (commitment) that returns the balance to Ana if the channel closes at that moment. So Ana is never held hostage by Bruno.

Life cycle of a channel

1. OpeningBlockchain transaction locks the SATs on a multisig 2 of 2.
→
2. PaymentsEach payment generates a new state signed by both, outside of the blockchain.
→
3. RevocationThe old state is invalidated with the delivery of a revocation secret.
→
4. CLOSINGThe last state goes to the blockchain and each one gets their balance.
Only opening and closing take up block space. Thousands of payments can happen in between.

When Ana pays Bruno 20,000 SATs, the two sign a new commitment transaction with the 980,000 division for ANA and 20,000 for Bruno. Nothing goes to the blockchain. Each new signature updates the score. The channel can be closed cooperatively (the two sign a simple final transaction) or unilateral (one side publishes the last commitment it has in hand).

Why no one publishes an old state

If Bruno wanted to cheat, he could publish an old commitment in which he had the most balance. The protocol prevents this with revocation keys. Every time the score changes, each side gives the other the secret that revokes the previous state. In addition, those who publish their own commitment only receive their share after a waiting time (a relative timelock). In this interval, if the state is old, the other party uses the revocation secret and takes the entire balance of the channel. In the original article, this is called Breach Remedy: Punishment makes fraud a bad deal.

Warning: Punishment only works if someone is watching the blockchain. If your node is turned off for a long time, use a wallet that monitors you or a watchtower service. Never restore an old channel backup as if it were the current one: Publishing an old state can make you lose the balance.

HTLC: Paying Who You Don't Know

Direct channels do not climb: no one will open a channel with each bakery. Lightning solves this by chaining channels. If Ana has a channel with Bruno, and Bruno has a channel with Carla, Ana can pay Carla passing through Bruno. The obvious risk is Bruno to receive from Ana and not pass it on. The lock that prevents this is the HTLC (Hashed Timelock Contract).

  1. Carla creates a secretIt generates a random number (the pre-image) and sends Ana only his hash, inside the invoice.
  2. Ana stops payment with BrunoAna offers to Bruno: "These sats are yours if you show the pre-image of this hash in up to X blocks".
  3. Bruno repeats with CarlaBruno offers Carla the same hash, with a shorter term than his, to have time to collect Ana.
  4. Carla reveals the secretTo receive from Bruno, Carla shows the pre-image. Now Bruno knows the secret.
  5. Bruno cobra AnaWith the same pre-image, Bruno rescues Ana's HTLC. All balances are right.

The secret works like a cryptographic receipt: if Carla revealed the pre-image, she received it. Deadlines decrease with each jump, so that each intermediary always has time to charge whoever is before him. If something fails in the middle, the HTLCs expire and the money comes back. Either the payment is complete, or it does not.

Routing in onion and privacy

The route is chosen by the payer. Ana's portfolio knows the public map of channels, because the nodes advertise their channels and fees by gossip messages (Gossip), standardized in the BOLT 7 specification. (onion routing). Each node opens only its layer and discovers only who received it and to whom it should pass. Bruno doesn't know if Ana is the original payer or if Carla is the final recipient.

This gives more privacy than a transaction on the blockchain, which is public forever. But it's not total anonymity: public channels, values and times can be analyzed, and those who open the channel make the opening transaction visible on the blockchain. We talk more about this in the guide Privacy on Bitcoin.

Route and liquidity rates

Each intermediate node charges to pass it on. The rate has two parts, defined in the policy of each channel: one base rate, in Milissatoshis (MSat, one thousandth of Satoshi), charged for payment, and a rate, in parts per million (ppm) of the amount transferred. A rate of 100 ppm is equivalent to 0.01% of the value. The calculator above does this account with the median values of the network.

The other essential concept is the liquidity. The capacity of a channel is fixed, but it is divided between the two sides. If Ana has 980,000 SATs on her side, she can send her up to that. Bruno can only send Ana what is on his side. To receive payments, you need inflow liquidity, that is, balance on the side of the other end of the channel. That's why shopkeepers buy channels with inbound liquidity or use providers that take care of it, which is why large payments sometimes fail: there is a lack of balance in some part of the route.

ConceptWhat does it meanWhy does it matter
CapacityTOTAL SATS LOCKED ON THE CHANNELLimits the biggest payment possible there
Outgoing liquidityBalance on your sideHow much can you send
Inflow liquidityBalance on the other endHow much can you get
Base Rate (MSAT)Fixed amount per transferWeighs more on small payments
Proportional Rate (PPM)Fraction of the amount transferredWeighs more on large payments

Invoices: BOLT 11 and BOLT 12

Lightning's specification is divided into documents called BOLT (Basis of Lightning Technology), which are publicly held on GitHub. BOLT 11 defines the classic invoice, that long text that starts with lnbc And it usually turns into QR code. It brings value, hash of payment, expiration date and route tips. Each invoice must be paid once.

BOLT 12 defines the offers (Offers): A reusable code that the payer's wallet uses to request a new invoice through the Lightning network itself. It serves better for donations, subscriptions and refunds, because the recipient does not need to generate a QR Code for each payment. The support depends on the wallet and software of the node.

Map of the main BOLTs

BOLT 2Protocol between peers: Open, update and close channels.
BOLT 3Format of commitment and HTLC transactions.
BOLT 4Routing on onion of payments.
BOLT 5What to do on blockchain when a channel closes.
BOLT 7Advertisement of channels and fees (Gossip).
BOLT 11 and 12Invoices and payment offers.
Source: Lightning/Bolts repository on GitHub.

Custodial, self-custody or proper node

There are three ways to use Lightning, with different degrees of control. at custodial wallet, a company operates the node and you only have one balance in her account: it is simple, but the keys are not yours. at autocustody card for mobile, the app manages a light node and you save recovery words, as in any crypto wallet. In the own knot, you run the software (such as LND, Core Lightning or Eclair) on a complete Bitcoin node, open channels and manage liquidity. If you want to follow this path, start with the How to run a bitcoin node.

Pros

  • Almost instant payments and very low rate
  • Real Bitcoin, no new token or other blockchain
  • More privacy than blockchain transactions
  • Ideal for small amounts, such as payments on Satoshis

Cons

  • Requires being online or delegated surveillance
  • Liquidity limits the size of payments
  • Opening and closing channels is a fee on the blockchain
  • Channel backups are different from a regular seed

Taproot and what's next

Traditional channels use a multisig script that, when the channel closes, is visible on the blockchain. like Taproot And the Schnorr signatures, the specification went on to describe "Simple Taproot" channels, where the opening looks like a common single-key spending, thanks to MuSig2. The expected result is more privacy and smaller transactions. Adoption depends on each implementation.

To follow the Layer 1 side, which continues to define the cost of opening and closing channels, it is worth seeing the Mempool live. For values, use the converter Satoshi in Reais.

Tip: To test risk-free Lightning, start with a small amount in a self-custody wallet and make a few Satoshis payments. Write down the recovery words on paper and keep away from your cell phone.
Want to have your first Satoshis?Buy Bitcoin with Pix and then send it to your Lightning wallet. advertising. Crypto involves risk.Open free account

Frequently asked questions

Does Lightning Network have its own currency?

No. What circulates in Lightning is Bitcoin, locked in channels that can be closed on the blockchain at any time. There is no Lightning token.

What is an HTLC?

It is a contract that releases the payment to anyone who reveals a secret (the pre-image of a hash) within a deadline. He ties every jump from a route to the same secret, so either everyone gets it or no one gets it.

Why did my Lightning payment fail?

The most common reason is a lack of liquidity in some route channel or in the recipient's channel. Trying a lower value, waiting or using another route usually solves.

Can I lose money at Lightning?

Yes, if you use a custodial wallet that breaks, loses recovery words, or restores an old channel backup. Start with small amounts and prefer known self-custody cards.

What is inflow liquidity?

It's the balance on the other end of the channel. It defines how much you can receive. Without entry liquidity, a node can send but not receive payments.

References

  1. Poon and Dryja: The Bitcoin Lightning Network (Draft 0.5.9.2, 01/14/2016)
  2. BOLT Specifications (Lightning/Bolts, GitHub)
  3. Mempool.Space: Lightning Statistics (API)
  4. Mempool.Space: Lightning Panel
  5. Beep 327: MuSig2
  6. Bitcoin Optech: Technical Topics
  • Lightning Network
  • HTLC
  • Payment Channels
  • layer 2
  • bitcoin
  • BOLT

This content is informative and is not an investment recommendation. found an error? Write to contato@boletimbitcoin.com.

Read also

Start in crypto today

Open your free account, deposit with PIX and access Spot, Futures, Copy Trading and Trading Robots in one app.

  • Easy deposits
  • Copy trading
  • Grid and DCA bots
  • All-in-one app
Advertising. Crypto involves risk, invest responsibly.
Create free account