[−][src]Struct merkle_tree_stream::MerkleTreeStream
A stream that generates a merkle tree based on the incoming data.
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); /// Constructed tree: /// /// 0(hello)-──┐ /// 1 /// 2(hashed)──┘ /// /// 4(world) let xor_hello = b"hello".iter().fold(0, |acc, x| { acc ^ x }); let xor_hashed = b"hashed".iter().fold(0, |acc, x| { acc ^ x }); let xor_world = b"world".iter().fold(0, |acc, x| { acc ^ x }); assert_eq!(nodes[0].index, 0); assert_eq!(nodes[0].parent, 1); assert_eq!(nodes[0].length, 5); assert_eq!(nodes[0].data, Some(b"hello".to_vec())); assert_eq!(nodes[0].hash, vec![xor_hello]); assert_eq!(nodes[1].index, 2); assert_eq!(nodes[1].parent, 1); assert_eq!(nodes[1].length, 6); assert_eq!(nodes[1].data, Some(b"hashed".to_vec())); assert_eq!(nodes[1].hash, vec![xor_hashed]); assert_eq!(nodes[2].index, 1); assert_eq!(nodes[2].parent, 3); assert_eq!(nodes[2].length, 11); assert_eq!(nodes[2].data, None); assert_eq!(nodes[2].hash, vec![xor_hello ^ xor_hashed]); assert_eq!(nodes[3].index, 4); assert_eq!(nodes[3].parent, 5); assert_eq!(nodes[3].length, 5); assert_eq!(nodes[3].data, Some(b"world".to_vec())); assert_eq!(nodes[3].hash, vec![xor_world]); assert_eq!(mts.roots().len(), 2); assert_eq!(mts.roots()[0].index, 1); assert_eq!(mts.roots()[1].index, 4);
Implementations
impl<H: HashMethods> MerkleTreeStream<H>
[src]
pub fn new(handler: H, roots: Vec<Arc<H::Node>>) -> MerkleTreeStream<H>
[src]
Create a new MerkleTreeStream instance.
pub fn next<'a>(&mut self, data: &[u8], nodes: &'a mut Vec<Arc<H::Node>>)
[src]
Pass a string buffer through the flat-tree hash functions, and write the result back out to "nodes".
pub fn roots(&self) -> &Vec<Arc<H::Node>>
[src]
Get the roots vector.
pub fn blocks(&self) -> u64
[src]
Get number of blocks
Trait Implementations
impl<T: Debug + HashMethods> Debug for MerkleTreeStream<T> where
T::Node: Debug,
[src]
T::Node: Debug,
Auto Trait Implementations
impl<T> RefUnwindSafe for MerkleTreeStream<T> where
T: RefUnwindSafe,
<T as HashMethods>::Node: RefUnwindSafe,
T: RefUnwindSafe,
<T as HashMethods>::Node: RefUnwindSafe,
impl<T> Send for MerkleTreeStream<T> where
T: Send,
<T as HashMethods>::Node: Send + Sync,
T: Send,
<T as HashMethods>::Node: Send + Sync,
impl<T> Sync for MerkleTreeStream<T> where
T: Sync,
<T as HashMethods>::Node: Send + Sync,
T: Sync,
<T as HashMethods>::Node: Send + Sync,
impl<T> Unpin for MerkleTreeStream<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for MerkleTreeStream<T> where
T: UnwindSafe,
<T as HashMethods>::Node: RefUnwindSafe,
T: UnwindSafe,
<T as HashMethods>::Node: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,