referrerpolicy=no-referrer-when-downgrade

polkadot_sdk_docs/polkadot_sdk/
substrate.rs

1//! # Substrate
2//!
3//! Substrate is a Rust framework for building blockchains in a modular and extensible way. While in
4//! itself un-opinionated, it is the main engine behind the Polkadot ecosystem.
5//!
6//! ## Overview, Philosophy
7//!
8//! Substrate approaches blockchain development with an acknowledgement of a few self-evident
9//! truths:
10//!
11//! 1. Society and technology evolves.
12//! 2. Humans are fallible.
13//!
14//! This makes the task of designing a correct, safe and long-lasting blockchain system hard.
15//!
16//! Nonetheless, in strive towards achieving this goal, Substrate embraces the following:
17//!
18//! 1. Use of **Rust** as a modern and safe programming language, which limits human error through
19//!    various means, most notably memory and type safety.
20//! 2. Substrate is written from the ground-up with a *generic, modular and extensible* design. This
21//!    ensures that software components can be easily swapped and upgraded. Examples of this is
22//!    multiple consensus mechanisms provided by Substrate, as listed below.
23//! 3. Lastly, the final blockchain system created with the above properties needs to be
24//!    upgradeable. In order to achieve this, Substrate is designed as a meta-protocol, whereby the
25//!    application logic of the blockchain (called "Runtime") is encoded as a WASM blob, and is
26//!    stored in the state. The rest of the system (called "node") acts as the executor of the WASM
27//!    blob.
28//!
29//! In essence, the meta-protocol of all Substrate based chains is the "Runtime as WASM blob"
30//! accord. This enables the Runtime to become inherently upgradeable, crucially without [forks](https://en.wikipedia.org/wiki/Fork_(blockchain)). The
31//! upgrade is merely a matter of the WASM blob being changed in the state, which is, in principle,
32//! same as updating an account's balance. Learn more about this in detail in
33//! [`crate::reference_docs::wasm_meta_protocol`].
34//!
35//! > A great analogy for substrate is the following: Substrate node is a gaming console, and a WASM
36//! > runtime, possibly created with FRAME is the game being inserted into the console.
37//!
38//! [`frame`], Substrate's default runtime development library, takes the above safety practices
39//! even further by embracing a declarative programming model whereby correctness is enhanced and
40//! the system is highly configurable through parameterization. Learn more about this in
41//! [`crate::reference_docs::trait_based_programming`].
42//!
43//! ## How to Get Started
44//!
45//! Substrate offers different options at the spectrum of technical freedom <-> development ease.
46//!
47//! * The easiest way to use Substrate is to use one of the templates (some of which listed at
48//!   [`crate::polkadot_sdk::templates`]) and only tweak the parameters of the runtime or node. This
49//!   allows you to launch a blockchain in minutes, but is limited in technical freedom.
50//! * Next, most developers wish to develop their custom runtime modules, for which the de-facto way
51//! is [`frame`](crate::polkadot_sdk::frame_runtime).
52//! * Finally, Substrate is highly configurable at the node side as well, but this is the most
53//!   technically demanding.
54//!
55//! > A notable Substrate-based blockchain that has built both custom FRAME pallets and custom
56//! > node-side components is <https://github.com/Cardinal-Cryptography/aleph-node>.
57#![doc = simple_mermaid::mermaid!("../../../mermaid/substrate_dev.mmd")]
58//!
59//! ## Structure
60//!
61//! Substrate contains a large number of crates, therefore it is useful to have an overview of what
62//! they are, and how they are organized. In broad terms, these crates are divided into three
63//! categories:
64//!
65//! * `sc-*` (short for *Substrate-client*) crates, located under `./client` folder. These are all
66//!   the crates that lead to the node software. Notable examples are [`sc_network`], various
67//!   consensus crates, RPC ([`sc_rpc_api`]) and database ([`sc_client_db`]), all of which are
68//!   expected to reside in the node side.
69//! * `sp-*` (short for *substrate-primitives*) crates, located under `./primitives` folder. These
70//!   are crates that facilitate both the node and the runtime, but are not opinionated about what
71//!   framework is using for building the runtime. Notable examples are [`sp_api`] and [`sp_io`],
72//!   which form the communication bridge between the node and runtime.
73//! * `pallet-*` and `frame-*` crates, located under `./frame` folder. These are the crates related
74//!   to FRAME. See [`frame`] for more information.
75//!
76//! ### WASM Build
77//!
78//! Many of the Substrate crates, such as entire `sp-*`, need to compile to both WASM (when a WASM
79//! runtime is being generated) and native (for example, when testing). To achieve this, Substrate
80//! follows the convention of the Rust community, and uses a `feature = "std"` to signify that a
81//!  crate is being built with the standard library, and is built for native. Otherwise, it is built
82//!  for `no_std`.
83//!
84//! This can be summarized in `#![cfg_attr(not(feature = "std"), no_std)]`, which you can often find
85//! in any Substrate-based runtime.
86//!
87//! Substrate-based runtimes use [`substrate_wasm_builder`] in their `build.rs` to automatically
88//! build their WASM files as a part of normal build command (e.g. `cargo build`). Once built, the
89//! wasm file is placed in `./target/{debug|release}/wbuild/{runtime_name}/{runtime_name}.wasm`.
90//!
91//! In order to ensure that the WASM build is **deterministic**, the [Substrate Runtime Toolbox (srtool)](https://github.com/paritytech/srtool) can be used.
92//!
93//! ### Anatomy of a Binary Crate
94//!
95//! From the above, [`node_cli`]/[`kitchensink_runtime`] and `node-template` are essentially
96//! blueprints of a Substrate-based project, as the name of the latter is implying. Each
97//! Substrate-based project typically contains the following:
98//!
99//! * Under `./runtime`, a `./runtime/src/lib.rs` which is the top level runtime amalgamator file.
100//!   This file typically contains the [`frame::runtime::prelude::construct_runtime`] and
101//!   [`frame::runtime::prelude::impl_runtime_apis`] macro calls, which is the final definition of a
102//!   runtime.
103//!
104//! * Under `./node`, a `main.rs`, which is the starting point, and a `./service.rs`, which contains
105//!   all the node side components. Skimming this file yields an overview of the networking,
106//!   database, consensus and similar node side components.
107//!
108//! > The above two are conventions, not rules.
109//!
110//! > See <https://github.com/paritytech/polkadot-sdk/issues/5> for an update on how the node side
111//! > components are being amalgamated.
112//!
113//! ## Parachain?
114//!
115//! As noted above, Substrate is the main engine behind the Polkadot ecosystem. One of the ways
116//! through which Polkadot can be utilized is by building "parachains", blockchains that are
117//! connected to Polkadot's shared security.
118//!
119//! To build a parachain, one could use [Cumulus](crate::polkadot_sdk::cumulus), the library on
120//! top of Substrate, empowering any substrate-based chain to be a Polkadot parachain.
121//!
122//! ## Where To Go Next?
123//!
124//! Additional noteworthy crates within substrate:
125//!
126//! - RPC APIs of a Substrate node: [`sc_rpc_api`]/[`sc_rpc`]
127//! - CLI Options of a Substrate node: [`sc_cli`]
128//! - All of the consensus related crates provided by Substrate:
129//!     - [`sc_consensus_aura`]
130//!     - [`sc_consensus_babe`]
131//!     - [`sc_consensus_grandpa`]
132//!     - [`sc_consensus_beefy`] (TODO: @adrian, add some high level docs <https://github.com/paritytech/polkadot-sdk-docs/issues/57>)
133//!     - [`sc_consensus_manual_seal`]
134//!     - [`sc_consensus_pow`]
135
136#[doc(hidden)]
137pub use crate::polkadot_sdk;