Skip to content

Commit

Permalink
jmt: expose NodeKey::new and restrict NibblePath (#111)
Browse files Browse the repository at this point in the history
* Add Nibblepath to API

* fmt

---------

Co-authored-by: Erwan Or <[email protected]>
  • Loading branch information
preston-evans98 and erwanor authored Mar 23, 2024
1 parent 0411140 commit 9d4fb54
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
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

0 comments on commit 9d4fb54

Please sign in to comment.