alloy_trie::hash_builder

Struct HashBuilder

Source
pub struct HashBuilder {
    pub key: Nibbles,
    pub value: HashBuilderValue,
    pub stack: Vec<RlpNode>,
    pub groups: Vec<TrieMask>,
    pub tree_masks: Vec<TrieMask>,
    pub hash_masks: Vec<TrieMask>,
    pub stored_in_database: bool,
    pub updated_branch_nodes: Option<HashMap<Nibbles, BranchNodeCompact>>,
    pub proof_retainer: Option<ProofRetainer>,
    pub rlp_buf: Vec<u8>,
}
Expand description

A component used to construct the root hash of the trie.

The primary purpose of a Hash Builder is to build the Merkle proof that is essential for verifying the integrity and authenticity of the trie’s contents. It achieves this by constructing the root hash from the hashes of child nodes according to specific rules, depending on the type of the node (branch, extension, or leaf).

Here’s an overview of how the Hash Builder works for each type of node:

  • Branch Node: The Hash Builder combines the hashes of all the child nodes of the branch node, using a cryptographic hash function like SHA-256. The child nodes’ hashes are concatenated and hashed, and the result is considered the hash of the branch node. The process is repeated recursively until the root hash is obtained.
  • Extension Node: In the case of an extension node, the Hash Builder first encodes the node’s shared nibble path, followed by the hash of the next child node. It concatenates these values and then computes the hash of the resulting data, which represents the hash of the extension node.
  • Leaf Node: For a leaf node, the Hash Builder first encodes the key-path and the value of the leaf node. It then concatenates the encoded key-path and value, and computes the hash of this concatenated data, which represents the hash of the leaf node.

The Hash Builder operates recursively, starting from the bottom of the trie and working its way up, combining the hashes of child nodes and ultimately generating the root hash. The root hash can then be used to verify the integrity and authenticity of the trie’s data by constructing and verifying Merkle proofs.

Fields§

§key: Nibbles§value: HashBuilderValue§stack: Vec<RlpNode>§groups: Vec<TrieMask>§tree_masks: Vec<TrieMask>§hash_masks: Vec<TrieMask>§stored_in_database: bool§updated_branch_nodes: Option<HashMap<Nibbles, BranchNodeCompact>>§proof_retainer: Option<ProofRetainer>§rlp_buf: Vec<u8>

Implementations§

Source§

impl HashBuilder

Source

pub fn with_updates(self, retain_updates: bool) -> Self

Enables the Hash Builder to store updated branch nodes.

Call HashBuilder::split to get the updates to branch nodes.

Source

pub fn with_proof_retainer(self, retainer: ProofRetainer) -> Self

Enable specified proof retainer.

Source

pub fn set_updates(&mut self, retain_updates: bool)

Enables the Hash Builder to store updated branch nodes.

Call HashBuilder::split to get the updates to branch nodes.

Source

pub fn split(self) -> (Self, HashMap<Nibbles, BranchNodeCompact>)

Splits the HashBuilder into a HashBuilder and hash builder updates.

Source

pub fn take_proof_nodes(&mut self) -> ProofNodes

Take and return retained proof nodes.

Source

pub fn updates_len(&self) -> usize

The number of total updates accrued. Returns 0 if Self::with_updates was not called.

Source

pub fn add_leaf(&mut self, key: Nibbles, value: &[u8])

Adds a new leaf element and its value to the trie hash builder.

§Panics

Panics if the new key does not come after the current key.

Source

pub fn add_leaf_unchecked(&mut self, key: Nibbles, value: &[u8])

Adds a new leaf element and its value to the trie hash builder, without checking the order of the new key. This is only for performance-critical usage that guarantees keys are inserted in sorted order.

Source

pub fn add_branch( &mut self, key: Nibbles, value: B256, stored_in_database: bool, )

Adds a new branch element and its hash to the trie hash builder.

Source

pub fn root(&mut self) -> B256

Returns the current root hash of the trie builder.

Trait Implementations§

Source§

impl Clone for HashBuilder

Source§

fn clone(&self) -> HashBuilder

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 Debug for HashBuilder

Source§

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

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

impl Default for HashBuilder

Source§

fn default() -> HashBuilder

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

Auto Trait Implementations§

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 u8)

🔬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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more