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
14pub use entry::{Entry, MAX_ENTRY_SIZE};
15pub use node_data::{NodeData, MAX_NODE_DATA_SIZE};
16pub use tree::Tree;
17pub use version::{Version, V1, V2};
18
19/// Crate-level error type
20#[derive(Debug)]
21pub enum Error {
22    /// Entry expected to be presented in the tree view while it was not.
23    ExpectedInMemory(EntryLink),
24    /// Entry expected to be a node (specifying for which link this is not true).
25    ExpectedNode(Option<EntryLink>),
26}
27
28impl std::fmt::Display for Error {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        match *self {
31            Self::ExpectedInMemory(l) => write!(f, "Node/leaf expected to be in memory: {}", l),
32            Self::ExpectedNode(None) => write!(f, "Node expected"),
33            Self::ExpectedNode(Some(l)) => write!(f, "Node expected, not leaf: {}", l),
34        }
35    }
36}
37
38/// Reference to the tree node.
39#[repr(C)]
40#[derive(Clone, Copy, Debug)]
41pub enum EntryLink {
42    /// Reference to the stored (in the array representation) leaf/node.
43    Stored(u32),
44    /// Reference to the generated leaf/node.
45    Generated(u32),
46}
47
48impl std::fmt::Display for EntryLink {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        match *self {
51            Self::Stored(v) => write!(f, "stored({})", v),
52            Self::Generated(v) => write!(f, "generated({})", v),
53        }
54    }
55}
56
57/// MMR Node. It is leaf when `left`, `right` are `None` and node when they are not.
58#[repr(C)]
59#[derive(Debug)]
60pub enum EntryKind {
61    /// Leaf entry.
62    Leaf,
63    /// Node entry with children links.
64    Node(EntryLink, EntryLink),
65}
66
67impl Error {
68    /// Entry expected to be a node (specifying for which link this is not true).
69    pub fn link_node_expected(link: EntryLink) -> Self {
70        Self::ExpectedNode(Some(link))
71    }
72
73    /// Some entry is expected to be node
74    pub fn node_expected() -> Self {
75        Self::ExpectedNode(None)
76    }
77
78    pub(crate) fn augment(self, link: EntryLink) -> Self {
79        match self {
80            Error::ExpectedNode(_) => Error::ExpectedNode(Some(link)),
81            val => val,
82        }
83    }
84}