Skip to content

Commit 1841e71

Browse files
committed
fix ring context
1 parent e0303e3 commit 1841e71

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

Utils/Sources/bandersnatch/src/bandersnatch_vrfs.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,23 @@ pub struct RingVrfSignature {
2525
}
2626

2727
pub 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

4047
fn ring_context_params() -> &'static PcsParams {

0 commit comments

Comments
 (0)