From 7d8e3a434ea1e760190456e8df1359aa8137e16a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bastian=20K=C3=B6cher?= <git@kchr.de>
Date: Mon, 13 Jan 2025 13:32:01 +0100
Subject: [PATCH] reference-docs: Start `state` and mention well known keys
 (#7037)

Closes: https://github.com/paritytech/polkadot-sdk/issues/7033
---
 Cargo.lock                              |  1 +
 docs/sdk/Cargo.toml                     |  1 +
 docs/sdk/src/reference_docs/mod.rs      |  3 +++
 docs/sdk/src/reference_docs/state.rs    | 12 ++++++++++++
 substrate/primitives/storage/src/lib.rs |  4 ++++
 5 files changed, 21 insertions(+)
 create mode 100644 docs/sdk/src/reference_docs/state.rs

diff --git a/Cargo.lock b/Cargo.lock
index 4e2272bdc98..cfb805fbe84 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -19202,6 +19202,7 @@ dependencies = [
  "sp-runtime 31.0.1",
  "sp-runtime-interface 24.0.0",
  "sp-std 14.0.0",
+ "sp-storage 19.0.0",
  "sp-tracing 16.0.0",
  "sp-version 29.0.0",
  "sp-weights 27.0.0",
diff --git a/docs/sdk/Cargo.toml b/docs/sdk/Cargo.toml
index f526c07796e..4d83e2045ab 100644
--- a/docs/sdk/Cargo.toml
+++ b/docs/sdk/Cargo.toml
@@ -110,6 +110,7 @@ sp-offchain = { workspace = true, default-features = true }
 sp-runtime = { workspace = true, default-features = true }
 sp-runtime-interface = { workspace = true, default-features = true }
 sp-std = { workspace = true, default-features = true }
+sp-storage = { workspace = true, default-features = true }
 sp-tracing = { workspace = true, default-features = true }
 sp-version = { workspace = true, default-features = true }
 sp-weights = { workspace = true, default-features = true }
diff --git a/docs/sdk/src/reference_docs/mod.rs b/docs/sdk/src/reference_docs/mod.rs
index e47eece784c..7ad8a37241b 100644
--- a/docs/sdk/src/reference_docs/mod.rs
+++ b/docs/sdk/src/reference_docs/mod.rs
@@ -111,3 +111,6 @@ pub mod custom_runtime_api_rpc;
 
 /// The [`polkadot-omni-node`](https://crates.io/crates/polkadot-omni-node) and its related binaries.
 pub mod omni_node;
+
+/// Learn about the state in Substrate.
+pub mod state;
diff --git a/docs/sdk/src/reference_docs/state.rs b/docs/sdk/src/reference_docs/state.rs
new file mode 100644
index 00000000000..a8138caebf1
--- /dev/null
+++ b/docs/sdk/src/reference_docs/state.rs
@@ -0,0 +1,12 @@
+//! # State
+//!
+//! The state is abstracted as a key-value like database. Every item that
+//! needs to be persisted by the [State Transition
+//! Function](crate::reference_docs::blockchain_state_machines) is written to the state.
+//!
+//! ## Special keys
+//!
+//! The key-value pairs in the state are represented as byte sequences. The node
+//! doesn't know how to interpret most the key-value pairs. However, there exist some
+//! special keys and its values that are known to the node, the so-called
+//! [`well-known-keys`](sp_storage::well_known_keys).
diff --git a/substrate/primitives/storage/src/lib.rs b/substrate/primitives/storage/src/lib.rs
index 4b25f85fba6..df7570a1854 100644
--- a/substrate/primitives/storage/src/lib.rs
+++ b/substrate/primitives/storage/src/lib.rs
@@ -191,11 +191,15 @@ pub mod well_known_keys {
 	/// Wasm code of the runtime.
 	///
 	/// Stored as a raw byte vector. Required by substrate.
+	///
+	/// Encodes to `0x3A636F6465`.
 	pub const CODE: &[u8] = b":code";
 
 	/// Number of wasm linear memory pages required for execution of the runtime.
 	///
 	/// The type of this value is encoded `u64`.
+	///
+	/// Encodes to `0x307833413633364636343635`
 	pub const HEAP_PAGES: &[u8] = b":heappages";
 
 	/// Current extrinsic index (u32) is stored under this key.
-- 
GitLab