chore: remove new method from hasher trait & implement test

This commit is contained in:
Magamedrasul Ibragimov 2022-11-01 20:24:31 +03:00
parent 6516b95507
commit 5f2b9c61ba
2 changed files with 19 additions and 19 deletions

View File

@ -5,9 +5,6 @@ pub trait Hasher {
/// 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;

View File

@ -1,7 +1,7 @@
use hex_literal::hex;
use pmtree::*;
use std::collections::HashMap;
use tiny_keccak::Keccak;
use tiny_keccak::{Hasher as _, Keccak};
pub struct MemoryDB(HashMap<DBKey, Value>);
pub struct MyKeccak(Keccak);
@ -10,52 +10,55 @@ pub struct MyKeccak(Keccak);
pub struct MyFr([u8; 32]);
impl Database for MemoryDB {
fn new(dbpath: &str) -> Self {
todo!()
fn new(_dbpath: &str) -> Self {
MemoryDB(HashMap::new())
}
fn load(dbpath: &str) -> Self {
todo!()
fn load(_dbpath: &str) -> Self {
panic!("Cannot load in-memory db!")
}
fn get(&self, key: DBKey) -> Option<Value> {
todo!()
self.0.get(&key).cloned()
}
fn put(&mut self, key: DBKey, value: Value) {
todo!()
self.0.insert(key, value);
}
fn delete(&mut self, key: DBKey) {
todo!()
self.0.remove(&key);
}
}
impl From<Vec<u8>> for MyFr {
fn from(v: Vec<u8>) -> Self {
todo!()
let v = v.try_into().unwrap();
MyFr(v)
}
}
impl From<MyFr> for Vec<u8> {
fn from(v: MyFr) -> Self {
todo!()
v.0.to_vec()
}
}
impl Hasher for MyKeccak {
type Fr = MyFr;
fn new() -> Self {
todo!()
}
fn default_leaf() -> Self::Fr {
todo!()
MyFr([0; 32])
}
fn hash(input: &[Self::Fr]) -> Self::Fr {
todo!()
let mut output = [0; 32];
let mut hasher = Keccak::v256();
for element in input {
hasher.update(&element.0);
}
hasher.finalize(&mut output);
MyFr(output)
}
}