Skip to content

Commit 07f518e

Browse files
committed
feat: implement simple cache for transaction prover
1 parent ab590fb commit 07f518e

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

mithril-aggregator/src/runtime/runner.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,14 @@ impl AggregatorRunnerTrait for AggregatorRunner {
399399
)
400400
})?;
401401

402+
if let SignedEntityType::CardanoTransactions(up_to_beacon) = signed_entity_type {
403+
self.dependencies.prover_service.clear_cache().await?;
404+
self.dependencies
405+
.prover_service
406+
.compute_cache(up_to_beacon)
407+
.await?;
408+
}
409+
402410
Ok(())
403411
}
404412

mithril-aggregator/src/services/prover.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ use std::{
66
use async_trait::async_trait;
77

88
use mithril_common::{
9-
crypto_helper::MKTree,
9+
crypto_helper::{MKMap, MKMapNode, MKTree},
1010
entities::{
1111
BlockRange, CardanoDbBeacon, CardanoTransaction, CardanoTransactionsSetProof,
1212
TransactionHash,
1313
},
1414
signable_builder::BlockRangeRootRetriever,
1515
StdResult,
1616
};
17+
use tokio::sync::Mutex;
1718

1819
/// Prover service is the cryptographic engine in charge of producing cryptographic proofs for transactions
1920
#[cfg_attr(test, mockall::automock)]
@@ -25,6 +26,16 @@ pub trait ProverService: Sync + Send {
2526
up_to: &CardanoDbBeacon,
2627
transaction_hashes: &[TransactionHash],
2728
) -> StdResult<Vec<CardanoTransactionsSetProof>>;
29+
30+
/// Compute the cache
31+
async fn compute_cache(&self, _up_to: &CardanoDbBeacon) -> StdResult<()> {
32+
Ok(())
33+
}
34+
35+
/// Clear the cache
36+
async fn clear_cache(&self) -> StdResult<()> {
37+
Ok(())
38+
}
2839
}
2940

3041
/// Transactions retriever
@@ -51,6 +62,7 @@ pub trait TransactionsRetriever: Sync + Send {
5162
pub struct MithrilProverService {
5263
transaction_retriever: Arc<dyn TransactionsRetriever>,
5364
block_range_root_retriever: Arc<dyn BlockRangeRootRetriever>,
65+
mk_map_cache: Mutex<Option<MKMap<BlockRange, MKMapNode<BlockRange>>>>,
5466
}
5567

5668
impl MithrilProverService {
@@ -62,6 +74,7 @@ impl MithrilProverService {
6274
Self {
6375
transaction_retriever,
6476
block_range_root_retriever,
77+
mk_map_cache: Mutex::new(None),
6578
}
6679
}
6780

@@ -123,10 +136,9 @@ impl ProverService for MithrilProverService {
123136
}
124137

125138
// 3 - Compute block range roots Merkle map
126-
let mut mk_map = self
127-
.block_range_root_retriever
128-
.compute_merkle_map_from_block_range_roots(up_to.immutable_file_number)
129-
.await?;
139+
self.compute_cache(up_to).await?;
140+
let mut mk_map = self.mk_map_cache.lock().await;
141+
let mk_map = mk_map.as_mut().unwrap();
130142

131143
// 4 - Enrich the Merkle map with the block ranges Merkle trees
132144
for (block_range, mk_tree) in mk_trees {
@@ -149,6 +161,27 @@ impl ProverService for MithrilProverService {
149161
Ok(vec![])
150162
}
151163
}
164+
165+
async fn compute_cache(&self, up_to: &CardanoDbBeacon) -> StdResult<()> {
166+
let mut mk_map = self.mk_map_cache.lock().await;
167+
if mk_map.is_none() {
168+
println!("Computing Merkle map from block range roots");
169+
let mk_map_cache = self
170+
.block_range_root_retriever
171+
.compute_merkle_map_from_block_range_roots(up_to.immutable_file_number)
172+
.await?;
173+
mk_map.replace(mk_map_cache);
174+
}
175+
176+
Ok(())
177+
}
178+
179+
async fn clear_cache(&self) -> StdResult<()> {
180+
let mut mk_map = self.mk_map_cache.lock().await;
181+
mk_map.take();
182+
183+
Ok(())
184+
}
152185
}
153186

154187
#[cfg(test)]

0 commit comments

Comments
 (0)