mithril_signer/services/cardano_transactions/
preloader_checker.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::sync::Arc;

use anyhow::Context;
use async_trait::async_trait;

use mithril_common::{
    cardano_transactions_preloader::CardanoTransactionsPreloaderChecker,
    entities::SignedEntityTypeDiscriminants, StdResult,
};

use crate::services::AggregatorClient;

/// CardanoTransactionsPreloaderActivationSigner
pub struct CardanoTransactionsPreloaderActivationSigner {
    aggregator_client: Arc<dyn AggregatorClient>,
}

impl CardanoTransactionsPreloaderActivationSigner {
    /// Create a new instance of `CardanoTransactionsPreloaderActivationSigner`
    pub fn new(aggregator_client: Arc<dyn AggregatorClient>) -> Self {
        Self { aggregator_client }
    }
}

#[async_trait]
impl CardanoTransactionsPreloaderChecker for CardanoTransactionsPreloaderActivationSigner {
    async fn is_activated(&self) -> StdResult<bool> {
        let message = self
            .aggregator_client
            .retrieve_aggregator_features()
            .await
            .with_context(|| "An error occurred while calling the Aggregator")?;

        let activated_signed_entity_types = message.capabilities.signed_entity_types;

        Ok(activated_signed_entity_types
            .contains(&SignedEntityTypeDiscriminants::CardanoTransactions))
    }
}

#[cfg(test)]
mod tests {
    use anyhow::anyhow;
    use std::collections::BTreeSet;

    use mithril_common::{
        entities::SignedEntityTypeDiscriminants, messages::AggregatorFeaturesMessage,
    };

    use crate::services::{AggregatorClientError, MockAggregatorClient};

    use super::*;

    #[tokio::test]
    async fn preloader_activation_state_activate_preloader_when_cardano_transactions_not_in_aggregator_capabilities(
    ) {
        let mut aggregator_client = MockAggregatorClient::new();
        aggregator_client
            .expect_retrieve_aggregator_features()
            .times(1)
            .returning(|| {
                let mut message = AggregatorFeaturesMessage::dummy();
                message.capabilities.signed_entity_types =
                    BTreeSet::from([SignedEntityTypeDiscriminants::MithrilStakeDistribution]);
                Ok(message)
            });
        let preloader =
            CardanoTransactionsPreloaderActivationSigner::new(Arc::new(aggregator_client));

        let is_activated = preloader.is_activated().await.unwrap();

        assert!(!is_activated);
    }

    #[tokio::test]
    async fn preloader_activation_state_activate_preloader_when_cardano_transactions_in_aggregator_capabilities(
    ) {
        let mut aggregator_client = MockAggregatorClient::new();
        aggregator_client
            .expect_retrieve_aggregator_features()
            .times(1)
            .returning(|| {
                let mut message = AggregatorFeaturesMessage::dummy();
                message.capabilities.signed_entity_types =
                    BTreeSet::from([SignedEntityTypeDiscriminants::CardanoTransactions]);
                Ok(message)
            });
        let preloader =
            CardanoTransactionsPreloaderActivationSigner::new(Arc::new(aggregator_client));

        let is_activated = preloader.is_activated().await.unwrap();

        assert!(is_activated);
    }

    #[tokio::test]
    async fn preloader_activation_state_activate_preloader_when_aggregator_call_fails() {
        let mut aggregator_client = MockAggregatorClient::new();
        aggregator_client
            .expect_retrieve_aggregator_features()
            .times(1)
            .returning(|| {
                Err(AggregatorClientError::RemoteServerTechnical(anyhow!(
                    "Aggregator call failed"
                )))
            });
        let preloader =
            CardanoTransactionsPreloaderActivationSigner::new(Arc::new(aggregator_client));

        preloader
            .is_activated()
            .await
            .expect_err("Should fail due to aggregator call failure");
    }
}