From 4c63644e742ccc04d1548f9f16f63494037b5f7a Mon Sep 17 00:00:00 2001 From: Odysseas Gabrielides Date: Tue, 30 Apr 2024 10:59:00 +0300 Subject: [PATCH] clippy fixes --- grovedb/src/batch/mod.rs | 2 +- grovedb/src/lib.rs | 75 ++++++++++++++-------------- grovedb/src/operations/auxiliary.rs | 1 - grovedb/src/operations/delete/mod.rs | 2 +- merk/src/merk/chunks.rs | 12 ++--- merk/src/merk/mod.rs | 13 ++--- merk/src/merk/restore.rs | 21 ++++---- merk/src/proofs/chunk/chunk_op.rs | 4 +- merk/src/proofs/chunk/util.rs | 2 +- 9 files changed, 61 insertions(+), 71 deletions(-) diff --git a/grovedb/src/batch/mod.rs b/grovedb/src/batch/mod.rs index 70c47619..3eac9289 100644 --- a/grovedb/src/batch/mod.rs +++ b/grovedb/src/batch/mod.rs @@ -560,7 +560,7 @@ impl GroveDbOp { } /// Verify consistency of operations - pub fn verify_consistency_of_operations(ops: &Vec) -> GroveDbOpConsistencyResults { + pub fn verify_consistency_of_operations(ops: &[GroveDbOp]) -> GroveDbOpConsistencyResults { let ops_len = ops.len(); // operations should not have any duplicates let mut repeated_ops = vec![]; diff --git a/grovedb/src/lib.rs b/grovedb/src/lib.rs index 9e8eae6b..9de0b339 100644 --- a/grovedb/src/lib.rs +++ b/grovedb/src/lib.rs @@ -171,14 +171,12 @@ use std::collections::{BTreeMap, BTreeSet}; #[cfg(feature = "full")] use std::{collections::HashMap, fmt, option::Option::None, path::Path}; -use blake3; #[cfg(any(feature = "full", feature = "verify"))] use element::helpers; #[cfg(any(feature = "full", feature = "verify"))] pub use element::Element; #[cfg(feature = "full")] pub use element::ElementFlags; -use grovedb_costs::storage_cost::key_value_cost::KeyValueStorageCost; #[cfg(feature = "full")] use grovedb_costs::{ cost_return_on_error, cost_return_on_error_no_add, CostResult, CostsExt, OperationCost, @@ -235,8 +233,6 @@ type Hash = [u8; 32]; pub struct GroveDb { #[cfg(feature = "full")] db: RocksDbStorage, - - version: i32, } // Struct governing state sync @@ -272,12 +268,18 @@ impl SubtreesMetadata { } } +impl Default for SubtreesMetadata { + fn default() -> Self { + Self::new() + } +} + impl fmt::Debug for SubtreesMetadata { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { for (prefix, metadata) in self.data.iter() { let metadata_path = &metadata.0; - let metadata_path_str = util_path_to_string(&metadata_path); - write!( + let metadata_path_str = util_path_to_string(metadata_path); + writeln!( f, " prefix:{:?} -> path:{:?}\n", hex::encode(prefix), @@ -312,7 +314,7 @@ impl GroveDb { /// Opens a given path pub fn open>(path: P) -> Result { let db = RocksDbStorage::default_rocksdb_with_path(path)?; - Ok(GroveDb { db, version: 1 }) + Ok(GroveDb { db }) } /// Uses raw iter to delete GroveDB key values pairs from rocksdb @@ -1092,7 +1094,7 @@ impl GroveDb { let current_path = SubtreePath::from(path); let parent_path_opt = current_path.derive_parent(); - if (parent_path_opt.is_some()) { + if parent_path_opt.is_some() { let parent_path = parent_path_opt.unwrap().0; let parent_merk = self .open_transactional_merk_at_path(parent_path, tx, None) @@ -1143,20 +1145,20 @@ impl GroveDb { global_chunk_id: &[u8], tx: &'db Transaction, ) -> Result, Error> { - let CHUNK_PREFIX_LENGTH: usize = 32; - if (global_chunk_id.len() < CHUNK_PREFIX_LENGTH) { + let chunk_prefix_length: usize = 32; + if global_chunk_id.len() < chunk_prefix_length { return Err(Error::CorruptedData( "expected global chunk id of at least 32 length".to_string(), )); } - let (chunk_prefix, chunk_id) = global_chunk_id.split_at(CHUNK_PREFIX_LENGTH); + let (chunk_prefix, chunk_id) = global_chunk_id.split_at(chunk_prefix_length); let mut array = [0u8; 32]; array.copy_from_slice(chunk_prefix); let chunk_prefix_key: SubtreePrefix = array; - let subtrees_metadata = self.get_subtrees_metadata(&tx)?; + let subtrees_metadata = self.get_subtrees_metadata(tx)?; match subtrees_metadata.data.get(&chunk_prefix_key) { Some(path_data) => { @@ -1165,14 +1167,14 @@ impl GroveDb { let path: &[&[u8]] = &subtree_path; let merk = self - .open_transactional_merk_at_path(path.into(), &tx, None) + .open_transactional_merk_at_path(path.into(), tx, None) .value?; - if (merk.is_empty_tree().unwrap()) { + if merk.is_empty_tree().unwrap() { return Ok(vec![]); } - let mut chunk_producer_res = ChunkProducer::new(&merk); + let chunk_producer_res = ChunkProducer::new(&merk); match chunk_producer_res { Ok(mut chunk_producer) => { let chunk_res = chunk_producer @@ -1180,21 +1182,21 @@ impl GroveDb { match chunk_res { Ok((chunk, _)) => Ok(chunk), Err(_) => { - return Err(Error::CorruptedData( + Err(Error::CorruptedData( "Unable to create to load chunk".to_string(), - )); + )) } } } Err(_) => { - return Err(Error::CorruptedData( + Err(Error::CorruptedData( "Unable to create Chunk producer".to_string(), - )); + )) } } } None => { - return Err(Error::CorruptedData("Prefix not found".to_string())); + Err(Error::CorruptedData("Prefix not found".to_string())) } } } @@ -1262,23 +1264,23 @@ impl GroveDb { let mut res = vec![]; let (global_chunk_id, chunk_data) = chunk; - let (chunk_prefix, chunk_id) = util_split_global_chunk_id(&global_chunk_id)?; + let (chunk_prefix, chunk_id) = util_split_global_chunk_id(global_chunk_id)?; match ( &mut state_sync_info.restorer, &state_sync_info.current_prefix, ) { (Some(restorer), Some(ref current_prefix)) => { - if (*current_prefix != chunk_prefix) { + if *current_prefix != chunk_prefix { return Err(Error::InternalError("Invalid incoming prefix")); } - if (!state_sync_info.pending_chunks.contains(global_chunk_id)) { + if !state_sync_info.pending_chunks.contains(global_chunk_id) { return Err(Error::InternalError( "Incoming global_chunk_id not expected", )); } state_sync_info.pending_chunks.remove(global_chunk_id); - if (!chunk_data.is_empty()) { + if !chunk_data.is_empty() { match restorer.process_chunk(chunk_id.to_string(), chunk_data) { Ok(next_chunk_ids) => { state_sync_info.num_processed_chunks += 1; @@ -1302,8 +1304,8 @@ impl GroveDb { } } - if (res.is_empty()) { - if (!state_sync_info.pending_chunks.is_empty()) { + if res.is_empty() { + if !state_sync_info.pending_chunks.is_empty() { return Ok((res, state_sync_info)); } match ( @@ -1311,16 +1313,13 @@ impl GroveDb { state_sync_info.current_prefix.take(), ) { (Some(restorer), Some(current_prefix)) => { - if (state_sync_info.num_processed_chunks > 0) { - if (!restorer.finalize().is_ok()) { - return Err(Error::InternalError("Unable to finalize merk")); - } + if (state_sync_info.num_processed_chunks > 0) && (restorer.finalize().is_err()) { + return Err(Error::InternalError("Unable to finalize merk")); } state_sync_info.processed_prefixes.insert(current_prefix); let subtrees_metadata = self.get_subtrees_metadata(tx)?; if let Some(value) = subtrees_metadata.data.get(¤t_prefix) { - let v_path = &value.0; println!(" path:{:?} done", util_path_to_string(&value.0)); } @@ -1360,10 +1359,10 @@ impl GroveDb { } // Converts a path into a human-readable string (for debuting) -pub fn util_path_to_string(path: &Vec>) -> Vec { +pub fn util_path_to_string(path: &[Vec]) -> Vec { let mut subtree_path_str: Vec = vec![]; - for subtree in path.to_vec() { - let string = std::str::from_utf8(&subtree).unwrap(); + for subtree in path { + let string = std::str::from_utf8(subtree).unwrap(); subtree_path_str.push(string.parse().unwrap()); } subtree_path_str @@ -1373,14 +1372,14 @@ pub fn util_path_to_string(path: &Vec>) -> Vec { pub fn util_split_global_chunk_id( global_chunk_id: &[u8], ) -> Result<(SubtreePrefix, String), Error> { - let CHUNK_PREFIX_LENGTH: usize = 32; - if global_chunk_id.len() < CHUNK_PREFIX_LENGTH { + let chunk_prefix_length: usize = 32; + if global_chunk_id.len() < chunk_prefix_length { return Err(Error::CorruptedData( "expected global chunk id of at least 32 length".to_string(), )); } - let (chunk_prefix, chunk_id) = global_chunk_id.split_at(CHUNK_PREFIX_LENGTH); + let (chunk_prefix, chunk_id) = global_chunk_id.split_at(chunk_prefix_length); let mut array = [0u8; 32]; array.copy_from_slice(chunk_prefix); let chunk_prefix_key: SubtreePrefix = array; @@ -1388,7 +1387,7 @@ pub fn util_split_global_chunk_id( match str_chunk_id { Ok(s) => Ok((chunk_prefix_key, s)), Err(_) => { - return Err(Error::CorruptedData( + Err(Error::CorruptedData( "unable to convert chunk id to string".to_string(), )) } diff --git a/grovedb/src/operations/auxiliary.rs b/grovedb/src/operations/auxiliary.rs index 2e0cbe69..1b6b884d 100644 --- a/grovedb/src/operations/auxiliary.rs +++ b/grovedb/src/operations/auxiliary.rs @@ -33,7 +33,6 @@ use grovedb_costs::{ cost_return_on_error, cost_return_on_error_no_add, storage_cost::key_value_cost::KeyValueStorageCost, CostResult, CostsExt, OperationCost, }; -use grovedb_merk::{proofs::Query, KVIterator}; use grovedb_path::SubtreePath; #[cfg(feature = "full")] use grovedb_storage::StorageContext; diff --git a/grovedb/src/operations/delete/mod.rs b/grovedb/src/operations/delete/mod.rs index f3dcc6cd..233efc3a 100644 --- a/grovedb/src/operations/delete/mod.rs +++ b/grovedb/src/operations/delete/mod.rs @@ -59,7 +59,7 @@ use grovedb_storage::{ #[cfg(feature = "full")] use crate::{ batch::{GroveDbOp, Op}, - util::{storage_context_optional_tx, storage_context_with_parent_optional_tx}, + util::{storage_context_with_parent_optional_tx}, Element, ElementFlags, Error, GroveDb, Transaction, TransactionArg, }; use crate::{raw_decode, util::merk_optional_tx_path_not_empty}; diff --git a/merk/src/merk/chunks.rs b/merk/src/merk/chunks.rs index 37c485cc..be2ecffc 100644 --- a/merk/src/merk/chunks.rs +++ b/merk/src/merk/chunks.rs @@ -364,6 +364,10 @@ where number_of_chunks(self.height) } + pub fn is_empty(&self) -> bool { + number_of_chunks(self.height) == 0 + } + /// Gets the next chunk based on the `ChunkProducer`'s internal index state. /// This is mostly useful for letting `ChunkIter` yield the chunks in order, /// optimizing throughput compared to random access. @@ -387,14 +391,6 @@ where }), ) } - - // TODO: test this logic out - fn get_chunk_encoding_length(chunk: &[Op]) -> usize { - // TODO: deal with error - chunk - .iter() - .fold(0, |sum, op| sum + op.encoding_length().unwrap()) - } } /// Iterate over each chunk, returning `None` after last chunk diff --git a/merk/src/merk/mod.rs b/merk/src/merk/mod.rs index ecd66dac..94b99add 100644 --- a/merk/src/merk/mod.rs +++ b/merk/src/merk/mod.rs @@ -71,7 +71,6 @@ use crate::{ tree::{ kv::ValueDefinedCostType, AuxMerkBatch, CryptoHash, Op, RefWalker, TreeNode, NULL_HASH, }, - BatchEntry, Error::{CostsError, EdError, StorageError}, Link, MerkType::{BaseMerk, LayeredMerk, StandaloneMerk}, @@ -662,19 +661,17 @@ where } let node = node.unwrap(); - if &node.hash().unwrap() != &hash { + if node.hash().unwrap() != hash { bad_link_map.insert(instruction_id.clone(), hash); parent_keys.insert(instruction_id, parent_key.to_vec()); return; } // Need to skip this when restoring a sum tree - if !skip_sum_checks { - if node.sum().unwrap() != sum { - bad_link_map.insert(instruction_id.clone(), hash); - parent_keys.insert(instruction_id, parent_key.to_vec()); - return; - } + if !skip_sum_checks && node.sum().unwrap() != sum { + bad_link_map.insert(instruction_id.clone(), hash); + parent_keys.insert(instruction_id, parent_key.to_vec()); + return; } // TODO: check child heights diff --git a/merk/src/merk/restore.rs b/merk/src/merk/restore.rs index 3fd42a1d..f38503d3 100644 --- a/merk/src/merk/restore.rs +++ b/merk/src/merk/restore.rs @@ -31,12 +31,11 @@ use std::collections::BTreeMap; -use grovedb_costs::cost_return_on_error; use grovedb_storage::{Batch, StorageContext}; use crate::{ merk, - merk::{committer::MerkCommitter, MerkSource}, + merk::{MerkSource}, proofs::{ chunk::{ chunk::{LEFT, RIGHT}, @@ -47,7 +46,7 @@ use crate::{ tree::{execute, Child, Tree as ProofTree}, Node, Op, }, - tree::{combine_hash, kv::ValueDefinedCostType, NoopCommit, RefWalker, TreeNode}, + tree::{combine_hash, kv::ValueDefinedCostType, RefWalker, TreeNode}, CryptoHash, Error, Error::{CostsError, StorageError}, Link, Merk, @@ -96,15 +95,15 @@ impl<'db, S: StorageContext<'db>> Restorer { .ok_or(Error::ChunkRestoringError(ChunkError::UnexpectedChunk))?; let mut parent_key_value_hash: Option = None; - if (chunk_id.len() == 0) { - parent_key_value_hash = self.parent_key_value_hash.clone(); + if chunk_id.is_empty() { + parent_key_value_hash = self.parent_key_value_hash; } let chunk_tree = Self::verify_chunk(chunk, expected_root_hash, &parent_key_value_hash)?; let mut root_traversal_instruction = string_as_traversal_instruction(&chunk_id)?; if root_traversal_instruction.is_empty() { - self.merk.set_base_root_key(Some(chunk_tree.key().to_vec())); + let _ = self.merk.set_base_root_key(Some(chunk_tree.key().to_vec())); } else { // every non root chunk has some associated parent with an placeholder link // here we update the placeholder link to represent the true data @@ -185,9 +184,9 @@ impl<'db, S: StorageContext<'db>> Restorer { debug_assert_eq!(chunk_len, ((kv_count + hash_count) * 2) - 1); // chunk structure verified, next verify root hash - let parent_key_value_hash = match parent_key_value_hash_opt { + match parent_key_value_hash_opt { Some(val_hash) => { - let combined_hash = combine_hash(&val_hash, &tree.hash().unwrap()).unwrap(); + let combined_hash = combine_hash(val_hash, &tree.hash().unwrap()).unwrap(); if &combined_hash != expected_root_hash { return Err(Error::ChunkRestoringError(ChunkError::InvalidChunkProof( "chunk doesn't match expected root hash", @@ -408,14 +407,14 @@ impl<'db, S: StorageContext<'db>> Restorer { } // get the latest version of the root node - self.merk + let _ = self.merk .load_base_root(None::<&fn(&[u8]) -> Option>); // if height values are wrong, rewrite height if self.verify_height().is_err() { - self.rewrite_heights(); + let _ = self.rewrite_heights(); // update the root node after height rewrite - self.merk + let _ = self.merk .load_base_root(None::<&fn(&[u8]) -> Option>); } diff --git a/merk/src/proofs/chunk/chunk_op.rs b/merk/src/proofs/chunk/chunk_op.rs index 55f5751b..6d0d08cd 100644 --- a/merk/src/proofs/chunk/chunk_op.rs +++ b/merk/src/proofs/chunk/chunk_op.rs @@ -45,7 +45,7 @@ impl Encode for ChunkOp { match self { Self::ChunkId(instruction) => { // write the marker then the len - dest.write_all(&[0_u8]); + let _ = dest.write_all(&[0_u8]); dest.write_all(instruction.len().encode_var_vec().as_slice())?; let instruction_as_binary: Vec = instruction .iter() @@ -54,7 +54,7 @@ impl Encode for ChunkOp { dest.write_all(&instruction_as_binary)?; } Self::Chunk(chunk) => { - dest.write_all(&[1_u8]); + let _ = dest.write_all(&[1_u8]); // chunk len represents the number of ops not the total encoding len of ops dest.write_all(chunk.len().encode_var_vec().as_slice())?; for op in chunk { diff --git a/merk/src/proofs/chunk/util.rs b/merk/src/proofs/chunk/util.rs index 530e00e7..2f64ba8d 100644 --- a/merk/src/proofs/chunk/util.rs +++ b/merk/src/proofs/chunk/util.rs @@ -340,7 +340,7 @@ pub fn generate_traversal_instruction_as_string( /// Convert traversal instruction to byte string /// 1 represents left (true) /// 0 represents right (false) -pub fn traversal_instruction_as_string(instruction: &Vec) -> String { +pub fn traversal_instruction_as_string(instruction: &[bool]) -> String { instruction .iter() .map(|v| if *v { "1" } else { "0" })