Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bump nybbles, use local encode_path #76

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/extension.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/nodes/leaf.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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"));
}

Expand Down
16 changes: 9 additions & 7 deletions src/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,24 @@ pub(crate) fn unpack_path_to_nibbles(first: Option<u8>, 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]> {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading