@@ -6,14 +6,15 @@ use std::{
66use async_trait:: async_trait;
77
88use 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 {
5162pub 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
5668impl 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