midnight-storage
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseStorage
存储
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.
| Property | Benefit |
|---|---|
| Merkle structure | Tamper-evident state root per block |
| Inclusion proofs | Efficient verification of contract state, balances, etc. |
| Incremental updates | Only changed paths recomputed per block |
Canonical ledger state from runtime pallets (including ) is organized in this trie and persisted via ParityDB.
pallet-midnightmermaid
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(包括)的规范账本状态通过该树结构组织,并通过ParityDB持久化。
pallet-midnightmermaid
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 --> Leaf2twoxhash (storage keys)
twoxhash(存储键)
twoxhash generates storage keys within the trie.
| Aspect | Detail |
|---|---|
| Type | Non-cryptographic hash |
| Purpose | Fast internal key-value lookups |
| Properties | Speed, low collision rate |
| Not for | Security-sensitive hashing (use Blake2-256) |
twoxhash significantly improves trie performance for map lookups without the cost of cryptographic hashing on every key derivation.
See for the full hash/signature split.
midnight-cryptography/twoxhash 在树结构内生成存储键。
| 方面 | 细节 |
|---|---|
| 类型 | 非加密哈希 |
| 用途 | 快速内部键值查找 |
| 特性 | 速度快、碰撞率低 |
| 不适用场景 | 对安全敏感的哈希运算(请使用Blake2-256) |
twoxhash在无需为每个键派生付出加密哈希成本的前提下,显著提升了树结构的映射查找性能。
查看了解完整的哈希/签名拆分实现。
midnight-cryptography/State commit flow
状态提交流程
Every block:
- Runtime executes transactions and updates pallet storage.
- Changed trie nodes are computed; state root is derived.
- Midnight Ledger commitment is persisted alongside standard Substrate state ().
pallet-midnight - 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每个区块执行以下步骤:
- 运行时执行交易并更新pallet存储。
- 计算变更后的树节点;推导状态根。
- Midnight账本承诺与标准Substrate状态()一同持久化。
pallet-midnight - 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 nodesQuerying storage
存储查询
| Access path | Use case |
|---|---|
| Raw storage key reads |
| Contract-specific state |
| Indexer GraphQL | Application-friendly contract/event queries |
| 访问路径 | 使用场景 |
|---|---|
| 原始存储键读取 |
| 合约专属状态查询 |
| 索引器GraphQL | 面向应用的合约/事件查询 |
Related skills
相关技能
- — what gets written to storage
midnight-onchain-logic/ - — Blake2-256 vs twoxhash
midnight-cryptography/ - — reading state via JSON-RPC
midnight-rpc/ - — how txs trigger storage updates
midnight-transactions/
- — 写入存储的内容说明
midnight-onchain-logic/ - — Blake2-256与twoxhash对比
midnight-cryptography/ - — 通过JSON-RPC读取状态
midnight-rpc/ - — 交易如何触发存储更新
midnight-transactions/