Web 3.0 development with Substrate. Part 4: Building blockchains.
This post is part of a series on Web 3.0 Development with Substrate.
A blockchain is both a dynamic database that relies on a changeable schema and a digital ledger that logs a series of transactions. As such, building blockchains means creating a logic and a finality for all operations, so that the state can be stored for future updates. Substrate facilitates these processes through the use of a blockchain framework that we explore in this article.
We have previously seen that a substrate node (also called a “substrate client”) is an application built with the Substrate Framework using Rust and WebAssembly/Wasm to interact with other nodes within the blockchain network. We have also seen that in a substrate node, it is the runtime that holds all the information necessary to build, run and maintain blockchain networks. One important point to note is that the runtime itself is built using a framework called FRAME, and it is this second layer of abstraction that allows a runtime to become highly customisable inside-out.
FRAME (Framework for Runtime Aggregation of Modularised Entities) is a set of pallets that can be used by blockchain developers to create specific runtimes and launch their blockchains with the support of in-built libraries. The pallets themselves are built using Rust crates to set the components of each runtime and customise the runtime’s functionality. For example, if I’m building a permissioned chain with off-chain governance, I might not need pallets like Democracy and Elections; but I will definitely need to include the pallets AURA and GRANDPA in my runtime in order to implement consensus functionalities.
Substrate supports the development of a variety of chains and blockchain networks by providing a collection of general-purpose pallets to manage account balances (Balances), fungible assets (Assets) or Wasm smart contracts (Contracts) on Public blockchains. This means that a developer with basic understanding of Substrate can straightforwardly move on to specialise in runtime development for different types of blockchains.
At node level, Private chains parameters are defined by network specifications and validator/authority keys contained in a Chain Spec file. Substrate builds a chain using a local, development, staging network or a custom chainspec to suit any blockchain project lifecycle. Substrate identifies the nodes that are tasked with producing and finalising blocks (aka “validator/authority”) though key pairs that are generated using available tools like Subkey or Polkadot-JS Apps. The chainspec created for a Private chain can then be stored into a JSON file that future participants can use to seamlessly bootstrap their node onto the existing network.
At runtime level, Permissioned chains are created through a node-authorization pallet that handles the configuration for well known nodes and PeerId ownership within the network. Substrate validates node authorisation by adding a normal node to a set of recognised nodes within the network (aka “well known nodes“) or by connecting a normal node to any node that is already part of the network. Each node on the network is then identified by a PeerId generated from the node’s own key (see “Subkey“) prior to initiating a connection, which makes network interactions more computationally efficient.
Substrate blockchains use components aggregated within three sets of native runtime pallets called System, Executive and Support to define, modify, broadcast and record their state within their own wasm runtime. These components themselves can be classified into three distinct but complementary categories: Type, Storage and Function. Type identifies the data structure of various parts of a component, for example: Account ID, Block number, Header, etc. Storage isolates the various parts of a component that are kept on-chain, for example: Account nonce, Block hash, Events, etc. Function summarises the series of computations carried out on various parts of a component, for example: read a block hash, verify an account ID, update the runtime version, etc.
Substrate blockchains runtimes are upgraded in a forkless manner: there is no need to stop and re-start all nodes in the network. During an upgrade, the existing logic of a blockchain that is stored in the runtime is replaced by a new logic, before being broadcast across the network. Other nodes in the network are notified of the upgrade and can then sync their runtimes to this new logic to access the new blockchain functionalities. This is made possible because each node can run both a native runtime that acts like a high-level/functional “cache” of the blockchain runtime, and a wasm runtime that encapsulates the blockchain logic and persists as a component within the blockchain itself.
Substrate blockchains states are updated by events (i.e state changes that are initiated by on-chain components) which are pre-defined in runtime pallets and by extrinsics (i.e state changes that are initiated by off-chain components) which are added into/removed from runtime pallets. This means that various changes such as a council runner up election or a staking reward distribution are managed by the blockchain logic itself and are automatically recorded into the blockchain state. By contrast, someone externally needs to submit extrinsics for a council candidacy bond or for a validator nomination from an account before any further events can happen internally on the blockchain.
Substrate puts in the hands of web developers all the tools that they need to foray and specialise in the field of blockchain development. Whether it is for Back-End, Front-End or Core Blockchain operations, Substrate reuses technologies and practices that are already popular in web development. When beginning to build with Substrate, web developers will gain foundational knowledge and skills for blockchain operations that they can increment by running their own projects.
In Part 5, we will present the specific knowledge and skills required to begin developing decentralised applications (DApps) with Substrate.
Substrate Developer Hub Tutorials:
- Create your first Substrate chain
- Start a Private Network
- Add a pallet to your runtime
- Upgrade a chain
- Build a permissioned network
Parity Technologies: An introduction to Substrate (Video)