Skip to content

Commit

Permalink
Update readme example
Browse files Browse the repository at this point in the history
  • Loading branch information
TheQuantumPhysicist committed Apr 18, 2024
1 parent d27a523 commit e905a3c
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ You can find examples for how to get started with this library in the [examples

```rust
use blake2::{digest::typenum, Digest};
use merkletree::{hasher::PairHasher, tree::MerkleTree};
use merkletree_mintlayer::{hasher::PairHasher, tree::MerkleTree};

// You can use any hashing function you like, we use blake2b here as an example
type Blake2bHasher = blake2::Blake2b<typenum::U32>;
Expand Down Expand Up @@ -61,16 +61,16 @@ impl HashAlgo {

// This is the important part, your hasher has to implement PairHasher
impl PairHasher for HashAlgo {
type Type = TreeNode;
type NodeType = TreeNode;

fn hash_pair(left: &Self::Type, right: &Self::Type) -> Self::Type {
fn hash_pair(left: &Self::NodeType, right: &Self::NodeType) -> Self::NodeType {
let mut h = Blake2bHasher::new();
Digest::update(&mut h, left);
Digest::update(&mut h, right);
h.finalize_reset().into()
}

fn hash_single(data: &Self::Type) -> Self::Type {
fn hash_single(data: &Self::NodeType) -> Self::NodeType {
let mut h = Blake2bHasher::new();
Digest::update(&mut h, data);
h.finalize_reset().into()
Expand All @@ -79,14 +79,14 @@ impl PairHasher for HashAlgo {

fn main() {
// You have to hash the leaves or create them (any way you like)
let leaf1 = hash_data("0");
let leaf2 = hash_data("1");
let leaf3 = hash_data("2");
let leaf4 = hash_data("3");
let leaf0 = hash_data("0");
let leaf1 = hash_data("1");
let leaf2 = hash_data("2");
let leaf3 = hash_data("3");

// The tree is defined from a vector of leaves, from left to right
let tree =
MerkleTree::<TreeNode, HashAlgo>::from_leaves(vec![leaf1, leaf2, leaf3, leaf4]).unwrap();
MerkleTree::<TreeNode, HashAlgo>::from_leaves(vec![leaf0, leaf1, leaf2, leaf3]).unwrap();

// Let's get the root
let tree_root = tree.root();
Expand All @@ -102,12 +102,12 @@ fn main() {

// We attempt to recreate the expected root manually
let mut node10 = HashAlgo::new();
node10.write(leaf0);
node10.write(leaf1);
node10.write(leaf2);

let mut node11 = HashAlgo::new();
node11.write(leaf2);
node11.write(leaf3);
node11.write(leaf4);

let mut node00 = HashAlgo::new();
let n10 = node10.finalize();
Expand Down

0 comments on commit e905a3c

Please sign in to comment.