midnight-storage

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Storage

存储

Midnight is built on the Polkadot SDK (Substrate) and uses ParityDB as its default database backend.
Midnight 基于Polkadot SDK (Substrate)构建,默认使用ParityDB作为数据库后端。

Storage stack

存储栈

mermaid
flowchart TB
  subgraph Runtime["Runtime execution"]
    STF["State transition function (WASM)"]
    PM["pallet-midnight + other pallets"]
  end

  subgraph Trie["State layer"]
    MPT["Patricia-Merkle trie"]
    T2H["twoxhash — storage key generation"]
    MPT --- T2H
  end

  subgraph Persist["Persistence"]
    PDB["ParityDB — key-value store"]
  end

  STF --> PM
  PM --> MPT
  MPT --> PDB
  MPT --> Commit["State root commitment per block"]

mermaid
flowchart TB
  subgraph Runtime["Runtime execution"]
    STF["State transition function (WASM)"]
    PM["pallet-midnight + other pallets"]
  end

  subgraph Trie["State layer"]
    MPT["Patricia-Merkle trie"]
    T2H["twoxhash — storage key generation"]
    MPT --- T2H
  end

  subgraph Persist["Persistence"]
    PDB["ParityDB — key-value store"]
  end

  STF --> PM
  PM --> MPT
  MPT --> PDB
  MPT --> Commit["State root commitment per block"]

ParityDB

ParityDB

ParityDB is a fast key-value store designed for blockchain workloads.
  • Stores all on-chain state.
  • Default backend for Substrate/Polkadot SDK chains including Midnight.
  • Optimized for high write throughput during block import and state commits.

ParityDB 是专为区块链工作负载设计的高性能键值存储。
  • 存储所有链上状态
  • 是Substrate/Polkadot SDK链(包括Midnight)的默认后端。
  • 针对区块导入和状态提交期间的高写入吞吐量进行了优化。

Patricia-Merkle trie

Patricia-Merkle树

The trie is the underlying data structure for state commitments.
PropertyBenefit
Merkle structureTamper-evident state root per block
Inclusion proofsEfficient verification of contract state, balances, etc.
Incremental updatesOnly changed paths recomputed per block
Canonical ledger state from runtime pallets (including
pallet-midnight
) is organized in this trie and persisted via ParityDB.
mermaid
flowchart LR
  BlockN["Block N state root"] --> Root["Trie root hash"]
  Root --> Leaf1["Storage item A"]
  Root --> Leaf2["Storage item B"]
  Root --> Leaf3["Contract state …"]
  Query["RPC / light client"] --> Proof["Merkle inclusion proof"]
  Proof --> Leaf2

该树结构是状态承诺的底层数据结构。
属性优势
Merkle结构每个区块的状态根具备防篡改特性
包含证明高效验证合约状态、余额等信息
增量更新每个区块仅重新计算变更路径
来自运行时pallet(包括
pallet-midnight
)的规范账本状态通过该树结构组织,并通过ParityDB持久化。
mermaid
flowchart LR
  BlockN["Block N state root"] --> Root["Trie root hash"]
  Root --> Leaf1["Storage item A"]
  Root --> Leaf2["Storage item B"]
  Root --> Leaf3["Contract state …"]
  Query["RPC / light client"] --> Proof["Merkle inclusion proof"]
  Proof --> Leaf2

twoxhash (storage keys)

twoxhash(存储键)

twoxhash generates storage keys within the trie.
AspectDetail
TypeNon-cryptographic hash
PurposeFast internal key-value lookups
PropertiesSpeed, low collision rate
Not forSecurity-sensitive hashing (use Blake2-256)
twoxhash significantly improves trie performance for map lookups without the cost of cryptographic hashing on every key derivation.
See
midnight-cryptography/
for the full hash/signature split.

twoxhash 在树结构内生成存储键
方面细节
类型非加密哈希
用途快速内部键值查找
特性速度快、碰撞率低
不适用场景对安全敏感的哈希运算(请使用Blake2-256)
twoxhash在无需为每个键派生付出加密哈希成本的前提下,显著提升了树结构的映射查找性能。
查看
midnight-cryptography/
了解完整的哈希/签名拆分实现。

State commit flow

状态提交流程

Every block:
  1. Runtime executes transactions and updates pallet storage.
  2. Changed trie nodes are computed; state root is derived.
  3. Midnight Ledger commitment is persisted alongside standard Substrate state (
    pallet-midnight
    ).
  4. ParityDB persists the updated trie backing store.
mermaid
sequenceDiagram
  participant RT as Runtime
  participant Trie as Patricia-Merkle trie
  participant PM as pallet-midnight
  participant DB as ParityDB

  RT->>Trie: Apply storage writes (twoxhash keys)
  RT->>PM: Commit ledger state
  Trie->>Trie: Compute new state root
  PM->>DB: Persist ledger commitment
  Trie->>DB: Persist trie nodes

每个区块执行以下步骤:
  1. 运行时执行交易并更新pallet存储。
  2. 计算变更后的树节点;推导状态根
  3. Midnight账本承诺与标准Substrate状态(
    pallet-midnight
    )一同持久化。
  4. ParityDB持久化更新后的树结构底层存储。
mermaid
sequenceDiagram
  participant RT as Runtime
  participant Trie as Patricia-Merkle trie
  participant PM as pallet-midnight
  participant DB as ParityDB

  RT->>Trie: Apply storage writes (twoxhash keys)
  RT->>PM: Commit ledger state
  Trie->>Trie: Compute new state root
  PM->>DB: Persist ledger commitment
  Trie->>DB: Persist trie nodes

Querying storage

存储查询

Access pathUse case
state_getStorage
(RPC)
Raw storage key reads
midnight_contractState
(RPC)
Contract-specific state
Indexer GraphQLApplication-friendly contract/event queries

访问路径使用场景
state_getStorage
(RPC)
原始存储键读取
midnight_contractState
(RPC)
合约专属状态查询
索引器GraphQL面向应用的合约/事件查询

Related skills

相关技能

  • midnight-onchain-logic/
    — what gets written to storage
  • midnight-cryptography/
    — Blake2-256 vs twoxhash
  • midnight-rpc/
    — reading state via JSON-RPC
  • midnight-transactions/
    — how txs trigger storage updates
  • midnight-onchain-logic/
    — 写入存储的内容说明
  • midnight-cryptography/
    — Blake2-256与twoxhash对比
  • midnight-rpc/
    — 通过JSON-RPC读取状态
  • midnight-transactions/
    — 交易如何触发存储更新