Skip to content

Commit ad30161

Browse files
committed
feat: implement a resource pool
1 parent 723e04d commit ad30161

File tree

4 files changed

+357
-20
lines changed

4 files changed

+357
-20
lines changed

mithril-aggregator/src/dependency_injection/builder.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use mithril_common::{
2424
CardanoImmutableDigester, DumbImmutableFileObserver, ImmutableDigester,
2525
ImmutableFileObserver, ImmutableFileSystemObserver,
2626
},
27-
entities::{CertificatePending, CompressionAlgorithm, Epoch},
27+
entities::{CardanoDbBeacon, CertificatePending, CompressionAlgorithm, Epoch},
2828
era::{
2929
adapters::{EraReaderAdapterBuilder, EraReaderDummyAdapter},
3030
EraChecker, EraMarker, EraReader, EraReaderAdapter, SupportedEra,
@@ -1369,6 +1369,14 @@ impl DependenciesBuilder {
13691369
let block_range_root_retriever = self.get_transaction_repository().await?;
13701370
let service = MithrilProverService::new(transaction_retriever, block_range_root_retriever);
13711371

1372+
service
1373+
.compute_cache(&CardanoDbBeacon::new(
1374+
self.configuration.get_network()?.to_string(),
1375+
0,
1376+
u64::MAX,
1377+
))
1378+
.await?;
1379+
13721380
Ok(Arc::new(service))
13731381
}
13741382

mithril-aggregator/src/services/prover.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ use rayon::prelude::*;
33
use std::{
44
collections::{BTreeMap, BTreeSet, HashMap},
55
sync::Arc,
6+
time::Duration,
67
};
7-
use tokio::sync::Mutex;
88

99
use mithril_common::{
1010
crypto_helper::{MKMap, MKMapNode, MKTree},
1111
entities::{
1212
BlockRange, CardanoDbBeacon, CardanoTransaction, CardanoTransactionsSetProof,
1313
TransactionHash,
1414
},
15+
resource_pool::ResourcePool,
1516
signable_builder::BlockRangeRootRetriever,
1617
StdResult,
1718
};
@@ -28,14 +29,10 @@ pub trait ProverService: Sync + Send {
2829
) -> StdResult<Vec<CardanoTransactionsSetProof>>;
2930

3031
/// Compute the cache
31-
async fn compute_cache(&self, _up_to: &CardanoDbBeacon) -> StdResult<()> {
32-
Ok(())
33-
}
32+
async fn compute_cache(&self, _up_to: &CardanoDbBeacon) -> StdResult<()>;
3433

3534
/// Clear the cache
36-
async fn clear_cache(&self) -> StdResult<()> {
37-
Ok(())
38-
}
35+
async fn clear_cache(&self) -> StdResult<()>;
3936
}
4037

4138
/// Transactions retriever
@@ -62,7 +59,7 @@ pub trait TransactionsRetriever: Sync + Send {
6259
pub struct MithrilProverService {
6360
transaction_retriever: Arc<dyn TransactionsRetriever>,
6461
block_range_root_retriever: Arc<dyn BlockRangeRootRetriever>,
65-
mk_map_cache: Mutex<Option<MKMap<BlockRange, MKMapNode<BlockRange>>>>,
62+
mk_map_cache: ResourcePool<MKMap<BlockRange, MKMapNode<BlockRange>>>,
6663
}
6764

6865
impl MithrilProverService {
@@ -74,7 +71,7 @@ impl MithrilProverService {
7471
Self {
7572
transaction_retriever,
7673
block_range_root_retriever,
77-
mk_map_cache: Mutex::new(None),
74+
mk_map_cache: ResourcePool::default(),
7875
}
7976
}
8077

@@ -119,7 +116,7 @@ impl MithrilProverService {
119116
impl ProverService for MithrilProverService {
120117
async fn compute_transactions_proofs(
121118
&self,
122-
up_to: &CardanoDbBeacon,
119+
_up_to: &CardanoDbBeacon,
123120
transaction_hashes: &[TransactionHash],
124121
) -> StdResult<Vec<CardanoTransactionsSetProof>> {
125122
// 1 - Compute the set of block ranges with transactions to prove
@@ -139,9 +136,9 @@ impl ProverService for MithrilProverService {
139136
let mk_trees = BTreeMap::from_iter(mk_trees?);
140137

141138
// 3 - Compute block range roots Merkle map
142-
self.compute_cache(up_to).await?;
143-
let mut mk_map = self.mk_map_cache.lock().await;
144-
let mk_map = mk_map.as_mut().unwrap();
139+
let mut mk_map = self
140+
.mk_map_cache
141+
.acquire_resource(Duration::from_millis(1000))?;
145142

146143
// 4 - Enrich the Merkle map with the block ranges Merkle trees
147144
for (block_range, mk_tree) in mk_trees {
@@ -150,6 +147,9 @@ impl ProverService for MithrilProverService {
150147

151148
// 5 - Compute the proof for all transactions
152149
if let Ok(mk_proof) = mk_map.compute_proof(transaction_hashes) {
150+
self.mk_map_cache
151+
.return_resource(mk_map.into_inner(), mk_map.discriminant())?;
152+
153153
let transaction_hashes_certified: Vec<TransactionHash> = transaction_hashes
154154
.iter()
155155
.filter(|hash| mk_proof.contains(&hash.as_str().into()).is_ok())
@@ -166,22 +166,30 @@ impl ProverService for MithrilProverService {
166166
}
167167

168168
async fn compute_cache(&self, up_to: &CardanoDbBeacon) -> StdResult<()> {
169-
let mut mk_map = self.mk_map_cache.lock().await;
170-
if mk_map.is_none() {
171-
println!("Computing Merkle map from block range roots");
169+
if self.mk_map_cache.count()? == 0 {
170+
println!("Computing Merkle map cache from block range roots");
171+
172172
let mk_map_cache = self
173173
.block_range_root_retriever
174174
.compute_merkle_map_from_block_range_roots(up_to.immutable_file_number)
175175
.await?;
176-
mk_map.replace(mk_map_cache);
176+
let discriminant_new = self.mk_map_cache.discriminant()? + 1;
177+
self.mk_map_cache.set_discriminant(discriminant_new)?;
178+
for i in 0..10 {
179+
println!("Computing Merkle map cache from block range roots: {}", i);
180+
self.mk_map_cache
181+
.return_resource(mk_map_cache.clone(), discriminant_new)?;
182+
}
183+
self.mk_map_cache
184+
.return_resource(mk_map_cache, discriminant_new)?;
185+
println!("Done computing Merkle map cache from block range roots");
177186
}
178187

179188
Ok(())
180189
}
181190

182191
async fn clear_cache(&self) -> StdResult<()> {
183-
let mut mk_map = self.mk_map_cache.lock().await;
184-
mk_map.take();
192+
self.mk_map_cache.drain();
185193

186194
Ok(())
187195
}
@@ -387,6 +395,7 @@ mod tests {
387395
});
388396
},
389397
);
398+
prover.compute_cache(&test_data.beacon).await.unwrap();
390399

391400
let transactions_set_proof = prover
392401
.compute_transactions_proofs(&test_data.beacon, &test_data.transaction_hashes_to_prove)
@@ -440,6 +449,7 @@ mod tests {
440449
});
441450
},
442451
);
452+
prover.compute_cache(&test_data.beacon).await.unwrap();
443453

444454
let transactions_set_proof = prover
445455
.compute_transactions_proofs(&test_data.beacon, &test_data.transaction_hashes_to_prove)
@@ -496,6 +506,7 @@ mod tests {
496506
});
497507
},
498508
);
509+
prover.compute_cache(&test_data.beacon).await.unwrap();
499510

500511
let transactions_set_proof = prover
501512
.compute_transactions_proofs(&test_data.beacon, &test_data.transaction_hashes_to_prove)
@@ -533,6 +544,7 @@ mod tests {
533544
.return_once(|_| MKMap::new(&[]));
534545
},
535546
);
547+
prover.compute_cache(&test_data.beacon).await.unwrap();
536548

537549
prover
538550
.compute_transactions_proofs(&test_data.beacon, &test_data.transaction_hashes_to_prove)

mithril-common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub mod entities;
5959
pub mod era;
6060
pub mod messages;
6161
pub mod protocol;
62+
pub mod resource_pool;
6263
pub mod signable_builder;
6364

6465
cfg_test_tools! {

0 commit comments

Comments
 (0)