mirror of https://github.com/vacp2p/pmtree.git
chore: remove error handling
This commit is contained in:
parent
72c4fd3875
commit
c22414378e
|
@ -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<Option<Value>>;
|
||||
fn get(&self, key: DBKey) -> Option<Value>;
|
||||
|
||||
/// 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);
|
||||
}
|
||||
|
|
|
@ -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<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// Denotes keys in a database
|
||||
pub type DBKey = [u8; 8];
|
||||
|
||||
|
|
46
src/tree.rs
46
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<Self> {
|
||||
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<Self> {
|
||||
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!()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue