Every blockchain technology is based on three principles: decentralization, scalability, and security. The challenge of fully adhering to all these principles is yet to be overcome. Decentralization is easily achieved by almost every blockchain startup, but ensuring scalability and security causes some troubles.
The speed of transaction handling and data processing, supported data volumes, and security of blockchain data and smart contracts should be improved.
There’s also a problem with cross-blockchain interoperability, which is becoming more and more relevant. Cryptocurrency exchanges, Oracle technologies, and many other blockchain-based services need new ways to interact with other blockchains and exchange data with them. Even the simplest possible scenario of Ethereum to Bitcoin exchange can be a lot simpler with convenient blockchain interoperability.
Blockchain: Cyber Security Pros and Cons
While blockchain interoperability is important for effective cooperation, most existing solutions that provide cross-blockchain communication are complicated, overloaded, narrow, and including a third party. Often, they’re not completely secure. There is one solution that meets some of these challenges, though.
The Polkadot network is building a protocol for blockchains to safely and reliably interact. This protocol allows for the creation of new blockchains, or parachains, connected to the main Polkadot chain. It brings the possibility to send messages and transactions between chains, providing shared security and great potential for scaling.
In this article, we’ll take a closer look at Polkadot and provide an example of how to build a parachain.
This article will be useful for businesses executives and developers who are interested in establishing cooperation between blockchains and looking for new secure ways to do that.
Polkadot is a blockchain platform written in the Rust programming language and developed by Parity, the team behind the Ethereum client by that name. Its concept is to use network effects from a network of blockchains to improve the decentralized internet.
Polkadot brings blockchain interoperability, so it can significantly increase the scalability, speed, and extensibility of blockchains. Polkadot is a platform that allows distinct blockchains to transfer messages and exchange transactions, turning them into a big multi-chain network.
All of this is possible due to the main feature of the Polkadot network: the parachain. The parachain is what allows private, public, and consortium blockchains to connect and scale. Polkadot provides a relay chain upon which a large number of verifiable data structures may be hosted.
These structures are called parallelized chains, or parachains. Polkadot provides networking and consensus layers that allow blockchain developers to focus on creating business logic with unique features and implementing it in a parachain.
Polkadot consists of many parachains with potentially different characteristics. So it’s possible to follow the principle of a single DApp on a single parachain. Even non-blockchain systems or data structures can become part of the network. Such stand-alone blockchains as Bitcoin, Ethereum, and Tezos can be connected to Polkadot by developing an appropriate parachain.
Tezos Blockchain and Smart Contract Overview
The Polkadot network is a Proof-of-Stake blockchain, and its native token is DOT. DOTs provide governance and give theoretical incentives for token holders to behave honestly and pay fees for message transmission. DOTs facilitate the consensus mechanism and provide the ability to create new parachains. These tokens will be available only after the mainnet launch, which is expected by the end of 2019.
The general idea of the Polkadot network can be represented in a single diagram:
- Relay chain
The relay chain is the central hub of the Polkadot network and
- stores blocks from all parachains
- is responsible for interchain messages
- ensures security for parachains
- provides a consensus mechanism
The main chain is hosted by validators and nominators, which we describe below.
A parachain is an independent blockchain that provides blocks for the relay chain. It can have its own monetary system, its own logic, and its own interactions with third-party applications or separate blockchains. Parachains inherit security from the relay chain. They can send messages to other parachains and can be rapidly updated due to their WebAssembly (Wasm) architecture. Each parachain is connected to some validator for a certain period of time and is hosted with its own set of collators.
- Parachain bridge
A parachain can serve not only as a blockchain for certain dApps but as a bridge to connect external services or another blockchain. Bridges are the Polkadot feature that makes a network of blockchains possible. A bridge is a specialized parachain developed to connect a Polkadot blockchain to an outside service or blockchain without any third-party service, increasing the speed, security, and scalability of blockchain-based operations.
- Second layer relay chain
This is the main technology that allows Polkadot to scale, as every relay chain can carry more parachains and relay chains. After some second layer relay chains are added to the main chain, the Polkadot network will look like a tree that connects independent clusters of parachains. This is the main advantage of Polkadot, as such scaling is theoretically unlimited.
A couple of times per day, the system selects a group of entities called validators. These are nodes responsible for the consensus protocol and relay chain block production and finalization. Each validator should maintain the full node of the relay chain. Validators are the main stakeholders of DOTs, and they guarantee the system’s proper behavior because in case of malicious behavior, a validator’s stake is slashed.
Validators also have a second responsibility. Each validator gets the responsibility for keeping parachains for some period. This involves receiving, validating, and republishing candidate blocks. Validators are not expected to maintain a fully synchronized database of all parachains, however, as that would take too much space to store. All these duties fall upon сollators.
Collators are full nodes that host complete parachains. They’re responsible for producing, storing, and validating new parachain blocks. After collators collect parachain transactions, they execute them and produce state transition proofs for the validators. These proofs are validated in the relay chain before they’re collected in the main chain block.
These are the nodes that select validators. They must stake a certain number of DOTs in order to do so. These DOTs may be lost if the validator doesn’t behave in the way prescribed by the protocol. This forces nominators to carefully select validators, increasing the overall security of the network.
Fishermen are like independent bounty hunters looking for misbehaving validators and collators. They provide an additional element of security. Once malicious behavior is reported by fishermen, they get a reward and the stake of the malefactor is slashed.
Polkadot works according to this simple scheme:
- Collators run parachains, collect transactions, execute them, create proofs of state changes, collect information, and pass the result to the validators.
- Validators provide checks upon parachain state changes and add blocks provided by collators to the relay chain.
- The parachain shares the security of the relay chain, so only functional logic needs to be developed.
How does Polkadot contribute to overcoming the main challenges for blockchain developers, such as interoperability, scaling, and security?
We’ve already touched upon interoperability, which is the main point of the Polkadot protocol. For now, a common way to transfer value from one chain to another is a centralized exchange. With Polkadot, multiple chains interconnect so that arbitrary messages can be transferred between them. This can be not only value but any kind of data or data structure, which unlocks new opportunities.
The multiple parachain architecture of Polkadot enables horizontal scaling, meaning a high number of transactions can be processed in parallel. And parachains can have state machines customized for particular tasks, which results in increased speed and efficient storage. Developers can delegate tasks to different parachains and run transactions in parallel instead of in sequence.
The Polkadot platform provides umbrella security due to the pool of security provided by validators, who are economically incentivized to behave honestly.
The following example shows that the development of a blockchain is much easier with Polkadot.
There are more advantages to creating a parachain than simply decreasing the time for development.
Each parachain is compiled into two executables:
- the blob of the Wasm code
WebAssembly (Wasm) is a binary instruction format for a stack-based virtual machine. It was designed to be portable and to be executed almost everywhere, as it is close enough to native code. For now, Wasm is supported by Google, Mozilla, Apple, and Microsoft.
Such widespread use makes Wasm more attractive for blockchain developers, as more services can be accessed. Another advantage of Wasm is that pretty much everything can be compiled into Wasm, so all features of full languages can be used.
There are reasons to work with both executables. In the first place, both provide quick code execution, even faster than on the Ethereum Virtual Machine.
Another important reason for working with both executables is that such a model avoids hard forks. Since a Wasm blob of blockchain logic is deployed on-chain, a native executable is used by the client side. So even if a client doesn’t get updated, it won’t start rejecting new blocks generated by clients that have been updated, as they all use the same Wasm runtime. If you never get the client update, you’ll just use the slower Wasm version, and you’ll get a nice speed boost when you do run the update.
Development begins with installing some tools.
The minimal set of tools used by Polkadot:
- WebAssembly compiler
- Polkadot source code
Install Rust and additional tools:
After Rust is installed with Wasm support, it’s time to install Wasm itself. The tool for this – cargo – is a default Rust project builder.
Now we need the Polkadot source code, as it contains the necessary API for our parachain and collator. After installation, test the components to make sure the build is stable:
Our environment is now ready for development.
The structure of the parachain in the project is very simple. It can be described in three folders.
This folder contains a main.rs file with the code for the full collator run by the client. It may also contain custom modules, a cargo build file with dependencies, and other support files.
This contains the parachain logic itself. It may also contain custom modules, cargo build files, etc.
This file contains the API, which will be visible to relay chain validators. More often, it contains wrappers around functions from the src folder or even a single line import of all public functions/interfaces from that folder.
The project also contains a Cargo.toml file (possibly for each folder), which contains dependencies used in the code: the API for accessing the parachain, the API for running collators, standard Polkadot types, etc. The Cargo.toml file may look like this:
The Apriorit blockchain team has lots of experience with IoT supply chain-based technologies. So as an example of Polkadot’s capabilities for blockchain development, we’ll provide the simplest prototype of a real-life application for on-chain data storage.
Before the development process, the team should also consider the Polkadot specifications, where the data types, APIs, interfaces, and main methods are described. The design of Polkadot’s multi-chain architecture allows developers to specify custom way of their parachain’s validation.
To implement a parachain, developers need to implement a state transition validation function, decide upon a state format, and select a transaction pooling mechanism.
1) Parachain logic
The first thing to create is src/lib.rs source. We should add simple logic to introduce some new data (e.g. collected from IoT devices) to the chain.
We’ll begin by including the API, interfaces, and data types. They’re specified in the Polkadot source code, and we should reuse and implement them for name matching:
Now we’ll implement the main parachain data types. Also, we’ll use inheritance from some standard Polkadot data types to get convenient properties for hashing and encoding. It’s important to use the names Polkadot offers for the main types, as we simply override standard types with custom ones. In general, for the functional logic, you can create custom elements instead of standard ones.
This is our custom type, to be stored on-chain:
Now, we proceed to the logic itself. We can implement any on-chain logic and public API for this logic to be accessed from parachain nodes or dApps. In our case, we’ll create a function for executing custom logic and a function for validating blocks and creating new headers:
2) Wasm interface
The next step is to develop an interface for validating parachain blocks. It may contain a custom error handling function, but it has to contain the validate function.
First, we import the necessary modules and data types from the parachain logic. custom_parachain is our functionality implemented in the previous step:
Then we implement the function (Polkadot provides some templates for on-chain data interaction):
3) Collator node
The last thing to implement is a collator node. It should invoke functions from the parachain logic to produce blocks for the relay chain.
Include the parachain API, data types, and interfaces. They should be stashed by our custom types:
Genesis block data is placed in the collator code too:
Our parachain logic should be derived from the common logic and interact with the blockchain database. These are provided by Polkadot templates, so our implementation should have the mandatory produce_candidate function:
The main function should contain only genesis values and a call to collator::run_collator:
Now the paracahin is ready for compilation and deployment. Sources from the Wasm folder are compiled into the Wasm blob by wasm-gc. This Wasm code can be deployed on Polkadot using the instructions provided on the Polkadot wiki. You’ll need only the encoded Genesis information and some DOTs for the parachain deployment. After that, you can compile your collator code, access the chain, and see the block on Polkaskan.
There’s another way to build a parachain. The Parity team and Web3 Foundation decided to build a universal tool that can build a blockchain in 15 minutes that will easily connect to Polkadot. This tool is called Substrate. You can often see Polkadot and Substrate mentioned together. Substrate is a blockchain framework that provides the basic building blocks – the networking layer, consensus mechanism, and Wasm interpreter. This frees developers to focus on the business logic.
Polkadot itself is developed on Substrate. Among other things, Substrate can be used for creating parachains. And any blockchain developed on Substrate can be easily turned into a parachain. It’s just a matter of declaring the interface.
At the moment, the Parity team is developing Cumulus, a library that will contain all of the Polkadot compatibility glue code. This means that parachain development may use all the features and modules provided by Substrate. And any blockchain developed on Substrate can run in the same environment as Polkadot.
The main question is why Polkadot should be used as a platform for blockchain development and dApp launch? These are the main arguments:
- The Polkadot network shares the security of the main chain with all hosted parachains.
- Parachain development allows developers to focus on business logic and features and considerably increases the speed of development.
- The Polkadot runtime environment supports natively compiled code (such as Rust) and any other programming language that can be compiled into Wsam. It allows for developing the chain in any appropriate language and on any environment with the WebAssembly interpreter. Since Wasm or natively compiled languages are used, the speed of execution is higher than, for example, on the Ethereum Virtual Machine. The use of both executables together helps avoiding hard forks.
- Parachains can interact with each other, so several connected dApps can be built within the same platform with no problems of scalability.
- Polkadot allows for cooperation with other blockchains and third-party applications through appropriate parachain bridges, solving the problem of interoperability.
A lot of features can potentially be implemented as Polkadot parachains. They can be used for custom token/monetary systems or simple ERC20/ERC721 implementation. A set of interacting parachains can be built for parallelized information storage, e.g. for IoT tools or supply chains. A stock exchange or decentralized exchange (DEX) can be built upon bridge parachains. A parachain can serve for communications with outer applications and inner parachains. It makes possible the launch of government, oracle, certification and banking services.
Polkadot is a rapidly evolving blockchain network, and many startups have already chosen it as a foundation for blockchain development. It promises a lot of profit from cross-application interoperability, not to mention the speed of blockchain development.
The Apriorit team is keeping an eye on all innovative technologies to increase our blockchain expertise. We already have vast experience developing blockchain-based solutions, providing blockchain security testing, and creating custom networks. If you have a challenging project in mind or need to test your own blockchain solution, feel free to contact us.