|
17 | 17 | //! `NodeCodec` implementation for Substrate's trie format.
|
18 | 18 |
|
19 | 19 | use rstd::marker::PhantomData;
|
| 20 | +use rstd::ops::Range; |
20 | 21 | use rstd::vec::Vec;
|
21 | 22 | use rstd::borrow::Borrow;
|
22 |
| -use codec::{Encode, Decode, Compact}; |
| 23 | +use codec::{Encode, Decode, Input, Compact}; |
23 | 24 | use hash_db::Hasher;
|
24 |
| -use trie_db::{self, NibbleSlice, node::Node, ChildReference, |
| 25 | +use trie_db::{self, node::{NibbleSlicePlan, NodePlan, NodeHandlePlan}, ChildReference, |
25 | 26 | nibble_ops, Partial, NodeCodec as NodeCodecT};
|
26 | 27 | use crate::error::Error;
|
27 | 28 | use crate::trie_constants;
|
28 | 29 | use super::{node_header::{NodeHeader, NodeKind}};
|
29 | 30 |
|
30 |
| -fn take<'a>(input: &mut &'a[u8], count: usize) -> Option<&'a[u8]> { |
31 |
| - if input.len() < count { |
32 |
| - return None |
| 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) |
33 | 82 | }
|
34 |
| - let r = &(*input)[..count]; |
35 |
| - *input = &(*input)[count..]; |
36 |
| - Some(r) |
37 | 83 | }
|
38 | 84 |
|
39 | 85 | /// Concrete implementation of a `NodeCodec` with Parity Codec encoding, generic over the `Hasher`
|
40 | 86 | #[derive(Default, Clone)]
|
41 | 87 | pub struct NodeCodec<H>(PhantomData<H>);
|
42 | 88 |
|
43 |
| -impl<H: Hasher> NodeCodecT<H> for NodeCodec<H> { |
| 89 | +impl<H: Hasher> NodeCodecT for NodeCodec<H> { |
44 | 90 | type Error = Error;
|
| 91 | + type HashOut = H::Out; |
45 | 92 |
|
46 | 93 | fn hashed_null_node() -> <H as Hasher>::Out {
|
47 |
| - H::hash(<Self as NodeCodecT<_>>::empty_node()) |
| 94 | + H::hash(<Self as NodeCodecT>::empty_node()) |
48 | 95 | }
|
49 | 96 |
|
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), |
| 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), |
55 | 101 | NodeHeader::Branch(has_value, nibble_count) => {
|
56 | 102 | let padding = nibble_count % nibble_ops::NIBBLE_PER_BYTE != 0;
|
57 | 103 | // check that the padding is valid (if any)
|
58 |
| - if padding && nibble_ops::pad_left(input[0]) != 0 { |
| 104 | + if padding && nibble_ops::pad_left(data[input.offset]) != 0 { |
59 | 105 | return Err(Error::BadFormat);
|
60 | 106 | }
|
61 |
| - let nibble_data = take( |
62 |
| - input, |
| 107 | + let partial = input.take( |
63 | 108 | (nibble_count + (nibble_ops::NIBBLE_PER_BYTE - 1)) / nibble_ops::NIBBLE_PER_BYTE,
|
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[..])?; |
| 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])?; |
71 | 113 | let value = if has_value {
|
72 |
| - let count = <Compact<u32>>::decode(input)?.0 as usize; |
73 |
| - Some(take(input, count).ok_or(Error::BadFormat)?) |
| 114 | + let count = <Compact<u32>>::decode(&mut input)?.0 as usize; |
| 115 | + Some(input.take(count)?) |
74 | 116 | } else {
|
75 | 117 | None
|
76 | 118 | };
|
77 |
| - let mut children = [None; 16]; |
78 |
| - |
| 119 | + let mut children = [ |
| 120 | + None, None, None, None, None, None, None, None, |
| 121 | + None, None, None, None, None, None, None, None, |
| 122 | + ]; |
79 | 123 | for i in 0..nibble_ops::NIBBLE_LENGTH {
|
80 | 124 | if bitmap.value_at(i) {
|
81 |
| - let count = <Compact<u32>>::decode(input)?.0 as usize; |
82 |
| - children[i] = Some(take(input, count).ok_or(Error::BadFormat)?); |
| 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 | + }); |
83 | 132 | }
|
84 | 133 | }
|
85 |
| - Ok(Node::NibbledBranch(nibble_slice, children, value)) |
| 134 | + Ok(NodePlan::NibbledBranch { |
| 135 | + partial: NibbleSlicePlan::new(partial, partial_padding), |
| 136 | + value, |
| 137 | + children, |
| 138 | + }) |
86 | 139 | }
|
87 | 140 | NodeHeader::Leaf(nibble_count) => {
|
88 | 141 | let padding = nibble_count % nibble_ops::NIBBLE_PER_BYTE != 0;
|
89 | 142 | // check that the padding is valid (if any)
|
90 |
| - if padding && nibble_ops::pad_left(input[0]) != 0 { |
| 143 | + if padding && nibble_ops::pad_left(data[input.offset]) != 0 { |
91 | 144 | return Err(Error::BadFormat);
|
92 | 145 | }
|
93 |
| - let nibble_data = take( |
94 |
| - input, |
| 146 | + let partial = input.take( |
95 | 147 | (nibble_count + (nibble_ops::NIBBLE_PER_BYTE - 1)) / nibble_ops::NIBBLE_PER_BYTE,
|
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)?)) |
| 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 | + }) |
103 | 155 | }
|
104 | 156 | }
|
105 | 157 | }
|
106 | 158 |
|
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 |
| - |
117 | 159 | fn is_empty_node(data: &[u8]) -> bool {
|
118 |
| - data == <Self as NodeCodecT<_>>::empty_node() |
| 160 | + data == <Self as NodeCodecT>::empty_node() |
119 | 161 | }
|
120 | 162 |
|
121 | 163 | fn empty_node() -> &'static [u8] {
|
|
0 commit comments