Scaling Blockchains With Layer 2 Solutions

Scaling Blockchains With Layer 2 Solutions

Blockchain scalability is a big problem that all growing blockchains face sooner or later. But what does it mean to scale a blockchain and why do we need to do it? Simply put, as the number of people that use a certain blockchain grow, the amount of computational and storage pressure on the blockchain increases. The blockchain now has to process a much higher number of transactions on the same architecture. This raises transaction costs significantly because people want miners to prioritise their transactions. The result is people who want to do small transactions getting priced out because the fees used in the transactions run up to hundreds of dollars for even very small transactions. Ethereum, I'm looking at you 👀

Also, the amount of data stored on the blockchain increases with each block that gets added. This means each node and validator needs to store much larger amounts of data that costs more. Thus, not a lot of people can run nodes and validate the blockchain, hence downplaying the actual security of the protocol. All of these problems need to be solved by scaling blockchains in a way that they can process a larger number of transactions seamlessly. That's where scaling solutions come in.

scalability chart.png

Layer 1 solutions basically involve keeping all transactions on-chain (i.e. on the main chain) and scaling the main chain itself. They include solutions like coming up with better consensus mechanisms, sharding, etc. However, in this blog, we're more interested in going over layer 2 solutions. We'll take a look at some of the famous layer 2 solutions and understand how they work. Let's get started!

Layer 2 Solutions

Solutions that help in scaling blockchains by moving transactions off-chain (i.e. away from the main chain) are called Layer 2 solutions. These solutions work by doing the actual execution and computation of the transactions off-chain. They interact with the main chain when transactions are completed and a historical record of the transactions happening properly needs to be recorded on the parent blockchain. Hence the execution and data pertaining to transactions are decoupled using these solutions. Multiple transactions can be bundled up and recorded together in a single transaction, thus decreasing the number of times that users have to interact with the main chain. This greatly increases throughput and decreases fees.

Let's take a look at some popular Layer 2 solutions.

1. State Channels

Let's assume there is a group of people that want to do many transactions among them. If they were to do these transactions on the main chain, they would have to pay fees for each transaction and would also congest the main network. This is where state channels come in.

All users first deposit funds to a multi-sig contract (a contract that requires signatures by multiple people) by interacting with the main chain. Once this is done, they can now start sending money to each other in multiple transactions without interacting with the main chain. They do this by recording signed updates of the money present in their account after each transaction. These records are called state updates, and that's where the name state channel comes from. Once the transactions are done, any participant can perform an exit by sending the latest state update to the main chain. The main chain then sends the deposited funds to the participants as per the amount reflected in their account according to this latest state update.

Untitled.png

It is possible for participants to send wrong state updates to the network to try and get more funds than what they rightfully own. Hence, after each exit claim, a review period is kept for other participants to verify the exit claim 🔍

State channels have some advantages like being simple in nature and users having to pay fees just twice - while depositing funds in the smart contract and while exiting it. They are very useful when a fixed number of people want to do a lot of transactions among themselves. But they aren't very widely used due to notable disadvantages like:

  1. State channels don't have the functionality to run smart contracts, hence can support just simple transactions between parties.
  2. Number of participants is fixed and must be known upfront, new participants can't be added.
  3. The funds stay locked in the smart contract until an exit is performed.
  4. All participants have to be available a lot of the time to ensure that no one is being malicious. Withdrawals from state channels take time due to the review period.

2. Sidechains

A sidechain is another popular layer 2 solution. Sidechains are completely different blockchains working alongside the main chain. Sidechains have their own separate token, consensus mechanism, tokenomics, etc. Thus, the security of each sidechain depends on the sidechain itself and not so much on the main chain.

Sidechains use a two-way bridge to connect to the main chain: Users on the main chain deposit their funds to the bridge and receive the sidechain’s tokens in return. They can then use these tokens to carry out their transactions on the sidechain, and once everything is done, they can use the bridge once again to receive back their funds on the main chain by giving back the sidechain’s tokens. This takes pressure away from the main chain.

Untitled (1).png

Sidechains are blockchain agnostic and can work with multiple blockchains. Each main chain too can work with multiple sidechains. Since sidechains are blockchains in their own rights, they are able to run smart contracts.

However, a notable disadvantage with sidechains is that they are much less secure than the main chain due to the presence of a lesser number of validators. This is because they are newer and smaller chains and haven't had the time yet to grow to acquire more validators, unlike main chains. Also, the token of the sidechain itself needs to have some inherent value, hence designing good tokenomics for these sidechains is a must.

3. Rollups

Rollups are a form of layer 2 solutions where actual transactions happen on layer 2 and just a record of these transactions needs to be submitted to layer 1. Thus thousands of transactions can happen in layer 2, while just the record of these transactions happening needs to be rolled up in a batch and recorded on layer 1 in a single transaction. This means that much lesser data gets stored on layer 1. But you may be thinking, if we're simply submitting a record of transactions, then malicious users can submit a wrong record which says something like "all the funds are transferred to my wallet in this set of transactions". This is where the two different types of rollups come in and solve this problem in two different ways:

3.a. Optimistic Rollups

Optimistic rollups post transactions' data to layer 1 and assume that it is valid and correct, hence the name optimistic! This means optimistic rollups don't need to do any extra validation in the cases where the submitted data is actually correct and no foul play is involved.

Optimistic rollups use something called a Fraud Proof for the cases where wrong and malicious data is submitted. Whenever a participant in the network finds an incorrect transaction that has been submitted, they can submit proof of the fraud to the rollup's smart contract. The smart contract then executes the contested transaction on layer 1 (remember that earlier this transaction was actually executed on layer 2 and just the record of it being executed was submitted to layer 1) and if the transaction indeed turns out to be fraudulent, then the participant who had tried to submit this fraudulent transaction gets penalised. How are they penalised? Well, their funds are snatched from them and given to the whistleblower - that is, the person who raised the alarm that a fraudulent transaction has been submitted. Hence bad actors are disincentivised to submit fraudulent transactions and good actors are incentivised to watch out for such transactions.

Withdrawals from optimistic rollups take some time since a dispute period is given for whistleblowers to check and report any fraudulent transactions.

3.b. ZK Rollups

We saw that optimistic rollups run the actual execution of transactions on layer 1 only when a transaction is contested. ZK rollups, however, contain something called a Validity Proof that must be generated for each set of transactions and verified on layer 1.

ZK rollups use a clever piece of cryptography called Zero-Knowledge Proofs which gives them the name - ZK rollups. In this type of rollup, each batch of transactions that is submitted to layer 1 contains a cryptographic proof called ZK-SNARK. This proof can be run on layer 1 to quickly validate whether the batch contains valid transactions or not. Running the proof on layer 1 takes much lesser time than the time it would have taken to run the actual transactions. This is the validity proof that I talked about in the previous paragraph.

The advantages of ZK rollups are that almost no time is required in moving funds between layer 1 and layer 2 since a validity proof has already been generated and verified. Therefore it's much faster than optimistic rollups to withdraw funds from ZK rollups. However, one disadvantage is that smart contract compatibility for ZK rollups is still in the early stages - it's much difficult to run smart contracts on ZK rollups based solutions due to the complexity of ZK rollups.

Conclusion

We took a look at some popular layer 2 solutions and compared their disadvantages and advantages. The general community currently feels that rollup technology is the solution that will be the most effective in solving the problem of blockchain scalability using layer 2 solutions in the near future. But all other solutions have their merits for specific use-cases and can't be written off very easily. This is a very interesting space and it'll be very exciting to see how these layer 2 solutions evolve and help make blockchains easier and more feasible to use.