merkle_log/
util.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::{maybestd::*, Error, Node, TreeID};

/// Represents access to immutable merkle tree nodes.
pub trait Store<N: Node> {
    /// Gets an intermediate [`Node`] by its [`TreeID`].
    ///
    /// [`Node`]: crate::Node
    /// [`TreeID`]: crate::TreeID
    fn get_node(&self, id: &TreeID) -> Result<N, Error>;

    /// Stores an intermediate [`Node`] by its [`TreeID`].
    ///
    /// [`Node`]: crate::Node
    /// [`TreeID`]: crate::TreeID
    fn set_node(&mut self, id: TreeID, node: N) -> Result<(), Error>;

    /// Gets a leaf [`Node`] by its [`TreeID`].
    ///
    /// [`Node`]: crate::Node
    /// [`TreeID`]: crate::TreeID
    fn get_leaf(&self, id: &TreeID) -> Result<N, Error>;

    /// Stores a leaf [`Node`] by its [`TreeID`].
    ///
    /// [`Node`]: crate::Node
    /// [`TreeID`]: crate::TreeID
    fn set_leaf(&mut self, id: TreeID, leaf: N) -> Result<(), Error>;

    /// Gets a [`Node`] by its [`TreeID`].
    ///
    /// [`Node`]: crate::Node
    /// [`TreeID`]: crate::TreeID
    fn get(&self, id: &TreeID) -> Result<N, Error> {
        if id.is_leaf() {
            self.get_leaf(id)
        } else {
            self.get_node(id)
        }
    }

    /// Stores a [`Node`] by its [`TreeID`].
    ///
    /// [`Node`]: crate::Node
    /// [`TreeID`]: crate::TreeID
    fn set(&mut self, id: TreeID, node: N) -> Result<(), Error> {
        if id.is_leaf() {
            self.set_leaf(id, node)
        } else {
            self.set_node(id, node)
        }
    }

    /// Gets many [`Node`]s from an [`Iterator`] of [`TreeID`]s.
    ///
    /// [`Node`]: crate::Node
    /// [`TreeID`]: crate::TreeID
    fn get_many<'a, I: Iterator<Item = TreeID>>(
        &self,
        ids: I,
    ) -> Result<BTreeMap<TreeID, N>, Error> {
        let mut nodes = BTreeMap::new();
        for id in ids {
            nodes.insert(id, self.get(&id)?);
        }
        Ok(nodes)
    }

    /// Sets many [`Node`]s from an [`Iterator`] of [`TreeID`]s and [`Node`]s.
    ///
    /// [`Node`]: crate::Node
    /// [`TreeID`]: crate::TreeID
    fn set_many<I: Iterator<Item = (TreeID, N)>>(&mut self, nodes: I) -> Result<(), Error> {
        for (id, node) in nodes {
            self.set(id, node)?
        }
        Ok(())
    }
}

/// An in-memory store for the intermediate nodes of a [`MerkleLog`].
///
/// [`MerkleLog`]: crate::MerkleLog
pub type MemoryStore<N> = BTreeMap<TreeID, N>;

impl<N: Node> Store<N> for BTreeMap<TreeID, N> {
    /// Delegates to [`BTreeMap::get`].
    ///
    /// [`BTreeMap::get`]: crate::maybestd::BTreeMap::get
    #[inline]
    fn get_node(&self, id: &TreeID) -> Result<N, Error> {
        let node = self.get(id).ok_or(Error::MissingNode(*id))?;
        Ok(*node)
    }

    /// Delegates to [`BTreeMap::insert`].
    ///
    /// [`BTreeMap::insert`]: crate::maybestd::BTreeMap::insert
    #[inline]
    fn set_node(&mut self, id: TreeID, node: N) -> Result<(), Error> {
        self.insert(id, node);
        Ok(())
    }

    /// Delegates to [`BTreeMap::get`].
    ///
    /// [`BTreeMap::get`]: crate::maybestd::BTreeMap::get
    #[inline]
    fn get_leaf(&self, id: &TreeID) -> Result<N, Error> {
        let leaf = self.get(id).ok_or(Error::MissingNode(*id))?;
        Ok(*leaf)
    }

    /// Delegates to [`BTreeMap::insert`].
    ///
    /// [`BTreeMap::insert`]: crate::maybestd::BTreeMap::insert
    #[inline]
    fn set_leaf(&mut self, id: TreeID, leaf: N) -> Result<(), Error> {
        self.insert(id, leaf);
        Ok(())
    }
}

///
pub trait Digest<N: Node>: Default {
    ///
    fn leaf_digest<R: BufRead>(entry: R) -> N;

    ///
    fn node_digest(left: (TreeID, &N), right: (TreeID, &N)) -> N;

    // ///
    // fn root_digest(leaf_id: TreeID, leaf_node: &N, height: u8)
}

#[cfg(feature = "digest")]
mod _digest {
    use super::*;
    use digest::Output;

    impl<D, N> Digest<N> for D
    where
        D: digest::Digest + Default,
        N: Node + From<Output<D>>,
    {
        fn leaf_digest<R: BufRead>(mut entry: R) -> N {
            let mut hasher = Self::default();
            loop {
                let bytes = entry
                    .fill_buf()
                    .expect("should not fail to fill BufReader from an io::Read");
                match bytes.len() {
                    0 => break,
                    len => {
                        hasher.update(bytes);
                        entry.consume(len);
                    }
                }
            }
            hasher.finalize().into()
        }

        fn node_digest(left: (TreeID, &N), right: (TreeID, &N)) -> N {
            let mut hasher = Self::default();
            hasher.update(left.1.as_ref());
            hasher.update(right.1.as_ref());
            hasher.finalize().into()
        }
    }
}

pub(crate) enum Either<I1, I2> {
    Left(I1),
    Right(I2),
}

impl<I1, I2> Iterator for Either<I1, I2>
where
    I1: Iterator<Item = TreeID>,
    I2: Iterator<Item = TreeID>,
{
    type Item = TreeID;
    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Left(i) => i.next(),
            Self::Right(i) => i.next(),
        }
    }
}