Categories
Blog

Polkadot and Substrate: How to Build a Custom Blockchain?

In this tutorial, you’ll be going through the fundamentals of blockchain technology and Polkadot how it works, and the polkadot framework.

Next, we’ll move on to the installation, running, and compilation sections for rust using the substrate. After that, you’ll see how to build a custom blockchain using the substrate framework.

Moving further, you’ll learn how to create a bootnode and connect it to three other nodes.

If you are someone new to the blockchain, you must understand the concepts of Blockchain, Polkadot, and Substrate

For this tutorial, you should also be comfortable with command-line interfaces and program development in general.

The Basic Concept of Blockchain

Blockchain enables the real-time recording of data about any event or transaction through a distributed, decentralized, peer-to-peer ledger that is immutable and replicated across numerous nodes connected by a network. It is made up of links in a chain that are used to securely record digital assets. A digital chain composed of unchangeable data blocks stored on a decentralized and dispersed network of computers.

What are The 4 Types of Blockchain?

  1. Public Blockchain

It is a distributed ledger that anyone can join and use to complete transactions. Every computer in the network has a copy of every other node or block in the network. We can also carry out transactions or record verification. Anybody with access to the internet and a computer with decent hardware can take part in this open blockchain.

Traditional financial systems can be replaced by public blockchains because they are secured by proof of work or proof of stake. The smart contract that allowed this blockchain to support decentralization represents its more advanced side. Bitcoin and Ethereum are two examples of public blockchains.

  1. Private Blockchain

A blockchain network operates in a private setting, such as a restricted network, or is managed by a single identity. only selected nodes can participate in the process, making them more secure than the others.

This blockchain, with proper security and maintenance, is a great asset for securing information without exposing it to the public eye. As a result, businesses use them for internal auditing, voting, and asset management.

  1. Hybrid Blockchain

Companies that want the best of both worlds use a hybrid blockchain, which combines the benefits of both private and public blockchains. some parts are controlled by one organization and others are made visible as a public blockchain.

It offers a more comprehensive solution to the healthcare industry, government, real estate, and financial firms. It provides a solution when data must be accessed publicly but must be protected privately.

  1. Consortium Blockchain

A consortium blockchain, also known as a federated blockchain, has both private and public blockchain features, similar to how a hybrid blockchain does.

It has a lot of potential in companies, banks, and other payment processors. The organizations’ food tracking frequently collaborates with their sectors, making it a federated solution ideal for their use.

Polkadot

Its multichain network seeks to link various specialized blockchains into a single cohesive network. the blockchain that is linked to Polkadot and works in parallel blockchains known as parachains.

Polkadot combines a network of custom-built blockchains into a single network. Bridges are also used by Polkadot to link to legacy networks and the cousin network kusama. With a tool called substrate, the blockchain on Polkadot is custom-built and customized for certain needs.

Four major components of Polkadot:

  1. Relay-chain

It is the heart of Polkadot that’s responsible for the network’s shared security, consensus, and cross-chain interoperability. Interoperability refers to the ability of the diff-diff platform to operate as a single platform or as a reference platform. 

  1. Parachains

A blockchain with the ability to issue its own tokens and tailor its features to particular use cases. Additionally, it is directly linked to the relay chain.

  1. Parathreads

It’s similar to parachains, but on a pay-as-you-go basis. It is more cost-effective because blockchain does not require continuous network connectivity.

  1. Bridge

Blockchain bridges allow two economically sovereign and technologically diverse chains to communicate with each other.

Substrate

It is a generic blockchain platform that offers plug-and-play components for creating blockchain networks and applications. The substrate chain was given the messaging protocols by the Polkadot network so they could communicate with one another.

In order to construct the runtime logic necessary for your blockchain, you can use, modify, and extend the modules referred to as pallets and support libraries provided by the FRAME development environment. For eg Aura, BABE, GRANDPA, Electrons, utility, Sudo, EVM, and more…

Three core technologies in substrate:

  1. Libp2p

This will use the P2P(Peer-to-peer) stack for networking.

  1. Wasm (Web assembly)

It is the key technology for the substrate nodes. Everything in runtime will compile in Web assembly. It is also known as the shell of the brain. 

  1.  GRANDPA

The Polkadot Relay Chain uses GRANDPA (GHOST-based Recursive ANcestor Deriving Prefix Agreement) as its finality device.

Blocks are finalized by the Polkadot Host using the GRANDPA Finality protocol. The validator nodes must vote in successive rounds in order to reach finality. In parallel with Block Production, Validators run the GRANDPA finality process as a separate service.

Custom Blockchain Development: Step-by-Step Process

Installations

Before you start digging into this installed Rust and configuring your development environment. We use Linux; if you are using a different platform, please refer to the official documentation of the substrate.

Rust is compatible with the vast majority of Linux distributions. You have to add some software dependencies to your environment. You have to install the build-essential.

Before installing Rust, the following packages are required:

sudo apt install build-essential
clang curl git make

Install the Basic tools for Rust

Connect to your computer and launch a terminal shell. Execute the command.

sudo apt install –assume-yes git clang curl libssl-dev
  • Download the rustup installation program and run the following command to install Rust.
curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh
  • Cargo should be added to your current shell.
source $HOME/.cargo/env
  • Run the following command to validate your installation:
rustc –version
  • Run the following commands to set the Rust toolchain to launch with the most recent stable version:
rustup default stable
rustup update
  • Run the following commands to add the nightly release.
rustup update nightly
rustup target add wasm32-unknown-unknown –toolchain nightly
  • For Verification
rustup show
rustup +nightly show

Compilation of the Substrate Node Template

Let’s get started on the project after you finish the installation.

  1. Open a terminal shell on your computer.
  2. Run this command
git clone https://github.com/substrate-developer-hub/substrate-node-template
  1. Change the root directory to substrate-node-template.
cd substrate-node-template
  1. Run the following command to compile the node template.
cargo build –release

To create optimal artifacts, always use the –release flag.

NOTE: If your installation is complete but you are receiving an error on this command, please select the most recent branch based on version.

Let’s move on to the next section if your build was running successfully.

  • Let’s get started with the local node:

Run the following command to put the node in development mode.

./target/release/node-template –dev

Output:

If your output looks like this, your local node is up and running.

The substrate node template also provides you with some sample users to use with their balances and different access levels. Which you can see in the users list. We will connect three nodes to the bootstrap node for these sample users.

That’s it! Following all the steps mentioned above, you can create your first custom blockchain on substrate.

Creating a Bootstrap Node (First Blockchain Node)

Let’s go through how to build a first blockchain node or a bootstrap node and connect it with the other nodes.

  1. First Purge old chain data:

To start a new node, we must first clear the data for that user before moving on.

./target/release/node-template purge-chain –base-path /tmp/alice –chain local

The command requests that you confirm the operation:

Are you sure to remove “/tmp/alice/chains/local_testnet/db”? [y/N]:

If you want to delete your old data, please choose y.

  1. Let’s get your first blockchain node up and running with Alice:

In this, a bootnode is created, to which all other nodes will connect.

./target/release/node-template \
–base-path /tmp/alice \
–chain local \
–alice \
–port 30333 \
–ws-port 9945 \
–rpc-port 9933 \
–node-key 0000000000000000000000000000000000000000000000000000000000000001 \
–telemetry-url “wss://telemetry.polkadot.io/submit/ 0” \
–validator

Options:

–base-path 

specifies the location of all data associated with this chain.

chain local

Indicates the chain specification to be used. Local, development and staging are examples of valid predefined chain specifications.

alice

Adds the Alice account’s predefined keys to the node’s Keystore. The Alice account is used for block production and finalization with this setting.

port 30333

Specifies the port to which peer-to-peer traffic should be routed. 9945 ws-port

Specifies the port to which incoming WebSocket traffic should be routed. 

rpc-port

Specifies the port to which incoming RPC traffic should be routed. 

–node-key

This option specifies the Ed25519 secret key to be used for libp2p networking. This option should only be used for development and testing.

telemetry-url

Specifies where telemetry data should be sent. For this tutorial, you can send telemetry data to a Parity-hosted server that anyone can use.

validator

Indicates that this node participates in network block production and finalization.

  1. Your output will look like this after you run the command:

🔨 Initializing Genesis block/state (state: 0xf46a…f94a, header-hash: 0x530e…11a0): 

The initial or genesis block that the node is using is identified. Check that these values are the same when you start the next node.

🏷  Local node identity is: 12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp

This node’s unique string is specified. This key will also be used so that another node can connect to the Alice node.

💤 Idle (0 peers):

indicates that no other nodes are present in the network and that no blocks are being produced. Before blocks can be produced, another node must join the network.

  1. Run the bob account to launch a second local blockchain node:

Open the second terminal. Please clear out old chain data for Bob first. Also in the command you with comparing both commands for Alice and for bob you will see that they all have increments on a port, –ws-port, and –rpc-port.

./target/release/node-template \
–base-path /tmp/bob \
–chain local \
–bob \
–port 30334 \
–ws-port 9946 \
–rpc-port 9934 \
–telemetry-url “wss://telemetry.polkadot.io/submit/ 0” \
–validator \
–bootnodes /ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp

If you notice, we are using –bootnode in this command to connect with the Alice node using their locality, TCP port, and peer-to-peer connection we are using the key 12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp.

When you run the command, the output will look like this:

In the output, you can see that your node is now 💤 Idle (1 peers), which means that it is successfully connected to the Alice node.

  1. Run the charlie account to launch the next local blockchain node:
./target/release/node-template \
–base-path /tmp/charlie \
–chain local \
–charlie \
–port 30335 \
–ws-port 9947 \
–rpc-port 9935 \
–telemetry-url “wss://telemetry.polkadot.io/submit/ 0” \
–validator \
–bootnodes /ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp
  1. Run the eve account to launch the next local blockchain node:
./target/release/node-template \
–base-path /tmp/eve \
–chain local \
–eve \
–port 30336 \
–ws-port 9948 \
–rpc-port 9936 \
–telemetry-url “wss://telemetry.polkadot.io/submit/ 0” \
–validator \
–bootnodes /ip4/127.0.0.1/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp

So that’s it. You have successfully built your custom blockchain with four nodes.

You can find your local node or custom node in the development section of the official https://polkadot.js.org/apps website, after which you can change the network if you want to connect with the UI version.

Now you can see all the blocks and accounts, and you can create an account with a mnemonic as well. You can also transfer tokens and inspect and use pallets.

Conclusion

After going through the blog, you must have got an idea of what is polkadot and substrate. Also, following the step-by-step process listed above, you can create a custom blockchain and how to connect it with the 4 nodes.

For the latest updates and upcoming blogs, stay tuned to BlockTech Brew.

Frequently Asked Questions

What is token development?

Token development is the process of building tokens over the blockchain network by implementing several protocols, token standards, security standards and other tech stacks.

How much does it cost to develop a token?

The cost of token development depends on the functionalities of the token and the tech stack used to develop it. Blocktech Brew offers world-class token development services within your budget. Our token developers will help you launch secure tokens over major blockchain networks with unique features.

How to develop tokens?

Hire our token developers to develop and launch a highly efficient token for your ICO. We are here to accelerate your token development journey by building tokens loaded with exclusive functional parameters.
Get in touch with our experts to explore more about our token development services.

Why choose Blocktech Brew as your token development company?

Blocktech Brew is a top-rated token development company that offers end-to-end token development services. Our developers have core expertise in blockchain protocols and tech stacks like React JS, PHP, Laravel, Node JS, Golang and more to build tokens on any token standard of your choice.
Book your free consultation call today to discuss your project requirements.

How can you make money through tokens?

You can make money by trading tokens. Moreover, as a business owner, you can build tokens native to your project ecosystem and offer them to your investors in ICO. Thus, investors can buy your tokens and you will indirectly get funding for your crypto project. Investors profit by buying tokens at a discounted price in ICO. Moreover, they enjoy exclusive benefits like voting rights, access to special services and governance rights as token holders in the ecosystem. In this way both business owner and investor profits through tokens.

Have A Vision?

Share Your Idea Now & Step-Ahead With Innovative Blockchain Solutions.