Skip to main content

zcash_history/
lib.rs

1//! To be used in Zebra. Originally also consumed by zcashd via FFI bindings,
2//! which reached End-of-Support in July 2026.
3
4#![no_std]
5// Catch documentation errors caused by code changes.
6#![deny(rustdoc::broken_intra_doc_links)]
7#![warn(missing_docs)]
8
9#[macro_use]
10extern crate alloc;
11
12mod entry;
13mod node_data;
14mod tree;
15mod version;
16
17#[cfg(test)]
18mod test_vectors;
19
20pub use entry::{Entry, MAX_ENTRY_SIZE};
21pub use node_data::{MAX_NODE_DATA_SIZE, NodeData, V2 as NodeDataV2, V3 as NodeDataV3};
22pub use tree::Tree;
23pub use version::{V1, V2, V3, Version};
24
25/// Crate-level error type
26#[derive(Debug)]
27pub enum Error {
28    /// Entry expected to be presented in the tree view while it was not.
29    ExpectedInMemory(EntryLink),
30    /// Entry expected to be a node (specifying for which link this is not true).
31    ExpectedNode(Option<EntryLink>),
32}
33
34impl core::fmt::Display for Error {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        match *self {
37            Self::ExpectedInMemory(l) => write!(f, "Node/leaf expected to be in memory: {l}"),
38            Self::ExpectedNode(None) => write!(f, "Node expected"),
39            Self::ExpectedNode(Some(l)) => write!(f, "Node expected, not leaf: {l}"),
40        }
41    }
42}
43
44/// Reference to the tree node.
45#[derive(Clone, Copy, Debug)]
46pub enum EntryLink {
47    /// Reference to the stored (in the array representation) leaf/node.
48    Stored(u32),
49    /// Reference to the generated leaf/node.
50    Generated(u32),
51}
52
53impl core::fmt::Display for EntryLink {
54    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
55        match *self {
56            Self::Stored(v) => write!(f, "stored({v})"),
57            Self::Generated(v) => write!(f, "generated({v})"),
58        }
59    }
60}
61
62/// MMR Node. It is leaf when `left`, `right` are `None` and node when they are not.
63#[derive(Debug)]
64pub enum EntryKind {
65    /// Leaf entry.
66    Leaf,
67    /// Node entry with children links.
68    Node(EntryLink, EntryLink),
69}
70
71impl Error {
72    /// Entry expected to be a node (specifying for which link this is not true).
73    pub fn link_node_expected(link: EntryLink) -> Self {
74        Self::ExpectedNode(Some(link))
75    }
76
77    /// Some entry is expected to be node
78    pub fn node_expected() -> Self {
79        Self::ExpectedNode(None)
80    }
81
82    pub(crate) fn augment(self, link: EntryLink) -> Self {
83        match self {
84            Error::ExpectedNode(_) => Error::ExpectedNode(Some(link)),
85            val => val,
86        }
87    }
88}