AgentSkillsCN

compact-core:ledger-adts

当您使用Compact进行链上状态管理,借助Cell、Counter、Map、Set、List、MerkleTree,或HistoricMerkleTree等链下数据结构,或需要了解Compact与TypeScript在可用操作上的差异时,可选用此功能。

SKILL.md
--- frontmatter
name: compact-core:ledger-adts
description: Use when working with Compact on-chain state management using ledger ADTs including Cell, Counter, Map, Set, List, MerkleTree, or HistoricMerkleTree, or when needing to understand which operations are available in Compact vs TypeScript.

Ledger ADTs

Reference for Midnight's ledger abstract data types for on-chain state management.

Available ADTs

ADTPurposeKey Operations
Cell<T>Single mutable valueread, write
CounterIncrement-only counterincrement, value
Map<K, V>Key-value storagelookup, insert, remove
Set<T>Membership collectionmember, insert, remove
List<T>Ordered collectionappend, nth, length
MerkleTree<T>Membership proofsinsert, root, pathForLeaf
HistoricMerkleTree<T>Historical rootsSame + resetHistory

Quick Examples

Counter

compact
ledger counter: Counter;

export circuit increment(): Uint<64> {
    counter.increment(1);
    return counter.value();
}

Map

compact
ledger balances: Map<Bytes<32>, Uint<64>>;

export circuit get_balance(user: Bytes<32>): Uint<64> {
    const result = balances.lookup(user);
    return if result is Maybe::Some(balance) { balance } else { 0 };
}

MerkleTree

compact
ledger members: MerkleTree<Bytes<32>>;

export circuit prove_membership(
    leaf: Bytes<32>,
    path: Vector<Bytes<32>, 20>
): Boolean {
    const computed_root = merkleTreePathRoot(leaf, path);
    return computed_root == members.root();
}

Compact vs TypeScript Operations

Some ADT operations are only available in TypeScript:

ADTCompactTypeScript Only
Countervalue, increment-
Maplookup, insert, removeentries, keys
Setmember, insert, removeentries, size
Listnth, appendentries, length
MerkleTreeinsert, rootpathForLeaf, iteration

References