MASTERING-BITCOIN · Interactive Practice | Unit 15 · Video 7

Paying a Stranger Trustlessly: Lightning Network Routing

IKey Formulas & Rules

Concept Rule Meaning
H=hash(R)H = \text{hash}(R) One-way hash Anyone can compute HH from RR, but not RR from HH
amountupstream=amountdownstream+fee\text{amount}_{\text{upstream}} = \text{amount}_{\text{downstream}} + \text{fee} Amounts grow upstream Each hop bundles in fees for hops below
Alice pays=payment+nfee\text{Alice pays} = \text{payment} + n \cdot \text{fee} Total cost nn = number of forwarding intermediaries
timelockupstream>timelockdownstream\text{timelock}_{\text{upstream}} > \text{timelock}_{\text{downstream}} Staggered deadlines Each party gets time to claim upstream after a downstream reveal

IIInteractive Visualizations

Visualization 1: How Amounts Shrink Downstream

Conceptual question: Why does Alice send more than the 1 BTC Eric is owed?

Each intermediary keeps a small fee for forwarding the payment, so the amount offered shrinks at every hop while Eric still receives the full payment. Adjust the fee per hop and the payment amount and watch the offers change.

⚙ interactive visualization — coming to this page format soon

Notice: Set the fee to 0 and all bars become equal — Eric gets exactly what Alice sends. As the fee grows, the gap between Alice's offer and Eric's receipt widens, because every hop above Eric bundles in the fees for the hops below.

Visualization 2: Why Timelocks Must Step Down

Conceptual question: What goes wrong if every HTLC has the same timelock?

After Eric reveals the secret RR at the last moment, each party needs time to turn around and claim from the party above them before their own refund deadline. Adjust the gap per hop and watch the safety margin appear or vanish.

⚙ interactive visualization — coming to this page format soon

Reflect: With a gap of 0, all bars are equal length — there is no safety margin, so a late reveal downstream could leave an upstream HTLC already expired and refunded (a race condition). A gap of at least 1 block per hop guarantees every party can always settle upstream after a downstream reveal.

Visualization 3: The Alice–Bob Channel Bookkeeping

Conceptual question: What happens to the channel balance while a payment is "in flight" versus after it settles?

The Alice–Bob channel holds 4 BTC total. During the payment it splits into three outputs (Bob's balance, Alice's balance, and the locked HTLC). Once Bob reveals RR, the locked output collapses into Bob's balance. Adjust the HTLC amount.

⚙ interactive visualization — coming to this page format soon

Notice: The total never exceeds the channel's 4 BTC capacity. With the HTLC at 1.003 BTC, Alice ends with 0.997 BTC and Bob ends with 3.003 BTC — Bob earned his 0.001 fee while passing the payment along. The HTLC is just a temporary lock that resolves into normal balance once the secret is revealed.

IIIQuiz Questions

Question 1

In the route Alice — Bob — Carol — Diana — Eric, Diana offers Eric exactly 1.000 BTC. Each intermediary keeps a fee of 0.001 BTC.

How much does Carol offer Diana?

Correct! Carol offers Diana 1.001 BTC so Diana can forward 1.000 and keep her 0.001 fee.

Not quite. Each upstream offer adds exactly one 0.001 BTC fee.

Show solution

Solution:

Amounts grow as we move upstream. Diana forwards 1.000 BTC to Eric and keeps a 0.001 BTC fee, so she must be offered:

amountCarolDiana=1.000+0.001=1.001 BTC\text{amount}_{\text{Carol}\to\text{Diana}} = 1.000 + 0.001 = 1.001 \text{ BTC}

The answer is 1.001 BTC.

Question 2

Suppose every HTLC in the route is given the same timelock instead of staggered (descending) timelocks.

What problem does this create?

Correct! Equal timelocks create a race condition where upstream HTLCs can expire before being claimed.

Not quite. Think about timing: who needs more time after a late downstream reveal?

Show solution

Solution:

Each upstream party needs a longer safety window than the party below them. If a downstream party reveals the secret at the last moment, the upstream party still needs time to turn around and claim before their own refund deadline.

With equal timelocks, a downstream party could reveal so late that the upstream HTLC has already expired and refunded — a race condition. Staggering the deadlines (e.g. one block gap per hop) eliminates this risk.

Question 3

True or False: An intermediary like Carol can claim her incoming HTLC payment from Bob without ever revealing the secret RR to Bob.

Correct! Claiming requires revealing R, which simultaneously lets the upstream party claim.

Not quite. The hash lock forces the claimant to reveal R to spend the output.

Show solution

Solution:

The claim branch of an HTLC requires providing RR where hash(R)=H\text{hash}(R) = H. So Carol cannot claim from Bob without presenting RR — and the moment she does, Bob learns RR too and can immediately claim from his upstream party (Alice).

This is the key to atomicity: revealing RR is the only way to claim, and revealing it automatically lets the party above you claim as well. Either everyone settles, or no one loses funds.

The statement is False.

Question 4

Alice pays Eric 1 BTC routed through three intermediaries (Bob, Carol, Diana), each charging a fee of 0.001 BTC.

What is Alice's total cost for this payment?

Correct! 1.000 + 3 × 0.001 = 1.003 BTC total.

Not quite. Add one 0.001 BTC fee for each of the three forwarding intermediaries.

Show solution

Solution:

Total cost is the payment plus one fee for each forwarding intermediary:

Alice pays=payment+nfee=1.000+3×0.001=1.003 BTC\text{Alice pays} = \text{payment} + n \cdot \text{fee} = 1.000 + 3 \times 0.001 = 1.003 \text{ BTC}

Eric still receives the full 1.000 BTC; the 0.003 BTC fee pool is shared among the three intermediaries — far cheaper than opening a direct on-chain channel.

The answer is 1.003 BTC.

Solved: 0 / 4