feat: add new/load function for the Merkle Tree

This commit is contained in:
Magamedrasul Ibragimov
2022-10-28 16:42:13 +03:00
parent 88ab1f459f
commit f10e9b595a
4 changed files with 32 additions and 39 deletions

View File

@@ -2,8 +2,14 @@ use crate::*;
/// Trait that must be implemented for a Database
pub trait Database {
/// Creates new instance of db
fn new(dbpath: &str) -> Self;
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()>;
fn delete(&mut self, key: &[u8]) -> Result<()>;
/// Loades existing db (existence check required)
fn load(dbpath: &str) -> Self;
/// Returns value from db by the key
fn get(&self, key: DBKey) -> Result<Option<Value>>;
/// Puts the value to the db by the key
fn put(&mut self, key: DBKey, value: Value) -> Result<()>;
/// Deletes the key from db
fn delete(&mut self, key: DBKey) -> Result<()>;
}

View File

@@ -1,12 +1,14 @@
pub trait ToBytes {
fn to_bytes(&self) -> Vec<u8>;
}
use crate::*;
/// Trait that must be implemented for Hash Function
pub trait Hasher {
type Fr: Copy + Eq + Default + From<Vec<u8>> + Into<Vec<u8>>;
/// Native type for the hash-function
type Fr: Copy + Eq + Default + From<Value> + Into<Value>;
/// Creates new hash-function instance
fn new() -> Self;
/// Outputs the default leaf (Fr::default())
fn default_leaf() -> Self::Fr;
/// Calculates hash-function
fn hash(input: &[Self::Fr]) -> Self::Fr;
}

View File

@@ -1,5 +1,5 @@
//! # pmtree
//! Persistent Merkle tree in Rust
//! Persistent Merkle Tree in Rust
pub mod database;
pub mod hasher;
@@ -9,11 +9,15 @@ pub use database::*;
pub use hasher::*;
pub use tree::MerkleTree;
/// Type denoting errors that occur when interacting
/// with Database
/// Denotes errors that occur when interacting with Database
#[derive(Debug)]
pub struct Error(String);
/// Custom `Result` type/alias; used for interaction
/// with Database
/// 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; 32];
/// Denotes values in a database
pub type Value = Vec<u8>;

View File

@@ -1,5 +1,6 @@
use crate::*;
/// Merkle Tree implementation
pub struct MerkleTree<D, H>
where
D: Database,
@@ -11,39 +12,19 @@ where
next_index: usize,
}
pub(crate) struct Key(usize, usize);
impl From<Key> for Vec<u8> {
fn from(key: Key) -> Vec<u8> {
todo!()
}
}
impl<D, H> MerkleTree<D, H>
where
D: Database,
H: Hasher,
{
/// Creates new `MerkleTree`
/// Creates new `MerkleTree` and store it to the specified path/db
pub fn new(depth: usize, dbpath: &str) -> Result<Self> {
// TODO: Use open instead of new, and check if the database is exists
let mut db: D = Database::new(dbpath);
todo!()
}
db.put(&Vec::<u8>::from(Key(depth, 0)), &H::default_leaf().into())?;
for i in (0..depth).rev() {
let prev = db.get(&Vec::<u8>::from(Key(i + 1, 0)))?.unwrap();
db.put(
&Vec::<u8>::from(Key(i, 0)),
&H::hash(&[prev.clone().into(), prev.into()]).into(),
)?;
}
Ok(Self {
db,
h: Hasher::new(),
depth,
next_index: 0,
})
/// Loads existing Merkle Tree from the specified path/db
pub fn load(dbpath: &str) -> Result<Self> {
todo!()
}
/// Inserts a leaf to the next available index
@@ -51,7 +32,7 @@ where
todo!()
}
/// Recalculates `Merkle Tree` from the specified key
// Recalculates `Merkle Tree` from the specified key
fn recalculate_from(&mut self, key: usize) {
todo!()
}