Skip to content

Commit

Permalink
Fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpringle committed Oct 25, 2023
1 parent 6952f06 commit 9a451e1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
62 changes: 33 additions & 29 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{nibbles::Nibbles, v2::api::Proof};
use sha3::Digest;
use shale::{disk_address::DiskAddress, ObjRef, ObjWriteError, ShaleError, ShaleStore};
use shale::{self, disk_address::DiskAddress, ObjWriteError, ShaleError, ShaleStore};
use std::{
cmp::Ordering,
collections::HashMap,
Expand All @@ -21,6 +21,10 @@ pub use node::{BranchNode, Data, ExtNode, LeafNode, Node, NodeType, NBRANCH};
pub use partial_path::PartialPath;
pub use trie_hash::{TrieHash, TRIE_HASH_LEN};

type ObjRef<'a> = shale::ObjRef<'a, Node>;
type ParentRefs<'a> = Vec<(ObjRef<'a>, u8)>;
type ParentAddresses = Vec<(DiskAddress, u8)>;

#[derive(Debug, Error)]
pub enum MerkleError {
#[error("merkle datastore error: {0:?}")]
Expand Down Expand Up @@ -58,11 +62,11 @@ pub struct Merkle<S> {
}

impl<S: ShaleStore<Node>> Merkle<S> {
pub fn get_node(&self, ptr: DiskAddress) -> Result<ObjRef<Node>, MerkleError> {
pub fn get_node(&self, ptr: DiskAddress) -> Result<ObjRef, MerkleError> {
self.store.get_item(ptr).map_err(Into::into)
}

pub fn put_node(&self, node: Node) -> Result<ObjRef<Node>, MerkleError> {
pub fn put_node(&self, node: Node) -> Result<ObjRef, MerkleError> {
self.store.put_item(node, 0).map_err(Into::into)
}

Expand Down Expand Up @@ -164,8 +168,8 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
#[allow(clippy::too_many_arguments)]
fn split<'b>(
&self,
mut node_to_split: ObjRef<'b, Node>,
parents: &mut [(ObjRef<'b, Node>, u8)],
mut node_to_split: ObjRef<'b>,
parents: &mut [(ObjRef<'b>, u8)],
insert_path: &[u8],
n_path: Vec<u8>,
n_value: Option<Data>,
Expand Down Expand Up @@ -392,7 +396,7 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
key: K,
mut val: Vec<u8>,
root: DiskAddress,
) -> Result<(impl Iterator<Item = ObjRef<Node>>, Vec<DiskAddress>), MerkleError> {
) -> Result<(impl Iterator<Item = ObjRef>, Vec<DiskAddress>), MerkleError> {
// as we split a node, we need to track deleted nodes and parents
let mut deleted = Vec::new();
let mut parents = Vec::new();
Expand Down Expand Up @@ -578,7 +582,7 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {

fn after_remove_leaf(
&self,
parents: &mut Vec<(ObjRef<'_, Node>, u8)>,
parents: &mut ParentRefs,
deleted: &mut Vec<DiskAddress>,
) -> Result<(), MerkleError> {
let (b_chd, val) = {
Expand Down Expand Up @@ -749,7 +753,7 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
fn after_remove_branch(
&self,
(c_ptr, idx): (DiskAddress, u8),
parents: &mut Vec<(ObjRef<'_, Node>, u8)>,
parents: &mut ParentRefs,
deleted: &mut Vec<DiskAddress>,
) -> Result<(), MerkleError> {
// [b] -> [u] -> [c]
Expand Down Expand Up @@ -950,17 +954,17 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {

fn get_node_by_key<'a, K: AsRef<[u8]>>(
&'a self,
node_ref: ObjRef<'a, Node>,
node_ref: ObjRef<'a>,
key: K,
) -> Result<Option<ObjRef<'a, Node>>, MerkleError> {
) -> Result<Option<ObjRef<'a>>, MerkleError> {
self.get_node_by_key_with_callback(node_ref, key, |_, _| {})
}

fn get_node_and_parents_by_key<'a, K: AsRef<[u8]>>(
&'a self,
node_ref: ObjRef<'a, Node>,
node_ref: ObjRef<'a>,
key: K,
) -> Result<(Option<ObjRef<'a, Node>>, Vec<(ObjRef<'a, Node>, u8)>), MerkleError> {
) -> Result<(Option<ObjRef<'a>>, ParentRefs<'a>), MerkleError> {
let mut parents = Vec::new();
let node_ref = self.get_node_by_key_with_callback(node_ref, key, |node_ref, nib| {
parents.push((node_ref, nib));
Expand All @@ -971,9 +975,9 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {

fn get_node_and_parent_addresses_by_key<'a, K: AsRef<[u8]>>(
&'a self,
node_ref: ObjRef<'a, Node>,
node_ref: ObjRef<'a>,
key: K,
) -> Result<(Option<ObjRef<'a, Node>>, Vec<(DiskAddress, u8)>), MerkleError> {
) -> Result<(Option<ObjRef<'a>>, ParentAddresses), MerkleError> {
let mut parents = Vec::new();
let node_ref = self.get_node_by_key_with_callback(node_ref, key, |node_ref, nib| {
parents.push((node_ref.into_ptr(), nib));
Expand All @@ -984,10 +988,10 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {

fn get_node_by_key_with_callback<'a, K: AsRef<[u8]>>(
&'a self,
mut node_ref: ObjRef<'a, Node>,
mut node_ref: ObjRef<'a>,
key: K,
mut loop_callback: impl FnMut(ObjRef<'a, Node>, u8),
) -> Result<Option<ObjRef<'a, Node>>, MerkleError> {
mut loop_callback: impl FnMut(ObjRef<'a>, u8),
) -> Result<Option<ObjRef<'a>>, MerkleError> {
let mut key_nibbles = Nibbles::<1>::new(key.as_ref()).into_iter();

loop {
Expand Down Expand Up @@ -1173,7 +1177,7 @@ impl<S: ShaleStore<Node> + Send + Sync> Merkle<S> {
}
}

fn set_parent(new_chd: DiskAddress, parents: &mut [(ObjRef<'_, Node>, u8)]) {
fn set_parent(new_chd: DiskAddress, parents: &mut [(ObjRef, u8)]) {
let (p_ref, idx) = parents.last_mut().unwrap();
p_ref
.write(|p| {
Expand All @@ -1187,11 +1191,11 @@ fn set_parent(new_chd: DiskAddress, parents: &mut [(ObjRef<'_, Node>, u8)]) {
.unwrap();
}

pub struct Ref<'a>(ObjRef<'a, Node>);
pub struct Ref<'a>(ObjRef<'a>);

pub struct RefMut<'a, S> {
ptr: DiskAddress,
parents: Vec<(DiskAddress, u8)>,
parents: ParentAddresses,
merkle: &'a mut Merkle<S>,
}

Expand All @@ -1207,7 +1211,7 @@ impl<'a> std::ops::Deref for Ref<'a> {
}

impl<'a, S: ShaleStore<Node> + Send + Sync> RefMut<'a, S> {
fn new(ptr: DiskAddress, parents: Vec<(DiskAddress, u8)>, merkle: &'a mut Merkle<S>) -> Self {
fn new(ptr: DiskAddress, parents: ParentAddresses, merkle: &'a mut Merkle<S>) -> Self {
Self {
ptr,
parents,
Expand Down Expand Up @@ -1413,26 +1417,26 @@ mod tests {

// insert values
for key_val in u8::MIN..=u8::MAX {
let key = vec![key_val];
let val = vec![key_val];
let key = &[key_val];
let val = &[key_val];

merkle.insert(&key, val.clone(), root).unwrap();
merkle.insert(key, val.to_vec(), root).unwrap();

let fetched_val = merkle.get(&key, root).unwrap();
let fetched_val = merkle.get(key, root).unwrap();

// make sure the value was inserted
assert_eq!(fetched_val.as_deref(), val.as_slice().into());
}

// remove values
for key_val in u8::MIN..=u8::MAX {
let key = vec![key_val];
let val = vec![key_val];
let key = &[key_val];
let val = &[key_val];

let removed_val = merkle.remove(&key, root).unwrap();
let removed_val = merkle.remove(key, root).unwrap();
assert_eq!(removed_val.as_deref(), val.as_slice().into());

let fetched_val = merkle.get(&key, root).unwrap();
let fetched_val = merkle.get(key, root).unwrap();
assert!(fetched_val.is_none());
}
}
Expand Down
3 changes: 1 addition & 2 deletions shale/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ impl<'a, T: Send + Sync> ObjRef<'a, T> {
}

pub fn into_ptr(self) -> DiskAddress {
let ptr = self.deref().as_ptr();
ptr
self.deref().as_ptr()
}
}

Expand Down

0 comments on commit 9a451e1

Please sign in to comment.