mithril_signer/services/cardano_transactions/
preloader_checker.rs1use std::sync::Arc;
2
3use anyhow::Context;
4use async_trait::async_trait;
5
6use mithril_common::{entities::SignedEntityTypeDiscriminants, StdResult};
7use mithril_signed_entity_preloader::CardanoTransactionsPreloaderChecker;
8
9use crate::services::AggregatorClient;
10
11pub struct CardanoTransactionsPreloaderActivationSigner {
13 aggregator_client: Arc<dyn AggregatorClient>,
14}
15
16impl CardanoTransactionsPreloaderActivationSigner {
17 pub fn new(aggregator_client: Arc<dyn AggregatorClient>) -> Self {
19 Self { aggregator_client }
20 }
21}
22
23#[async_trait]
24impl CardanoTransactionsPreloaderChecker for CardanoTransactionsPreloaderActivationSigner {
25 async fn is_activated(&self) -> StdResult<bool> {
26 let message = self
27 .aggregator_client
28 .retrieve_aggregator_features()
29 .await
30 .with_context(|| "An error occurred while calling the Aggregator")?;
31
32 let activated_signed_entity_types = message.capabilities.signed_entity_types;
33
34 Ok(activated_signed_entity_types
35 .contains(&SignedEntityTypeDiscriminants::CardanoTransactions))
36 }
37}
38
39#[cfg(test)]
40mod tests {
41 use anyhow::anyhow;
42 use std::collections::BTreeSet;
43
44 use mithril_common::{
45 entities::SignedEntityTypeDiscriminants, messages::AggregatorFeaturesMessage,
46 };
47
48 use crate::services::{AggregatorClientError, MockAggregatorClient};
49
50 use super::*;
51
52 #[tokio::test]
53 async fn preloader_activation_state_activate_preloader_when_cardano_transactions_not_in_aggregator_capabilities(
54 ) {
55 let mut aggregator_client = MockAggregatorClient::new();
56 aggregator_client
57 .expect_retrieve_aggregator_features()
58 .times(1)
59 .returning(|| {
60 let mut message = AggregatorFeaturesMessage::dummy();
61 message.capabilities.signed_entity_types =
62 BTreeSet::from([SignedEntityTypeDiscriminants::MithrilStakeDistribution]);
63 Ok(message)
64 });
65 let preloader =
66 CardanoTransactionsPreloaderActivationSigner::new(Arc::new(aggregator_client));
67
68 let is_activated = preloader.is_activated().await.unwrap();
69
70 assert!(!is_activated);
71 }
72
73 #[tokio::test]
74 async fn preloader_activation_state_activate_preloader_when_cardano_transactions_in_aggregator_capabilities(
75 ) {
76 let mut aggregator_client = MockAggregatorClient::new();
77 aggregator_client
78 .expect_retrieve_aggregator_features()
79 .times(1)
80 .returning(|| {
81 let mut message = AggregatorFeaturesMessage::dummy();
82 message.capabilities.signed_entity_types =
83 BTreeSet::from([SignedEntityTypeDiscriminants::CardanoTransactions]);
84 Ok(message)
85 });
86 let preloader =
87 CardanoTransactionsPreloaderActivationSigner::new(Arc::new(aggregator_client));
88
89 let is_activated = preloader.is_activated().await.unwrap();
90
91 assert!(is_activated);
92 }
93
94 #[tokio::test]
95 async fn preloader_activation_state_activate_preloader_when_aggregator_call_fails() {
96 let mut aggregator_client = MockAggregatorClient::new();
97 aggregator_client
98 .expect_retrieve_aggregator_features()
99 .times(1)
100 .returning(|| {
101 Err(AggregatorClientError::RemoteServerTechnical(anyhow!(
102 "Aggregator call failed"
103 )))
104 });
105 let preloader =
106 CardanoTransactionsPreloaderActivationSigner::new(Arc::new(aggregator_client));
107
108 preloader
109 .is_activated()
110 .await
111 .expect_err("Should fail due to aggregator call failure");
112 }
113}