Note: This article provides a general and slightly technical introduction to Parachains and indicates how to get started building a Substrate based blockchain which can be launched as Parachain in the Kusama & Polkadot ecosystem. It will explain some parts of Polkadot but it won’t get too much into specific details like staking, slashing or other important mechanics in the ecosystem. Check out the official wiki to learn more about Polkadot.
First of all it is important to mention that the Parachain functionality is not live yet. This functionality is the last missing piece of the Polkadot whitepaper and there is a public rollout plan available that provides an overview about the progress. However, since August 2020 the Parachain functionality is being tested in the official Parachain Testnet Rococo which in V0 only ran with prototype code. In December 2020 Rococo V1 was launched and recently Plasm Network announced to be the first project that joined the Rococo V1 Testnet which surely is a big milestone as the codebase of Rococo V1 will also be used in Kusama and Polkadot. Meanwhile other possible future Parachains like KILT followed and announced their own Roadmap.
Kusama vs. Polkadot
Although Kusama can be seen as the pre-production environment for Polkadot you should be aware that Kusama might and probably will evolve independently from Polkadot. If your project is in an early stage or you want to experiment with Parachains in a real world environment before moving it into production you will probably prefer Kusama over Polkadot first. You might also be interested in testing specific features in Kusama which aren’t yet available on Polkadot.
Relay Chain & Parachains
The Relay Chain is the central chain of Polkadot. All validators of Polkadot are staked on the Relay Chain in DOT and validate for the Relay Chain. It has deliberately minimal functionality and its main responsibility is to coordinate the system as a whole, including Parachains. Other specific work is delegated to the Parachains that serve different purposes. All Parachains that are connected to the Relay Chain share the same security. Polkadot also has a shared state between the Relay Chain and all connected Parachains to ensure the validity of the entire system. Smart Contracts are not supported on the Relay Chain but can be supported by Parachains.
It’s expected that each Parachain will be as light and application specific as possible and serve a specific use case.
In The Path of a Parachain Block it is described in detail how a new Parachain Block is produced.
You may notice the term Parathread in the picture above. The current (optimistic) assumption is that the Relay Chain supports up to 100 Parachains and some of these Parachain slots will be permanently reserved to serve the network on system level (e.g. Governance). Thus there probably won’t be enough slots available for all projects which is why they need to be obtained in a so-called auction (more on that below). Whereas Parachains get a guaranteed high throughput by bonding DOT tokens, Parathreads follow a pay as you go model where a fee in DOT is being paid to validators for creating blocks. In some cases this even makes sense if there aren’t many or high frequent state updates expected (e.g. DNS, Oracles).
Parachains can become Parathreads and vice versa as they share the same technical base.
Parachain Slot Auction & Parachain Crowdloans
In order to become a Parachain in Kusama or Polkadot a slot must be obtained in a Parachain Slot Auction. Each slot duration is capped to 2 years and divided into 6-month lease periods. Parachains may lease more than one slot over time in order to extend their lease to Polkadot past the 2 year slot duration. Depending on the auction it might be possible that a slot is owned by 4 different Parachains during the 2 year slot duration.
Parachains don’t need to always inhabit the same slot. As long as a Parachain inhabits any slot it can continue as Parachain.
Parachain candidates can place bids in unpermissioned candle auctions that rely on verifiable random functions (VRFs) where the original mechanism has been slightly modified to be secure on a blockchain. The auction can be divided into two phases:
- Opening Phase
- Closing Phase
No matter in what phase the auction is, the bids are public. From the first block until the last block during the Closing Phase a winner will be determined randomly and nobody knows which block determines the winner until the Closing Phase is finished.
Projects will also be able to launch a Crowdloan Campaign with specific parameters which allows them to loan DOTs from the community to obtain a Parachain slot without having to bond all the required DOTs on their own.
In the video above core developer Shawn Tabrizi explains both variants very well.
After giving an overview about the Polkadot ecosystem it’s time to introduce Substrate. It is a modular framework that enables you to create purpose-built blockchains by composing custom and/or pre-built components.
Substrate is the foundation of all blockchains in the Polkadot ecosystem and requires knowledge in Rust.
In the Substrate Developer Hub you can learn everything that is required to get started developing your own Substrate-based chain.
A few important things to know about Substrate:
- The runtime of Substrate is referred to as the “state transition function” which contains the business logic that defines the behavior of the blockchain. You as a developer can define storage items that represent the state of the blockchain and functions that allow users to make changes to the state.
- Substrate ships with FRAME (Framework for Runtime Aggregation of Modularized Entities) which is a set of modules (called Pallets) and support libraries that simplify runtime development. Pallets are individual modules within FRAME that host domain-specific logic.
- To enable forkless runtime upgrade capabilities, Substrate uses runtimes that are built as WebAssembly (WASM) bytecode.
To learn more about how to perform a forkless runtime upgrade you can take a look at the official tutorial.
Pallets vs. Smart Contracts
For people heading over from other chains and ecosystems like Ethereum it might be obvious that application specific logic is always written in Smart Contracts which in turn are deployed on the blockchain. But as you already learned the idea of the Polkadot ecosystem is to have different Parachains that are as light and as application specific as possible.
There are basically 3 possibilities to add custom logic to a Substrated-based blockchain:
- Write custom Pallets and include it in the runtime
- Include the Contracts Pallet in the runtime and write contracts in ink! (Rust based eDSL)
- Include Frontier (Pallets that serve as Ethereum compatibility layer) to the runtime and write contracts in Solidity
Custom Pallets are the best choice if you are building a greenfield project that serves a specific purpose and needs to run its own network.
Substrate already provides a lot of Pallets which you can combine together as needed, including your custom Pallet(s) in order to build a lightweight and unique runtime for your application specific blockchain:
- Default Pallets of Substrate (Note: The link points to the v3.0.0 version of Substrate which was released a few days ago. Most tutorials are still based on v2.0.0)
- NFT Pallet (There is also a WIP reference implementation that showcases CryptoKitties on Substrate)
I also discovered a Substrate Marketplace where you are able to find different Pallets (at least the default ones) which you can include into your runtime.
If you aim to write contracts in ink! or port a Solidity based application from Ethereum over to a Substrate-based Parachain you don’t necessarily need to launch your own Parachain as there already exist projects that plan to launch a Parachain and include the required Pallets in their runtime:
Note: To check out how Moonbeam works you can clone the following repository: https://github.com/51nodes/moonbeam-playground
Polkadot compatibility with Cumulus
When you have written (and hopefully tested ;-)) your custom Pallets you need to make sure that you build a Polkadot compatible runtime that exposes an interface for validating its state transition and provides interfaces to send and receive messages of other Parachains.
Cumulus is an extension to Substrate that makes it easy to transform any Substrate-built runtime into a Polkadot-compatible Parachain. It is still in development, but the idea is that it should be simple to take a Substrate chain and add the Parachain code by importing the crates and adding a single line of code. You can also dive deeper into that topic and learn more about the mechanics of Polkadot by reading the Parachain Implementers Guide.
If you have built your own Parachain and want to join the official Rococo Testnet you have to follow this guide. The auction mechanism mentioned above is not active yet and Rococo is currently controlled by the development leads at Parity Technologies. For joining the network you have to run at least 1 Collator for your Parachain and 1 Validator node for Rococo.
Note: Learn how to setup a local Polkadot network based on Rococo by following the Readme of our repository: https://github.com/51nodes/polkadot-local-rococo
Substrate has a clean design and makes it very easy to build an application specific blockchain for everyone familiar with Rust. If you decide to run your own Parachain (or Parathread) you should also think about how to incentivise other players to run a Collator node in your network — for example by introducing your own network token.
Although Parachains are not live on Kusama and Polkadot yet we can already see many projects that aim to become a Parachain as soon as possible. It will be interesting to watch the battle of those projects to secure their exclusive Parachain slot on Kusama and Polkadot. Personally I am very interested to see how the auction mechanism works and which projects will secure their Parachain slot via a Crowdloan Campaign.
I am also looking forward to play around with the interoperability features that Polkadot provides when the implementation is mature enough.
51nodes GmbH based in Stuttgart is a provider of Crypto Economy solutions.
51nodes is member of the Substrate Delivery Partners program and supports companies and other organizations in realizing their blockchain projects. 51nodes offers technical consulting and implementation with a focus on smart contracts, decentralized apps (dApps), integration of blockchain with industry applications, and tokenization of assets.