Skip to content

Commit

Permalink
Reuse hasher
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-D-coder committed Mar 26, 2024
1 parent f1d88e7 commit ef22b2e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/stored/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::{cell::RefCell, ops::Deref};
use alloc::{boxed::Box, format, vec::Vec};
use bumpalo::Bump;
use ouroboros::self_referencing;
use sha2::{Digest, Sha256};

use crate::{
transaction::nodes::{NodeRef, TrieRoot},
Expand Down Expand Up @@ -85,14 +86,15 @@ impl<V: AsRef<[u8]>> Store<V> for Snapshot<V> {
let idx = node as usize;
let leaf_offset = self.branches.len();
let unvisited_offset = leaf_offset + self.leaves.len();
let mut hasher = Sha256::new();

if let Some(branch) = self.branches.get(idx) {
let left = self.calc_subtree_hash(branch.left)?;
let right = self.calc_subtree_hash(branch.right)?;

Ok(branch.hash_branch(&left, &right))
Ok(branch.hash_branch(&mut hasher, &left, &right))
} else if let Some(leaf) = self.leaves.get(idx - leaf_offset) {
Ok(leaf.hash_leaf())
Ok(leaf.hash_leaf(&mut hasher))
} else if let Some(hash) = self.unvisited_nodes.get(idx - unvisited_offset) {
Ok(*hash)
} else {
Expand Down
11 changes: 9 additions & 2 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub(crate) mod nodes;

use alloc::{boxed::Box, format};
use core::mem;
use sha2::{Digest, Sha256};

use crate::{stored, KeyHash, NodeHash};
use crate::{
Expand Down Expand Up @@ -67,9 +68,12 @@ impl<S: Store<V>, V: AsRef<[u8]>> Transaction<S, V> {
) -> Result<(), TrieError>,
on_modified_leaf: &mut impl FnMut(&NodeHash, &Leaf<V>) -> Result<(), TrieError>,
) -> Result<TrieRoot<NodeHash>, TrieError> {
let mut hasher = Sha256::new();

let root_hash = match &self.current_root {
TrieRoot::Empty => return Ok(TrieRoot::Empty),
TrieRoot::Node(node_ref) => Self::calc_root_hash_node(
&mut hasher,
&self.data_store,
node_ref,
on_modified_leaf,
Expand All @@ -87,6 +91,7 @@ impl<S: Store<V>, V: AsRef<[u8]>> Transaction<S, V> {

#[inline]
fn calc_root_hash_node(
hasher: &mut Sha256,
data_store: &S,
node_ref: &NodeRef<V>,
on_modified_leaf: &mut impl FnMut(&NodeHash, &Leaf<V>) -> Result<(), TrieError>,
Expand All @@ -101,24 +106,26 @@ impl<S: Store<V>, V: AsRef<[u8]>> Transaction<S, V> {
match node_ref {
NodeRef::ModBranch(branch) => {
let left = Self::calc_root_hash_node(
hasher,
data_store,
&branch.left,
on_modified_leaf,
on_modified_branch,
)?;
let right = Self::calc_root_hash_node(
hasher,
data_store,
&branch.right,
on_modified_leaf,
on_modified_branch,
)?;

let hash = branch.hash_branch(&left, &right);
let hash = branch.hash_branch(hasher, &left, &right);
on_modified_branch(&hash, branch, left, right)?;
Ok(hash)
}
NodeRef::ModLeaf(leaf) => {
let hash = leaf.hash_leaf();
let hash = leaf.hash_leaf(hasher);

on_modified_leaf(&hash, leaf)?;
Ok(hash)
Expand Down
15 changes: 7 additions & 8 deletions src/transaction/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::{boxed::Box, vec::Vec};
use core::{iter, mem};

use sha2::{Digest, Sha256};
use sha2::{digest::FixedOutputReset, Digest, Sha256};

use crate::{stored, KeyHash, NodeHash};

Expand Down Expand Up @@ -282,9 +282,8 @@ impl<NR> Branch<NR> {
}

#[inline]
pub fn hash_branch(&self, left: &NodeHash, right: &NodeHash) -> NodeHash {
let mut hasher = Sha256::new();

pub fn hash_branch(&self, hasher: &mut Sha256, left: &NodeHash, right: &NodeHash) -> NodeHash {
hasher.reset();
hasher.update(left);
hasher.update(right);
hasher.update(self.mask.bit_idx.to_le_bytes());
Expand All @@ -295,7 +294,7 @@ impl<NR> Branch<NR> {
.iter()
.for_each(|word| hasher.update(word.to_le_bytes()));

NodeHash::new(hasher.finalize().into())
NodeHash::new(hasher.finalize_fixed_reset().into())
}
}

Expand Down Expand Up @@ -489,10 +488,10 @@ pub struct Leaf<V> {

impl<V: AsRef<[u8]>> Leaf<V> {
#[inline]
pub fn hash_leaf(&self) -> NodeHash {
let mut hasher = Sha256::new();
pub fn hash_leaf(&self, hasher: &mut Sha256) -> NodeHash {
hasher.reset();
hasher.update(self.key_hash.to_bytes());
hasher.update(self.value.as_ref());
NodeHash::new(hasher.finalize().into())
NodeHash::new(hasher.finalize_fixed_reset().into())
}
}

0 comments on commit ef22b2e

Please sign in to comment.