Topics In Demand
Notification
New

No notification found.

How to use Substrate to create Parachains in Polkadot
How to use Substrate to create Parachains in Polkadot

November 8, 2022

74

0

 

The Substrate is an open-source framework that enables developers to build extensively customized blockchains. Straight out of the box, Substrate comes with support to connect to Polkadot and Kusama, which makes it the ideal framework for creating Parachains. 

The Parachain model was created by Polkadot to enable the overall advancement of decentralized systems and Web 3.0 at large. 

Let us now dive deeper into understanding what Parachains is, followed by how to use Substrate to create Parachains in Polkadot. 

What are Parachains?

Parachains are heterogeneous multichain that allow multiple solo chains to connect to the Polkadot Relay chain by procuring a Parachain slot. In this sequence, Parachains are the Layer-1 (L1) blockchains on top of the Layer-0 (L0) Relay chain, and both run in parallel. 

As Polkadot Parachains are L1 blockchains, they can have a native token, runtime logic, and economies, and by connecting to the Polkadot’s Relay chain, Parachains become much more secure. In addition, Parachains get to enjoy Polkadot’s benefits like interoperability with other Parachains, scalability, governance and pooled security. 

Moreover, digital wallets, gaming applications, IoT apps, DeFi apps, and Web 3.0 infrastructure can also be deployed as Parachains. 

Polkadot recommends developers deploy Parachains on Polkadot to overcome challenges like scalability, applicability, and interoperability present in siloed blockchains. 

What is a Parachain Development Kit (PDK)?

To make it easier for developers to create Parachains, Polkadot has come with PDKs and they should contain the following components: 

State transition function (STF)

STF helps applications move from one state to another, and they should be easily verifiable through proof of witness. The STF must be verified easily as the Relay Chain validators validate each state sent by the Collator node, without having the necessity of running it through the entire computation process. 

Collator node

In the protocol, the collator node is like a network maintainer and it is a P2P node in the Polkadot network. It is responsible for keeping all the states of Parachain available, including new ones that are returned from the iteration of the STF. Collator nodes must be online to track said state and XCMP messages routing between other Parachains and itself. 

With that said, there are two PDKs in existence: Substrate and Cumulus. 

As mentioned earlier, Substrate is a framework that is used to build blockchains elements like a consensus, wasm interpreter, and networking layer. It also offers an intuitive method to build runtime to start a new chain. However, Substrate does not directly support Polkadot compatibility. 

Here, Cumulus comes into the picture as a Substrate extension that simplifies the conversion of Substrate-built runtime into a Parachain that is Polkadot compatible. 

The elements managed by Cumulus are 

  • Compatibility for block authorship
  • Embedded full-client Relau Chain
  • Out of the box Collator node set up
  • (XCMP) cross-chain messaging

Now that we have the fundamentals out of the way, let us start setting up the Parachain. 

Parachain set up

Setting up Parachain to deploy your own Blockchain is an intricate process. The following steps will help you. Start by writing a runtime logic.

Step 1: Runtime Logic

On Substrate, write a runtime logic, which is similar to writing a runtime logic for any solo chain. For this, you may use the following Substrate Parachain’s template. 

# Clone the parachain templategit clone https://github.com/substrate-developer-hub/substrate-parachain-template

# Switch into the parachain template directorycd substrate-parachain-template

# Checkout the proper commitgit checkout polkadot-v0.9.30

# Build the parachain template collatorcargo build --release

# Check if the help page prints to ensure the node is built correctly./target/release/parachain-collator --help


 

Step 2: Build Wasm Executable

Next, compile this runtime logic to a Wasm executable as the Wasm code blob will contain the chain’s entire STF. This Wasm code blob will be essential in deploying the project as a Parachain. 

Step 3: Submit Wasm Code for Validation

The next step is to submit your Wasm code which Polkadot validators utilize to validate the STF of your chain. 

Step 4: Bring the Collator Node into the Picture

Here, the Polkadot validator will use Collator need to check which is the recent STF. As the Collator node is the maintainer of your Parachain, it must perform the vital action of producing new block candidates and pass them on to the validators to be included in the Polkadot Relay chain. You can use Cumulus to convert your Substrate built chain logic into Polkadot compatible Parachain. 

Cumulus extension will come into the picture as Substrate has its own in-built networking layer that only supports solo chains but not chains connected to any Relay chain. Here, the Cumulus extension makes your Substrate-built chain logic compatible with Polkadot, establishing your blockchain as a Parachain.

Parachain testing

The next step is to test your Parachain and we’ll be using Polkadot’s own Rococo testnet for this. 

Rococo testnet

Testing your parachain on Rococo will check for the passing of messages and transfers between the Relay Chain and the Parachain. 

Messages are sent to the Relay chain and then to the Parachain, and Rococo uses HRMP (Horizontal Relay-routed Message Passing) and Cumulus for the testing. 

Next, procure ROC tokens by running the following command. 

!drip YOUR_ROCOCO_ADDRESS​

 

Although Rococo Parachains use the same runtime code, the Parachain IDs to register with the Relay chain will differ. 

Now, run a Rococo collator by compiling the binary below. 

cargo build --release --locked -p polkadot-collator

 

The last step in the testing phase is procuring some ROC tokens from the Rococo Faucet. Use the following command for this. 

./target/release/polkadot-collator --chain $CHAIN --validator

 

Parachain deployment

After successfully testing your Parachain for cross-chain transfer, the next step is to deploy it on Polkadot, and you’ll need to obtain a Parachain slot for this. 

SS58 encoding is used by Substrate-based chains for address formats and you may verify which chain corresponds to prefixes here. 

Obtain a slot

Obtaining a Parachain slot is easier said than done as Polkadot has limited unlocked slots each month. But it is imperative to connect to the Polkadot network and for the Parachain to take the place in an available slot. 

Availing of the slot is a must if the Parachain wants to have an assured block inclusion at every Relay Chain block. 

The candle auction

To make the process of allotting slots easier and seamless, Polkadot sells Parachain slots using the candle auction idea. 

Just like mainstream auctions, candle auctions are open to bidders who quote higher bids to obtain a Parachain slot. 

But here, bidders are unaware of the opening phase or the closing phase; to make the bidding random and the bidders are also given random numbers for the same reason. 

Nonetheless, bids that are submitted during the open phase have a higher probability of winning rather than bids that are made at the later phase. 

This auction is also modified for blockchain security. 

Final thoughts

This is how you use Substrate to create Parachains on Polkadot, and there are quite a few benefits to deploying a Parachain as mentioned. Still, it is a tricky business to create, deploy, and maintain Polkadot Parachains. Developers must constantly remember a series of do’s & don’ts, keep up with the latest upgrades, adhere to best practices, and undertake security measures constantly.

About The Author 

Author

Dr. Ravi Chamria is co-founder CEO of Zeeve Inc, an Enterprise Blockchain company. He has an experience of 18+ years in IT consulting spanning across Fintech, InsureTech, Supply Chain and eCommerce. He is an executive MBA from IIM, Lucknow and a prolific speaker on emerging technologies like Blockchain, IoT and AI/ML.

Passionate About: Blockchain, Supply Chain Management, Digital Lending, Digital Payments, AI/ML, IoT

Specialities: Strategic Management, Technology Innovation, Product Management


That the contents of third-party articles/blogs published here on the website, and the interpretation of all information in the article/blogs such as data, maps, numbers, opinions etc. displayed in the article/blogs and views or the opinions expressed within the content are solely of the author's; and do not reflect the opinions and beliefs of NASSCOM or its affiliates in any manner. NASSCOM does not take any liability w.r.t. content in any manner and will not be liable in any manner whatsoever for any kind of liability arising out of any act, error or omission. The contents of third-party article/blogs published, are provided solely as convenience; and the presence of these articles/blogs should not, under any circumstances, be considered as an endorsement of the contents by NASSCOM in any manner; and if you chose to access these articles/blogs , you do so at your own risk.


Zeeve is an enterprise-grade Blockchain Infrastructure Automation Platform. Join the growing list of clients that trust us with their Blockchain initiatives

© Copyright nasscom. All Rights Reserved.