From 9d3fdba7c1d94b72e4c6eb3225126ae7a8181367 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:10:33 +0100 Subject: [PATCH] feat: bump nybbles, use local encode_path (#76) --- Cargo.toml | 2 +- src/nodes/extension.rs | 4 ++-- src/nodes/leaf.rs | 8 ++++---- src/nodes/mod.rs | 16 +++++++++------- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2773f67..0cc407a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,7 +54,7 @@ derive_more = { version = "1", default-features = false, features = [ "from", "not", ] } -nybbles = { version = "0.2", default-features = false } +nybbles = { version = "0.3", default-features = false } smallvec = { version = "1.0", default-features = false, features = [ "const_new", ] } diff --git a/src/nodes/extension.rs b/src/nodes/extension.rs index b7177b0..9caf9ce 100644 --- a/src/nodes/extension.rs +++ b/src/nodes/extension.rs @@ -1,4 +1,4 @@ -use super::{super::Nibbles, unpack_path_to_nibbles, RlpNode}; +use super::{super::Nibbles, encode_path_leaf, unpack_path_to_nibbles, RlpNode}; use alloy_primitives::{hex, Bytes}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header}; use core::fmt; @@ -104,7 +104,7 @@ impl Encodable for ExtensionNodeRef<'_> { #[inline] fn encode(&self, out: &mut dyn BufMut) { Header { list: true, payload_length: self.rlp_payload_length() }.encode(out); - self.key.encode_path_leaf(false).as_slice().encode(out); + encode_path_leaf(self.key, false).as_slice().encode(out); // Pointer to the child is already RLP encoded. out.put_slice(self.child); } diff --git a/src/nodes/leaf.rs b/src/nodes/leaf.rs index bcdc866..28e9bf8 100644 --- a/src/nodes/leaf.rs +++ b/src/nodes/leaf.rs @@ -1,4 +1,4 @@ -use super::{super::Nibbles, unpack_path_to_nibbles, RlpNode}; +use super::{super::Nibbles, encode_path_leaf, unpack_path_to_nibbles, RlpNode}; use alloy_primitives::{hex, Bytes}; use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header}; use core::fmt; @@ -103,7 +103,7 @@ impl Encodable for LeafNodeRef<'_> { #[inline] fn encode(&self, out: &mut dyn BufMut) { Header { list: true, payload_length: self.rlp_payload_length() }.encode(out); - self.key.encode_path_leaf(true).as_slice().encode(out); + encode_path_leaf(self.key, true).as_slice().encode(out); self.value.encode(out); } @@ -146,8 +146,8 @@ mod tests { // From manual regression test #[test] fn encode_leaf_node_nibble() { - let nibble = Nibbles::from_nibbles_unchecked(hex!("0604060f")); - let encoded = nibble.encode_path_leaf(true); + let nibbles = Nibbles::from_nibbles_unchecked(hex!("0604060f")); + let encoded = encode_path_leaf(&nibbles, true); assert_eq!(encoded[..], hex!("20646f")); } diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index 1b95795..0f1d494 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -206,22 +206,24 @@ pub(crate) fn unpack_path_to_nibbles(first: Option, rest: &[u8]) -> Nibbles /// # Examples /// /// ``` -/// # use nybbles::Nibbles; +/// use alloy_trie::nodes::encode_path_leaf; +/// use nybbles::Nibbles; +/// /// // Extension node with an even path length: /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]); -/// assert_eq!(nibbles.encode_path_leaf(false)[..], [0x00, 0xAB, 0xCD]); +/// assert_eq!(encode_path_leaf(&nibbles, false)[..], [0x00, 0xAB, 0xCD]); /// /// // Extension node with an odd path length: /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C]); -/// assert_eq!(nibbles.encode_path_leaf(false)[..], [0x1A, 0xBC]); +/// assert_eq!(encode_path_leaf(&nibbles, false)[..], [0x1A, 0xBC]); /// /// // Leaf node with an even path length: /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]); -/// assert_eq!(nibbles.encode_path_leaf(true)[..], [0x20, 0xAB, 0xCD]); +/// assert_eq!(encode_path_leaf(&nibbles, true)[..], [0x20, 0xAB, 0xCD]); /// /// // Leaf node with an odd path length: /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C]); -/// assert_eq!(nibbles.encode_path_leaf(true)[..], [0x3A, 0xBC]); +/// assert_eq!(encode_path_leaf(&nibbles, true)[..], [0x3A, 0xBC]); /// ``` #[inline] pub fn encode_path_leaf(nibbles: &Nibbles, is_leaf: bool) -> SmallVec<[u8; 36]> { @@ -335,7 +337,7 @@ mod tests { prop_assert!(input.iter().all(|&nibble| nibble <= 0xf)); let input_is_odd = input.len() % 2 == 1; - let compact_leaf = input.encode_path_leaf(true); + let compact_leaf = encode_path_leaf(&input, true); let leaf_flag = compact_leaf[0]; // Check flag assert_ne!(leaf_flag & LeafNode::EVEN_FLAG, 0); @@ -344,7 +346,7 @@ mod tests { assert_eq!(leaf_flag & 0x0f, input.first().unwrap()); } - let compact_extension = input.encode_path_leaf(false); + let compact_extension = encode_path_leaf(&input, false); let extension_flag = compact_extension[0]; // Check first byte assert_eq!(extension_flag & LeafNode::EVEN_FLAG, 0);