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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use crate::{
nibble::{self, nibble_ops, NibbleSlice},
node_codec::NodeCodec,
DBValue,
};
use hash_db::Hasher;
use crate::rstd::{borrow::Borrow, ops::Range};
pub type NodeKey = (usize, nibble::BackingByteVec);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeHandle<'a> {
Hash(&'a [u8]),
Inline(&'a [u8]),
}
pub fn decode_hash<H: Hasher>(data: &[u8]) -> Option<H::Out> {
if data.len() != H::LENGTH {
return None
}
let mut hash = H::Out::default();
hash.as_mut().copy_from_slice(data);
Some(hash)
}
#[derive(Eq, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum Value<'a> {
Inline(&'a [u8]),
Node(&'a [u8], Option<DBValue>),
}
impl<'a> Value<'a> {
pub(crate) fn new_inline(value: &'a [u8], threshold: Option<u32>) -> Option<Self> {
if let Some(threshold) = threshold {
if value.len() >= threshold as usize {
return None
} else {
Some(Value::Inline(value))
}
} else {
Some(Value::Inline(value))
}
}
}
#[derive(Eq, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum Node<'a> {
Empty,
Leaf(NibbleSlice<'a>, Value<'a>),
Extension(NibbleSlice<'a>, NodeHandle<'a>),
Branch([Option<NodeHandle<'a>>; nibble_ops::NIBBLE_LENGTH], Option<Value<'a>>),
NibbledBranch(
NibbleSlice<'a>,
[Option<NodeHandle<'a>>; nibble_ops::NIBBLE_LENGTH],
Option<Value<'a>>,
),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodeHandlePlan {
Hash(Range<usize>),
Inline(Range<usize>),
}
impl NodeHandlePlan {
pub fn build<'a, 'b>(&'a self, data: &'b [u8]) -> NodeHandle<'b> {
match self {
NodeHandlePlan::Hash(range) => NodeHandle::Hash(&data[range.clone()]),
NodeHandlePlan::Inline(range) => NodeHandle::Inline(&data[range.clone()]),
}
}
}
#[derive(Eq, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct NibbleSlicePlan {
bytes: Range<usize>,
offset: usize,
}
impl NibbleSlicePlan {
pub fn new(bytes: Range<usize>, offset: usize) -> Self {
NibbleSlicePlan { bytes, offset }
}
pub fn len(&self) -> usize {
(self.bytes.end - self.bytes.start) * nibble_ops::NIBBLE_PER_BYTE - self.offset
}
pub fn build<'a, 'b>(&'a self, data: &'b [u8]) -> NibbleSlice<'b> {
NibbleSlice::new_offset(&data[self.bytes.clone()], self.offset)
}
}
#[derive(Eq, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum ValuePlan {
Inline(Range<usize>),
Node(Range<usize>),
}
impl ValuePlan {
pub fn build<'a, 'b>(&'a self, data: &'b [u8]) -> Value<'b> {
match self {
ValuePlan::Inline(range) => Value::Inline(&data[range.clone()]),
ValuePlan::Node(range) => Value::Node(&data[range.clone()], None),
}
}
}
#[derive(Eq, PartialEq, Clone)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum NodePlan {
Empty,
Leaf { partial: NibbleSlicePlan, value: ValuePlan },
Extension { partial: NibbleSlicePlan, child: NodeHandlePlan },
Branch {
value: Option<ValuePlan>,
children: [Option<NodeHandlePlan>; nibble_ops::NIBBLE_LENGTH],
},
NibbledBranch {
partial: NibbleSlicePlan,
value: Option<ValuePlan>,
children: [Option<NodeHandlePlan>; nibble_ops::NIBBLE_LENGTH],
},
}
impl NodePlan {
pub fn build<'a, 'b>(&'a self, data: &'b [u8]) -> Node<'b> {
match self {
NodePlan::Empty => Node::Empty,
NodePlan::Leaf { partial, value } => Node::Leaf(partial.build(data), value.build(data)),
NodePlan::Extension { partial, child } =>
Node::Extension(partial.build(data), child.build(data)),
NodePlan::Branch { value, children } => {
let mut child_slices = [None; nibble_ops::NIBBLE_LENGTH];
for i in 0..nibble_ops::NIBBLE_LENGTH {
child_slices[i] = children[i].as_ref().map(|child| child.build(data));
}
Node::Branch(child_slices, value.as_ref().map(|v| v.build(data)))
},
NodePlan::NibbledBranch { partial, value, children } => {
let mut child_slices = [None; nibble_ops::NIBBLE_LENGTH];
for i in 0..nibble_ops::NIBBLE_LENGTH {
child_slices[i] = children[i].as_ref().map(|child| child.build(data));
}
Node::NibbledBranch(
partial.build(data),
child_slices,
value.as_ref().map(|v| v.build(data)),
)
},
}
}
pub fn value_plan(&self) -> Option<&ValuePlan> {
match self {
NodePlan::Extension { .. } | NodePlan::Empty => None,
NodePlan::Leaf { value, .. } => Some(value),
NodePlan::Branch { value, .. } | NodePlan::NibbledBranch { value, .. } =>
value.as_ref(),
}
}
pub fn value_plan_mut(&mut self) -> Option<&mut ValuePlan> {
match self {
NodePlan::Extension { .. } | NodePlan::Empty => None,
NodePlan::Leaf { value, .. } => Some(value),
NodePlan::Branch { value, .. } | NodePlan::NibbledBranch { value, .. } =>
value.as_mut(),
}
}
}
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(PartialEq, Eq)]
pub struct OwnedNode<D: Borrow<[u8]>> {
data: D,
plan: NodePlan,
}
impl<D: Borrow<[u8]>> OwnedNode<D> {
pub fn new<C: NodeCodec>(data: D) -> Result<Self, C::Error> {
let plan = C::decode_plan(data.borrow())?;
Ok(OwnedNode { data, plan })
}
pub fn data(&self) -> &[u8] {
self.data.borrow()
}
pub fn node_plan(&self) -> &NodePlan {
&self.plan
}
pub fn node_plan_mut(&mut self) -> &mut NodePlan {
&mut self.plan
}
pub fn node(&self) -> Node {
self.plan.build(self.data.borrow())
}
}