mithril_aggregator/dependency_injection/builder/protocol/
certificates.rs

1use anyhow::Context;
2use std::sync::Arc;
3
4use mithril_common::certificate_chain::{CertificateVerifier, MithrilCertificateVerifier};
5use mithril_common::crypto_helper::{
6    ProtocolGenesisSigner, ProtocolGenesisVerificationKey, ProtocolGenesisVerifier,
7};
8
9use crate::database::repository::{BufferedSingleSignatureRepository, SingleSignatureRepository};
10use crate::dependency_injection::{DependenciesBuilder, DependenciesBuilderError, Result};
11use crate::services::{
12    BufferedCertifierService, CertifierService, MithrilCertifierService,
13    MithrilSignerRegistrationFollower, SignerSynchronizer,
14};
15use crate::{
16    ExecutionEnvironment, MithrilSignerRegistrationLeader, MithrilSignerRegistrationVerifier,
17    MultiSigner, MultiSignerImpl, SignerRegisterer, SignerRegistrationRoundOpener,
18    SignerRegistrationVerifier, SingleSignatureAuthenticator,
19};
20
21impl DependenciesBuilder {
22    /// Create [CertifierService] service
23    pub async fn build_certifier_service(&mut self) -> Result<Arc<dyn CertifierService>> {
24        let cardano_network = self.configuration.get_network().with_context(|| {
25            "Dependencies Builder can not get Cardano network while building the chain observer"
26        })?;
27        let sqlite_connection = self.get_sqlite_connection().await?;
28        let open_message_repository = self.get_open_message_repository().await?;
29        let single_signature_repository =
30            Arc::new(SingleSignatureRepository::new(sqlite_connection.clone()));
31        let certificate_repository = self.get_certificate_repository().await?;
32        let certificate_verifier = self.get_certificate_verifier().await?;
33        let genesis_verifier = self.get_genesis_verifier().await?;
34        let multi_signer = self.get_multi_signer().await?;
35        let epoch_service = self.get_epoch_service().await?;
36        let logger = self.root_logger();
37
38        let certifier = Arc::new(MithrilCertifierService::new(
39            cardano_network,
40            open_message_repository,
41            single_signature_repository,
42            certificate_repository,
43            certificate_verifier,
44            genesis_verifier,
45            multi_signer,
46            epoch_service,
47            logger,
48        ));
49
50        Ok(Arc::new(BufferedCertifierService::new(
51            certifier,
52            Arc::new(BufferedSingleSignatureRepository::new(sqlite_connection)),
53            self.root_logger(),
54        )))
55    }
56
57    /// [CertifierService] service
58    pub async fn get_certifier_service(&mut self) -> Result<Arc<dyn CertifierService>> {
59        if self.certifier_service.is_none() {
60            self.certifier_service = Some(self.build_certifier_service().await?);
61        }
62
63        Ok(self.certifier_service.as_ref().cloned().unwrap())
64    }
65
66    async fn build_multi_signer(&mut self) -> Result<Arc<dyn MultiSigner>> {
67        let multi_signer =
68            MultiSignerImpl::new(self.get_epoch_service().await?, self.root_logger());
69
70        Ok(Arc::new(multi_signer))
71    }
72
73    /// Get a configured multi signer
74    pub async fn get_multi_signer(&mut self) -> Result<Arc<dyn MultiSigner>> {
75        if self.multi_signer.is_none() {
76            self.multi_signer = Some(self.build_multi_signer().await?);
77        }
78
79        Ok(self.multi_signer.as_ref().cloned().unwrap())
80    }
81
82    async fn build_certificate_verifier(&mut self) -> Result<Arc<dyn CertificateVerifier>> {
83        let verifier = Arc::new(MithrilCertificateVerifier::new(
84            self.root_logger(),
85            self.get_certificate_repository().await?,
86        ));
87
88        Ok(verifier)
89    }
90
91    /// [CertificateVerifier] service.
92    pub async fn get_certificate_verifier(&mut self) -> Result<Arc<dyn CertificateVerifier>> {
93        if self.certificate_verifier.is_none() {
94            self.certificate_verifier = Some(self.build_certificate_verifier().await?);
95        }
96
97        Ok(self.certificate_verifier.as_ref().cloned().unwrap())
98    }
99
100    async fn build_genesis_verifier(&mut self) -> Result<Arc<ProtocolGenesisVerifier>> {
101        let genesis_verifier: ProtocolGenesisVerifier = match self.configuration.environment {
102            ExecutionEnvironment::Production => ProtocolGenesisVerifier::from_verification_key(
103                ProtocolGenesisVerificationKey::from_json_hex(
104                    &self.configuration.genesis_verification_key,
105                )
106                .map_err(|e| DependenciesBuilderError::Initialization {
107                    message: format!(
108                        "Could not decode hex key to build genesis verifier: '{}'",
109                        self.configuration.genesis_verification_key
110                    ),
111                    error: Some(e),
112                })?,
113            ),
114            _ => ProtocolGenesisSigner::create_deterministic_signer().create_verifier(),
115        };
116
117        Ok(Arc::new(genesis_verifier))
118    }
119
120    /// Return a [ProtocolGenesisVerifier]
121    pub async fn get_genesis_verifier(&mut self) -> Result<Arc<ProtocolGenesisVerifier>> {
122        if self.genesis_verifier.is_none() {
123            self.genesis_verifier = Some(self.build_genesis_verifier().await?);
124        }
125
126        Ok(self.genesis_verifier.as_ref().cloned().unwrap())
127    }
128
129    /// Return a [MithrilSignerRegistrationLeader] service
130    async fn build_mithril_signer_registration_leader(
131        &mut self,
132    ) -> Result<Arc<MithrilSignerRegistrationLeader>> {
133        let registerer = MithrilSignerRegistrationLeader::new(
134            self.get_verification_key_store().await?,
135            self.get_signer_store().await?,
136            self.get_signer_registration_verifier().await?,
137            self.configuration.safe_epoch_retention_limit(),
138        );
139
140        Ok(Arc::new(registerer))
141    }
142
143    /// Return a [MithrilSignerRegistrationLeader]
144    pub async fn get_mithril_signer_registration_leader(
145        &mut self,
146    ) -> Result<Arc<MithrilSignerRegistrationLeader>> {
147        if self.mithril_signer_registration_leader.is_none() {
148            self.mithril_signer_registration_leader =
149                Some(self.build_mithril_signer_registration_leader().await?);
150        }
151
152        Ok(self
153            .mithril_signer_registration_leader
154            .as_ref()
155            .cloned()
156            .unwrap())
157    }
158
159    /// Return a [MithrilSignerRegistrationFollower] service
160    async fn build_mithril_signer_registration_follower(
161        &mut self,
162    ) -> Result<Arc<MithrilSignerRegistrationFollower>> {
163        let registerer = MithrilSignerRegistrationFollower::new(
164            self.get_epoch_service().await?,
165            self.get_verification_key_store().await?,
166            self.get_signer_store().await?,
167            self.get_signer_registration_verifier().await?,
168            self.get_leader_aggregator_client().await?,
169            self.get_stake_store().await?,
170            self.configuration.safe_epoch_retention_limit(),
171        );
172
173        Ok(Arc::new(registerer))
174    }
175
176    /// Return a [MithrilSignerRegistrationFollower]
177    pub async fn get_mithril_signer_registration_follower(
178        &mut self,
179    ) -> Result<Arc<MithrilSignerRegistrationFollower>> {
180        if self.mithril_signer_registration_follower.is_none() {
181            self.mithril_signer_registration_follower =
182                Some(self.build_mithril_signer_registration_follower().await?);
183        }
184
185        Ok(self
186            .mithril_signer_registration_follower
187            .as_ref()
188            .cloned()
189            .unwrap())
190    }
191
192    /// Return a [SignerRegisterer]
193    pub async fn get_signer_registerer(&mut self) -> Result<Arc<dyn SignerRegisterer>> {
194        if self.signer_registerer.is_none() {
195            self.signer_registerer = Some(if self.configuration.is_follower_aggregator() {
196                self.get_mithril_signer_registration_follower().await?
197            } else {
198                self.get_mithril_signer_registration_leader().await?
199            });
200        }
201
202        Ok(self.signer_registerer.as_ref().cloned().unwrap())
203    }
204
205    /// Return a [SignerSynchronizer]
206    pub async fn get_signer_synchronizer(&mut self) -> Result<Arc<dyn SignerSynchronizer>> {
207        if self.signer_synchronizer.is_none() {
208            self.signer_synchronizer = Some(if self.configuration.is_follower_aggregator() {
209                self.get_mithril_signer_registration_follower().await?
210            } else {
211                self.get_mithril_signer_registration_leader().await?
212            });
213        }
214
215        Ok(self.signer_synchronizer.as_ref().cloned().unwrap())
216    }
217
218    async fn build_signer_registration_verifier(
219        &mut self,
220    ) -> Result<Arc<dyn SignerRegistrationVerifier>> {
221        let registerer = MithrilSignerRegistrationVerifier::new(self.get_chain_observer().await?);
222
223        Ok(Arc::new(registerer))
224    }
225
226    /// Return a [SignerRegistrationVerifier]
227    pub async fn get_signer_registration_verifier(
228        &mut self,
229    ) -> Result<Arc<dyn SignerRegistrationVerifier>> {
230        if self.signer_registration_verifier.is_none() {
231            self.signer_registration_verifier =
232                Some(self.build_signer_registration_verifier().await?);
233        }
234
235        Ok(self.signer_registration_verifier.as_ref().cloned().unwrap())
236    }
237
238    /// Return a [SignerRegistrationRoundOpener]
239    pub async fn get_signer_registration_round_opener(
240        &mut self,
241    ) -> Result<Arc<dyn SignerRegistrationRoundOpener>> {
242        if self.signer_registration_round_opener.is_none() {
243            if self.configuration.is_follower_aggregator() {
244                self.signer_registration_round_opener =
245                    Some(self.get_mithril_signer_registration_follower().await?);
246            } else {
247                self.signer_registration_round_opener =
248                    Some(self.get_mithril_signer_registration_leader().await?);
249            }
250        }
251
252        Ok(self
253            .signer_registration_round_opener
254            .as_ref()
255            .cloned()
256            .unwrap())
257    }
258
259    async fn build_single_signature_authenticator(
260        &mut self,
261    ) -> Result<Arc<SingleSignatureAuthenticator>> {
262        let authenticator =
263            SingleSignatureAuthenticator::new(self.get_multi_signer().await?, self.root_logger());
264
265        Ok(Arc::new(authenticator))
266    }
267
268    /// [SingleSignatureAuthenticator] service
269    pub async fn get_single_signature_authenticator(
270        &mut self,
271    ) -> Result<Arc<SingleSignatureAuthenticator>> {
272        if self.single_signer_authenticator.is_none() {
273            self.single_signer_authenticator =
274                Some(self.build_single_signature_authenticator().await?);
275        }
276
277        Ok(self.single_signer_authenticator.as_ref().cloned().unwrap())
278    }
279}