Create your first Substrate blockchain
Blockchain software enables individual computers—called nodes—to communicate with each other to form a decentralized peer-to-peer (P2P) network. To ensure the security of the data on the chain and the ongoing progress of the chain, the nodes use some form of consensus to agree on the state of data in each block of data and the order in which the blocks are processed.
At a high level, a blockchain node consists of the following key components:
- Peer-to-peer networking
- Consensus capabilities
- Data handling capabilities for external or "extrinsic" information
- A Runtime
Because of the complexity involved in building these components, most blockchain projects are forked from an existing blockchain project. For example, the Bitcoin repository was forked to create: Litecoin, ZCash, Namecoin, and Bitcoin Cash. Similarly, the Ethereum repository was forked to create Quorum, POA Network, KodakCoin, and Musicoin.
However, the existing blockchain platforms were not designed to allow for modification. As a result, building a new blockchain by forking has serious limitations.
Substrate is an open source, modular, and extensible framework for building blockchains.
Substrate has been designed from the ground up to be flexible and allow innovators to design and build a blockchain network that meets their needs. It provides all the core components you need to build a customized blockchain node.
To get you started, the Substrate Developer Hub provides an out-of-the-box working Substrate-based node template. Without making any changes, you can use this node template to create a working blockchain network with some predefined user accounts and funds.
The first step in becoming a blockchain developer is to learn how to compile and launch a single local blockchain node. In this tutorial, you build and start a single node blockchain using a node template. After you start your new blockchain, you can use the Substrate Developer Hub front-end template to view information about blockchain activity and submit a transaction.
This tutorial provides a basic introduction to Substrate and prepares a minimal working development environment that you can use to explore further in additional tutorials. It is intended for anyone interested in learning about Substrate and blockchain development. The tutorial assumes you have no prior experience or knowledge of Substrate. You don't need any programming or blockchain experience to complete this tutorial. This is just the first step, but hopefully, it inspires you to continue your journey.
This tutorial requires compiling Rust code and takes approximately one to two hours to complete.
For this tutorial, you download and use working code. Before you begin, verify the following:
You have good internet connection and access to a shell terminal on your local computer.
You are generally familiar with software development and use command-line interfaces.
You are generally familiar with blockchains and smart contract platforms.
You want to learn about the bleeding edge of blockchain development.
By completing this tutorial, you will accomplish the following objectives:
1. Set up a Substrate development environment on your computer.
2. Use a node template project to run a Substrate-based blockchain.
3. Use a front-end template project to interact with the template node.
Substrate development is easiest on UNIX-based operating systems like macOS or Linux. If you are using Microsoft Windows, refer to the Windows installation page.
To install required packages on macOS or Linux:
Open a terminal shell on your computer.
Locate your operating system in the following table and run the appropriate commands for your development environment.
OS Installation commands Ubuntu or Debian
sudo apt update && sudo apt install -y git clang curl libssl-dev llvm libudev-dev
pacman -Syu --needed --noconfirm curl git clang
sudo dnf update sudo dnf install clang curl git openssl-devel
sudo zypper install clang curl git openssl-devel llvm-devel libudev-devel
brew update && brew install openssl
Windows Refer to this installation guide.
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, run
brew install openssl.
To install and configure Rust manually:
rustupby running the following command:
curl https://sh.rustup.rs -sSf | sh
Configure your current shell to reload your PATH environment variable so that it includes the Cargo
bindirectory by running the following command:
Configure the Rust toolchain to default to the latest
stableversion by running the following commands:
rustup default stable rustup update
nightlyrelease and the
wasm) targets by running the following commands:
rustup update nightly rustup target add wasm32-unknown-unknown --toolchain nightly
Verify your installation by running the following commands:
rustc --version rustup show
The previous steps walked you through the installation and configuration of Rust and the Rust toolchain so that you could see the full process for yourself.
It is also possible to automate the steps using a script. If you want to try installing and configuring Rust using a script, see the
After you install the required packages and the Rust compiler and toolchain, you are ready to set up a development environment on your local computer. You could build a development environment manually using the tools of your choice, but the Substrate Developer Hub has templates to get you started. After you set up the development environment, you can use it in subsequent tutorials as you learn more about building on Substrate.
The Substrate Node Template provides a working development environment so that you can start building on Substrate right away.
To compile the Substrate Node Template:
Clone the node template repository using the version
latestbranch by running the following command:
git clone -b latest --depth 1 https://github.com/substrate-developer-hub/substrate-node-template
Change to the root of the node template directory by running the following command:
Compile the node template by running the following command:
cargo build --release
You should always use the
--releaseflag to build optimized artifacts.
The Front-end Template uses ReactJS to render a web browser interface that enables you to interact with the Substrate-based blockchain node. You can use this Front-end template as a starting point for creating user interfaces for your own projects in future.
To install the Front-end Template:
nodeis installed on your local computer by running the following command:
If the command doesn’t return a version number, download and install
nodeby following the instructions for the operating system you use on the Node.js website. Node version should be at least v14 to run the Front-end Template.
yarnis installed on your local computer by running the following command:
If the command doesn’t return a version number, download and install
yarnby running the following command:
npm install -g yarn
Clone the Front-end Template repository by running the following command:
git clone -b latest --depth 1 https://github.com/substrate-developer-hub/substrate-front-end-template
Change to the root of the front-end template directory by running the following command:
Install the dependencies for the front-end template by running the following command:
After your node compiles, you are ready to start exploring what it does using the 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 --tmp
node-templatecommand-line options specify how you want the running node to operate. In this case, the options specify the following:
--devoption specifies that the node is run as a developer node chain specification.
--tmpoption specifies that the node will delete all active data—such as keys, blockchain database, and networking information when you stop the node by pressing Control-c. You can use
--tmpoption to ensure you have a clean working state any time you stop and restart the node.
Verify your node is up and running successfully by reviewing the output displayed in the terminal.
The terminal should display output similar to this:
2021-03-16 10:56:51 Running in --dev mode, RPC CORS has been disabled. 2021-03-16 10:56:51 Substrate Node 2021-03-16 10:56:51 ✌️ version 3.0.0-8370ddd-x86_64-linux-gnu 2021-03-16 10:56:51 ❤️ by Substrate DevHub <https://github.com/substrate-developer-hub>, 2017-2021 2021-03-16 10:56:51 📋 Chain specification: Development 2021-03-16 10:56:51 🏷 Node name: few-size-5380 2021-03-16 10:56:51 👤 Role: AUTHORITY 2021-03-16 10:56:51 💾 Database: RocksDb at /tmp/substrateP1jD7H/chains/dev/db 2021-03-16 10:56:51 ⛓ Native runtime: node-template-100 (node-template-1.tx1.au1) 2021-03-16 10:56:51 🔨 Initializing Genesis block/state (state: 0x17df…04a0, header-hash: 0xc43b…ed16) 2021-03-16 10:56:51 👴 Loading GRANDPA authority set from genesis on what appears to be first startup. 2021-03-16 10:56:51 ⏱ Loaded block-time = 6000 milliseconds from genesis on first-launch 2021-03-16 10:56:51 Using default protocol ID "sup" because none is configured in the chain specs 2021-03-16 10:56:51 🏷 Local node identity is: 12D3KooWQdU84EJCqDr4aqfhb7dxXU2fzd6i2Rn1XdNtsiM5jvEC 2021-03-16 10:56:51 📦 Highest known block at #0 ... ... ... 2021-03-16 10:56:56 💤 Idle (0 peers), best: #2 (0x05bd…de3f), finalized #0 (0xc43b…ed16), ⬇ 0 ⬆ 0
If the number after
finalizedis increasing, your blockchain is producing new blocks and reaching consensus about the state they describe.
We'll look into the details of what's reported in the log output in a later tutorial. For now, it's only important to know that your node is running and producing blocks.
Keep the terminal that displays the node output open to continue.
The Substrate Front-end Template consists of user interface components to enable you to interact with the Substrate node and perform a few common tasks.
To use the Front-end 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:
Open http://localhost:8000 in a browser to view the Front-end Template.
The top section has an Account selection list for selecting the account to work with when you want to perform on-chain operations. The top section of the template also displays information about the chain to which you're connected.
You might also notice that the Front-end Template displays a Balances table with some predefined accounts and that a few of those accounts are preconfigured with funds. You can use this sample data to try out operations like transferring funds.
Now that you have a blockchain node running on your local computer and you have a Front-end Template available for performing on-chain operations, you are ready to explore different ways to interact with the blockchain.
By default, the Front-end Template includes several components that allow you to try different common tasks. For this tutorial, you can perform a simple transfer operation that moves 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 associated with them.
Under the Balances table, the front-end template also displays a Transfer component. You use this component to transfer funds from one account to another.
Copy and paste the address for the dave account to specify the address to which you are transferring funds.
Specify at least 1000000000000 as the amount to transfer, then click Submit.
Notice that the values in Balances table is updated with the transfer.
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 Events components to see details about each operation performed as part of the transfer. For example:
When the transaction has been completed and included in a block, you see a confirmation message similar to the following:
😉 Finalized. 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.
--tmp flag specified when running the Node Template, stopping the local node stops the
blockchain and purge all persistent block data that goes with it so you can start with a clean new
state next time you start again.
To stop the local Substrate node:
Return to the terminal shell where the node output is displayed.
Press Control-c to terminate the running process.
Verify your terminal returns to the terminal prompt in the
In this tutorial, you learned:
How to start a working Substrate-based blockchain node using the node.
How to view and interact with the blockchain node using a front-end user interface.
How to make a simple transfer from one account to another.
The Front-end Template includes several additional components for you to experiment with while you're connected to a local development node.
You can explore these components on your own or may interested to learn more about the following:
If you've experienced any issues with this tutorial or wanted to provide feedback: