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;
|
fn load(dbpath: &str) -> Self;
|
||||||
|
|
||||||
/// Returns value from db by the key
|
/// 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
|
/// 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
|
/// 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 hasher::*;
|
||||||
pub use tree::MerkleTree;
|
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
|
/// Denotes keys in a database
|
||||||
pub type DBKey = [u8; 8];
|
pub type DBKey = [u8; 8];
|
||||||
|
|
||||||
|
|
46
src/tree.rs
46
src/tree.rs
|
@ -33,84 +33,78 @@ where
|
||||||
H: Hasher,
|
H: Hasher,
|
||||||
{
|
{
|
||||||
/// Creates new `MerkleTree` and store it to the specified path/db
|
/// 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
|
// Create new db instance
|
||||||
let mut db = D::new(dbpath);
|
let mut db = D::new(dbpath);
|
||||||
|
|
||||||
// Insert depth val into db
|
// Insert depth val into db
|
||||||
let depth_val = depth.to_be_bytes().to_vec();
|
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
|
// Insert next_index val into db
|
||||||
let next_index = 0usize;
|
let next_index = 0usize;
|
||||||
let next_index_val = next_index.to_be_bytes().to_vec();
|
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
|
// Initialize one branch of the `Merkle Tree` from bottom to top
|
||||||
let mut prev = H::default_leaf();
|
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() {
|
for i in (0..depth).rev() {
|
||||||
prev = H::hash(&[prev, prev]);
|
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,
|
db,
|
||||||
h: H::new(),
|
h: H::new(),
|
||||||
depth,
|
depth,
|
||||||
next_index,
|
next_index,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads existing Merkle Tree from the specified path/db
|
/// 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
|
// Load existing db instance
|
||||||
let db = D::load(dbpath);
|
let db = D::load(dbpath);
|
||||||
|
|
||||||
// Load depth & next_index values from db
|
// Load depth & next_index values from db
|
||||||
// TODO: proper handling instead of unwrap
|
// 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 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);
|
let next_index = usize::from_be_bytes(next_index);
|
||||||
|
|
||||||
Ok(Self {
|
Self {
|
||||||
db,
|
db,
|
||||||
h: H::new(),
|
h: H::new(),
|
||||||
depth,
|
depth,
|
||||||
next_index,
|
next_index,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Inserts a leaf to the next available index
|
/// Inserts a leaf to the next available index
|
||||||
pub fn insert(&mut self, leaf: H::Fr) -> Result<()> {
|
pub fn insert(&mut self, leaf: H::Fr) {
|
||||||
// TODO: Return error
|
assert!(self.next_index < 1 << self.depth, "Merkle Tree is full!");
|
||||||
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;
|
self.next_index += 1;
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes a leaf at the `key` by setting it to its default value
|
/// Deletes a leaf at the `key` by setting it to its default value
|
||||||
pub fn delete(&mut self, key: usize) -> Result<()> {
|
pub fn delete(&mut self, key: usize) {
|
||||||
// TODO: Return error
|
assert!(key < self.next_index, "The key doesn't exist!");
|
||||||
assert!(key < self.next_index, "Already default");
|
|
||||||
|
|
||||||
self.set(key, H::default_leaf())?;
|
self.set(key, H::default_leaf());
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a leaf at the specified tree index
|
/// 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!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recalculates `Merkle Tree` from the specified key
|
// Recalculates `Merkle Tree` from the specified key
|
||||||
fn recalculate_from(&mut self, key: usize) -> Result<()> {
|
fn recalculate_from(&mut self, key: usize) {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue