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

simplify by: depth + prefix.bit_len()== point #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
extern crate alloc;

pub mod path;
#[cfg(test)]
pub mod path_test;
pub mod subtree;

#[cfg(feature = "std")]
Expand Down
5 changes: 4 additions & 1 deletion src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ impl<T: BitLength + AsRef<[u8]>> PathUtils for T {
Direction::Left
}

/// Returns the first differing bit of `self`, from `start` bit of `self`(inclusive).
/// If all bits are the same, returns None instead.
fn split_point<S: BitLength + PathUtils>(&self, start: usize, b: S) -> Option<usize> {
let max_bit_len = core::cmp::min(self.bit_len(), b.bit_len());
assert!(self.bit_len() >= start);
let max_bit_len = core::cmp::min(self.bit_len() - start, b.bit_len());
let (src_start_byte, src_start_bit, seg_end_byte) =
(start / 8, start % 8, (max_bit_len + 7) / 8);
let mut count = 0;
Expand Down
19 changes: 19 additions & 0 deletions src/path_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::path::{BitLength, Path, PathSegment};
use crate::ZERO_HASH;

#[test]
fn path_bit_len() {
let current_key = Path(ZERO_HASH);
for depth in 0..256 {
for point in depth..256 {
let seg = PathSegment::from_path(current_key, depth, point);
assert_eq!(
seg.bit_len(),
point - depth,
"depth:{} point:{}",
depth,
point
);
}
}
}
2 changes: 1 addition & 1 deletion src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ impl<'db, H: NodeHasher> WriteTransaction<'db, H> {
let point = current_key.split_point(0, key).unwrap();
let prefix = PathSegment::from_path(current_key, depth, point);

let depth = depth + prefix.bit_len();
let depth = point;
let node_direction = key.direction(depth);
let current_node = Node::from_leaf(current_key, current_value);
let node = Node::from_leaf(key, value);
Expand Down