1use super::{DBValue, HashMap, Hasher, Node, NodeHash, TreeError};
2
3type Proof<H> = (Option<DBValue>, <H as Hasher>::Out, Vec<DBValue>);
7
8pub trait KeyedTree<H: Hasher, const D: usize> {
10 fn root(&self) -> &H::Out;
12
13 fn depth(&self) -> usize {
15 D * 8
16 }
17
18 fn value(&self, key: &[u8]) -> Result<Option<DBValue>, TreeError>;
20
21 fn leaf(&self, key: &[u8]) -> Result<Option<H::Out>, TreeError>;
23
24 fn proof(&self, key: &[u8]) -> Result<Proof<H>, TreeError>;
26
27 fn verify(
29 key: &[u8],
30 value: &[u8],
31 proof: &[DBValue],
32 root: &H::Out,
33 ) -> Result<bool, TreeError>;
34}
35
36pub trait KeyedTreeMut<H: Hasher, const D: usize> {
38 fn root(&mut self) -> &H::Out;
40
41 fn depth(&self) -> usize {
43 D * 8
44 }
45
46 fn value(&self, key: &[u8]) -> Result<Option<DBValue>, TreeError>;
48
49 fn leaf(&self, key: &[u8]) -> Result<Option<H::Out>, TreeError>;
51
52 fn proof(&self, key: &[u8]) -> Result<Proof<H>, TreeError>;
54
55 fn insert(&mut self, key: &[u8], value: DBValue) -> Result<Option<DBValue>, TreeError>;
57
58 fn remove(&mut self, key: &[u8]) -> Result<Option<DBValue>, TreeError>;
60
61 fn verify(
63 key: &[u8],
64 value: &[u8],
65 proof: &[DBValue],
66 root: &H::Out,
67 ) -> Result<bool, TreeError>;
68}
69
70pub trait IndexTree<H: Hasher, const D: usize> {
72 fn root(&self) -> &H::Out;
74
75 fn depth(&self) -> usize {
77 D * 8
78 }
79
80 fn value(&self, index: &u64) -> Result<Option<DBValue>, TreeError>;
82
83 fn leaf(&self, index: &u64) -> Result<Option<H::Out>, TreeError>;
85
86 fn proof(&self, index: &u64) -> Result<Proof<H>, TreeError>;
88
89 fn verify(
91 index: &u64,
92 value: &[u8],
93 proof: &[DBValue],
94 root: &H::Out,
95 ) -> Result<bool, TreeError>;
96}
97
98pub trait IndexTreeMut<H: Hasher, const D: usize> {
100 fn root(&mut self) -> &H::Out;
102
103 fn depth(&self) -> usize {
105 D * 8
106 }
107
108 fn value(&self, index: &u64) -> Result<Option<DBValue>, TreeError>;
110
111 fn leaf(&self, index: &u64) -> Result<Option<H::Out>, TreeError>;
113
114 fn proof(&self, index: &u64) -> Result<Proof<H>, TreeError>;
116
117 fn insert(&mut self, index: &u64, value: DBValue) -> Result<Option<DBValue>, TreeError>;
119
120 fn remove(&mut self, index: &u64) -> Result<Option<DBValue>, TreeError>;
122
123 fn verify(
125 index: &u64,
126 value: &[u8],
127 proof: &[DBValue],
128 root: &H::Out,
129 ) -> Result<bool, TreeError>;
130}
131
132pub trait TreeRecorder<H: Hasher> {
134 fn record(&mut self, node: &Node<H>);
135}
136
137pub fn null_nodes<H: Hasher>(depth: usize) -> (HashMap<H::Out, Node<H>>, H::Out) {
142 let mut hashes = HashMap::with_capacity(depth);
143 let mut current_hash = H::hash(&[]);
144
145 hashes.insert(
146 current_hash,
147 Node::Value {
148 hash: current_hash,
149 value: vec![],
150 },
151 );
152
153 for _ in 0..depth {
154 let next_hash = H::hash(&[current_hash.as_ref(), current_hash.as_ref()].concat());
155 hashes.insert(
156 next_hash,
157 Node::Inner {
158 hash: next_hash,
159 left: NodeHash::Default(current_hash),
160 right: NodeHash::Default(current_hash),
161 },
162 );
163 current_hash = next_hash;
164 }
165
166 (hashes, current_hash)
167}