File tree Expand file tree Collapse file tree 1 file changed +16
-9
lines changed
Utils/Sources/bandersnatch/src Expand file tree Collapse file tree 1 file changed +16
-9
lines changed Original file line number Diff line number Diff line change @@ -25,16 +25,23 @@ pub struct RingVrfSignature {
2525}
2626
2727pub fn ring_context ( size : usize ) -> Option < RingProofParams > {
28+ use std:: collections:: HashMap ;
2829 use std:: sync:: OnceLock ;
29- static RING_CONTEXT : OnceLock < Option < RingProofParams > > = OnceLock :: new ( ) ;
30-
31- // size is number of validators, so it won't change
32- RING_CONTEXT
33- . get_or_init ( || {
34- let pcs_params = ring_context_params ( ) ;
35- RingProofParams :: from_pcs_params ( size, pcs_params. clone ( ) ) . ok ( )
36- } )
37- . clone ( )
30+
31+ static RING_CONTEXTS : OnceLock < std:: sync:: Mutex < HashMap < usize , Option < RingProofParams > > > > =
32+ OnceLock :: new ( ) ;
33+
34+ let contexts = RING_CONTEXTS . get_or_init ( || std:: sync:: Mutex :: new ( HashMap :: new ( ) ) ) ;
35+ let mut cache = contexts. lock ( ) . unwrap ( ) ;
36+
37+ if let Some ( cached_params) = cache. get ( & size) {
38+ cached_params. clone ( )
39+ } else {
40+ let pcs_params = ring_context_params ( ) ;
41+ let params = RingProofParams :: from_pcs_params ( size, pcs_params. clone ( ) ) . ok ( ) ;
42+ cache. insert ( size, params. clone ( ) ) ;
43+ params
44+ }
3845}
3946
4047fn ring_context_params ( ) -> & ' static PcsParams {
You can’t perform that action at this time.
0 commit comments