What every developer should know to build scalable DApps
A quick overview of Plasma
Part 1 : 35,000 ft view of Plasma
Part 2 : Technical and implementation details
Blockchain scalability
There’s been a lot of noise on the scalability aspect of blockchains in general. It is well documented as to how blockchains cannot handle large number of transactions. Ethereum in particular has atleast three major tracks of research to mitigate this
- Casper — Moving away from proof of work to proof of stake will enable faster transaction speed and lesser fuel burn.
- Sharding — Making transactions final by arriving at a consensus in small quorums instead of the entire network will enable faster transaction clearance.
- Plasma — Moving application specific data to a side chain while preserving the main chain security guarantees.
Plasma is probably the first of the research to be ready for implementation. We will dig deeper into what it offers.
What is Plasma?
Plasma is a scaling solution. It is not a new technology, it is not a new blockchain, it’s not a new DApp. Plasma is just a plain vanilla smart contract cleverly written so that a DApp can process all its transaction data without putting all the transactions on the Ethereum main chain. At the same time, the contract is written in such a way that the users have a guarantee that their assets (ETH, ERC-20 tokens or ERC-721 non fungible tokens) cannot be stolen by the DApp developer.
Plasma basically makes the following claim
A DApp developer can process transactions in any manner they wish which is immutably coded into a smart contract. If the developer cheats, they can at best clear invalid transactions — Plasma ensures that an honest user’s money is never stolen.
Let’s build an app.
For the sake of this explanation, lets take an example.
Let us assume we’re building a payment app like PayPal, but using ETH.
People can send money to others on the app for zero transaction fee, unlike the main chain where transaction fee is upto $0.5 per transaction.
Let’s design using Plasma. The Plasma contract consists of three top functions
- Transfering money
- Depositing money
- Withdrawing money
Transfering money
Since we are the developers of the DApp, we will run a server. This server will maintain a record of all the transactions that have happened on the chain. That is, who sent whom how much ETH and when.
All this data is to be made publicly available in real time.
All transaction data is collected in blocks of say one hour each. That is all transactions that happened in a given hour is contained in the same block. At the end of each hour, the server will submit a hash of all the transactions that have been included in the block — called a merkle hash— to the Plasma contract that resides on the Ethereum main chain. For every transaction that has been recorded in the block, a proof can be generated that the transaction indeed was included in the block. This proof is called a merkle proof.
The contract now has the hashes of all the blocks that have ever been submitted, each seperated by one hour. It is at the sole discretion of the server to include or reject a transaction. A transaction can be denied because either the transaction is invalid (sender doesn’t have enough balance to make the transfer) or the server wants to troll a user.
However, once a merkle hash has been submitted to the main chain, there is no way to refute that a certain transaction was included in a block, because we have a merkle proof of all transactions.
[Tech details, feel free to skip] All transactions happen as UTXOs. UTXO stands for unspent transaction output. If Alice sends 1 ETH to Bob, she creates a new UTXO U1. Now Bob can use the amount in the UTXO U1 (1 ETH). If Bob wants to send 0.7 ETH to Charlie, he would use the UTXO U1 and create a UTXO U2 of 0.7 ETH towards Charlie and U3 with 0.3 ETH to himself. Now UTXO U1 is spent. It cannot be used again. Charlie and Bob may now use U2 and U3 respectively. The balance of Bob’s account is the sum total of all the UTXOs addressed to him.
Depositing money
To start sending money on our app, the person needs to fill up her wallet, just like on PayPal. To do this, she will send say 10 ETH to the contract on the main chain.
The contract will immediately create a new block with exactly one transaction and add it to the list of all blocks submitted by our server. This allows our user to be sure that her initial balance will definitely be irrefutable.
Once this transfer is made, our server should update the balance of the user who just deposited the 10 ETH and allow transactions to happen with this new balance immediately.
Withdrawing money
This is the tricky part.
There are two reasons why a person might want to withdraw money.
- Just wants to cash out the money she has made on the app. Equivalent of converting money on PayPal to your bank balance.
- If she sees that the server is misbehaving and loses trust. She doesn’t want to lose her money.
In either case, the user will make a call to the Plasma contract on the main chain wanting to exit.
To exit, the user must make a claim about the amount she wants to withdraw. To do so she’ll present the UTXO she wants to withdraw, and show that this UTXO actually exists in a block that has already been submitted to the main chain in the past.
Once this exit request is made, and it has been proven that the amount (UTXO) she wants to withdraw is indeed a valid withdrawal, the request is added to an exit queue. The ETH will actually get withdrawn after 7 days from the time of requesting withdrawal.
In these 7 days, someone can challenge the withdrawal. If someone is able to prove that the actual amount the person is trying to withdraw is more than the amount she owns, the exit is cancelled. To do this, the challenger will present the proof of a transaction where the UTXO of which the exit was attempted has been used.
The 7 days is ofcourse variable. It should be long enough that everyone on the system has access to internet once in this time interval so that she may challenge when someone tries to exit with her money. But it should be short enough to provide for a good user experience.
Advantages of the Plasma Scheme
- Low per transaction cost
- Main chain security guarantees of asset ownership. That is, if you trust the Ethereum blockchain, you can trust that your money is safe even if you don’t trust the operator of the Plasma server.
- More transactions per second. A transaction can be settled as long as it has been accepted by the operator of the Plasma server, which can be instantaneous as against 10 minutes on the Ethereum main chain.
Disadvantages of the Plasma Scheme
There are a bunch of tradeoffs made.
- Withdrawal takes time (7 days)
- Every client needs to watch for fraud. And exit within 7 days of a potential fraud being included in a block. If a user is off-grid for more than 7 days, their money is at risk. Either the user must withdraw before going off grid or have access to the internet for atleast once in the span of every 7 days.
Implementation
There are two popular implementations proposed as of date
Part 2
Coming soon, diving deep into Plasma MVP and the Plasma whitepaper.