Book a free call

How to Build a Blockchain with Substrate

Substrate lets you build a custom blockchain from ready-made parts in Rust. Here is the setup, the pallet workflow, and the mistakes to avoid.

Hertiv engineering teamReviewed by a senior engineer
6 min read
How to Build a Blockchain with Substrate
On this page

Short answer

Substrate is a framework for building custom blockchains, written in Rust and maintained as part of the Polkadot SDK. It gives a developer the networking, consensus and storage layers ready-made, so the work focuses on the chain logic, which is written as modular pieces called pallets and compiled to WebAssembly.

What is Substrate?

Substrate is a framework for building custom blockchains, written in Rust and maintained as part of the Polkadot SDK. Instead of writing a blockchain from scratch, a developer starts from ready-made pieces and adds only the logic that makes their chain different.

The framework gives you the parts every chain needs: networking, consensus, storage and a runtime that defines what the chain does. The runtime is the heart of the chain, and it compiles to WebAssembly, which is what lets Substrate chains upgrade their logic without a hard fork.

Substrate chains can run on their own, or connect to the Polkadot relay chain as parachains and share its security. For the basic concepts first, see what Substrate is.

What are the pieces of a Substrate chain?

A Substrate chain is built from three layers, and knowing which layer does what keeps the build organised. The node handles networking and block production, the runtime holds the chain logic, and pallets are the modules that make up the runtime.

PieceWhat it doesWhere it lives
NodeNetworking, block production, RPCnode/ directory
RuntimeState transition logic of the chainruntime/ directory
PalletsModules such as balances, staking, governancepallets/ directory
FRAMELibrary of pallets and runtime helpersUsed by the runtime
ConsensusBlock production and finalityBABE, GRANDPA or Aura

FRAME, short for Framework for Runtime Aggregation of Modularized Entities, is the library of ready-made pallets. Common ones cover balances, staking, governance, assets and smart contracts, and a chain can use them as they are or write its own. For what people build with it, see Substrate use cases for custom chains.

How to build a Substrate blockchain, step by step

Building a Substrate blockchain means setting up the Rust toolchain, starting from the node template, adding a custom pallet, wiring it into the runtime, and running the chain locally. The template does most of the heavy work, so the build focuses on the logic that is new.

  1. Install Rust. Install rustup, add the nightly toolchain, and add the WebAssembly target, since the runtime compiles to Wasm.
  2. Clone the node template. Clone the Substrate node template, which gives a runnable chain with a basic runtime, then check that it compiles.
  3. Learn the layout. Read the node, runtime and pallets directories so you know where each piece lives before you change anything.
  4. Write a custom pallet. Create a pallet in the pallets directory, defining its storage, events, errors and the functions users can call.
  5. Wire it into the runtime. Add the pallet as a dependency, implement its Config trait, and add it to the construct_runtime macro.
  6. Build and run. Build the node, run it in development mode, and open Polkadot-JS Apps to try your pallet through the browser.
  7. Test and upgrade. Write tests for the pallet, then try a forkless runtime upgrade by changing the pallet and submitting the new Wasm.

Build times are long on the first compile, because the Rust toolchain builds the whole node and runtime. Later builds are faster, since only the changed parts rebuild.

What is a pallet and how do you write one?

A pallet is a module of chain logic: it holds storage items, events, errors and the dispatchable functions that users call. A balances pallet holds account balances and transfer logic; a staking pallet holds validator selection and rewards; a custom pallet holds whatever the chain needs that no ready-made pallet covers.

A pallet is written in Rust with the frame_support macros. The pallet macro marks the module, the config trait lists what the pallet needs from the runtime, and each dispatchable function is a call that changes state. Storage items are declared with the storage macro, and every change emits an event so off-chain tools can follow what happened.

Keep pallets small and single-purpose. A pallet that does one job is easier to test, easier to reuse in another chain, and easier to upgrade later than a pallet that tries to cover several jobs at once. For the wider Polkadot picture, see what Polkadot is.

How do consensus and finality work in Substrate?

Substrate offers several consensus options, and the template picks a sensible default. BABE produces blocks, GRANDPA finalises them, and Aura offers a simpler authority-based round-robin for chains that do not need BABE.

Block production and finality are separate jobs. BABE decides which validator may produce each block, using a lottery based on stake, while GRANDPA gathers votes from validators and finalises blocks once enough of them agree. A block that is finalised cannot be reverted, which is what applications rely on when they treat a transaction as settled.

For a development chain, a single-authority setup is enough. For a live chain, the validator set, the staking pallet and the session keys need careful planning, since a mistake there can stall the chain. For a comparison with the other major framework, see Cosmos vs Polkadot.

Example (hypothetical): a supply-chain tracking chain

Example (hypothetical): a team wants a chain that tracks food shipments from farm to shop. Each shipment is an item on the chain, and each handover is a transaction signed by the party that holds the goods.

The team starts from the node template and writes one custom pallet with three functions: register a shipment, record a handover, and mark a shipment delivered. The pallet stores 1,000 shipments in its first month, and each shipment averages 4 handovers, so the chain records 1,000 x 4 = 4,000 handover transactions plus 1,000 registrations, or 5,000 transactions in total.

The build takes 9 days: 1 day to set up the toolchain and template, 5 days to write and test the pallet, and 3 days to build the front end that reads the chain. The lesson is that the pallet is the small part of the work; the front end and the testing take most of the time.

What are the common mistakes?

The most common mistakes when building with Substrate are skipping the tests, making pallets too big, and treating the first build as the finished chain.

  • No pallet tests. A pallet without tests breaks silently when the runtime changes. Write tests for every dispatchable function.
  • One giant pallet. A pallet that covers balances, governance and custom logic in one module is hard to test and hard to upgrade. Split by job.
  • Wrong toolchain. Substrate needs a specific nightly Rust toolchain and the Wasm target. Building with the wrong toolchain fails in confusing ways.
  • Ignoring weights. Every dispatchable function needs a weight, which is how the chain charges for computation. Guessing weights invites spam.
  • Upgrading without a plan. Forkless upgrades are powerful, but an upgrade that changes storage layout without a migration can corrupt state.
  • Testing only in dev mode. A chain that works with one validator in dev mode can stall with real validators. Test with several nodes before launch.

Key takeaways

  • Substrate gives you networking, consensus and storage ready-made, so you build only the chain logic
  • Pallets are the modules of the runtime; FRAME is the library of ready-made ones
  • Start from the node template, add a custom pallet, wire it into the runtime, then run it
  • BABE produces blocks and GRANDPA finalises them; Aura is the simpler option
  • The runtime compiles to WebAssembly, which allows forkless upgrades
  • Keep pallets small, test every function, and set weights for each call

Frequently asked questions

Do I need to know Rust to build with Substrate?

Yes. Substrate is written in Rust, and pallets are Rust code. A developer who knows Rust can follow the node template and write a first pallet in days, while a team without Rust should budget time to learn the language first.

What is a pallet in Substrate?

A pallet is a module of chain logic that holds storage, events, errors and the functions users can call. Ready-made pallets cover balances, staking, governance and assets, and a custom pallet holds the logic that makes a chain different from the template.

What is the Substrate node template?

The node template is a ready-to-run Substrate chain with a basic runtime. It gives a developer a working node, a runtime and a place to add custom pallets, so the build starts from a chain that already compiles and runs.

Can a Substrate chain upgrade without a hard fork?

Yes. The runtime compiles to WebAssembly and can be replaced on-chain through a runtime upgrade, so the chain logic changes without splitting the network. Storage changes still need a migration plan, so upgrades need testing before they ship.

What is the difference between Substrate and Polkadot?

Substrate is the framework for building a blockchain, while Polkadot is a network that Substrate chains can join as parachains to share security. A Substrate chain can also run on its own without joining Polkadot.

How long does it take to build a Substrate blockchain?

A simple chain from the node template with one custom pallet can run locally in days, while a production chain with several pallets, tests, a front end and an audit takes weeks or months. The pallet is usually the small part; testing and the front end take most of the time.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: blockchain systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building a custom blockchain with Substrate? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Cosmos & Substrate service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call