Source: Byte Yuan CKB
As a P2P network built on the Bitcoin blockchain, the Lightning Network can not only effectively solve the problem of Bitcoin being "slow and expensive", but also has unparalleled advantages over other solutions in the field of encrypted payments. It is the foundation for us to realize the P2P economy and an important part of the Web5 world we envision.
In this article today, we will continue to introduce the Lightning Network and explain the operating principles and related technologies of the Lightning Network.
The cornerstone of the Lightning Network: Payment Channel
The Lightning Network is essentially a carefully designed payment channel system.Take the transaction between Alice and Bob as an example. When they use the Lightning Network for settlement, they will go through the following three key stages: opening the channel, conducting transactions, and closing the channel.
1. Opening a channel: Building a bridge of trust
Opening a channel essentially means creating a multi-signature wallet controlled by both parties and injecting funds into the wallet. The total amount of funds received by this wallet is the balance of this channel.
Let's assume that Alice and Bob each hold 100,000 satoshis BTC. First, they need to transfer their respective 100,000 satoshis to a multi-signature wallet address controlled by both parties as a deposit to establish the channel. This transaction, called a "Funding Transaction" or "Anchor Transaction", needs to be broadcast to the network and recorded on the Bitcoin blockchain to indicate the official opening of the channel. In this example, the payment channel connecting Alice and Bob is actually an ordinary 2-of-2 multi-signature wallet with a balance of 200,000 satoshis (for simplicity, we will not consider the mining fee that needs to be paid when opening the channel).
2. Start trading: the art of off-chain trading
After the channel is opened, all subsequent transactions between the two parties will be conducted outside the Bitcoin blockchain, achieving efficient off-chain settlement. Let's assume that Alice and Bob have the following three transactions:
Alice transfers 10,000 satoshis to Bob. At this time, Alice has 90,000 satoshis and Bob has 110,000 satoshis.
Alice continues to transfer 20,000 satoshis to Bob. At this time, Alice has 70,000 satoshis and Bob has 130,000 satoshis.
Bob transfers 10,000 satoshis to Alice. At this time, Alice has 80,000 satoshis and Bob has 120,000 satoshis.
From the above example, we can see that no matter how many times Alice and Bob transfer money, the balance of the channel between them always remains the same, which is 200,000 satoshis.
Every time Alice and Bob have a transaction, both parties need to update their respective balances in the channel and exchange signed "Commitment Transactions". These transactions are valid in themselves and can be sent to the Bitcoin network at any time, but both parties usually save them and do not broadcast them unless they intend to close the channel. In this way, the balance status of Alice and Bob in the channel can change dozens or hundreds of times in a second. The speed of the update is only limited by the speed at which the two parties create, sign and send commitment transactions to each other.
3. Closing the channel: the moment of final settlement
Channels can be closed in two ways: one is that both parties agree to close and send a settlement transaction to the Bitcoin network; the other is that one party decides to close and sends the last commitment transaction to the Bitcoin network. The latter is designed to prevent the situation where one party is offline and the other party's balance in the channel is always locked.
In the example of Alice and Bob, they decided to close the channel after conducting 3 transactions. After the closure, Alice gets back 80,000 Satoshis and Bob gets back 120,000 Satoshis (again, to simplify the explanation, we do not consider the mining fees generated when closing the channel).
Security of Bidirectional Payment Channels: From LN-Penalty to eltoo to Daric
As mentioned above, a channel can be closed by mutual agreement or by a unilateral decision. So, in a bidirectional payment channel, how to prevent one party from committing fraud? For example, in the example above, how to prevent Bob from closing the channel with the second commitment transaction instead of the third? If the second commitment transaction is used to close the channel, Bob can deny the last payment of 10,000 satoshis to Alice, which obviously looks very tempting.
The Bitcoin Lightning Network introduces the LN-Penalty mechanism to prevent one party from sending an outdated but beneficial state to the chain to close the channel. LN-Penalty uses complex techniques such as "asymmetric commitment transactions" and "revoke keys" to ensure that if Bob tries to close the channel using an outdated commitment transaction, Alice will be entitled to all the funds in the channel - 200,000 satoshis. This penalty mechanism effectively curbs potential fraud.
However, LN-Penalty also has some shortcomings. In addition to its own complexity, it also creates a storage burden - users must save information about every time the channel status has been updated in the past, and there are some rare situations that may cause it to accidentally punish honest users.
To overcome these shortcomings, as early as 2018, the Bitcoin community proposed a solution called "eltoo" to eliminate the risk of storage burden and accidental punishment. In this solution, users only need to save the latest commitment transaction and its settlement transaction. However, the eltoo solution is not available yet because its implementation requires a soft fork of Bitcoin to introduce a new signature hash type - SIGHASH_ANYPREVOUT.
Nervos CKB's Lightning Network Fiber Network chose the Daric solution proposed in 2022. Daric effectively solves the cost and security issues of Watchtowers based on eltoo. Watchtowers are Lightning Network nodes running on independent machines and networks. They monitor payment channels and help victims execute penalty transactions when malicious behavior is detected, thereby protecting users' funds. Node operators usually run their own watchtowers to protect their own nodes.
Conclusion
As a solution for Bitcoin expansion, the core of the Lightning Network lies in the cleverly designed payment channel system. Through the three stages of opening channels, off-chain transactions, and closing channels, the Lightning Network has greatly increased transaction speed and reduced costs.
To ensure the security of the two-way payment channel, the Lightning Network uses the LN-Penalty mechanism. The Bitcoin community has also proposed better security mechanisms such as eltoo and Daric to effectively prevent potential fraud.
These innovations have enabled the Lightning Network to significantly improve the performance of the Bitcoin network while ensuring security, paving the way for fast, low-cost small payments.