Learning Layer 2 Technologies -- Lightning Network
An introduction to layer 2 technologies, why they are needed and how lightning network works.
Introduction
For the last few weeks, I started learning about cryptocurrencies to see if investing in crypto through crypto exchanges is worthwhile. I learned a few things along the way and in this post, I want to elaborate on something I found interesting: Layer 2 technologies used on top of the blockchain.

How do payments work on traditional Bitcoin blockchain?
To understand the use of layer 2 technologies, it is important to understand what it means to buy or sell crypto. Let’s take Bitcoin as an example. On average, it takes 10 mins to confirm a Bitcoin transaction. This means once you make a buy or a sell request, you have to wait 10 mins for it to go through. This is because there are certain steps that the blockchain must take before your transaction is considered valid.
When you make a transaction request, your transaction is added to the Bitcoin Mempool. A mempool has all the transactions that are yet unconfirmed, which means the transaction request to buy or sell has been made, but it is yet to be validated and added to the blockchain as an immutable record.
Once the transaction is added to the mempool, miners can pick up transactions and solve the mathematical cryptographic puzzle to confirm a set of transactions. The miners receive rewards for confirming the block. Now it is easy to see that without any incentive to pick up a particular transaction, the miners will pretty much pick up the transactions in random order. This could mean longer wait times for buyers and sellers. Hence in the transaction, the participants can add a fee. For example, if A wants to sell 1 BTC to B, A can send 1.5 BTC in the transaction, with the instruction to send 1 BTC of that to B and the rest to the miner who confirms the transaction. Since this is in addition to the default reward, the miner will likely pick up this transaction first. These incentives go up as more transactions remain unconfirmed and hence raise the cost of transactions making it unfeasible to do micropayments. (It does not make sense to send BTC worth 10 rupees if the transaction cost is 50 rupees)
Once the transaction is added to the block, sent out to the BTC network, and receives 6 confirmations, you can securely say that the transaction is completed successfully.
Now if you have ever used any crypto exchange, you will know that you get the confirmation of buying crypto almost instantaneously. So the question I had was if the process of confirmation of a transaction takes 10 minutes, how can the exchanges give an instant confirmation for my buy or sell request? Although the logic behind this question is grossly incorrect, it led me to the world of layer 2 technologies, which I will elaborate on in the next section. I just want to make it clear that exchanges do not directly use layer 2 technologies, but they do use off-chain transactions i.e. they add transactions in their order book instead of the actual blockchain for faster transactions. An exchange is just a platform to connect buyers and sellers. Regardless, because of my lack of understanding regarding the traditional exchanges, I stumbled upon the layer 2 technologies.
Layer 2 Technologies
Introduction to the lightning network
Wait time of 10 minutes is not feasible if we want to transact at scale. Imagine waiting 10 mins to buy a bottle of coke after making the payment in bitcoin. This is where layer 2 technologies come in. The idea is simple, make the transaction speed quicker while maintaining the basic security promise offered by the blockchain consensus mechanism. The off-chain solution for Bitcoin blockchain is the Lightning Network. It is an off-chain peer-to-peer protocol that allows wait times much lower than the Bitcoin main-chain.
The core idea that the lightning network uses is: In a peer-to-peer transaction process where the transactions continue for let’s say n days, the main-chain does not need to know all the transactions that happened in those n days, it only needs to know the final balances that the two parties have after all the transactions over the n days are complete. It can be easily seen that this process will be faster as in a P2P transactions, the layer 2 network has to keep track of the transactions that happen only between n parties and then broadcast final balances back to the main chain.

This record over n days is called a channel in the Lightning Network. Now opening unique channels between every set of 2 individuals will again run into scalability issues. The Lightning Network instead posits that for a large set of channels open and with at least one open channel per user, the system can be scaled. For example, if A has a channel with B and B has a channel with C and C has a channel with D, A can transact with D through B-C path. This is similar to strongly connected components in a directed graph, where you can reach any vertex from any other vertex in the graph.
How it works under the hood
The lightning network uses something called as multisig address. In a x-of-y multisig address, y private keys can transact and x must sign the transaction for it to be valid. In a traditional two-party peer-to-peer transaction, it must be a 2-of-2 multisig address which means if A and B are the participants in the channel, both must sign every transaction for it to be valid.
In addition to this, the lightning network also uses Hash Timelock Contracts (HTLC) to keep the parties honest. Let’s see in brief, how HTLC works in a 2-of-2 multisig scheme. If A and B open a channel, for every transaction, A will calculate a hash of a secret, SecA that only A knows, and send this hash value Hash(SecA) to B. Similarly B will send Hash(SecB) to A.
Now if A receives funds from B, the only way they can spend it is to provide the secret SecB that matches with value Hash(SecB) sent by B. Due to the property of the hash function, it is near impossible to guess the original secret from its hash value.
It is important to note that, the reason this mechanism is required is that in a multisig address, the funds are added mutually by A and B (A adds Na BTC and B adds Nb BTC then the multisig address holds Na+Nb BTC) and the transactions (Change in balances) can also be added by A and B. So if A adds a transaction saying B sent me 1 BTC, unless B provides his original secret, A cannot spend the 1 BTC and B will send it only if they agree with the transaction entry.
Now, this runs into another problem, what if B is a bad actor and they decide to stall sharing their secret forever? Then A’s payment of 1 BTC is stuck in the multisig address. Hence, we have the timelock concept, which adds to our original rule as follows:
Now if A receives funds from B, the only way they can spend it is to provide the secret SecB that matches with value Hash(SecB) sent by B. Due to the property of the hash, it is near impossible to guess the original secret from its hash value, or after the timelock expires. This ensures that B must agree or deny the transaction within the timelock period or they lose their vote.
When the transactions have to be settled, both parties can sign and broadcast the most-recent transaction back to the main-chain. In this way, all the transactions from the inception to the closure of the channel are abstracted out of the main-chain and only the final balances are settled.
Conclusion
For cryptocurrencies to be adopted globally, it is extremely important to enable fast transactions at low transaction fees. Layer 2 technology offers a promising solution to scale up such transactions while keeping intact all the core principles of the underlying blockchain.
Hope you liked this article. Cheers!
