pub struct AppendOnlyTree<L> { /* private fields */ }Expand description
Append-only Merkle transparency log over any L: Serialize leaf.
Each Self::append serializes the leaf with postcard and pushes the
resulting bytes into a ct_merkle::MemoryBackedTree<Sha256, _>. The
original L is not stored; the tree retains the serialized bytes plus
the internal Merkle nodes.
The empty tree’s root is RFC 6962’s deterministic zero-leaf root —
SHA-256("") paired with num_leaves = 0. After n appends, Self::root
returns the SHA-256 Merkle Tree Hash of the n leaves with num_leaves = n. Two trees built from the same Serialize sequence in the same order
produce the same root.
§The leaf encoding is a FROZEN WIRE CONTRACT
Leaves are hashed via their postcard encoding, which is derived
entirely from L’s shape: fields, field declaration order, and enum
variant declaration order. Once leaves of L exist in a persisted or
published tree, that shape must never change — reordering or adding
fields, or reordering enum variants, silently alters the bytes of
historical leaves, so every previously published root and proof stops
verifying with no error and no version signal. Schema evolution
requires a new versioned leaf type or an explicitly versioned envelope.
When long-lived verifiability matters, prefer a leaf type that pins the
payload as pre-encoded bytes (e.g. a struct holding Vec<u8>) so the
hashed bytes are under explicit consumer control. See the
leaf module docs for the full contract.
Implementations§
Source§impl<L> AppendOnlyTree<L>
impl<L> AppendOnlyTree<L>
Sourcepub fn new() -> Self
pub fn new() -> Self
Builds a new empty tree.
The empty tree’s Self::root is the deterministic zero-leaf root
(SHA-256("") with num_leaves = 0) defined by RFC 6962 and
implemented by ct-merkle.
Sourcepub fn root(&self) -> RootHash
pub fn root(&self) -> RootHash
Returns the current root hash, capturing both the SHA-256 Merkle Tree Hash and the current leaf count.
Sourcepub fn prove_inclusion(&self, leaf_index: u64) -> TrustResult<InclusionProof>
pub fn prove_inclusion(&self, leaf_index: u64) -> TrustResult<InclusionProof>
Builds an inclusion proof for the leaf at leaf_index.
§Errors
Returns TrustError::MerkleTree if leaf_index is greater than or
equal to the current tree length, or if the index does not fit in a
usize on this target. Both conditions are pre-checked so the
underlying ct-merkle call (which would otherwise panic) is never made
with an invalid argument.
Sourcepub fn prove_consistency(
&self,
old_size: u64,
new_size: u64,
) -> TrustResult<ConsistencyProof>
pub fn prove_consistency( &self, old_size: u64, new_size: u64, ) -> TrustResult<ConsistencyProof>
Builds a consistency proof showing that the tree of size old_size is
a prefix of the current tree (which must be exactly new_size leaves).
§Errors
Returns TrustError::MerkleTree if new_size does not equal the
current tree length, old_size is zero, old_size exceeds new_size,
or the resulting num_additions does not fit in a usize. These
checks ensure the underlying ct-merkle call (which would otherwise
panic when num_additions >= self.len()) is never made with an
invalid argument.
Source§impl AppendOnlyTree<RawLeaf>
impl AppendOnlyTree<RawLeaf>
Sourcepub fn append_raw(&mut self, leaf_bytes: &[u8]) -> u64
pub fn append_raw(&mut self, leaf_bytes: &[u8]) -> u64
Appends raw bytes verbatim and returns the new tree size.
The bytes are hashed exactly as supplied — no postcard, no length
prefix — so the RFC 6962 leaf hash is SHA-256(0x00 ‖ leaf_bytes)
(see raw_leaf_hash). Infallible:
there is no serialization step to fail.
Sourcepub fn reconstruct_from_raw_leaves<I>(leaves: I) -> Self
pub fn reconstruct_from_raw_leaves<I>(leaves: I) -> Self
Rebuilds a raw-leaf tree from leaves in their original append order.
The reconstruction path for restart and prefix rebuilds, mirroring
AppendOnlyTree::reconstruct_from_leaves for the raw encoding.
Because it reuses Self::append_raw, the rebuilt tree reproduces
the original root bit-for-bit.
Source§impl<L: Serialize> AppendOnlyTree<L>
impl<L: Serialize> AppendOnlyTree<L>
Sourcepub fn append(&mut self, leaf: L) -> TrustResult<u64>
pub fn append(&mut self, leaf: L) -> TrustResult<u64>
Appends leaf to the tree and returns the new tree size.
The leaf is serialized via postcard before being hashed into the
tree. The first append returns 1, the next 2, and so on.
§Errors
Returns TrustError::MerkleTree if the leaf cannot be serialized.
No other failure modes exist at this scale of tree (ct-merkle’s
internal limits are well beyond practical use).
Sourcepub fn reconstruct_from_leaves(leaves: Vec<L>) -> TrustResult<Self>
pub fn reconstruct_from_leaves(leaves: Vec<L>) -> TrustResult<Self>
Rebuilds a tree from a sequence of leaves in their original append order.
This is the mechanism for reconstructing in-memory tree state from
persistent storage on restart. Because reconstruction reuses
Self::append, it goes through the same serialization path and
therefore reproduces the original root hash bit-for-bit.
§Errors
Returns TrustError::MerkleTree if any leaf fails to serialize.