Routing & HTLCs Deep Dive
14 min readarticleIncludes quiz · 2 questions
Lightning routing enables payments to travel across multiple channels, creating a network effect that scales Bitcoin transactions globally. The secret sauce is Hash Time Locked Contracts (HTLCs).
How routing works:
1. Source Routing: Sender finds path to receiver 2. HTLC Creation: Each hop creates conditional payment 3. Secret Sharing: Payment secret flows back along path 4. Settlement: Each hop settles when secret is revealed 5. Cleanup: Failed routes are automatically cleaned up
HTLC components:
- •Hash Lock: Payment conditional on revealing preimage
- •Time Lock: Expiry mechanism to prevent stuck payments
- •Atomic Execution: Either entire payment succeeds or fails
- •Fee Structure: Each hop charges small routing fee
Route finding algorithms:
- •Dijkstra's Algorithm: Find shortest path by fee
- •Multi-path Payments: Split payment across routes
- •Liquidity Probing: Discover available channel capacity
HTLC Implementation Example
# HTLC Output Script
OP_DUP OP_HASH160 <H> OP_EQUALVERIFY OP_CHECKSIG
OP_IF
<recipient_pubkey>
OP_ELSE
<timeout> OP_CHECKSEQUENCEVERIFY OP_DROP
<sender_pubkey>
OP_ENDIF
# Payment flow
def create_htlc(amount, hash_lock, timeout, recipient, sender):
return ConditionalPayment(
amount=amount,
condition=hash_lock,
timeout=timeout,
recipient=recipient,
fallback=sender
)Test Your Knowledge
This lesson includes a 2-question quiz (passing score: 85%).
Quiz functionality available in the mobile app.