[][src]Crate merkle_tree_stream

Example

use merkle_tree_stream::{DefaultNode, HashMethods, MerkleTreeStream, Node, PartialNode, NodeKind};
use std::sync::Arc;
use std::vec::Vec;

struct XorHashMethods;
impl HashMethods for XorHashMethods {
  type Node = DefaultNode;
  type Hash = Vec<u8>;

  fn leaf(&self, leaf: &PartialNode, _roots: &[Arc<Self::Node>]) -> Self::Hash {
    // bitwise XOR the data into u8
    let hash = match leaf.data() {
      NodeKind::Parent => 0,
      NodeKind::Leaf(data) => data.iter().fold(0, |acc, x| acc ^ x),
    };
    vec![hash]
  }

  fn parent(&self, a: &Self::Node, b: &Self::Node) -> Self::Hash {
    let hash = Node::hash(a).iter().chain(Node::hash(b).iter()).fold(0, |acc, x| acc ^ x);
    vec![hash]
  }
}

let mut mts = MerkleTreeStream::new(XorHashMethods, Vec::new());
let mut nodes = Vec::new();
mts.next(b"hello", &mut nodes);
mts.next(b"hashed", &mut nodes);
mts.next(b"world", &mut nodes);

Structs

DefaultNode

Node representation.

MerkleTreeStream

A stream that generates a merkle tree based on the incoming data.

NodeParts

The parts that make up a full Node from a PartialNode

PartialNode

Intermediate Node representation. Same as Node, but without the .hash field.

Enums

NodeKind

Custom Option type that encodes the presence or absense of data at this node

Traits

HashMethods

Functions that need to be implemented for MerkleTreeStream.

Node

Functions that need to be implemented for the Data that MerkleTreeStream works with.