agentleFS
Kali-DecoderKali-Decoder/Midnight-Skills/.agents/skills/midnight-storage/SKILL.md

midnight-storage

Skill18 starsChanged 17 days agoMITView on GitHub

Midnight on-chain storage — ParityDB key-value backend, Patricia-Merkle trie state commitments, twoxhash storage keys, and how state transitions are persisted. Use when explaining state proofs, database layout, or trie performance on Midnight nodes.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Default backend for Substrate/Polkadot SDK chains including Midnight.

    line 43

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
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

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

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.