polkadot_sdk_docs/reference_docs/wasm_meta_protocol.rs
1//! # WASM Meta Protocol
2//!
3//! All Substrate based chains adhere to a unique architectural design novel to the Polkadot
4//! ecosystem. We refer to this design as the "**WASM Meta Protocol**".
5//!
6//! Consider the fact that a traditional blockchain software is usually a monolithic artifact.
7//! **Upgrading any part of the system implies upgrading the entire system**. This has historically
8//! led to cumbersome forkful upgrades to be the status quo in blockchain ecosystems. In other
9//! words, the entire node software is the specification of the blockchain's [`state transition
10//! function`](crate::reference_docs::blockchain_state_machines).
11//!
12//! Moreover, the idea of "storing code in the state" is explored in the context of smart contracts
13//! platforms, but has not been expanded further.
14//!
15//! Substrate mixes these two ideas together, and takes the novel approach of storing the
16//! blockchain's main "state transition function" in the main blockchain state, in the same fashion
17//! that a smart contract platform stores the code of individual contracts in its state. As noted in
18//! [`crate::reference_docs::blockchain_state_machines`], this state transition function is called
19//! the **Runtime**, and WASM is chosen as the bytecode. The Runtime is stored under a special key
20//! in the state (see [`sp_core::storage::well_known_keys`]) and can be updated as a part of the
21//! state transition function's execution, just like a user's account balance can be updated.
22//!
23//! > Note that while we drew an analogy between smart contracts and runtimes in the above, there
24//! > are fundamental differences between the two, explained in
25//! > [`crate::reference_docs::runtime_vs_smart_contract`].
26//!
27//! The rest of the system that is NOT the state transition function is called the
28//! [**Node**](crate::reference_docs::glossary#node), and is a normal binary that is compiled from
29//! Rust to different hardware targets.
30//!
31//! This design enables all Substrate-based chains to be fork-less-ly upgradeable, because the
32//! Runtime can be updated on the fly, within the execution of a block, and the node is (for the
33//! most part) oblivious to the change that is happening.
34//!
35//! Therefore, the high-level architecture of a any Substrate-based chain can be demonstrated as
36//! follows:
37#![doc = simple_mermaid::mermaid!("../../../mermaid/substrate_simple.mmd")]
38//! The node and the runtime need to communicate. This is done through two concepts:
39//!
40//! 1. **Host functions**: a way for the (WASM) runtime to talk to the node. All host functions are
41//! defined in [`sp_io`]. For example, [`sp_io::storage`] are the set of host functions that
42//! allow the runtime to read and write data to the on-chain state.
43//! 2. **Runtime APIs**: a way for the node to talk to the WASM runtime. Runtime APIs are defined
44//! using macros and utilities in [`sp_api`]. For example, [`sp_api::Core`] is the most
45//! fundamental runtime API that any blockchain must implement in order to be able to (re)
46//! execute blocks.
47#![doc = simple_mermaid::mermaid!("../../../mermaid/substrate_client_runtime.mmd")]
48//! A runtime must have a set of runtime APIs in order to have any meaningful blockchain
49//! functionality, but it can also expose more APIs. See
50//! [`crate::reference_docs::custom_runtime_api_rpc`] as an example of how to add custom runtime
51//! APIs to your FRAME-based runtime.
52//!
53//! Similarly, for a runtime to be "compatible" with a node, the node must implement the full set of
54//! host functions that the runtime at any point in time requires. Given the fact that a runtime can
55//! evolve in time, and a blockchain node (typically) wishes to be capable of re-executing all the
56//! previous blocks, this means that a node must always maintain support for the old host functions.
57//! **This implies that adding a new host function is a big commitment and should be done with
58//! care**. This is why, for example, adding a new host function to Polkadot always requires an RFC.
59//! Learn how to add a new host function to your runtime in
60//! [`crate::reference_docs::custom_host_functions`].
61//!
62//! ## Node vs. Runtime
63//!
64//! A common question is: which components of the system end up being part of the node, and which
65//! ones of the runtime?
66//!
67//! Recall from [`crate::reference_docs::blockchain_state_machines`] that the runtime is the state
68//! transition function. Anything that needs to influence how your blockchain's state is updated,
69//! should be a part of the runtime. For example, the logic around currency, governance, identity or
70//! any other application-specific logic that has to do with the state is part of the runtime.
71//!
72//! Anything that does not have to do with the state-transition function and will only
73//! facilitate/enable it is part of the node. For example, the database, networking, and even
74//! consensus algorithm are all node-side components.
75//!
76//! > The consensus is to your runtime what HTTP is to a web-application. It is the underlying
77//! > engine that enables trustless execution of the runtime in a distributed manner whilst
78//! > maintaining a canonical outcome of that execution.
79#![doc = simple_mermaid::mermaid!("../../../mermaid/substrate_with_frame.mmd")]
80//! ## State
81//!
82//! From the previous sections, we know that the database component is part of the node, not the
83//! runtime. We also hinted that a set of host functions ([`sp_io::storage`]) are how the runtime
84//! issues commands to the node to read/write to the state. Let's dive deeper into this.
85//!
86//! The state of the blockchain, what we seek to come to consensus about, is indeed *kept* in the
87//! node side. Nonetheless, the runtime is the only component that:
88//!
89//! 1. Can update the state.
90//! 2. Can fully interpret the state.
91//!
92//! In fact, [`sp_core::storage::well_known_keys`] are the only state keys that the node side is
93//! aware of. The rest of the state, including what logic the runtime has, what balance each user
94//! has and such, are all only comprehensible to the runtime.
95#![doc = simple_mermaid::mermaid!("../../../mermaid/state.mmd")]
96//! In the above diagram, all of the state keys and values are opaque bytes to the node. The node
97//! does not know what they mean, and it does not know what is the type of the corresponding value
98//! (e.g. if it is a number of a vector). Contrary, the runtime knows both the meaning of their
99//! keys, and the type of the values.
100//!
101//! This opaque-ness is the fundamental reason why Substrate-based chains can fork-less-ly upgrade:
102//! because the node side code is kept oblivious to all of the details of the state transition
103//! function. Therefore, the state transition function can freely upgrade without the node needing
104//! to know.
105//!
106//! ## Native Runtime
107//!
108//! Historically, the node software also kept a native copy of the runtime at the time of
109//! compilation within it. This used to be called the "Native Runtime". The main purpose of the
110//! native runtime used to be leveraging the faster execution time and better debugging
111//! infrastructure of native code. However, neither of the two arguments strongly hold and the
112//! native runtime is being fully removed from the node-sdk.
113//!
114//! See: <https://github.com/paritytech/polkadot-sdk/issues/62>
115//!
116//! > Also, note that the flags [`sc_cli::ExecutionStrategy::Native`] is already a noop and all
117//! > chains built with Substrate only use WASM execution.
118//!
119//! ### Runtime Versions
120//!
121//! An important detail of the native execution worth learning about is that the node software,
122//! obviously, only uses the native runtime if it is the same code as with the wasm blob stored
123//! onchain. Else, nodes who run the native runtime will come to a different state transition. How
124//! do nodes determine if two runtimes are the same? Through the very important
125//! [`sp_version::RuntimeVersion`]. All runtimes expose their version via a runtime api
126//! ([`sp_api::Core::version`]) that returns this struct. The node software, or other applications,
127//! inspect this struct to examine the identity of a runtime, and to determine if two runtimes are
128//! the same. Namely, [`sp_version::RuntimeVersion::spec_version`] is the main key that implies two
129//! runtimes are the same.
130//!
131//! Therefore, it is utmost important to make sure before any runtime upgrade, the spec version is
132//! updated.
133//!
134//! ## Example: Block Execution.
135//!
136//! As a final example to recap, let's look at how Substrate-based nodes execute blocks. Blocks are
137//! received in the node side software as opaque blobs and in the networking layer.
138//!
139//! At some point, based on the consensus algorithm's rules, the node decides to import (aka.
140//! *validate*) a block.
141//!
142//! * First, the node will fetch the state of the parent hash of the block that wishes to be
143//! imported.
144//! * The runtime is fetched from this state, and placed into a WASM execution environment.
145//! * The [`sp_api::Core::execute_block`] runtime API is called and the block is passed in as an
146//! argument.
147//! * The runtime will then execute the block, and update the state accordingly. Any state update is
148//! issued via the [`sp_io::storage`] host functions.
149//! * Both the runtime and node will check the state-root of the state after the block execution to
150//! match the one claimed in the block header.
151//!
152//! > Example taken from [this
153//! > lecture](https://www.youtube.com/watch?v=v0cKuddbF_Q&list=PL-w_i5kwVqbkRmfDn5nzeuU1S_FFW8dDg&index=4)
154//! > of the Polkadot Blockchain Academy.