2022-11-01 09:08:27 +00:00
|
|
|
use hex_literal::hex;
|
2023-07-28 08:39:54 +00:00
|
|
|
use vacp2p_pmtree::*;
|
2022-11-01 09:08:27 +00:00
|
|
|
use std::collections::HashMap;
|
2022-11-01 17:24:31 +00:00
|
|
|
use tiny_keccak::{Hasher as _, Keccak};
|
2022-11-01 09:08:27 +00:00
|
|
|
|
2022-11-02 10:45:49 +00:00
|
|
|
struct MemoryDB(HashMap<DBKey, Value>);
|
|
|
|
struct MyKeccak(Keccak);
|
2022-11-01 09:08:27 +00:00
|
|
|
|
2023-02-10 10:52:45 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct MemoryDBConfig;
|
|
|
|
|
2022-11-01 09:08:27 +00:00
|
|
|
impl Database for MemoryDB {
|
2023-02-10 10:52:45 +00:00
|
|
|
type Config = MemoryDBConfig;
|
|
|
|
|
2023-04-18 08:52:38 +00:00
|
|
|
fn new(_db_config: MemoryDBConfig) -> PmtreeResult<Self> {
|
2022-11-06 12:25:19 +00:00
|
|
|
Ok(MemoryDB(HashMap::new()))
|
2022-11-01 09:08:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:52:38 +00:00
|
|
|
fn load(_db_config: MemoryDBConfig) -> PmtreeResult<Self> {
|
2023-04-18 09:00:03 +00:00
|
|
|
Err(PmtreeErrorKind::DatabaseError(
|
|
|
|
DatabaseErrorKind::CannotLoadDatabase,
|
|
|
|
))
|
2022-11-01 09:08:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:52:38 +00:00
|
|
|
fn get(&self, key: DBKey) -> PmtreeResult<Option<Value>> {
|
2022-11-06 12:25:19 +00:00
|
|
|
Ok(self.0.get(&key).cloned())
|
2022-11-01 09:08:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 08:52:38 +00:00
|
|
|
fn put(&mut self, key: DBKey, value: Value) -> PmtreeResult<()> {
|
2022-11-01 17:24:31 +00:00
|
|
|
self.0.insert(key, value);
|
2022-11-06 12:25:19 +00:00
|
|
|
|
|
|
|
Ok(())
|
2022-11-01 09:08:27 +00:00
|
|
|
}
|
2023-02-02 10:31:18 +00:00
|
|
|
|
2023-04-18 08:52:38 +00:00
|
|
|
fn put_batch(&mut self, subtree: HashMap<DBKey, Value>) -> PmtreeResult<()> {
|
2023-02-02 11:47:19 +00:00
|
|
|
self.0.extend(subtree.into_iter());
|
|
|
|
|
2023-02-02 10:31:18 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-28 08:33:50 +00:00
|
|
|
|
|
|
|
fn close(&mut self) -> PmtreeResult<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-01 09:08:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 18:50:10 +00:00
|
|
|
impl Hasher for MyKeccak {
|
|
|
|
type Fr = [u8; 32];
|
2022-11-01 09:08:27 +00:00
|
|
|
|
2022-11-30 18:50:10 +00:00
|
|
|
fn default_leaf() -> Self::Fr {
|
|
|
|
[0; 32]
|
2022-11-01 09:08:27 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 18:50:10 +00:00
|
|
|
fn serialize(value: Self::Fr) -> Value {
|
|
|
|
value.to_vec()
|
|
|
|
}
|
2022-11-01 09:08:27 +00:00
|
|
|
|
2022-11-30 18:50:10 +00:00
|
|
|
fn deserialize(value: Value) -> Self::Fr {
|
|
|
|
value.try_into().unwrap()
|
2022-11-01 09:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn hash(input: &[Self::Fr]) -> Self::Fr {
|
2022-11-01 17:24:31 +00:00
|
|
|
let mut output = [0; 32];
|
|
|
|
let mut hasher = Keccak::v256();
|
|
|
|
for element in input {
|
2022-11-30 18:50:10 +00:00
|
|
|
hasher.update(element);
|
2022-11-01 17:24:31 +00:00
|
|
|
}
|
|
|
|
hasher.finalize(&mut output);
|
2022-11-30 18:50:10 +00:00
|
|
|
output
|
2022-11-01 09:08:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-04-18 08:52:38 +00:00
|
|
|
fn insert_delete() -> PmtreeResult<()> {
|
2023-02-10 10:52:45 +00:00
|
|
|
let mut mt = MerkleTree::<MemoryDB, MyKeccak>::new(2, MemoryDBConfig)?;
|
2022-11-01 18:01:35 +00:00
|
|
|
|
|
|
|
assert_eq!(mt.capacity(), 4);
|
|
|
|
assert_eq!(mt.depth(), 2);
|
|
|
|
|
|
|
|
let leaves = [
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000003"),
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000004"),
|
|
|
|
];
|
|
|
|
|
|
|
|
let default_tree_root =
|
|
|
|
hex!("b4c11951957c6f8f642c4af61cd6b24640fec6dc7fc607ee8206a99e92410d30");
|
|
|
|
|
2022-11-30 18:50:10 +00:00
|
|
|
assert_eq!(mt.root(), default_tree_root);
|
2022-11-01 18:01:35 +00:00
|
|
|
|
|
|
|
let roots = [
|
|
|
|
hex!("c1ba1812ff680ce84c1d5b4f1087eeb08147a4d510f3496b2849df3a73f5af95"),
|
|
|
|
hex!("893760ec5b5bee236f29e85aef64f17139c3c1b7ff24ce64eb6315fca0f2485b"),
|
|
|
|
hex!("222ff5e0b5877792c2bc1670e2ccd0c2c97cd7bb1672a57d598db05092d3d72c"),
|
|
|
|
hex!("a9bb8c3f1f12e9aa903a50c47f314b57610a3ab32f2d463293f58836def38d36"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for i in 0..leaves.len() {
|
2022-11-30 18:50:10 +00:00
|
|
|
mt.update_next(leaves[i])?;
|
|
|
|
assert_eq!(mt.root(), roots[i]);
|
2022-11-01 18:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 12:36:17 +00:00
|
|
|
for (i, &leaf) in leaves.iter().enumerate() {
|
2022-11-30 18:50:10 +00:00
|
|
|
assert!(mt.verify(&leaf, &mt.proof(i)?));
|
2022-11-08 12:36:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 18:01:35 +00:00
|
|
|
for i in (0..leaves.len()).rev() {
|
2022-11-06 12:25:19 +00:00
|
|
|
mt.delete(i)?;
|
2022-11-01 18:01:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 18:50:10 +00:00
|
|
|
assert_eq!(mt.root(), default_tree_root);
|
2022-11-06 12:25:19 +00:00
|
|
|
|
2022-11-30 18:50:10 +00:00
|
|
|
assert!(mt.update_next(leaves[0]).is_err());
|
2022-11-06 12:25:19 +00:00
|
|
|
|
|
|
|
Ok(())
|
2022-11-01 18:01:35 +00:00
|
|
|
}
|
2023-02-02 11:47:19 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-04-18 08:52:38 +00:00
|
|
|
fn batch_insertions() -> PmtreeResult<()> {
|
2023-02-10 10:52:45 +00:00
|
|
|
let mut mt = MerkleTree::<MemoryDB, MyKeccak>::new(2, MemoryDBConfig)?;
|
2023-02-02 11:47:19 +00:00
|
|
|
|
|
|
|
let leaves = [
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000003"),
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000004"),
|
|
|
|
];
|
|
|
|
|
2023-04-24 12:48:59 +00:00
|
|
|
mt.batch_insert(None, &leaves)?;
|
2023-02-02 11:47:19 +00:00
|
|
|
|
2023-02-02 16:24:58 +00:00
|
|
|
assert_eq!(
|
|
|
|
mt.root(),
|
|
|
|
hex!("a9bb8c3f1f12e9aa903a50c47f314b57610a3ab32f2d463293f58836def38d36")
|
|
|
|
);
|
|
|
|
|
2023-02-02 11:47:19 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-04-24 12:48:59 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-04-24 12:53:27 +00:00
|
|
|
fn set_range() -> PmtreeResult<()> {
|
2023-04-24 12:48:59 +00:00
|
|
|
let mut mt = MerkleTree::<MemoryDB, MyKeccak>::new(2, MemoryDBConfig)?;
|
|
|
|
|
|
|
|
let leaves = [
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000001"),
|
|
|
|
hex!("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
];
|
|
|
|
|
2023-04-25 06:55:50 +00:00
|
|
|
mt.set_range(2, leaves)?;
|
2023-04-24 12:48:59 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
mt.root(),
|
|
|
|
hex!("1e9f6c8d3fd5b7ae3a29792adb094c6d4cc6149d0c81c8c8e57cf06c161a92b8")
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|