@@ -4,14 +4,14 @@ use std::{
4
4
collections:: { BTreeMap , BTreeSet , HashMap } ,
5
5
sync:: Arc ,
6
6
} ;
7
- use tokio:: sync:: Mutex ;
8
7
9
8
use mithril_common:: {
10
9
crypto_helper:: { MKMap , MKMapNode , MKTree } ,
11
10
entities:: {
12
11
BlockRange , CardanoDbBeacon , CardanoTransaction , CardanoTransactionsSetProof ,
13
12
TransactionHash ,
14
13
} ,
14
+ resource_pool:: ResourcePool ,
15
15
signable_builder:: BlockRangeRootRetriever ,
16
16
StdResult ,
17
17
} ;
@@ -62,7 +62,7 @@ pub trait TransactionsRetriever: Sync + Send {
62
62
pub struct MithrilProverService {
63
63
transaction_retriever : Arc < dyn TransactionsRetriever > ,
64
64
block_range_root_retriever : Arc < dyn BlockRangeRootRetriever > ,
65
- mk_map_cache : Mutex < Option < MKMap < BlockRange , MKMapNode < BlockRange > > > > ,
65
+ mk_map_cache : ResourcePool < MKMap < BlockRange , MKMapNode < BlockRange > > > ,
66
66
}
67
67
68
68
impl MithrilProverService {
@@ -74,7 +74,7 @@ impl MithrilProverService {
74
74
Self {
75
75
transaction_retriever,
76
76
block_range_root_retriever,
77
- mk_map_cache : Mutex :: new ( None ) ,
77
+ mk_map_cache : ResourcePool :: new ( vec ! [ ] ) ,
78
78
}
79
79
}
80
80
@@ -139,9 +139,9 @@ impl ProverService for MithrilProverService {
139
139
let mk_trees = BTreeMap :: from_iter ( mk_trees?) ;
140
140
141
141
// 3 - Compute block range roots Merkle map
142
+ // TODO: the cache computation should be done in the state machine only when new artifact is produced and at node startup
142
143
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 ( ) ;
144
+ let mut mk_map = self . mk_map_cache . acquire_resource ( ) . await ;
145
145
146
146
// 4 - Enrich the Merkle map with the block ranges Merkle trees
147
147
for ( block_range, mk_tree) in mk_trees {
@@ -150,6 +150,8 @@ impl ProverService for MithrilProverService {
150
150
151
151
// 5 - Compute the proof for all transactions
152
152
if let Ok ( mk_proof) = mk_map. compute_proof ( transaction_hashes) {
153
+ self . mk_map_cache . return_resource ( mk_map) . await ;
154
+
153
155
let transaction_hashes_certified: Vec < TransactionHash > = transaction_hashes
154
156
. iter ( )
155
157
. filter ( |hash| mk_proof. contains ( & hash. as_str ( ) . into ( ) ) . is_ok ( ) )
@@ -166,22 +168,27 @@ impl ProverService for MithrilProverService {
166
168
}
167
169
168
170
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" ) ;
171
+ if self . mk_map_cache . count ( ) . await == 0 {
172
+ println ! ( "Computing Merkle map cache from block range roots" ) ;
172
173
let mk_map_cache = self
173
174
. block_range_root_retriever
174
175
. compute_merkle_map_from_block_range_roots ( up_to. immutable_file_number )
175
176
. await ?;
176
- mk_map. replace ( mk_map_cache) ;
177
+ for i in 0 ..100 {
178
+ println ! ( "Computing Merkle map cache from block range roots: {}" , i) ;
179
+ self . mk_map_cache
180
+ . return_resource ( mk_map_cache. clone ( ) )
181
+ . await ;
182
+ }
183
+ self . mk_map_cache . return_resource ( mk_map_cache) . await ;
184
+ println ! ( "Done computing Merkle map cache from block range roots" ) ;
177
185
}
178
186
179
187
Ok ( ( ) )
180
188
}
181
189
182
190
async fn clear_cache ( & self ) -> StdResult < ( ) > {
183
- let mut mk_map = self . mk_map_cache . lock ( ) . await ;
184
- mk_map. take ( ) ;
191
+ self . mk_map_cache . drain ( ) . await ;
185
192
186
193
Ok ( ( ) )
187
194
}
0 commit comments