fuel_merkle::binary

Struct MerkleTree

source
pub struct MerkleTree<TableType, StorageType> { /* private fields */ }

Implementations§

source§

impl<TableType, StorageType> MerkleTree<TableType, StorageType>

source

pub const fn empty_root() -> &'static Bytes32

source

pub fn root(&self) -> Bytes32

source

pub fn leaves_count(&self) -> u64

source§

impl<TableType, StorageType, StorageError> MerkleTree<TableType, StorageType>
where TableType: Mappable<Key = u64, Value = Primitive, OwnedValue = Primitive>, StorageType: StorageInspect<TableType, Error = StorageError>,

source

pub fn new(storage: StorageType) -> Self

source

pub fn load( storage: StorageType, leaves_count: u64, ) -> Result<Self, MerkleTreeError<StorageError>>

A binary Merkle tree can be built from a collection of Merkle Mountain Range (MMR) peaks. The MMR structure can be accurately defined by the number of leaves in the leaf row.

Consider a binary Merkle tree with seven leaves, producing the following MMR structure:

      03
     /  \
    /    \
  01      05      09
 /  \    /  \    /  \
00  02  04  06  08  10  12

We observe that the tree has three peaks at positions 03, 09, and 12. These peak positions are recorded in the order that they appear, reading left to right in the tree structure, and only descend in height. These peak positions communicate everything needed to determine the remaining internal nodes building upwards to the root position:

           07
          /  \
         /    \
        /      \
       /        \
      /          \
     /            \
   03              11
  /  \            /  \
...  ...         /    \
               09      \
              /  \      \
            ...  ...    12

No additional intermediate nodes or leaves are required to calculate the root position.

The positions of the MMR peaks can be deterministically calculated as a function of n + 1 where n is the number of leaves in the tree. By appending an additional leaf node to the tree, we generate a new tree structure with additional internal nodes (N.B.: this may also change the root position if the tree is already balanced).

In our example, we add an additional leaf at leaf index 7 (in-order index 14):

           07
          /  \
         /    \
        /      \
       /        \
      /          \
     /            \
   03              11
  /  \            /  \
...  ...         /    \
               09      13
              /  \    /  \
            ...  ... 12  14

We observe that the path from the root position to our new leaf position yields a set of side positions that includes our original peak positions (see Path Iterator):

Path positionSide position
0707
1103
1309
1412

By excluding the root position 07, we have established the set of side positions 03, 09, and 12, matching our set of MMR peaks.

source

pub fn prove( &self, proof_index: u64, ) -> Result<(Bytes32, ProofSet), MerkleTreeError<StorageError>>

source

pub fn reset(&mut self)

source§

impl<TableType, StorageType, StorageError> MerkleTree<TableType, StorageType>
where TableType: Mappable<Key = u64, Value = Primitive, OwnedValue = Primitive>, StorageType: StorageMutate<TableType, Error = StorageError>,

source

pub fn push(&mut self, data: &[u8]) -> Result<(), MerkleTreeError<StorageError>>

Adds a new leaf node to the tree.

§WARNING

This code might modify the storage, and then return an error. TODO: fix this issue

Trait Implementations§

source§

impl<TableType: Clone, StorageType: Clone> Clone for MerkleTree<TableType, StorageType>

source§

fn clone(&self) -> MerkleTree<TableType, StorageType>

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<TableType: Debug, StorageType: Debug> Debug for MerkleTree<TableType, StorageType>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<TableType, StorageType> Freeze for MerkleTree<TableType, StorageType>
where StorageType: Freeze,

§

impl<TableType, StorageType> RefUnwindSafe for MerkleTree<TableType, StorageType>
where StorageType: RefUnwindSafe, TableType: RefUnwindSafe,

§

impl<TableType, StorageType> Send for MerkleTree<TableType, StorageType>
where StorageType: Send, TableType: Send,

§

impl<TableType, StorageType> Sync for MerkleTree<TableType, StorageType>
where StorageType: Sync, TableType: Sync,

§

impl<TableType, StorageType> Unpin for MerkleTree<TableType, StorageType>
where StorageType: Unpin, TableType: Unpin,

§

impl<TableType, StorageType> UnwindSafe for MerkleTree<TableType, StorageType>
where StorageType: UnwindSafe, TableType: 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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> StorageAsMut for T

source§

fn storage<Type>(&mut self) -> StorageMut<'_, Self, Type>
where Type: Mappable,

source§

fn storage_as_mut<Type>(&mut self) -> StorageMut<'_, Self, Type>
where Type: Mappable,

source§

impl<T> StorageAsRef for T

source§

fn storage<Type>(&self) -> StorageRef<'_, Self, Type>
where Type: Mappable,

source§

fn storage_as_ref<Type>(&self) -> StorageRef<'_, Self, Type>
where Type: Mappable,

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.