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
impl HashBuilder
Sourcepub fn with_updates(self, retain_updates: bool) -> Self
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.
Sourcepub fn with_proof_retainer(self, retainer: ProofRetainer) -> Self
pub fn with_proof_retainer(self, retainer: ProofRetainer) -> Self
Enable specified proof retainer.
Sourcepub fn set_updates(&mut self, retain_updates: bool)
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.
Sourcepub fn split(self) -> (Self, HashMap<Nibbles, BranchNodeCompact>)
pub fn split(self) -> (Self, HashMap<Nibbles, BranchNodeCompact>)
Splits the HashBuilder into a HashBuilder and hash builder updates.
Sourcepub fn take_proof_nodes(&mut self) -> ProofNodes
pub fn take_proof_nodes(&mut self) -> ProofNodes
Take and return retained proof nodes.
Sourcepub fn updates_len(&self) -> usize
pub fn updates_len(&self) -> usize
The number of total updates accrued.
Returns 0
if Self::with_updates was not called.
Sourcepub fn add_leaf(&mut self, key: Nibbles, value: &[u8])
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.
Sourcepub fn add_leaf_unchecked(&mut self, key: Nibbles, value: &[u8])
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.
Sourcepub fn add_branch(
&mut self,
key: Nibbles,
value: B256,
stored_in_database: bool,
)
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.
Trait Implementations§
Source§impl Clone for HashBuilder
impl Clone for HashBuilder
Source§fn clone(&self) -> HashBuilder
fn clone(&self) -> HashBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more