Substrate: Build your first blockchain

Substrate Blockchain
Substrate Blockchain

Hello, today we are going to learn how you can build your first blockchain with Substrate. If you haven’t read my previous story on Overview of substrate. Go ahead and read this for a better understanding of the Substrate platform.

The substrate can be described as a blockchain framework. It is a open source framework for construction custom blockchains. These blockchains can run completely autonomously, meaning they do not rely on external technology to run. It allows you to build your blockchain from previously built components. i.e., allowing you to quickly build blockchains based on field-tested code that powers a large ecosystem of blockchain projects worldwide.

Consists of an extensive collection of tools and libraries, Substrate is the primary blockchain software development kit (SDK) which was used to build Polkadot layer-0 protocol and can be used by developers to create any type blockchain.

As you already know Blockchain consists of decentralized computers — called Nodes — to form a network.

The foundation provides a flexible, open and extensible development environment that allows you to design and build fully customized blockchain nodes to suit your application or business model needs.

If you want to become a blockchain developer. You need to learn how to compile and start a single local blockchain node. In this step-by-step guide, you’ll build and launch a single node blockchain using the node template.

The Substrate node template provides a working single-node blockchain that you can run locally in your development environment. The node template includes several predefined components—such as user accounts and account balances—so you can experiment with performing common tasks. Without making any changes to the template, you can run a functional node that produces blocks and allows transactions.

These are the steps you should follow.

After you’ve started your local blockchain node, we’ll also learn how to use a Substrate frontend template to view information about blockchain activity and submit a transaction.

Let’s move on.

Before you can start developing a Substratum-based blockchain, you need to prepare the development environment with the necessary compiler and tools. The first step in preparing your computer is to install Rust. The procedure for installation of Rust depends on the operating system of the computer you use for substrate development.

If you already have Rust installed, you can skip this step.

The Substrate node template provides a working development environment so you can start building on Substrate right away.

I have also discussed with you in my previous story how to build the node template. If you have already compiled the node template on your local computer, you can skip this step as well and proceed to the next step.

To compile the substrate node template:

  • Open a terminal shell on your computer.
  • Clone the node template repository by running the following command:
git clone https://github.com/substrate-developer-hub/substrate-node-template
  • Change to the root of the node template directory and check out polkadot-v0.9.26 branch by running the following command:
cd substrate-node-template && git checkout polkadot-v0.9.26
  • Compile the node template by running the following command:
cargo build --release

You should always use --release flag to build optimized artifacts.

After your node is compiled, you’re ready to start exploring what it does using front-end template.

To start the local substrate node:

  • Open a terminal shell.
  • Change to the root directory where you compiled the Substrate node template.
  • Start the node in development mode by running the following command:
./target/release/node-template --dev

The node-template command line options specify how you want the running node to behave. In this case --dev option specifies that the node runs in developer mode using the predefined development chain specification. By default, this option also deletes all active data – such as keys, the blockchain database, and network information when you stop the node by pressing Ctrl-c. User --dev option ensures that you have a clean working state every time you stop and restart the node.

  • Verify that your node is up and running by reviewing the output displayed in the terminal.

The terminal should display output similar to 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 < 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: 0xa59b…5331, header-hash: 0xc5d2…37f3)
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 (0xc5d2…37f3), ⬇ 0 ⬆ 0

If the number after finalized increases, your blockchain produces new blocks and achieves consensus on the state they describe.

I will explore the details of the log output in later stories. For now, it’s just important to know that your node is running and producing blocks

  • Keep the terminal showing the node output open to continue.

Now you have fully learned how to start a working Substratum-based blockchain node using the node template.

You can interact with the blockchain node using a front-end user interface.

The front-end template User ReactJS to render a browser interface that allows you to interact with the Substrat-based blockchain node. You can use this interface template as a starting point to create user interfaces for your own projects in the future.

The front-end template requires Yarn and Node.js. If you don’t have these tools, install them first.

To install the frontend template:

  • Check again node installed on your local computer by running the following command:
node --version
  • If the command does not return a version number, download and install node by following the instructions for the operating system you are using Node.js website. The node version should be at least v14 to run the frontend template.
  • Check again yarn installed on your local computer by running the following command:
yarn --version

The yarn version should be at least v3 to run the frontend template.

  • If the command does not return a version number, download and install yarn by running the following command:
npm install -g yarn
  • Clone the front-end template repository by running the following command:
git clone https://github.com/substrate-developer-hub/substrate-front-end-template
  • Switch to the root of the front-end template directory by running the following command:
cd substrate-front-end-template

The Substrate front-end template consists of user interface components that allow you to interact with the Substrate node and perform a few common tasks.

To use the frontend template:

  • Open a new terminal shell on your computer, change to the root directory where you installed the front-end template.
  • Start the front-end template by running the following command:
yarn start

Open in a browser to view the frontend template.

The top part has an account selection list to select the account you want to work with when you want to perform operations in the chain. The top part of the template also shows information about the chain you are connected to.

Account selection
Account selection

You may also notice that the front-end template shows a balance table with some pre-defined accounts, and that a few of these accounts are pre-configured with funds. You can use this sample data to test operations such as transferring funds.

Balance sheet for interface template
Balance sheet for interface template

Now that you have one blockchain node running on your local computer and you have one front-end template available to perform on-chain operations, you are ready to explore different ways to interact with the blockchain.

As standard is front-end template contains several components that allow you to try different common tasks. You can perform a simple transfer operation that moves funds from one account to another.

How to transfer money to an account:

  • Note the predefined accounts – for example dave – that have no funds associated with them in the balance table.
Balance table for transferring funds
Balance sheet for transferring funds
  • Below the Balance table, the front-end template also displays a transfer component. You use this component to transfer money from one account to another.
  • Copy and paste the address of the dave account to specify the address you are transferring money to.
  • Enter at least 10000000000000 as the amount to transfer, then click Send.
  • Notice that the values ​​in the Balance table are updated with the transfer.
Updated balance sheet
Updated balance sheet
  • Check the Events component to see events related to the transfer you just completed.

The Substrate blockchain reports the result of asynchronous operations as events, so you can use the event components to see details about each operation performed as part of the transfer. For example:

Example of event component
Example of event component
  • When the transaction is complete and included in a block, you’ll see a confirmation message similar to the following:

😉 Done. Block hash: 0xda7e9e935abf5a3a2fdb0a27d67cd7a69e628165b5827255af2635ba226411a4

After a successful transfer, you can continue to explore the front-end template components or stop the local Substrate node the state changes you made. Because you specified --dev option when you started the node, the local node stops the blockchain and clears all persistent block data so you can start with a clean state the next time you start the node.

To stop the local substrate node:

  • Return to the terminal shell where the node output is displayed.
  • Press Ctrl-c to end the running process.
  • Make sure your terminal returns to the terminal prompt i substrate-node-template catalog.

In this step-by-step guide to building a blockchain, you’ve learned that how to build your first blockchain with Substrate. You have also learned how to view and interact with the blockchain node using a front-end user interface and how to make a simple transfer from one account to another. Continue reading for more stories related to Substrate.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *