Back to Browse

Use with your AI agent

  1. Install — run npx skills add Kali-Decoder/Midnight-skills --skill midnight-storage for this skill, or npx skills add Kali-Decoder/Midnight-skills for all
  2. Prompt — copy the example prompt and paste it into Cursor, Claude Code, or Codex
  3. Build — the agent loads this skill and scaffolds your Midnight work

Storage

FoundationIntermediateReviewedUnchanged since v1.0.7v1.0.0

Agent picks this when: Explain Midnight on-chain storage — ParityDB, trie, and state roots

ParityDB backend, Patricia-Merkle trie state commitments, and twoxhash storage key generation.

Good for

  • ParityDB key-value store with Patricia-Merkle trie state commitments.
  • twoxhash for fast storage keys; inclusion proofs for on-chain data.

Preview in agent

Suggested prompts

Explain Midnight on-chain storage — ParityDB, trie, and state roots. Use the "midnight-storage" skill from MIDSKILLS. Follow Compact smart-contract best practices, wire wallet flows where needed, and explain each step as you implement. Target Midnight Preprod unless I specify otherwise.

Loaded skill midnight-storage

What the skill guides the agent to do:

  1. ParityDB key-value store with Patricia-Merkle trie state commitments.
  2. twoxhash for fast storage keys; inclusion proofs for on-chain data.

Illustrative — based on the skill's description, not recorded agent output.

Try it without installing: npx skills use Kali-Decoder/Midnight-skills@midnight-storage

What changed in v1.0.16

All registry changes

No changes in v1.0.16 or the releases before it that we track.

On this page · 7 sections
SKILL.md

Storage

Midnight is built on the Polkadot SDK (Substrate) and uses ParityDB as its default database backend.

Storage stack

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 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.

Patricia-Merkle trie

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.

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 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.


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.
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

  • 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

Start in 2 steps

  1. 1. Install — one skill or the full set
  2. 2. Prompt — paste the example into your agent

Install all skills (recommended)

npx skills add Kali-Decoder/Midnight-skills

One command adds every Midnight skill to Cursor, Claude Code, and similar agents.

Install this skill only

npx skills add Kali-Decoder/Midnight-skills --skill midnight-storage

Installs only "midnight-storage" into every detected agent (Cursor, Claude Code, Codex, …).

Example agent prompt

Explain Midnight on-chain storage — ParityDB, trie, and state roots. Use the "midnight-storage" skill from MIDSKILLS. Follow Compact smart-contract best practices, wire wallet flows where needed, and explain each step as you implement. Target Midnight Preprod unless I specify otherwise.

Paste into Cursor, Claude Code, or Codex after installing.

Author
Kali-Decoder
Category
Foundation
Difficulty
intermediate
Version
v1.0.0

Tags

StorageParityDBMerkle trieState

Files included

SKILL.md
Create Social CardLive HackathonsGallery
Mock Demo
Storage