Skip to content

Commit

Permalink
BlockBody: cache merkle_tree, not mast_hash
Browse files Browse the repository at this point in the history
Cache the Merkle tree instead of the MAST hash of BlockBody. Cf.
#274 (comment)
and #274 in general.
  • Loading branch information
Sword-Smith committed Dec 2, 2024
1 parent 992adca commit 771f00d
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/models/blockchain/block/block_body.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::sync::OnceLock;

use get_size::GetSize;
use itertools::Itertools;
use serde::Deserialize;
use serde::Serialize;
use strum::EnumCount;
use tasm_lib::triton_vm::prelude::Digest;
use tasm_lib::twenty_first::math::b_field_element::BFieldElement;
use tasm_lib::twenty_first::prelude::AlgebraicHasher;
use tasm_lib::twenty_first::prelude::MerkleTree;
use tasm_lib::twenty_first::prelude::MerkleTreeMaker;
use tasm_lib::twenty_first::util_types::mmr::mmr_accumulator::MmrAccumulator;
use twenty_first::math::bfield_codec::BFieldCodec;

Expand Down Expand Up @@ -85,7 +89,7 @@ pub struct BlockBody {
#[serde(skip)]
#[bfield_codec(ignore)]
#[get_size(ignore)]
mast_hash: OnceLock<Digest>,
merkle_tree: OnceLock<MerkleTree>,
}

impl PartialEq for BlockBody {
Expand All @@ -107,7 +111,7 @@ impl BlockBody {
mutator_set_accumulator,
lock_free_mmr_accumulator,
block_mmr_accumulator,
mast_hash: OnceLock::default(), // calc'd in mast_hash()
merkle_tree: OnceLock::default(), // calc'd in merkle_tree()
}
}
}
Expand All @@ -124,8 +128,23 @@ impl MastHash for BlockBody {
]
}

fn mast_hash(&self) -> Digest {
*self.mast_hash.get_or_init(|| self.merkle_tree().root())
fn merkle_tree(&self) -> MerkleTree {
self.merkle_tree
.get_or_init(|| {
let mut digests = self
.mast_sequences()
.into_iter()
.map(|seq| crate::models::blockchain::shared::Hash::hash_varlen(&seq))
.collect_vec();

// pad until length is a power of two
while digests.len() & (digests.len() - 1) != 0 {
digests.push(Digest::default());
}

twenty_first::prelude::CpuParallel::from_digests(&digests).unwrap()
})
.clone()
}
}

Expand Down Expand Up @@ -160,7 +179,7 @@ mod test {
mutator_set_accumulator: mutator_set_accumulator.clone(),
lock_free_mmr_accumulator,
block_mmr_accumulator,
mast_hash: OnceLock::default(),
merkle_tree: OnceLock::default(),
}
},
)
Expand Down

0 comments on commit 771f00d

Please sign in to comment.