Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit a1dee7e

Browse files
authored
Remove partial key size limit from trie codec (#12566)
* remove size limit from trie codec * test previous upper limit is not enforced anymore * fmt * restore test
1 parent b70c64d commit a1dee7e

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

primitives/trie/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,6 @@ where
527527
/// Constants used into trie simplification codec.
528528
mod trie_constants {
529529
const FIRST_PREFIX: u8 = 0b_00 << 6;
530-
pub const NIBBLE_SIZE_BOUND: usize = u16::max_value() as usize;
531530
pub const LEAF_PREFIX_MASK: u8 = 0b_01 << 6;
532531
pub const BRANCH_WITHOUT_MASK: u8 = 0b_10 << 6;
533532
pub const BRANCH_WITH_MASK: u8 = 0b_11 << 6;
@@ -987,6 +986,21 @@ mod tests {
987986
assert_eq!(first_storage_root, second_storage_root);
988987
}
989988

989+
#[test]
990+
fn big_key() {
991+
let check = |keysize: usize| {
992+
let mut memdb = PrefixedMemoryDB::<Blake2Hasher>::default();
993+
let mut root = Default::default();
994+
let mut t = TrieDBMutBuilder::<LayoutV1>::new(&mut memdb, &mut root).build();
995+
t.insert(&vec![0x01u8; keysize][..], &[0x01u8, 0x23]).unwrap();
996+
std::mem::drop(t);
997+
let t = TrieDBBuilder::<LayoutV1>::new(&memdb, &root).build();
998+
assert_eq!(t.get(&vec![0x01u8; keysize][..]).unwrap(), Some(vec![0x01u8, 0x23]));
999+
};
1000+
check(u16::MAX as usize / 2); // old limit
1001+
check(u16::MAX as usize / 2 + 1); // value over old limit still works
1002+
}
1003+
9901004
#[test]
9911005
fn node_with_no_children_fail_decoding() {
9921006
let branch = NodeCodec::<Blake2Hasher>::branch_node_nibbled(

primitives/trie/src/node_codec.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,6 @@ fn partial_from_iterator_encode<I: Iterator<Item = u8>>(
279279
nibble_count: usize,
280280
node_kind: NodeKind,
281281
) -> Vec<u8> {
282-
let nibble_count = sp_std::cmp::min(trie_constants::NIBBLE_SIZE_BOUND, nibble_count);
283-
284282
let mut output = Vec::with_capacity(4 + (nibble_count / nibble_ops::NIBBLE_PER_BYTE));
285283
match node_kind {
286284
NodeKind::Leaf => NodeHeader::Leaf(nibble_count).encode_to(&mut output),

primitives/trie/src/node_header.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ pub(crate) fn size_and_prefix_iterator(
117117
prefix: u8,
118118
prefix_mask: usize,
119119
) -> impl Iterator<Item = u8> {
120-
let size = sp_std::cmp::min(trie_constants::NIBBLE_SIZE_BOUND, size);
121-
122120
let max_value = 255u8 >> prefix_mask;
123121
let l1 = sp_std::cmp::min((max_value as usize).saturating_sub(1), size);
124122
let (first_byte, mut rem) = if size == l1 {
@@ -165,12 +163,11 @@ fn decode_size(
165163
return Ok(result)
166164
}
167165
result -= 1;
168-
while result <= trie_constants::NIBBLE_SIZE_BOUND {
166+
loop {
169167
let n = input.read_byte()? as usize;
170168
if n < 255 {
171169
return Ok(result + n + 1)
172170
}
173171
result += 255;
174172
}
175-
Ok(trie_constants::NIBBLE_SIZE_BOUND)
176173
}

primitives/trie/src/trie_stream.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ fn branch_node_bit_mask(has_children: impl Iterator<Item = bool>) -> (u8, u8) {
5454

5555
/// Create a leaf/branch node, encoding a number of nibbles.
5656
fn fuse_nibbles_node(nibbles: &[u8], kind: NodeKind) -> impl Iterator<Item = u8> + '_ {
57-
let size = sp_std::cmp::min(trie_constants::NIBBLE_SIZE_BOUND, nibbles.len());
58-
57+
let size = nibbles.len();
5958
let iter_start = match kind {
6059
NodeKind::Leaf => size_and_prefix_iterator(size, trie_constants::LEAF_PREFIX_MASK, 2),
6160
NodeKind::BranchNoValue =>

0 commit comments

Comments
 (0)