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 /// Native type for the hash-function
type Fr: Copy + Eq + Default + From<Value> + Into<Value>; type Fr: Copy + Eq + Default + From<Value> + Into<Value>;
/// Creates new hash-function instance
fn new() -> Self;
/// Outputs the default leaf (Fr::default()) /// Outputs the default leaf (Fr::default())
fn default_leaf() -> Self::Fr; fn default_leaf() -> Self::Fr;

View File

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