Skip to main content

tempo_commonware_node/epoch/
scheme_provider.rs

1//! Epoch aware schemes and peers.
2
3use std::{
4    collections::HashMap,
5    sync::{Arc, Mutex},
6};
7
8use commonware_consensus::{simplex::scheme::bls12381_threshold::vrf::Scheme, types::Epoch};
9use commonware_cryptography::{
10    bls12381::primitives::variant::MinSig, certificate::Provider, ed25519::PublicKey,
11};
12
13#[derive(Clone)]
14#[expect(clippy::type_complexity)]
15pub(crate) struct SchemeProvider {
16    inner: Arc<Mutex<HashMap<Epoch, Arc<Scheme<PublicKey, MinSig>>>>>,
17}
18
19impl SchemeProvider {
20    pub(crate) fn new() -> Self {
21        Self {
22            inner: Default::default(),
23        }
24    }
25
26    pub(crate) fn register(&self, epoch: Epoch, scheme: Scheme<PublicKey, MinSig>) -> bool {
27        self.inner
28            .lock()
29            .unwrap()
30            .insert(epoch, Arc::new(scheme))
31            .is_none()
32    }
33
34    pub(crate) fn delete(&self, epoch: &Epoch) -> bool {
35        self.inner.lock().unwrap().remove(epoch).is_some()
36    }
37}
38
39impl Provider for SchemeProvider {
40    type Scope = Epoch;
41    type Scheme = Scheme<PublicKey, MinSig>;
42
43    fn scoped(&self, scope: Self::Scope) -> Option<Arc<Self::Scheme>> {
44        self.inner.lock().unwrap().get(&scope).cloned()
45    }
46
47    /// Always returned `None`.
48    ///
49    /// While we are using bls12-381 threshold cryptography, the constant term
50    /// of the public polynomial can change in a full re-dkg and so tempo can
51    /// never verify certificates from all epochs.
52    fn all(&self) -> Option<Arc<Self::Scheme>> {
53        None
54    }
55}