Skip to main content

HashBuilder

Struct HashBuilder 

pub struct HashBuilder<K = AddedRemovedKeys> {
    pub key: Nibbles,
    pub value: HashBuilderValue,
    pub stack: Vec<RlpNode>,
    pub state_masks: 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, DefaultHashBuilder>>,
    pub all_branch_nodes: bool,
    pub proof_retainer: Option<ProofRetainer<K>>,
    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>§state_masks: Vec<TrieMask>§tree_masks: Vec<TrieMask>§hash_masks: Vec<TrieMask>§stored_in_database: bool§updated_branch_nodes: Option<HashMap<Nibbles, BranchNodeCompact, DefaultHashBuilder>>§all_branch_nodes: bool§proof_retainer: Option<ProofRetainer<K>>§rlp_buf: Vec<u8>

Implementations§

§

impl<K> HashBuilder<K>

pub fn with_updates(self, retain_updates: bool) -> HashBuilder<K>

Enables the Hash Builder to store updated branch nodes.

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

pub fn with_proof_retainer<K2>( self, retainer: ProofRetainer<K2>, ) -> HashBuilder<K2>

Enable specified proof retainer.

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.

pub const fn with_all_branch_nodes( self, all_branch_nodes: bool, ) -> HashBuilder<K>

Sets whether updates include every branch node the Hash Builder computes.

By default, updates leave out branch nodes that are cheap to rebuild from their leaves, such as one whose children are all leaves. With this enabled, every computed branch node is included, the root too, and each parent’s tree mask marks it. Use this when the updates must describe the whole trie, such as when syncing it to a store that does not keep the leaves.

Has no effect unless updates are enabled with HashBuilder::with_updates.

pub const fn set_all_branch_nodes(&mut self, all_branch_nodes: bool)

Sets whether updates include every branch node the Hash Builder computes.

See HashBuilder::with_all_branch_nodes.

§

impl<K> HashBuilder<K>
where K: AsRef<AddedRemovedKeys>,

pub fn split( self, ) -> (HashBuilder<K>, HashMap<Nibbles, BranchNodeCompact, DefaultHashBuilder>)

Splits the HashBuilder into a HashBuilder and hash builder updates.

pub fn take_proof_nodes(&mut self) -> ProofNodes

Take and return retained proof nodes.

pub fn updates_len(&self) -> usize

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

pub fn print_stack(&self)

Available on crate feature std only.

Print the current stack of the Hash Builder.

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.

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.

pub fn add_branch( &mut self, key: Nibbles, value: FixedBytes<32>, stored_in_database: bool, )

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

pub fn root(&mut self) -> FixedBytes<32>

Returns the current root hash of the trie builder.

Trait Implementations§

§

impl<K> Clone for HashBuilder<K>
where K: Clone,

§

fn clone(&self) -> HashBuilder<K>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
§

impl<K> Debug for HashBuilder<K>
where K: Debug,

§

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

Formats the value using the given formatter. Read more
§

impl Default for HashBuilder

§

fn default() -> HashBuilder

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

impl From<HashBuilder> for HashBuilderState

§

fn from(state: HashBuilder) -> HashBuilderState

Converts to this type from the input type.
§

impl From<HashBuilderState> for HashBuilder

§

fn from(state: HashBuilderState) -> HashBuilder

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<K> Freeze for HashBuilder<K>
where Option<ProofRetainer<K>>: Freeze,

§

impl<K> RefUnwindSafe for HashBuilder<K>
where Option<ProofRetainer<K>>: RefUnwindSafe,

§

impl<K> Send for HashBuilder<K>
where Option<ProofRetainer<K>>: Send,

§

impl<K> Sync for HashBuilder<K>
where Option<ProofRetainer<K>>: Sync,

§

impl<K> Unpin for HashBuilder<K>
where Option<ProofRetainer<K>>: Unpin,

§

impl<K> UnsafeUnpin for HashBuilder<K>
where Option<ProofRetainer<K>>: UnsafeUnpin,

§

impl<K> UnwindSafe for HashBuilder<K>
where Option<ProofRetainer<K>>: 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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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 #126799)
Performs copy-assignment from self to dest. Read more
Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

fn in_current_span(self) -> Instrumented<Self> ⓘ

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> MaybeDebug for T
where T: Debug,

§

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

§

fn type_id(&self) -> TypeId

Returns the lifetime-erased type ID of self.
§

impl<T> Pointable for T

§

const ALIGN: usize

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
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
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

Layout§

Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.