[−][src]Struct mohan::merkle::MerkleTree
Merkle Tree.
All leafs and nodes are stored in a linear array (vec).
A merkle tree is a tree in which every non-leaf node is the hash of its children nodes. A diagram depicting how it works:
root = h1234 = h(h12 + h34)
/ \
h12 = h(h1 + h2) h34 = h(h3 + h4)
/ \ / \
h1 = h(tx1) h2 = h(tx2) h3 = h(tx3) h4 = h(tx4)
In memory layout:
[h1 h2 h3 h4 h12 h34 root]
Merkle root is always the last element in the array.
The number of inputs is not always a power of two which results in a balanced tree structure as above. In that case, parent nodes with no children are also zero and parent nodes with only a single left node are calculated by concatenating the left node with itself before hashing. Since this function uses nodes that are pointers to the hashes, empty nodes will be nil.
TODO: Ord
Methods
impl<T: Element, A: Algorithm<T>, K: Store<T>> MerkleTree<T, A, K>
[src]
pub fn new<I: IntoIterator<Item = T>>(data: I) -> MerkleTree<T, A, K>
[src]
Creates new merkle from a sequence of hashes.
pub fn from_data<O: Hashable<A>, I: IntoIterator<Item = O>>(
data: I
) -> MerkleTree<T, A, K>
[src]
data: I
) -> MerkleTree<T, A, K>
Creates new merkle tree from a list of hashable objects.
pub fn from_data_with_store<I: IntoIterator<Item = T>>(
into: I,
leaves: K,
top_half: K
) -> MerkleTree<T, A, K>
[src]
into: I,
leaves: K,
top_half: K
) -> MerkleTree<T, A, K>
Creates new merkle from an already allocated Store
(used with
DiskMmapStore::new_with_path
to set its path before instantiating
the MT, which would otherwise just call DiskMmapStore::new
).
pub fn try_offload_store(&self) -> bool
[src]
pub fn gen_proof(&self, i: usize) -> Proof<T>
[src]
Generate merkle tree inclusion proof for leaf i
pub fn root(&self) -> T
[src]
Returns merkle root
pub fn len(&self) -> usize
[src]
Returns number of elements in the tree.
pub fn is_empty(&self) -> bool
[src]
Returns true
if the vector contains no elements.
pub fn height(&self) -> usize
[src]
Returns height of the tree
pub fn leafs(&self) -> usize
[src]
Returns original number of elements the tree was built upon.
pub fn read_at(&self, i: usize) -> T
[src]
Returns merkle root
pub fn read_range(&self, start: usize, end: usize) -> Vec<T>
[src]
pub fn read_into(&self, pos: usize, buf: &mut [u8])
[src]
Reads into a pre-allocated slice (for optimization purposes).
pub fn from_byte_slice(leafs: &[u8]) -> Self
[src]
Build the tree given a slice of all leafs, in bytes form.
Trait Implementations
impl<T: Clone, A: Clone, K: Clone> Clone for MerkleTree<T, A, K> where
T: Element,
A: Algorithm<T>,
K: Store<T>,
[src]
T: Element,
A: Algorithm<T>,
K: Store<T>,
fn clone(&self) -> MerkleTree<T, A, K>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T: Eq, A: Eq, K: Eq> Eq for MerkleTree<T, A, K> where
T: Element,
A: Algorithm<T>,
K: Store<T>,
[src]
T: Element,
A: Algorithm<T>,
K: Store<T>,
impl<T: PartialEq, A: PartialEq, K: PartialEq> PartialEq<MerkleTree<T, A, K>> for MerkleTree<T, A, K> where
T: Element,
A: Algorithm<T>,
K: Store<T>,
[src]
T: Element,
A: Algorithm<T>,
K: Store<T>,
fn eq(&self, other: &MerkleTree<T, A, K>) -> bool
[src]
fn ne(&self, other: &MerkleTree<T, A, K>) -> bool
[src]
impl<T: Debug, A: Debug, K: Debug> Debug for MerkleTree<T, A, K> where
T: Element,
A: Algorithm<T>,
K: Store<T>,
[src]
T: Element,
A: Algorithm<T>,
K: Store<T>,
impl<T: Element, A: Algorithm<T>, K: Store<T>> FromIterator<T> for MerkleTree<T, A, K>
[src]
fn from_iter<I: IntoIterator<Item = T>>(into: I) -> Self
[src]
Creates new merkle tree from an iterator over hashable objects.
impl<T: Element, A: Algorithm<T>, K: Store<T>> FromParallelIterator<T> for MerkleTree<T, A, K>
[src]
fn from_par_iter<I: IntoParallelIterator<Item = T>>(into: I) -> Self
[src]
Creates new merkle tree from an iterator over hashable objects.
Auto Trait Implementations
impl<T, A, K> Send for MerkleTree<T, A, K> where
A: Send,
A: Send,
impl<T, A, K> Sync for MerkleTree<T, A, K> where
A: Sync,
A: Sync,
impl<T, A, K> Unpin for MerkleTree<T, A, K> where
A: Unpin,
K: Unpin,
T: Unpin,
A: Unpin,
K: Unpin,
T: Unpin,
impl<T, A, K> UnwindSafe for MerkleTree<T, A, K> where
A: UnwindSafe,
K: UnwindSafe,
T: UnwindSafe,
A: UnwindSafe,
K: UnwindSafe,
T: UnwindSafe,
impl<T, A, K> RefUnwindSafe for MerkleTree<T, A, K> where
A: RefUnwindSafe,
K: RefUnwindSafe,
T: RefUnwindSafe,
A: RefUnwindSafe,
K: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
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>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
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> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<Q, K> Equivalent<K> for Q where
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
[src]
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
fn equivalent(&self, key: &K) -> bool
[src]
impl<T> Same<T> for T
type Output = T
Should always be Self