Skip to main content

zcash_history/
lib.rs

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