kelvin

A merkle-tree toolkit and backend.

Kelvin is designed to make designing and using Merkle Tree structures a breeze.

Here is an example of all you need to construct a program state that can be persisted as a Merkle Tree. (taken from examples/simple)

```rust use std::io;

use kelvin::{Blake2b, ByteHash, Content, Map, Root, Sink, Source}; use kelvin_btree::BTree;

[derive(Clone)]

struct State { mapa: BTree, mapb: BTree, counter: u64, }

// The initial root state impl Default for State { fn default() -> Self { // Set up a default kv for mapa: let mut mapa = BTree::new(); mapa .insert("Hello".into(), "World".into()) .expect("in memory"); State { mapa, map_b: BTree::default(), counter: 0, } } }

impl Content for State { fn persist(&mut self, sink: &mut Sink) -> io::Result<()> { self.mapa.persist(sink)?; self.mapb.persist(sink)?; self.counter.persist(sink) }

fn restore(source: &mut Source<H>) -> io::Result<Self> {
    Ok(State {
        map_a: BTree::restore(source)?,
        map_b: BTree::restore(source)?,
        counter: u64::restore(source)?,
    })
}

}

fn main() -> io::Result<()> { let mut root = Root::<_, Blake2b>::new("/tmp/kelvin-example")?;

let mut state: State<_> = root.restore()?;

match state.map_a.get(&"Foo".to_owned())? {
    Some(path) => println!("Foo is {}", *path),
    None => println!("Foo is `None`"),
}

println!("Counter is {}", state.counter);

state.counter += 1;
state.map_a.insert(
    "Foo".into(),
    format!("Bar {}", state.counter * state.counter),
)?;

root.set_root(&mut state)?;

Ok(())

} ```