|
17 | 17 | //! `NodeCodec` implementation for Substrate's trie format.
|
18 | 18 |
|
19 | 19 | use rstd::marker::PhantomData;
|
20 |
| -use rstd::ops::Range; |
21 | 20 | use rstd::vec::Vec;
|
22 | 21 | use rstd::borrow::Borrow;
|
23 |
| -use codec::{Encode, Decode, Input, Compact}; |
| 22 | +use codec::{Encode, Decode, Compact}; |
24 | 23 | use hash_db::Hasher;
|
25 |
| -use trie_db::{self, node::{NibbleSlicePlan, NodePlan, NodeHandlePlan}, ChildReference, |
| 24 | +use trie_db::{self, NibbleSlice, node::Node, ChildReference, |
26 | 25 | nibble_ops, Partial, NodeCodec as NodeCodecT};
|
27 | 26 | use crate::error::Error;
|
28 | 27 | use crate::trie_constants;
|
29 | 28 | use super::{node_header::{NodeHeader, NodeKind}};
|
30 | 29 |
|
31 |
| -/// Helper struct for trie node decoder. This implements `codec::Input` on a byte slice, while |
32 |
| -/// tracking the absolute position. This is similar to `std::io::Cursor` but does not implement |
33 |
| -/// `Read` and `io` is not in `rstd`. |
34 |
| -struct ByteSliceInput<'a> { |
35 |
| - data: &'a [u8], |
36 |
| - offset: usize, |
37 |
| -} |
38 |
| - |
39 |
| -impl<'a> ByteSliceInput<'a> { |
40 |
| - fn new(data: &'a [u8]) -> Self { |
41 |
| - ByteSliceInput { |
42 |
| - data, |
43 |
| - offset: 0, |
44 |
| - } |
45 |
| - } |
46 |
| - |
47 |
| - fn take(&mut self, count: usize) -> Result<Range<usize>, codec::Error> { |
48 |
| - if self.offset + count > self.data.len() { |
49 |
| - return Err("out of data".into()); |
50 |
| - } |
51 |
| - |
52 |
| - let range = self.offset..(self.offset + count); |
53 |
| - self.offset += count; |
54 |
| - Ok(range) |
55 |
| - } |
56 |
| -} |
57 |
| - |
58 |
| -impl<'a> Input for ByteSliceInput<'a> { |
59 |
| - fn remaining_len(&mut self) -> Result<Option<usize>, codec::Error> { |
60 |
| - let remaining = if self.offset <= self.data.len() { |
61 |
| - Some(self.data.len() - self.offset) |
62 |
| - } else { |
63 |
| - None |
64 |
| - }; |
65 |
| - Ok(remaining) |
66 |
| - } |
67 |
| - |
68 |
| - fn read(&mut self, into: &mut [u8]) -> Result<(), codec::Error> { |
69 |
| - let range = self.take(into.len())?; |
70 |
| - into.copy_from_slice(&self.data[range]); |
71 |
| - Ok(()) |
72 |
| - } |
73 |
| - |
74 |
| - fn read_byte(&mut self) -> Result<u8, codec::Error> { |
75 |
| - if self.offset + 1 > self.data.len() { |
76 |
| - return Err("out of data".into()); |
77 |
| - } |
78 |
| - |
79 |
| - let byte = self.data[self.offset]; |
80 |
| - self.offset += 1; |
81 |
| - Ok(byte) |
| 30 | +fn take<'a>(input: &mut &'a[u8], count: usize) -> Option<&'a[u8]> { |
| 31 | + if input.len() < count { |
| 32 | + return None |
82 | 33 | }
|
| 34 | + let r = &(*input)[..count]; |
| 35 | + *input = &(*input)[count..]; |
| 36 | + Some(r) |
83 | 37 | }
|
84 | 38 |
|
85 | 39 | /// Concrete implementation of a `NodeCodec` with Parity Codec encoding, generic over the `Hasher`
|
86 | 40 | #[derive(Default, Clone)]
|
87 | 41 | pub struct NodeCodec<H>(PhantomData<H>);
|
88 | 42 |
|
89 |
| -impl<H: Hasher> NodeCodecT for NodeCodec<H> { |
| 43 | +impl<H: Hasher> NodeCodecT<H> for NodeCodec<H> { |
90 | 44 | type Error = Error;
|
91 |
| - type HashOut = H::Out; |
92 | 45 |
|
93 | 46 | fn hashed_null_node() -> <H as Hasher>::Out {
|
94 |
| - H::hash(<Self as NodeCodecT>::empty_node()) |
| 47 | + H::hash(<Self as NodeCodecT<_>>::empty_node()) |
95 | 48 | }
|
96 | 49 |
|
97 |
| - fn decode_plan(data: &[u8]) -> rstd::result::Result<NodePlan, Self::Error> { |
98 |
| - let mut input = ByteSliceInput::new(data); |
99 |
| - match NodeHeader::decode(&mut input)? { |
100 |
| - NodeHeader::Null => Ok(NodePlan::Empty), |
| 50 | + fn decode(data: &[u8]) -> rstd::result::Result<Node, Self::Error> { |
| 51 | + let input = &mut &*data; |
| 52 | + let head = NodeHeader::decode(input)?; |
| 53 | + match head { |
| 54 | + NodeHeader::Null => Ok(Node::Empty), |
101 | 55 | NodeHeader::Branch(has_value, nibble_count) => {
|
102 | 56 | let padding = nibble_count % nibble_ops::NIBBLE_PER_BYTE != 0;
|
103 | 57 | // check that the padding is valid (if any)
|
104 |
| - if padding && nibble_ops::pad_left(data[input.offset]) != 0 { |
| 58 | + if padding && nibble_ops::pad_left(input[0]) != 0 { |
105 | 59 | return Err(Error::BadFormat);
|
106 | 60 | }
|
107 |
| - let partial = input.take( |
| 61 | + let nibble_data = take( |
| 62 | + input, |
108 | 63 | (nibble_count + (nibble_ops::NIBBLE_PER_BYTE - 1)) / nibble_ops::NIBBLE_PER_BYTE,
|
109 |
| - )?; |
110 |
| - let partial_padding = nibble_ops::number_padding(nibble_count); |
111 |
| - let bitmap_range = input.take(BITMAP_LENGTH)?; |
112 |
| - let bitmap = Bitmap::decode(&data[bitmap_range])?; |
| 64 | + ).ok_or(Error::BadFormat)?; |
| 65 | + let nibble_slice = NibbleSlice::new_offset( |
| 66 | + nibble_data, |
| 67 | + nibble_ops::number_padding(nibble_count), |
| 68 | + ); |
| 69 | + let bitmap_slice = take(input, BITMAP_LENGTH).ok_or(Error::BadFormat)?; |
| 70 | + let bitmap = Bitmap::decode(&bitmap_slice[..])?; |
113 | 71 | let value = if has_value {
|
114 |
| - let count = <Compact<u32>>::decode(&mut input)?.0 as usize; |
115 |
| - Some(input.take(count)?) |
| 72 | + let count = <Compact<u32>>::decode(input)?.0 as usize; |
| 73 | + Some(take(input, count).ok_or(Error::BadFormat)?) |
116 | 74 | } else {
|
117 | 75 | None
|
118 | 76 | };
|
119 |
| - let mut children = [ |
120 |
| - None, None, None, None, None, None, None, None, |
121 |
| - None, None, None, None, None, None, None, None, |
122 |
| - ]; |
| 77 | + let mut children = [None; 16]; |
| 78 | + |
123 | 79 | for i in 0..nibble_ops::NIBBLE_LENGTH {
|
124 | 80 | if bitmap.value_at(i) {
|
125 |
| - let count = <Compact<u32>>::decode(&mut input)?.0 as usize; |
126 |
| - let range = input.take(count)?; |
127 |
| - children[i] = Some(if count == H::LENGTH { |
128 |
| - NodeHandlePlan::Hash(range) |
129 |
| - } else { |
130 |
| - NodeHandlePlan::Inline(range) |
131 |
| - }); |
| 81 | + let count = <Compact<u32>>::decode(input)?.0 as usize; |
| 82 | + children[i] = Some(take(input, count).ok_or(Error::BadFormat)?); |
132 | 83 | }
|
133 | 84 | }
|
134 |
| - Ok(NodePlan::NibbledBranch { |
135 |
| - partial: NibbleSlicePlan::new(partial, partial_padding), |
136 |
| - value, |
137 |
| - children, |
138 |
| - }) |
| 85 | + Ok(Node::NibbledBranch(nibble_slice, children, value)) |
139 | 86 | }
|
140 | 87 | NodeHeader::Leaf(nibble_count) => {
|
141 | 88 | let padding = nibble_count % nibble_ops::NIBBLE_PER_BYTE != 0;
|
142 | 89 | // check that the padding is valid (if any)
|
143 |
| - if padding && nibble_ops::pad_left(data[input.offset]) != 0 { |
| 90 | + if padding && nibble_ops::pad_left(input[0]) != 0 { |
144 | 91 | return Err(Error::BadFormat);
|
145 | 92 | }
|
146 |
| - let partial = input.take( |
| 93 | + let nibble_data = take( |
| 94 | + input, |
147 | 95 | (nibble_count + (nibble_ops::NIBBLE_PER_BYTE - 1)) / nibble_ops::NIBBLE_PER_BYTE,
|
148 |
| - )?; |
149 |
| - let partial_padding = nibble_ops::number_padding(nibble_count); |
150 |
| - let count = <Compact<u32>>::decode(&mut input)?.0 as usize; |
151 |
| - Ok(NodePlan::Leaf { |
152 |
| - partial: NibbleSlicePlan::new(partial, partial_padding), |
153 |
| - value: input.take(count)?, |
154 |
| - }) |
| 96 | + ).ok_or(Error::BadFormat)?; |
| 97 | + let nibble_slice = NibbleSlice::new_offset( |
| 98 | + nibble_data, |
| 99 | + nibble_ops::number_padding(nibble_count), |
| 100 | + ); |
| 101 | + let count = <Compact<u32>>::decode(input)?.0 as usize; |
| 102 | + Ok(Node::Leaf(nibble_slice, take(input, count).ok_or(Error::BadFormat)?)) |
155 | 103 | }
|
156 | 104 | }
|
157 | 105 | }
|
158 | 106 |
|
| 107 | + fn try_decode_hash(data: &[u8]) -> Option<<H as Hasher>::Out> { |
| 108 | + if data.len() == H::LENGTH { |
| 109 | + let mut r = <H as Hasher>::Out::default(); |
| 110 | + r.as_mut().copy_from_slice(data); |
| 111 | + Some(r) |
| 112 | + } else { |
| 113 | + None |
| 114 | + } |
| 115 | + } |
| 116 | + |
159 | 117 | fn is_empty_node(data: &[u8]) -> bool {
|
160 |
| - data == <Self as NodeCodecT>::empty_node() |
| 118 | + data == <Self as NodeCodecT<_>>::empty_node() |
161 | 119 | }
|
162 | 120 |
|
163 | 121 | fn empty_node() -> &'static [u8] {
|
|
0 commit comments