Before you go, check out these stories!

0
Hackernoon logoRChain — Multi-Threading Blockchain — Visa alike Scalability by@michielmulders

RChain — Multi-Threading Blockchain — Visa alike Scalability

Author profile picture

@michielmuldersMichiel Mulders

As everyone knows, cryptocurrencies had a fantastic year in 2017. As more tokens, investors, and startups joined the space, scalability emerged as a serious issue. Blockchain scalability is a key element for mass adoption and further growth of the industry. We all know the CryptoKitties example which caused the Ethereum network to slow down due to an enormous backlog of transactions. Since then, awareness increased for more scalable solutions.

RChain is a project which focuses on scalability by using a multi-threaded blockchain with its own smart contract language. It wants to compete with top projects like Ethereum. RChain is an open-sourced project with the goal of building a decentralized and censorship-resistant economic blockchain platform to be used as a public computing infrastructure. Let’s take a look.

Architecture

RChain is built up following a couple of minimal requirements:

  • Dynamic smart contract functionality to allow more use cases to be implemented.
  • Concurrent execution; Multiple independent smart contracts can operate next to each other.
  • Computationally non-intensive consensus protocol in order not to waste resources: Casper Protocol.

The Java Virtual Machine (JVM) is the foundation of Rho Virtual Machine (RVM). The RVM Execution Environment can operate multiple RVMs with each running a smart contract concurrently and in a multi-threaded fashion. The concurrent structure allows for independent processes to compose into complex processes without competing for resources. This architecture will enable a multi-chain (multiple blockchains per node) effect where transactions are handled on independently executing VM instances.

Consensus — Correct by Construction Casper

Correct-by-Construction (CBC) Casper is a form of Proof of Stake consensus protocol. It has been designed to work in an asynchronous environment like RChain. It’s a Byzantine fault tolerant protocol with very low message passing overhead. Consensus is happening on a directed acyclic graph of blocks (blockDAG), rather than a blockchain. This means that a block can have multiple parents and multiple children. The DAG structure allows for the acknowledgment of multiple blocks. This in turn serves as a “join” operation and allows independent branches to be merged into a single branch, thus reducing the time to reach consensus.

Besides that, they introduce an agency of validators through block acknowledgment and political capital (PC). A block with more PC is more likely to remain a part of the main blockDAG. The amount of PC a validator chooses to attach to a block can be thought of as a “bet” on the success of that block. The other aspect of validator agency is the ability to acknowledge blocks.

Formation of the BlockDAG

Rholang Contracts

Rholang contracts can be used on RChain nodes. Rholang is “process-oriented”: all computation is done by means of message passing. Messages are passed via “channels”, which are rather like message queues. Note that throughout this article the words “name” and “channel” are used interchangeably. This is because in the rho-calculus (on which Rholang is based) the term name is used, however, because you can send and receive information on names, semantically they are like channels. You can play around with some code using the Rholang web interface.

Syntax

0 stands for a null process, do nothing.

for (x <- y) {P} is used for receiving messages on a channel. x and y are both channels.

x!(P) is used for sending messages on a channel.

P | Q the pipe symbol is used for doing two things at once.

*x is used for evaluating code.

Sending Data — Hello World

This example is the basis of every language. We create a new private channel using the new keyword. Only our process can now send and receive messages over this channel. If we want other processes to be able to use our channel, we have to explicitly send this channel to other processes.

The contract will create a copy of itself whenever it receives a message to facilitate the concurrent execution.

We define a contract HelloWorld which returns a message Hello, World!.

Next, the example defines a new channel which we call myChannel. We want to explicitly give this channel access to our HelloWorld channel which contains the contract. The * operator “unquotes” a channel to get its underlying process. In Rholang you can only send processes over channels; you cannot send channels over channels directly. Therefore, we use * to turn the private channel into a process prior to sending.

Receiving Data — Hello Again

You will notice the underscore operator in the contract’s constructor. Like in Golang, the underscore is used to discard the parameter. We create again a new channel called chan which is responsible for first sending a new hello world message using the send syntax x!(P). Next, we listen for a message on the chan channel. The ‘@text’ variable binds the string process to the free variable text. For this example, we don’t want to process the text variable further, so we give Nil.

At last, you will notice the pipe symbol. We use this to trigger the contract at the same time and give it a Nil parameter as this will be discarded.

Token Contract

RChain has recreated ERC20 functionality for Rholang. It’s now possible to create your token smart contract and deploy it on RChain. You can find the example on their Github. Let’s take a brief look at the transfer() function.

It’s pretty straightforward actually. All operations are executed after each other by using the pipe symbol.

  1. Get balance of sender.
  2. Check how much the sender is allowed to send to the contract.
  3. Get balance of receiver.
  4. Listen for input from three previous actions. When we have all input, we can start the transfer.
  5. Match operation is used to perform a couple of checks like checking if you have sufficient balance.
  6. A Case operation is used accordingly to check whether all checks were successful. If yes, the balance of both sender and receiver will be updated. A ‘true’ boolean will be returned to notify the successful transfer.

Conclusion Key Points

  • RChain is faster because Rholang doesn’t impose a single global transaction order like Ethereum and because they’re using a better sharding approach than anyone else.
  • Asynchronous executing enables high throughput per second — Multi-threading.
  • Ethereum smart contracts can be written in Rholang language — even for ICOs.

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!