How to Build Your First Blockchain on Substrate

How to Build Your First Blockchain on Substrate

·

10 min read

Blockchain software's sole purpose is to allow computers known as nodes to communicate with one another. Let me explain what a node is in more detail. A blockchain consists of many blocks of data. Nodes, which are servers, store these blocks.

On a blockchain, all nodes are linked to one another and constantly exchange the most recent information on the blockchain.

The nodes use a consensus algorithm to agree on the state and processing order of the blocks to ensure the chain's security and ongoing progress. Here's how to build your first blockchain on Substrate.

D_D Newsletter CTA

Blockchain Nodes

The nodes of a blockchain consist of the following parts:

  • Storage
  • Peer-to-peer networking
  • Capabilities for reaching consensus
  • External information data handling capabilities
  • A runtime environment

Creating these components is quite complex; most blockchain projects you see today are forks from an earlier blockchain project. A fork is a repository copy that allows you to experiment with changes without affecting the original project. You own your copy of a repository once you fork it. This means you can edit the contents of your forked repository without affecting the parent repo.

For example, people forked the Bitcoin repository to produce Litecoin, ZCash, Namecoin, and Bitcoin Cash. Similarly, some people forked the Ethereum repository to create Quorum, POA Network, KodakCoin, and Musicoin.

![forks-1024x453.jpg](Upload failed. Please re-upload the image align="left")

On the other hand, existing blockchain platforms aren't customizable. As a result, forking to create a new blockchain has significant drawbacks.

What is Substrate?

Substrate: A Rustic Vision for Polkadot by Gavin Wood at Web3 Summit 2018

Substrate is a blockchain framework that allows users to create their own custom blockchains. These blockchains can run entirely independently of any external technology. Substrate is open source, free, and extensible software. Substrate comes with exciting features like upgradeable blockchains, modular architecture, adjustable block execution logic, and hot-swappable consensus.

![1_XAo1yiwl_nlZkuE16tokHQ-1024x512.png](Upload failed. Please re-upload the image align="left")

Its creators built Substrate from the ground up to be adaptable, allowing developers to create their own blockchain networks. Everything you'll need to make your own blockchain node is included in the package.

The Substrate Developer Hub comes with a ready-to-use Substrate node template. Without changing anything, you can create a working blockchain network with predefined user accounts and funds using this node template.

Let's get right down to business. The most fundamental step toward becoming a blockchain developer is learning to compile and launch a single local blockchain node. This lesson will teach you how to use a node template to create and launch a single node blockchain. Following the launch of your new blockchain, you can view information about blockchain activity and submit a transaction to the blockchain using the frontend template from the Substrate Developer Hub.

You do not need prior programming or blockchain knowledge or experience to complete this lesson. This is only the first step, but hopefully, it will inspire you to continue your developer journey.

Installing Packages

UNIX-based operating systems, such as macOS or Linux, make it easier to develop substrates. To install the required packages on macOS or Linux, follow these steps:

  • On your computer, launch a terminal shell.
  • Find your operating system in the table below and run the appropriate commands for your development environment.
OSInstallation commands
Ubuntu or Debiansudo apt update && sudo apt install -y git clang curl libssl-dev llvm libudev-dev
Arch Linuxpacman -Syu –needed –noconfirm curl git clang
Fedorasudo dnf update sudo dnf install clang curl git openssl-devel
OpenSUSEsudo zypper install clang curl git openssl-devel llvm-devel libudev-devel
macOSbrew update && brew install openssl

If you are using macOS and do not have Homebrew installed, run the following command to install Homebrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" After installing Homebrew, runbrew install openssl.` "

We will stop here for today in the following article will show you how to install Rust and the Rust toolchain.

Installing Rust and the Rust Toolchain

One of the most intriguing development trends in Web3 is the advent of Rust as a programming language for blockchains. Rust is a low-level programming language that provides direct access to hardware and memory, making it an excellent choice for embedded and bare-metal development.

  • Start by installing rustup by running the following command:

    curl sh.rustup.rs -sSf | sh

  • Configure your current shell to reload your PATH environment variable so that it includes the Cargo bin directory by running the following command:

    source ~/.cargo/env

  • Now configure the Rust toolchain to the latest stable version by running the following commands:

rustup default stable 
rustup update

Now add the nightly release & the nightly WebAssembly (wasm) targets by running the following commands:

rustup update nightly
rustup target add wasm32-unknown-unknown --toolchain nightly

Here, you can verify your installation by running the following commands:

rustc --version
rustup show

The preceding steps guide you through the installation and configuration of Rust and the Rust toolchain, allowing you to experience the entire process for yourself.

You can also use a script to automate the steps. If you wish to attempt installing and configuring Rust with a script, consider the getsubstrate automation script.

Creating a Development Environment

You are now ready to set up a development environment on your local computer after you have completed the above steps.

You may create a development environment using your preferred tools, but the Substrate Developer Hub provides templates to get you started. You can use the development environment in later courses as you learn more about building on Substrate.

Preparing a Substrate Node Using the Node Template

The Substrate node template provides a working development environment, allowing you to begin building on Substrate right immediately.

Compiling the Substrate Node Template

  • By using the following command, you can clone the node template repository using the version latest branch:
git clone https://github.com/substrate-developer-hub/substrate-node-template
  • By running the following command, you can navigate to the root of the node template directory:
cd substrate-node-template
# We want to use the `latest` tag throughout all of this tutorial
git checkout latest
  • Run the following command to compile the node template:
cargo build --release

Always use the -release flag to create optimal artifacts.

Setting up the Frontend Template

The frontend template renders a web browser interface that allows you to communicate with the Substrate-based blockchain node using ReactJS. You can use this frontend template to create user interfaces for your own projects in the future.

The frontend template requires Yarn and Node.js. If you don't have these tools, install them first.

To install the frontend template, check whether Node.js is installed on your local computer by running the following command:

node --version

If the command does not produce a version number, download and install Node.js with the instructions on the Node.js websitefor your operating system. To run the frontend template, Node should be at least version 14.

Check whether Yarn is installed on your local computer by running the following command:

yarn --version

If the command does not provide a version number, run the following command to download and install Yarn:

npm install -g yarn

Next, clone the frontend template repository by running the following command:

git clone https://github.com/substrate-developer-hub/substrate-front-end-template

Run the following command to go to the root of the frontend template directory:

cd substrate-front-end-template
# We want to use the `latest` tag throughout all of this tutorial
git checkout latest

Install the frontend template's dependencies by running the following command:

yarn install

Starting the Node and Exploring Blocks

After your node compiles, you may use the frontend template to investigate what it does.

To launch the local Substrate node, follow these steps:

  • Launch a terminal shell.
  • Navigate to the root directory where you compiled the Substrate node template.
  • Run the following command to put the node in development mode:
./target/release/node-template --dev

The node-template command-line arguments indicate how the operating node should behave. The --dev option tells the node to run in developer mode, with the predefined development chain specification. When you shut down the node by pressing control-c, this option deletes all active data, including keys, the blockchain database, and networking information. When you use the --dev option, you ensure that you always have a clean working state when you stop and resume the node.

Examine the output in the terminal to ensure your node is up and functioning. The terminal should output something like this:

2021-11-24 15:36:35 Running in --dev mode, RPC CORS has been disabled.
2021-11-24 15:36:35 Substrate Node
2021-11-24 15:36:35 ✌️  version 4.0.0-dev-82b7c2c-aarch64-macos
2021-11-24 15:36:35 ❤️  by Substrate DevHub <https://github.com/substrate-developer-hub>, 2017-2021
2021-11-24 15:36:35 📋 Chain specification: Development
2021-11-24 15:36:35 🏷 Node name: six-wash-9274
2021-11-24 15:36:35 👤 Role: AUTHORITY
2021-11-24 15:36:35 💾 Database: RocksDb at /tmp/substrateP1jD7H/chains/dev/db
2021-11-24 15:36:35 ⛓  Native runtime: node-template-100 (node-template-1.tx1.au1)
2021-11-24 15:36:35 🔨 Initializing Genesis block/state (state: 0xa59b5331, header-hash: 0xc5d237f3)
2021-11-24 15:36:35 👴 Loading GRANDPA authority set from genesis on what appears to be first startup.
2021-11-24 15:36:35 ⏱  Loaded block-time = 6s from block 0xc5d2fdad35e14684753f087c1a20f022274e154d39add4f7efe34e95476a37f3
2021-11-24 15:36:35 Using default protocol ID "sup" because none is configured in the chain specs
2021-11-24 15:36:35 🏷 Local node identity is: 12D3KooWG5niQF5bjsFao3D8DZRpUUB6uWZC2pK8hCDZ94zsr8Sc
2021-11-24 15:36:35 📦 Highest known block at #0
...
...
...
2021-11-24 15:36:40 💤 Idle (0 peers), best: #1 (0xd2b5…d03f), finalized #0 (0xc5d237f3), ⬇ 00

Starting the Frontend Template

The user interface components of the Substrate frontend template allow you to interact with the Substrate node and execute a few typical operations.

To use the frontend template:

  • Launch a new terminal shell and navigate to the root directory where you installed the frontend template.
  • Run the following command to launch the Front-end template:
yarn start

To view the frontend template, navigate to localhost:8000 in a browser. The top portion has an Account selection list for choosing the account to operate with when doing on-chain actions. The template's top part also displays information about the chain to which you're connected.

blockchain-top.png

You may also observe that the frontend template includes a balances table with various predefined accounts, some of which are preconfigured with funds. You can use this example data to experiment with procedures such as fund transfers.

balances.png

Transfering Funds From One Account to Another

You're ready to experiment with different methods to interface with the blockchain now that you have a blockchain node on your local computer and a frontend template for conducting on-chain operations.

The frontend template provides several components by default that allow you to experiment with various typical activities. With is steps, you can perform a simple transfer operation to move funds from one account to another.

To transfer funds to an account:

In the Balances table, notice the predefined accounts—such as dave—that have no funds.

dave-account.png

The frontend template also has a Transfer component under the Balances table. You use this component to move money from one account to another.

  • To identify the address to whom you are transferring funds, copy and paste the address for the Dave account.
  • The amount to transfer must be at least 1000000000000, then click Submit.

transfer-filled.png

With the transfer, the values in the Balances table are updated.

balance-result.png

To observe occurrences linked to the transfer you just finished, go to the Events component. You can use the Events components to see details about each activity conducted as part of the transfer because the Substrate blockchain reports the result of asynchronous activities as events. As an illustration:

event-panel.png

When a transaction is completed and included in a block, you will receive a confirmation message that looks like this:

Finalized. Block hash: 0xda7e9e935abf5a3a2fdb0a27d67cd7a69e628165b5827255af2635ba226411a4

Stopping the Local Node

You can continue exploring the frontend template components or stop the local Substrate node after a successful transfer. Stopping the local node terminates the blockchain and purges all permanent block data so that you can start with a clean state the next time you start the node because you used the —dev option when you started it.

To disable the local Substrate node, do the following:

  • Return to the terminal shell, where you may see the node output.
  • To stop a running process, press control-c.
  • Check that your terminal returns to the substrate-node-template directory's terminal prompt.

D_D Newsletter CTA

Summary

In this tutorial, you learned how to use the substrate-node template to create a functioning Substrate-based blockchain node, how to use a frontend user interface to see and interact with a blockchain node, and how to transfer funds from one account to another.