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

Add NodeKey constructor to API; Restrict NibblePath manipulation #111

Merged
merged 4 commits into from
Mar 23, 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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub mod storage {
pub use node_type::{LeafNode, Node, NodeKey};
pub use reader::HasPreimage;
pub use reader::TreeReader;
pub use types::nibble::nibble_path::NibblePath;
pub use writer::{
NodeBatch, NodeStats, StaleNodeIndex, StaleNodeIndexBatch, TreeUpdateBatch, TreeWriter,
};
Expand Down
4 changes: 2 additions & 2 deletions src/node_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct NodeKey {

impl NodeKey {
/// Creates a new `NodeKey`.
pub(crate) fn new(version: Version, nibble_path: NibblePath) -> Self {
pub fn new(version: Version, nibble_path: NibblePath) -> Self {
Self {
version,
nibble_path,
Expand All @@ -75,7 +75,7 @@ impl NodeKey {
}

/// Gets the nibble path.
pub(crate) fn nibble_path(&self) -> &NibblePath {
pub fn nibble_path(&self) -> &NibblePath {
&self.nibble_path
}

Expand Down
13 changes: 8 additions & 5 deletions src/types/nibble/nibble_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,17 @@ prop_compose! {

impl NibblePath {
/// Creates a new `NibblePath` from a vector of bytes assuming each byte has 2 nibbles.
pub fn new(bytes: Vec<u8>) -> Self {
pub(crate) fn new(bytes: Vec<u8>) -> Self {
checked_precondition!(bytes.len() <= ROOT_NIBBLE_HEIGHT / 2);
let num_nibbles = bytes.len() * 2;
NibblePath { num_nibbles, bytes }
}

/// Similar to `new()` but assumes that the bytes have one less nibble.
pub fn new_odd(bytes: Vec<u8>) -> Self {
// Unlike `new`, this function is not used under all feature combinations - so
// we #[allow(unused)] to silence the warnings
#[allow(unused)]
pub(crate) fn new_odd(bytes: Vec<u8>) -> Self {
checked_precondition!(bytes.len() <= ROOT_NIBBLE_HEIGHT / 2);
assert_eq!(
bytes.last().expect("Should have odd number of nibbles.") & 0x0f,
Expand All @@ -117,7 +120,7 @@ impl NibblePath {
}

/// Adds a nibble to the end of the nibble path.
pub fn push(&mut self, nibble: Nibble) {
pub(crate) fn push(&mut self, nibble: Nibble) {
assert!(ROOT_NIBBLE_HEIGHT > self.num_nibbles);
if self.num_nibbles % 2 == 0 {
self.bytes.push(u8::from(nibble) << 4);
Expand All @@ -128,7 +131,7 @@ impl NibblePath {
}

/// Pops a nibble from the end of the nibble path.
pub fn pop(&mut self) -> Option<Nibble> {
pub(crate) fn pop(&mut self) -> Option<Nibble> {
let poped_nibble = if self.num_nibbles % 2 == 0 {
self.bytes.last_mut().map(|last_byte| {
let nibble = *last_byte & 0x0f;
Expand Down Expand Up @@ -195,7 +198,7 @@ impl NibblePath {
}

/// Get the underlying bytes storing nibbles.
pub fn bytes(&self) -> &[u8] {
pub(crate) fn bytes(&self) -> &[u8] {
&self.bytes
}
}
Expand Down
Loading