Struct MerkleTree

Source
pub struct MerkleTree<H: Hasher = Keccak256> {
    pub root: MerkleRoot<H>,
    pub nodes: Vec<H::Hash>,
}
Expand description

A MerkleTree is a binary tree where each node is the hash of its children.

Fields§

§root: MerkleRoot<H>§nodes: Vec<H::Hash>

Implementations§

Source§

impl<H: Hasher> MerkleTree<H>

Implement a MerkleTree-specific interface for interacting with trees.

Source

pub fn new(items: &[&[u8]]) -> Option<Self>

Construct a new MerkleTree from a list of byte slices.

This list does not have to be a set which means the tree may contain duplicate items. It is up to the caller to enforce a strict set-like object if that is desired.

Source

pub fn find_path(&self, index: usize) -> MerklePath<H>

Produces a Proof of membership for an index in the tree.

Source

pub fn verify_path(&self, proof: MerklePath<H>, item: &[u8]) -> bool

Check if a given MerklePath is a valid proof for a corresponding item.

Source

pub fn hash_leaf(leaf: &[u8]) -> H::Hash

Source

pub fn hash_node(l: &H::Hash, r: &H::Hash) -> H::Hash

Source

pub fn hash_null() -> H::Hash

Source

pub fn serialize(&self, slot: u64, ring_size: u32) -> Vec<u8>

Serialize a MerkleTree into a Vec.

Layout:

4 bytes:  magic number
1 byte:   update type
4 byte:   storage id
32 bytes: root hash

TODO: This code does not belong to MerkleTree, we should be using the wire data types in calling code to wrap this value.

Trait Implementations§

Source§

impl<'a, H: Hasher + 'a> Accumulator<'a> for MerkleTree<H>

Presents an Accumulator friendly interface for MerkleTree.

Source§

fn from_set(items: impl Iterator<Item = &'a [u8]>) -> Option<Self>

Construct a MerkleTree from an iterator of items.

Source§

fn prove(&'a self, item: &[u8]) -> Option<Self::Proof>

Prove an item is in the tree by returning a MerklePath.

Source§

type Proof = MerklePath<H>

Source§

fn check(&'a self, proof: Self::Proof, item: &[u8]) -> bool

Verify an item is a member of the accumulator.
Source§

impl<H: Hasher> BorshDeserialize for MerkleTree<H>

Source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>

Source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
Source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
Source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

Source§

impl<H: Hasher> BorshSerialize for MerkleTree<H>

Source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

Source§

fn try_to_vec(&self) -> Result<Vec<u8>, Error>

Serialize this instance into a vector of bytes.
Source§

impl<H: Clone + Hasher> Clone for MerkleTree<H>
where H::Hash: Clone,

Source§

fn clone(&self) -> MerkleTree<H>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<H: Debug + Hasher> Debug for MerkleTree<H>
where H::Hash: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<H: Default + Hasher> Default for MerkleTree<H>
where H::Hash: Default,

Source§

fn default() -> MerkleTree<H>

Returns the “default value” for a type. Read more
Source§

impl<'de, H> Deserialize<'de> for MerkleTree<H>
where H: Deserialize<'de> + Hasher,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<H: PartialEq + Hasher> PartialEq for MerkleTree<H>
where H::Hash: PartialEq,

Source§

fn eq(&self, other: &MerkleTree<H>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<H> Serialize for MerkleTree<H>
where H: Serialize + Hasher,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<H: Eq + Hasher> Eq for MerkleTree<H>
where H::Hash: Eq,

Source§

impl<H: Hasher> StructuralPartialEq for MerkleTree<H>

Auto Trait Implementations§

§

impl<H> Freeze for MerkleTree<H>
where <H as Hasher>::Hash: Freeze,

§

impl<H> RefUnwindSafe for MerkleTree<H>
where <H as Hasher>::Hash: RefUnwindSafe,

§

impl<H> Send for MerkleTree<H>
where <H as Hasher>::Hash: Send,

§

impl<H> Sync for MerkleTree<H>
where <H as Hasher>::Hash: Sync,

§

impl<H> Unpin for MerkleTree<H>
where <H as Hasher>::Hash: Unpin,

§

impl<H> UnwindSafe for MerkleTree<H>
where <H as Hasher>::Hash: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,