HTLC, what is this?
An HTLC (Hash Time-Locked Contract) is a special type of Bitcoin script used in the Lightning Network to route payments securely across multiple channels. They ensure a payment either goes through entirely or is refunded, with no possibility of funds getting stuck or someone cheating. This is necessary for transactions being routed correctly and fairly on the network.
How does a HTLC work?
Hashlock: The recipient creates a secret value (payment preimage) and calculates its cryptographic hash (payment hash). This hash acts like a lock on the funds, and only the recipient, who knows the secret preimage, can unlock it.
Timelock: Each HTLC has a time limit (block height) set by the sender. If the recipient doesn't claim the funds by revealing the preimage before the timelock expires, the sender gets a refund.
This combination of hashlock and timelock ensures atomicity and trustless operation. Either all the nodes in a payment route successfully process and unlock the HTLCs in sequence, or the entire payment fails, and everyone gets their funds back.
Simplified example of HTLC script:
OP_SHA256 <H> OP_EQUALVERIFY <Bob's Pub> OP_CHECKSIG
OP_ELSE
<Alice's Pub> OP_CHECKSIG
<Timelock> OP_CHECKLOCKTIMEVERIFY OP_DROP
OP_ENDIF
The commitment transaction
The commitment transaction serves as a record of the current balance between two parties (channel partners) engaged in a payment channel. It commits both the channel partners to distribute the channel balance.
Let’s imagine Alice and Bob open a payment channel with a funding transaction. This transaction locks the desired amount of Bitcoin into a 2-of-2 multisignature address, requiring both Alice and Bob to sign any spending transaction. This locked Bitcoin represents the channel capacity, the total amount that can be transacted within the channel.
As Alice and Bob make payments back and forth within the channel, they don't broadcast each transaction to the Bitcoin timechain. Instead, they update their channel's balance by creating a new commitment transaction that reflects the latest state of their agreement. Each commitment transaction represents the status of the channel's balance at a particular moment.
Transactions Causing Channel Closure
A Lightning Network channel can be closed in three ways:
Mutual Close: Both channel partners agree to close and co-operate to create a closing transaction that distributes the final channel balance immediately to their respective wallets. This is the preferred method, as it's typically faster and involves lower fees compared to other methods.
Force Close: One partner decides to close the channel unilaterally, usually when the other partner is unresponsive. They broadcast the latest commitment transaction, which includes timelocks that delay the claiming of funds and higher on-chain fees.
Protocol Breach: A channel partner attempts to cheat by broadcasting an outdated commitment transaction to claim more funds than they are entitled to. The honest partner can then use a penalty transaction to seize all funds in the channel.
Specific transactions that can trigger a channel closure:
Broadcasting a commitment transaction: Whether it’s a force close or a protocol breach, broadcasting a commitment transaction to the Bitcoin blockchain will always close the channel.
Publishing a closing transaction: A mutual close finalizes with a closing transaction being broadcast, effectively closing the channel and distributing the funds.
Failure to resolve HTLCs: While not a specific transaction, if HTLCs in a channel cannot be settled or refunded (e.g., due to a node going offline), it might lead to a force close to reclaim funds.
The choice of closing method depends on the circumstances and whether the channel partner is cooperative. However, the most important point is that the channel closure is always triggered by a transaction being broadcast to the Bitcoin blockchain.
Failure to Resolve HTLCs
When a payment is routed through the Lightning Network, each intermediary node (hop) creates an HTLC with the next hop in the route. The HTLCs ensure that either the entire payment is successful, or the funds are refunded to the sender. A failure to resolve these HTLCs can happen for several reasons, ultimately leading to the closure of the channel.
Node Failure: If an intermediary node in a payment route goes offline, it cannot settle or refund the HTLCs it is holding. This node failure can be temporary (e.g., a software crash) or permanent (e.g., a hardware failure).
Channel Failure: A channel may not be able to handle an HTLC due to insufficient liquidity, incorrect timelocks, or specific channel features not being met. This can lead to the HTLC being returned with an error.
Stuck Payments: Occasionally, an HTLC can become "stuck," meaning it is neither fulfilled nor cancelled with an error. This can be due to bugs, unresponsive nodes, or even malicious behaviour.
Consequences of Unresolved HTLCs
When HTLCs cannot be resolved, it disrupts the payment flow and can have several consequences:
Payment Failure: The intended recipient does not receive the payment, and the sender might receive an error message or no notification at all.
Funds Locked Up: The funds committed to the unresolved HTLCs remain unavailable to the sender until the HTLCs expire, which can be a significant inconvenience.
Channel Closure: If the unresolved HTLCs are not addressed within a certain timeframe, one of the channel partners might decide to force close the channel to reclaim their funds. This involves broadcasting the latest commitment transaction to the Bitcoin blockchain, which can be costly due to on-chain fees.
Resolving Unresolved HTLCs
The resolution process for unresolved HTLCs depends on the cause of the failure:
Node Failure: If the node comes back online, the HTLCs can often be settled or refunded normally. If the node remains offline, the timelock in the HTLC will eventually expire, allowing the sender to claim a refund.
Channel Failure: The sender can try to find an alternative path for the payment. The error messages returned by the network can help update the sender's understanding of channel liquidity and routing conditions, improving the success rate of future payment attempts.
Stuck Payments: Currently, stuck payments are resolved by waiting for the HTLC timelock to expire. Research is ongoing to implement "stuckless payments" using Point Time-Locked Contracts (PTLCs) to offer more efficient solutions in the future.
Feel free to share and comment this article. Follow me also on social networks for more infos and value contents.
Massimo, the educator- good stuff