Skip to main content

encode_compact_skip_duplicates

Function encode_compact_skip_duplicates 

Source
pub fn encode_compact_skip_duplicates<L>(
    db: &TrieDB<'_, '_, L>,
    seen: &mut SeenHashes<L>,
) -> Result<Vec<Vec<u8>>, TrieHash<L>, CError<L>>
where L: TrieLayout,
Expand description

Variant of encode_compact that emits each distinct item — trie node or detached value node — only once. Later occurrences keep a plain hash reference to the emitted item, exactly like references to items outside the partial trie. Only items at least as large as a hash are referenced this way, so deduplication never grows the encoding.

seen collects the emitted hashes, keeping trie-node and detached-value hashes in disjoint namespaces (see SeenHashes).

All encodings sharing one seen set must be generated from a single, fixed backing node set: a skipped subtree is reconstructable only if everything below it was emitted when its root was first seen. Encoding from per-proof recorded sets whose coverage of a shared node diverges silently drops the divergent nodes and produces unverifiable proofs.

A deduplicated occurrence is indistinguishable from a reference to an item outside the partial trie, so any decoder reconstructs a readable hash-keyed node set: every item is present under its hash from its first occurrence. Per-position bookkeeping is deliberately not reconstructable: decoding into a position-keyed (prefixed) database, or relying on the reconstruction’s reference counts, is unsupported.

Assumes occurrences of an item are interchangeable, as they are when db is hash-keyed.