@@ -23,13 +23,14 @@ use crate::shim::{
2323 gas:: { Gas , price_list_by_network_version} ,
2424} ;
2525use crate :: state_manager:: is_valid_for_sending;
26+ use crate :: utils:: cache:: SizeTrackingLruCache ;
27+ use crate :: utils:: get_size:: CidWrapper ;
2628use ahash:: { HashMap , HashMapExt , HashSet , HashSetExt } ;
2729use anyhow:: Context as _;
2830use cid:: Cid ;
2931use futures:: StreamExt ;
3032use fvm_ipld_encoding:: to_vec;
3133use itertools:: Itertools ;
32- use lru:: LruCache ;
3334use nonzero_ext:: nonzero;
3435use parking_lot:: { Mutex , RwLock as SyncRwLock } ;
3536use tokio:: { sync:: broadcast:: error:: RecvError , task:: JoinSet , time:: interval} ;
@@ -182,9 +183,9 @@ pub struct MessagePool<T> {
182183 /// Sender half to send messages to other components
183184 pub network_sender : flume:: Sender < NetworkMessage > ,
184185 /// A cache for BLS signature keyed by Cid
185- pub bls_sig_cache : Arc < Mutex < LruCache < Cid , Signature > > > ,
186+ pub bls_sig_cache : Arc < SizeTrackingLruCache < CidWrapper , Signature > > ,
186187 /// A cache for BLS signature keyed by Cid
187- pub sig_val_cache : Arc < Mutex < LruCache < Cid , ( ) > > > ,
188+ pub sig_val_cache : Arc < SizeTrackingLruCache < CidWrapper , ( ) > > ,
188189 /// A set of republished messages identified by their Cid
189190 pub republished : Arc < SyncRwLock < HashSet < Cid > > > ,
190191 /// Acts as a signal to republish messages from the republished set of
@@ -261,14 +262,14 @@ where
261262 fn verify_msg_sig ( & self , msg : & SignedMessage ) -> Result < ( ) , Error > {
262263 let cid = msg. cid ( ) ;
263264
264- if let Some ( ( ) ) = self . sig_val_cache . lock ( ) . get ( & cid ) {
265+ if let Some ( ( ) ) = self . sig_val_cache . get_cloned ( & ( cid ) . into ( ) ) {
265266 return Ok ( ( ) ) ;
266267 }
267268
268269 msg. verify ( self . chain_config . eth_chain_id )
269270 . map_err ( |e| Error :: Other ( e. to_string ( ) ) ) ?;
270271
271- self . sig_val_cache . lock ( ) . put ( cid , ( ) ) ;
272+ self . sig_val_cache . push ( cid . into ( ) , ( ) ) ;
272273
273274 Ok ( ( ) )
274275 }
@@ -413,7 +414,7 @@ where
413414
414415 msg_vec. append ( smsgs. as_mut ( ) ) ;
415416 for msg in umsg {
416- let smsg = recover_sig ( & mut self . bls_sig_cache . lock ( ) , msg) ?;
417+ let smsg = recover_sig ( self . bls_sig_cache . as_ref ( ) , msg) ?;
417418 msg_vec. push ( smsg)
418419 }
419420 }
@@ -471,8 +472,14 @@ where
471472 let local_addrs = Arc :: new ( SyncRwLock :: new ( Vec :: new ( ) ) ) ;
472473 let pending = Arc :: new ( SyncRwLock :: new ( HashMap :: new ( ) ) ) ;
473474 let tipset = Arc :: new ( Mutex :: new ( api. get_heaviest_tipset ( ) ) ) ;
474- let bls_sig_cache = Arc :: new ( Mutex :: new ( LruCache :: new ( BLS_SIG_CACHE_SIZE ) ) ) ;
475- let sig_val_cache = Arc :: new ( Mutex :: new ( LruCache :: new ( SIG_VAL_CACHE_SIZE ) ) ) ;
475+ let bls_sig_cache = Arc :: new ( SizeTrackingLruCache :: new_with_default_metrics_registry (
476+ "bls_sig_cache" . into ( ) ,
477+ BLS_SIG_CACHE_SIZE ,
478+ ) ) ;
479+ let sig_val_cache = Arc :: new ( SizeTrackingLruCache :: new_with_default_metrics_registry (
480+ "sig_val_cache" . into ( ) ,
481+ SIG_VAL_CACHE_SIZE ,
482+ ) ) ;
476483 let local_msgs = Arc :: new ( SyncRwLock :: new ( HashSet :: new ( ) ) ) ;
477484 let republished = Arc :: new ( SyncRwLock :: new ( HashSet :: new ( ) ) ) ;
478485 let block_delay = chain_config. block_delay_secs ;
@@ -583,7 +590,7 @@ where
583590/// hash-map.
584591pub ( in crate :: message_pool) fn add_helper < T > (
585592 api : & T ,
586- bls_sig_cache : & Mutex < LruCache < Cid , Signature > > ,
593+ bls_sig_cache : & SizeTrackingLruCache < CidWrapper , Signature > ,
587594 pending : & SyncRwLock < HashMap < Address , MsgSet > > ,
588595 msg : SignedMessage ,
589596 sequence : u64 ,
@@ -592,7 +599,7 @@ where
592599 T : Provider ,
593600{
594601 if msg. signature ( ) . signature_type ( ) == SignatureType :: Bls {
595- bls_sig_cache. lock ( ) . put ( msg. cid ( ) , msg. signature ( ) . clone ( ) ) ;
602+ bls_sig_cache. push ( msg. cid ( ) . into ( ) , msg. signature ( ) . clone ( ) ) ;
596603 }
597604
598605 if msg. message ( ) . gas_limit > 100_000_000 {
0 commit comments