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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! # Cardano Transaction Preloader
//!
//! This module provides a preload mechanism for Cardano Transaction signed entity, allowing
//! to compute in advance the Transactions & Block Range Root to be signed.

use std::sync::Arc;

use anyhow::Context;
use async_trait::async_trait;
use slog::{debug, info, Logger};

use crate::chain_observer::ChainObserver;
use crate::entities::{BlockNumber, SignedEntityTypeDiscriminants};
use crate::logging::LoggerExtensions;
use crate::signable_builder::TransactionsImporter;
use crate::signed_entity_type_lock::SignedEntityTypeLock;
use crate::StdResult;
#[cfg(test)]
use mockall::automock;

/// CardanoTransactionsPreloaderChecker gives the ability to determine
/// if the Cardano Transactions Preloader should import the transactions.
#[cfg_attr(test, automock)]
#[async_trait]
pub trait CardanoTransactionsPreloaderChecker: Send + Sync {
    /// Determine if the Cardano Transactions Preloader should preload.
    async fn is_activated(&self) -> StdResult<bool>;
}

/// CardanoTransactionsPreloaderActivation
pub struct CardanoTransactionsPreloaderActivation {
    activation: bool,
}

impl CardanoTransactionsPreloaderActivation {
    /// Create a new instance of `CardanoTransactionsPreloaderActivation`
    pub fn new(activation: bool) -> Self {
        Self { activation }
    }
}

#[async_trait]
impl CardanoTransactionsPreloaderChecker for CardanoTransactionsPreloaderActivation {
    async fn is_activated(&self) -> StdResult<bool> {
        Ok(self.activation)
    }
}

/// Preload mechanism for Cardano Transaction signed entity, allowing
/// to compute in advance the Transactions & Block Range Root to be signed.
pub struct CardanoTransactionsPreloader {
    signed_entity_type_lock: Arc<SignedEntityTypeLock>,
    importer: Arc<dyn TransactionsImporter>,
    security_parameter: BlockNumber,
    chain_observer: Arc<dyn ChainObserver>,
    logger: Logger,
    activation_state: Arc<dyn CardanoTransactionsPreloaderChecker>,
}

impl CardanoTransactionsPreloader {
    /// Create a new instance of `CardanoTransactionPreloader`.
    pub fn new(
        signed_entity_type_lock: Arc<SignedEntityTypeLock>,
        importer: Arc<dyn TransactionsImporter>,
        security_parameter: BlockNumber,
        chain_observer: Arc<dyn ChainObserver>,
        logger: Logger,
        activation_state: Arc<dyn CardanoTransactionsPreloaderChecker>,
    ) -> Self {
        Self {
            signed_entity_type_lock,
            importer,
            security_parameter,
            chain_observer,
            logger: logger.new_with_component_name::<Self>(),
            activation_state,
        }
    }

    /// Preload the Cardano Transactions by running the importer up to the current chain block number.
    pub async fn preload(&self) -> StdResult<()> {
        if !self.is_activated().await? {
            debug!(self.logger, "Not running, conditions not met");
            return Ok(());
        }

        info!(self.logger, "Started");
        debug!(self.logger, "Locking signed entity type"; "entity_type" => "CardanoTransactions");
        self.signed_entity_type_lock
            .lock(SignedEntityTypeDiscriminants::CardanoTransactions)
            .await;

        let preload_result = self.do_preload().await;

        debug!(self.logger, "Releasing signed entity type"; "entity_type" => "CardanoTransactions");
        self.signed_entity_type_lock
            .release(SignedEntityTypeDiscriminants::CardanoTransactions)
            .await;
        info!(self.logger, "Finished");

        preload_result
    }

    /// Return the activation state of the preloader.
    pub async fn is_activated(&self) -> StdResult<bool> {
        self.activation_state.is_activated().await
    }

    async fn do_preload(&self) -> StdResult<()> {
        let chain_point = self
            .chain_observer
            .get_current_chain_point()
            .await?
            .with_context(|| {
                "No chain point yielded by the chain observer, is your cardano node ready?"
            })?;
        let up_to_block_number = chain_point.block_number - self.security_parameter;
        self.importer.import(up_to_block_number).await?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use async_trait::async_trait;
    use mockall::predicate::eq;

    use crate::chain_observer::{FakeObserver, MockChainObserver};
    use crate::entities::{BlockNumber, ChainPoint, TimePoint};
    use crate::signable_builder::MockTransactionsImporter;
    use crate::test_utils::TestLogger;

    use super::*;

    struct ImporterWithSignedEntityTypeLockCheck {
        signed_entity_type_lock: Arc<SignedEntityTypeLock>,
    }
    #[async_trait]
    impl TransactionsImporter for ImporterWithSignedEntityTypeLockCheck {
        async fn import(&self, _up_to_beacon: BlockNumber) -> StdResult<()> {
            assert!(
                self.signed_entity_type_lock
                    .is_locked(SignedEntityTypeDiscriminants::CardanoTransactions)
                    .await
            );
            Ok(())
        }
    }

    #[tokio::test]
    async fn call_its_inner_importer_when_is_activated() {
        let chain_block_number = BlockNumber(5000);
        let security_parameter = BlockNumber(542);
        let chain_observer = FakeObserver::new(Some(TimePoint {
            chain_point: ChainPoint {
                block_number: chain_block_number,
                ..ChainPoint::dummy()
            },
            ..TimePoint::dummy()
        }));
        let expected_parsed_block_number = chain_block_number - security_parameter;

        let mut importer = MockTransactionsImporter::new();
        importer
            .expect_import()
            .times(1)
            .with(eq(expected_parsed_block_number))
            .returning(|_| Ok(()));

        let preloader = CardanoTransactionsPreloader::new(
            Arc::new(SignedEntityTypeLock::default()),
            Arc::new(importer),
            security_parameter,
            Arc::new(chain_observer),
            TestLogger::stdout(),
            Arc::new(CardanoTransactionsPreloaderActivation::new(true)),
        );

        preloader.preload().await.unwrap();
    }

    #[tokio::test]
    async fn do_not_call_its_inner_importer_when_is_not_activated() {
        let mut chain_observer = MockChainObserver::new();
        chain_observer.expect_get_current_chain_point().never();
        let mut importer = MockTransactionsImporter::new();
        importer.expect_import().never();

        let preloader = CardanoTransactionsPreloader::new(
            Arc::new(SignedEntityTypeLock::default()),
            Arc::new(importer),
            BlockNumber(542),
            Arc::new(chain_observer),
            TestLogger::stdout(),
            Arc::new(CardanoTransactionsPreloaderActivation::new(false)),
        );

        preloader.preload().await.unwrap();
    }

    #[tokio::test]
    async fn return_error_when_is_activated_return_error() {
        let mut chain_observer = MockChainObserver::new();
        chain_observer.expect_get_current_chain_point().never();
        let mut importer = MockTransactionsImporter::new();
        importer.expect_import().never();

        let mut preloader_checker = MockCardanoTransactionsPreloaderChecker::new();
        preloader_checker
            .expect_is_activated()
            .returning(|| Err(anyhow::anyhow!("error")));

        let preloader = CardanoTransactionsPreloader::new(
            Arc::new(SignedEntityTypeLock::default()),
            Arc::new(importer),
            BlockNumber(542),
            Arc::new(chain_observer),
            TestLogger::stdout(),
            Arc::new(preloader_checker),
        );

        preloader
            .preload()
            .await
            .expect_err("should raise an error with error from the activation");
    }

    #[tokio::test]
    async fn fail_if_chain_point_is_not_available() {
        let chain_observer = FakeObserver::new(None);
        let mut importer = MockTransactionsImporter::new();
        importer.expect_import().never();

        let preloader = CardanoTransactionsPreloader::new(
            Arc::new(SignedEntityTypeLock::default()),
            Arc::new(importer),
            BlockNumber(0),
            Arc::new(chain_observer),
            TestLogger::stdout(),
            Arc::new(CardanoTransactionsPreloaderActivation::new(true)),
        );

        preloader
            .preload()
            .await
            .expect_err("should raise an error when chain point is not available");
    }

    #[tokio::test]
    async fn should_lock_entity_type_while_preloading() {
        let signed_entity_type_lock = Arc::new(SignedEntityTypeLock::default());

        let preloader = CardanoTransactionsPreloader::new(
            signed_entity_type_lock.clone(),
            Arc::new(ImporterWithSignedEntityTypeLockCheck {
                signed_entity_type_lock: signed_entity_type_lock.clone(),
            }),
            BlockNumber(0),
            Arc::new(FakeObserver::new(Some(TimePoint::dummy()))),
            TestLogger::stdout(),
            Arc::new(CardanoTransactionsPreloaderActivation::new(true)),
        );

        assert!(
            !signed_entity_type_lock
                .is_locked(SignedEntityTypeDiscriminants::CardanoTransactions)
                .await
        );

        preloader.preload().await.unwrap();

        assert!(
            !signed_entity_type_lock
                .is_locked(SignedEntityTypeDiscriminants::CardanoTransactions)
                .await
        );
    }

    #[tokio::test]
    async fn should_release_locked_entity_type_when_preloading_fail() {
        let signed_entity_type_lock = Arc::new(SignedEntityTypeLock::default());
        let chain_observer = FakeObserver::new(None);

        let preloader = CardanoTransactionsPreloader::new(
            signed_entity_type_lock.clone(),
            Arc::new(ImporterWithSignedEntityTypeLockCheck {
                signed_entity_type_lock: signed_entity_type_lock.clone(),
            }),
            BlockNumber(0),
            Arc::new(chain_observer),
            TestLogger::stdout(),
            Arc::new(CardanoTransactionsPreloaderActivation::new(true)),
        );

        preloader.preload().await.unwrap_err();

        assert!(
            !signed_entity_type_lock
                .is_locked(SignedEntityTypeDiscriminants::CardanoTransactions)
                .await
        );
    }
}