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.
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.
Contents:
- What is Polkadot?
- Polkadot network overview
- Polkadot network structure
- Polkadot roles
- Polkadot helps to overcome some important challenges of blockchain development
- Example of building a parachain
- Prepare environment
- Structure and components
- Parachain development
- Using Substrate for building parachains
- Why is Polkadot a great platform for blockchain development?
- Conclusion
What is Polkadot?
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.
Plan to deploy your solution on Polkadot?
Leverage our experience with this network for the benefit of your project. Letโs build secure and efficient parachains that will get your business to where you want it to be.
Polkadot network 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:
Polkadot network structure
- 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.
- Parachain
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.
Read also
Parity Substrate: How to Build a Blockchain in 15 Minutes
Speed up blockchain deployment to test your project idea, enter the market quickly, and use time on your business goals instead of development nuances. Master the blockchain framework based on the Ethereum client and the Polkadot network to achieve this goal.
Polkadot roles
- Validator
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.
- Collator
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.
- Nominator
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.
- Fisherman
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.
Polkadot helps to overcome some important challenges of blockchain development
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.
Related project
Evaluating Smart Contract Security for Decentralized Finance
Discover the details of a real-life blockchain success story. Explore how our in-depth audit helped the client detect and fix security vulnerabilities in their smart contract, and deliver a reliable product to their customers.
Example of building a parachain
There are more advantages to creating a parachain than simply decreasing the time for development.
Each parachain is compiled into two executables:
- from a natively compiled language (Rust, JavaScript, C++)
- 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.
Prepare the environment
The minimal set of tools used by Polkadot:
- Rust
- WebAssembly compiler
- Polkadot source code
The first step is to install the Rust language compiler and some additional tools like WebAssembly compiler and project builder. Polkadot is currently written in Rust, though other implementations (C++, JavaScript, Go) are being developed.
Install Rust and additional tools:
curl https://sh.rustup.rs -sSf | sh
rustup update nightly
rustup default nightly
rustup target add wasm32-unknown-unknown --toolchain nightly
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.
cargo install --git https://github.com/alexcrichton/wasm-gc
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:
git clone https://github.com/paritytech/polkadot.git
cd polkadot
./scripts/build.sh # Builds the WebAssembly binaries
cargo build # Builds all native code
cargo test --all
Our environment is now ready for development.
Structure and components
The structure of the parachain in the project is very simple. It can be described in three folders.
1) Collator
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.
2) src/lib.rs
This contains the parachain logic itself. It may also contain custom modules, cargo build files, etc.
3) wasm/lib.rs
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:
[package]
name = "custom_parachain"
version = "0.1.0"
[dependencies]
polkadot-parachain = { path = "<path to polkadot/parachain>" }
polkadot-collator = { path = "<path to polkadot/collator>" }
polkadot-primitives = { path = "<path to polkadot/primitives>" }
... other libs
[dependencies]
polkadot-parachain = { path = "<path to polkadot/parachain>" } polkadot-collator = { path = "<path to polkadot/collator>" } polkadot-primitives = { path = "<path to polkadot/primitives>" } ... other libs
Read also
Does Your Business Need NFTs? Use Cases, Benefits, and Nuances to Consider
Determine whether your project can benefit from NFTs by exploring their concept, use cases, and implementation ways using Polkadotโs native NFT infrastructure.
Parachain development
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:
#[macro_use]
extern crate parity_codec_derive;
extern crate parity_codec;
extern crate polkadot_parachain as parachain;
extern crate tiny_keccak;
use parity_codec::{Encode, Decode};
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.
#[derive(Default, Clone, Hash, Eq, PartialEq, Encode, Decode)]
pub struct HeadData {
pub block_number: u64,
pub parent_hash: [u8; 32],
pub state_hash: [u8; 32],
}
#[derive(Default, Clone, Encode, Decode)]
pub struct BlockData {
pub data_hash: [u8; 32],
pub some_data: IotData,
}
This is our custom type, to be stored on-chain:
#[derive(Default, Clone, Encode, Decode)]
pub struct IotData {
pub iotId: u64,
pub parameter: [char; 20],
pub value: u64,
}
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:
pub fn produce_valid_header(
parent_hash: [u8; 32],
parent_head: HeadData,
block_data: &BlockData,
) - Result<HeadData, Debug> {
///All appropriate checks should be placed here
debug_assert_eq!(parent_hash, parent_head.hash());
///Here may be state_hash check for many block_data parts.
///For our example it can be dismissed
///Block data check
let calculated_hash = ::tiny_keccak::keccak256( block_data.some_data[..] )
if calculated_hash != block_data.data_hash {
return Err(Debug);
}
let new_state_hash = ::tiny_keccak::keccak256( block_data.encode().as_slice() );
Ok(HeadData {
block_number: parent_head.block_number + 1,
parent_hash,
state_hash: new_state_hash,
})
}
pub fn execute(
parent_hash: [u8; 32],
parent_head: HeadData,
some_data: &SomeData,
) -> Result<HeadData, Debug> {
let block_data: BlockData;
///Custom operations: process some_data into BlockData
match produce_valid_header( parent_hash, parent_head, &block_data ) {
Ok(new_head) => new_head,
Err(_) => Err(Debug),
}
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:
use parachain::ValidationResult;
use parachain::codec::{Encode, Decode};
use custom_parachain::{HeadData, BlockData};
Then we implement the function (Polkadot provides some templates for on-chain data interaction):
pub extern fn validate(offset: usize, len: usize) -> usize {
let params = unsafe { ::parachain::wasm_api::load_params(offset, len) };
let parent_head = HeadData::decode(&mut ¶ms.parent_head[..])
.expect("invalid parent head format.");
let block_data = BlockData::decode(&mut ¶ms.block_data[..])
.expect("invalid block data format.");
let parent_hash = ::tiny_keccak::keccak256( ¶ms.parent_head[..]);
match ::custom_parachain::produce_valid_header( parent_hash, parent_head, &block_data) {
Ok(new_head) => parachain::wasm_api::write_result(
ValidationResult { head_data: new_head.encode() }
),
Err(_) => panic!("validation failure"),
}
}
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:
use custom_parachain::{HeadData as CustomHead, BlockData as CustomBody, IotData};
use primitives::parachain::{HeadData, BlockData, Id as ParaId, Message, Extrinsic};
use collator::{InvalidHead, ParachainContext, VersionInfo};
Genesis block data is placed in the collator code too:
const GENESIS: CustomHead = CustomHead {
block_number: 0,
parent_hash: [0; 32],
state_hash: [some_value],///calculated initial hash value
};
const GENESIS_BODY: CustomBody = CustomBody {
data_hash: [some_value],
some_data: IotData {
iotId: 0,
parameter: ['n', 'o', 'n', 'e'],
value: 0,
}
};
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:
#[derive(Clone)]
struct CustomContext {
db: Arc<Mutex<HashMap<CustomHead, CustomBody>>>,
}
impl ParachainContext for CustomContext {
fn produce_candidate<I: IntoIterator<Item=(ParaId, Message)>>(
& self,
last_head: HeadData,
ingress: I,
) -> Result<(BlockData, HeadData, Extrinsic), InvalidHead>
{
///Get parent_head from last_head
///Get previous block_data from the database by adder_head or GENESIS_BODY
...
///This is the place for custom logic for block creation
...
let next_head = ::custom_paracain::execute( parent_head.hash(), parent_head, &next_body );
let encoded_head = HeadData(next_head.encode());
let encoded_body = BlockData(next_body.encode());
///Put new block to the database
db.insert(next_head.clone(), next_body);
Ok((encoded_body, encoded_head, Extrinsic { outgoing_messages: Vec::new() }))
}
}
The main function should contain only genesis values and a call to collator::run_collator:
fn main() {
let id: ParaId = 100.into();
...
let encoded = GENESIS.encode();
let context = CustomContext {
db: Arc::new(Mutex::new(HashMap::new())),
};
....
let res = ::collator::run_collator(
context,
id,
exit_function,
key_function,
::std::env::args(),
VersionInfo {
......
}
);
...
}
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.
Related project
Building AWS-based Blockchain Infrastructure for International Banking
Find out how our cooperation with the client provided them with an efficient, scalable, and secure blockchain infrastructure that helped them conduct successful demos for international banks and government organizations.
Using Substrate for building parachains
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.
Why is Polkadot a great platform for blockchain development?
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.
- A parachain can contain logic with more features than smart contracts can provide. This happens because a parachain is developed with a full programming language (Rust for now or JavaScript or C++ in the future). A fully unique monetary system, a custom system of fees, and DAO mechanisms can be implemented within a parachain.
- 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, certification, and banking services.
Conclusion
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.
Want to incorporate blockchain into your business?
Reach out to Apriorit experts for any blockchain-related project: deliver secure smart contracts, plan and build solid architecture, create efficient decentralized apps, and more.