mirror of https://github.com/vacp2p/pmtree.git
docs: add usage example
This commit is contained in:
parent
3095ec2b73
commit
75aa714030
74
README.md
74
README.md
|
@ -22,18 +22,68 @@ To use batch insertions you must enable `batch_insert` feature:
|
||||||
pmtree = { git = "https://github.com/Rate-Limiting-Nullifier/pmtree", features = ["batch_insert"] }
|
pmtree = { git = "https://github.com/Rate-Limiting-Nullifier/pmtree", features = ["batch_insert"] }
|
||||||
```
|
```
|
||||||
|
|
||||||
## Clone & Build
|
## Example
|
||||||
```bash
|
|
||||||
git clone git@github.com:Rate-Limiting-Nullifier/pmtree.git && cd pmtree
|
|
||||||
cargo build --release
|
|
||||||
```
|
|
||||||
|
|
||||||
## Run tests
|
In-Memory DB (HashMap) + Keccak
|
||||||
```bash
|
```rust
|
||||||
cargo test --release
|
struct MemoryDB(HashMap<DBKey, Value>);
|
||||||
```
|
struct MyKeccak(Keccak);
|
||||||
|
|
||||||
## Docs
|
impl Database for MemoryDB {
|
||||||
```bash
|
fn new(_dbpath: &str) -> Result<Self> {
|
||||||
cargo docs --open
|
Ok(MemoryDB(HashMap::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load(_dbpath: &str) -> Result<Self> {
|
||||||
|
Err(Error("Cannot load in-memory DB".to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get(&self, key: DBKey) -> Result<Option<Value>> {
|
||||||
|
Ok(self.0.get(&key).cloned())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn put(&mut self, key: DBKey, value: Value) -> Result<()> {
|
||||||
|
self.0.insert(key, value);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Hasher for MyKeccak {
|
||||||
|
type Fr = [u8; 32];
|
||||||
|
|
||||||
|
fn default_leaf() -> Self::Fr {
|
||||||
|
[0; 32]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize(value: Self::Fr) -> Value {
|
||||||
|
value.to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize(value: Value) -> Self::Fr {
|
||||||
|
value.try_into().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash(input: &[Self::Fr]) -> Self::Fr {
|
||||||
|
let mut output = [0; 32];
|
||||||
|
let mut hasher = Keccak::v256();
|
||||||
|
for element in input {
|
||||||
|
hasher.update(element);
|
||||||
|
}
|
||||||
|
hasher.finalize(&mut output);
|
||||||
|
output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut mt = MerkleTree::<MemoryDB, MyKeccak>::new(2, "abacaba").unwrap();
|
||||||
|
|
||||||
|
assert_eq!(mt.capacity(), 4);
|
||||||
|
assert_eq!(mt.depth(), 2);
|
||||||
|
|
||||||
|
mt.update_next(hex!(
|
||||||
|
"c1ba1812ff680ce84c1d5b4f1087eeb08147a4d510f3496b2849df3a73f5af95"
|
||||||
|
))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in New Issue