From c22414378e37c01e8d78747bb2331acc03ca4354 Mon Sep 17 00:00:00 2001 From: Magamedrasul Ibragimov Date: Sat, 29 Oct 2022 14:09:53 +0300 Subject: [PATCH] chore: remove error handling --- src/database.rs | 6 +++--- src/lib.rs | 7 ------- src/tree.rs | 46 ++++++++++++++++++++-------------------------- 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/database.rs b/src/database.rs index 1aefb45..48fa16f 100644 --- a/src/database.rs +++ b/src/database.rs @@ -9,11 +9,11 @@ pub trait Database { fn load(dbpath: &str) -> Self; /// Returns value from db by the key - fn get(&self, key: DBKey) -> Result>; + fn get(&self, key: DBKey) -> Option; /// Puts the value to the db by the key - fn put(&mut self, key: DBKey, value: Value) -> Result<()>; + fn put(&mut self, key: DBKey, value: Value); /// Deletes the key from db - fn delete(&mut self, key: DBKey) -> Result<()>; + fn delete(&mut self, key: DBKey); } diff --git a/src/lib.rs b/src/lib.rs index d718f25..ac608cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,13 +14,6 @@ pub use database::*; pub use hasher::*; pub use tree::MerkleTree; -/// Denotes errors that occur when interacting with Database -#[derive(Debug)] -pub struct Error(String); - -/// Custom `Result` type/alias; used for interaction with Database -pub type Result = std::result::Result; - /// Denotes keys in a database pub type DBKey = [u8; 8]; diff --git a/src/tree.rs b/src/tree.rs index 0c519ce..546d466 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -33,84 +33,78 @@ where H: Hasher, { /// Creates new `MerkleTree` and store it to the specified path/db - pub fn new(depth: usize, dbpath: &str) -> Result { + pub fn new(depth: usize, dbpath: &str) -> Self { // Create new db instance let mut db = D::new(dbpath); // Insert depth val into db let depth_val = depth.to_be_bytes().to_vec(); - db.put(DEPTH_KEY, depth_val)?; + db.put(DEPTH_KEY, depth_val); // Insert next_index val into db let next_index = 0usize; let next_index_val = next_index.to_be_bytes().to_vec(); - db.put(NEXT_INDEX_KEY, next_index_val)?; + db.put(NEXT_INDEX_KEY, next_index_val); // Initialize one branch of the `Merkle Tree` from bottom to top let mut prev = H::default_leaf(); - db.put(Key(depth, 0).into(), prev.into())?; + db.put(Key(depth, 0).into(), prev.into()); for i in (0..depth).rev() { prev = H::hash(&[prev, prev]); - db.put(Key(i, 0).into(), prev.into())?; + db.put(Key(i, 0).into(), prev.into()); } - Ok(Self { + Self { db, h: H::new(), depth, next_index, - }) + } } /// Loads existing Merkle Tree from the specified path/db - pub fn load(dbpath: &str) -> Result { + pub fn load(dbpath: &str) -> Self { // Load existing db instance let db = D::load(dbpath); // Load depth & next_index values from db // TODO: proper handling instead of unwrap - let depth = db.get(DEPTH_KEY)?.unwrap().try_into().unwrap(); + let depth = db.get(DEPTH_KEY).unwrap().try_into().unwrap(); let depth = usize::from_be_bytes(depth); - let next_index = db.get(NEXT_INDEX_KEY)?.unwrap().try_into().unwrap(); + let next_index = db.get(NEXT_INDEX_KEY).unwrap().try_into().unwrap(); let next_index = usize::from_be_bytes(next_index); - Ok(Self { + Self { db, h: H::new(), depth, next_index, - }) + } } /// Inserts a leaf to the next available index - pub fn insert(&mut self, leaf: H::Fr) -> Result<()> { - // TODO: Return error - assert!(self.next_index < 1 << self.depth, "Merkle Tree is full"); + pub fn insert(&mut self, leaf: H::Fr) { + assert!(self.next_index < 1 << self.depth, "Merkle Tree is full!"); - self.set(self.next_index, leaf)?; + self.set(self.next_index, leaf); self.next_index += 1; - - Ok(()) } /// Deletes a leaf at the `key` by setting it to its default value - pub fn delete(&mut self, key: usize) -> Result<()> { - // TODO: Return error - assert!(key < self.next_index, "Already default"); + pub fn delete(&mut self, key: usize) { + assert!(key < self.next_index, "The key doesn't exist!"); - self.set(key, H::default_leaf())?; - - Ok(()) + self.set(key, H::default_leaf()); } /// Sets a leaf at the specified tree index - fn set(&mut self, key: usize, leaf: H::Fr) -> Result<()> { + fn set(&mut self, key: usize, leaf: H::Fr) { todo!() } // Recalculates `Merkle Tree` from the specified key - fn recalculate_from(&mut self, key: usize) -> Result<()> { + fn recalculate_from(&mut self, key: usize) { todo!() }