#[repr(C)]
pub struct ConcurrentMerkleTree<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> { pub sequence_number: u64, pub active_index: u64, pub buffer_size: u64, pub change_logs: [ChangeLog<MAX_DEPTH>; MAX_BUFFER_SIZE], pub rightmost_proof: Path<MAX_DEPTH>, }
Expand description

Conurrent Merkle Tree is a Merkle Tree that allows multiple tree operations targeted for the same tree root to succeed.

In a normal merkle tree, only the first tree operation will succeed because the following operations will have proofs for the unmodified tree state. ConcurrentMerkleTree avoids this by storing a buffer of modified nodes (change_logs) which allows it to implement fast-forwarding of concurrent merkle tree operations.

As long as the concurrent merkle tree operations have proofs that are valid for a previous state of the tree that can be found in the stored buffer, that tree operation’s proof can be fast-forwarded and the tree operation can be applied.

There are two primitive operations for Concurrent Merkle Trees: set_leaf and append. Setting a leaf value requires passing a proof to perform that tree operation, but appending does not require a proof.

An additional key property of ConcurrentMerkleTree is support for append operations, which do not require any proofs to be passed. This is accomplished by keeping track of the proof to the rightmost leaf in the tree (rightmost_proof).

Fields§

§sequence_number: u64§active_index: u64

Index of most recent root & changes

§buffer_size: u64

Number of active changes we are tracking

§change_logs: [ChangeLog<MAX_DEPTH>; MAX_BUFFER_SIZE]

Proof for respective root

§rightmost_proof: Path<MAX_DEPTH>

Implementations§

source§

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

source

pub fn new() -> Self

source

pub fn is_initialized(&self) -> bool

source

pub fn initialize(&mut self) -> Result<Node, ConcurrentMerkleTreeError>

This is the trustless initialization method that should be used in most cases.

source

pub fn initialize_with_root( &mut self, root: Node, rightmost_leaf: Node, proof_vec: &[Node], index: u32 ) -> Result<Node, ConcurrentMerkleTreeError>

This is a trustful initialization method that assumes the root contains the expected leaves.

At the time of this crate’s publishing, there is no supported way to efficiently verify a pre-initialized root on-chain. Using this method before having a method for on-chain verification will prevent other applications from indexing the leaf data stored in this tree.

source

pub fn prove_tree_is_empty(&self) -> Result<(), ConcurrentMerkleTreeError>

Errors if one of the leaves of the current merkle tree is non-EMPTY

source

pub fn get_root(&self) -> [u8; 32]

Returns the current root of the merkle tree

source

pub fn get_change_log(&self) -> Box<ChangeLog<MAX_DEPTH>>

Returns the most recent changelog

source

pub fn prove_leaf( &self, current_root: Node, leaf: Node, proof_vec: &[Node], leaf_index: u32 ) -> Result<(), ConcurrentMerkleTreeError>

This method will fail if the leaf cannot be proven to exist in the current tree root.

This method will attempts to prove the leaf first using the proof nodes provided. However if this fails, then a proof will be constructed by inferring a proof from the changelog buffer.

Note: this is not the same as verifying that a (proof, leaf) combination is valid for the given root. That functionality is provided by check_valid_proof.

source

pub fn append(&mut self, node: Node) -> Result<Node, ConcurrentMerkleTreeError>

Appending a non-empty Node will always succeed .

source

pub fn fill_empty_or_append( &mut self, current_root: Node, leaf: Node, proof_vec: &[Node], index: u32 ) -> Result<Node, ConcurrentMerkleTreeError>

Convenience function for set_leaf

This method will set_leaf if the leaf at index is an empty node, otherwise it will append the new leaf.

source

pub fn set_leaf( &mut self, current_root: Node, previous_leaf: Node, new_leaf: Node, proof_vec: &[Node], index: u32 ) -> Result<Node, ConcurrentMerkleTreeError>

This method will update the leaf at index.

However if the proof cannot be verified, this method will fail.

source

pub fn get_seq(&self) -> u64

Returns the Current Seq of the tree, the seq is the monotonic counter of the tree operations that is incremented every time a mutable operation is performed on the tree.

source

pub fn check_valid_proof( &self, leaf: Node, proof: &[Node; MAX_DEPTH], leaf_index: u32 ) -> bool

Checks that the proof provided is valid for the current root.

Trait Implementations§

source§

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> Clone for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

source§

fn clone(&self) -> ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

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<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> Default for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

source§

fn default() -> Self

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

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> Zeroable for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

source§

fn zeroed() -> Self

source§

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> Copy for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

source§

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> Pod for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

Auto Trait Implementations§

§

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> RefUnwindSafe for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

§

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> Send for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

§

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> Sync for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

§

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> Unpin for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

§

impl<const MAX_DEPTH: usize, const MAX_BUFFER_SIZE: usize> UnwindSafe for ConcurrentMerkleTree<MAX_DEPTH, MAX_BUFFER_SIZE>

Blanket Implementations§

§

impl<T> AbiExample for T

§

default fn example() -> T

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CheckedBitPattern for Twhere T: AnyBitPattern,

§

type Bits = T

Self must have the same layout as the specified Bits except for the possible invalid bit patterns being checked during is_valid_bit_pattern.
source§

fn is_valid_bit_pattern(_bits: &T) -> bool

If this function returns true, then it must be valid to reinterpret bits as &Self.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> AnyBitPattern for Twhere T: Pod,

source§

impl<T> NoUninit for Twhere T: Pod,