|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use async_trait::async_trait; |
| 4 | + |
| 5 | +use mithril_common::entities::{BlockNumber, ImmutableFileNumber}; |
| 6 | +use mithril_common::signable_builder::TransactionsImporter; |
| 7 | +use mithril_common::StdResult; |
| 8 | + |
| 9 | +/// Cardano transactions pruner |
| 10 | +#[cfg_attr(test, mockall::automock)] |
| 11 | +#[async_trait] |
| 12 | +pub trait TransactionPruner: Send + Sync { |
| 13 | + /// Prune the transactions older than the given number of blocks (based on the block range root |
| 14 | + /// stored). |
| 15 | + async fn prune(&self, number_of_blocks_to_keep: BlockNumber) -> StdResult<()>; |
| 16 | +} |
| 17 | + |
| 18 | +/// A decorator of [TransactionsImporter] that prunes the transactions older than a given number of |
| 19 | +/// blocks after running the import. |
| 20 | +/// |
| 21 | +/// If the number of blocks to keep is not provided, no pruning is performed. |
| 22 | +pub struct TransactionsImporterWithPruneDecorator { |
| 23 | + number_of_blocks_to_keep: Option<BlockNumber>, |
| 24 | + transaction_pruner: Arc<dyn TransactionPruner>, |
| 25 | + wrapped_importer: Arc<dyn TransactionsImporter>, |
| 26 | +} |
| 27 | + |
| 28 | +impl TransactionsImporterWithPruneDecorator { |
| 29 | + /// Create a new instance of [TransactionsImporterWithPruneDecorator]. |
| 30 | + pub fn new( |
| 31 | + number_of_blocks_to_keep: Option<BlockNumber>, |
| 32 | + transaction_pruner: Arc<dyn TransactionPruner>, |
| 33 | + wrapped_importer: Arc<dyn TransactionsImporter>, |
| 34 | + ) -> Self { |
| 35 | + Self { |
| 36 | + number_of_blocks_to_keep, |
| 37 | + transaction_pruner, |
| 38 | + wrapped_importer, |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[async_trait] |
| 44 | +impl TransactionsImporter for TransactionsImporterWithPruneDecorator { |
| 45 | + async fn import(&self, up_to_beacon: ImmutableFileNumber) -> StdResult<()> { |
| 46 | + self.wrapped_importer.import(up_to_beacon).await?; |
| 47 | + |
| 48 | + if let Some(number_of_blocks_to_keep) = self.number_of_blocks_to_keep { |
| 49 | + self.transaction_pruner |
| 50 | + .prune(number_of_blocks_to_keep) |
| 51 | + .await?; |
| 52 | + } |
| 53 | + |
| 54 | + Ok(()) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod tests { |
| 60 | + use mockall::mock; |
| 61 | + use mockall::predicate::eq; |
| 62 | + |
| 63 | + use super::*; |
| 64 | + |
| 65 | + mock! { |
| 66 | + pub TransactionImporterImpl {} |
| 67 | + |
| 68 | + #[async_trait] |
| 69 | + impl TransactionsImporter for TransactionImporterImpl { |
| 70 | + async fn import(&self, up_to_beacon: ImmutableFileNumber) -> StdResult<()>; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + impl TransactionsImporterWithPruneDecorator { |
| 75 | + pub fn new_with_mock<Ti, Pr>( |
| 76 | + number_of_blocks_to_keep: Option<BlockNumber>, |
| 77 | + transaction_pruner_mock_config: Pr, |
| 78 | + importer_mock_config: Ti, |
| 79 | + ) -> Self |
| 80 | + where |
| 81 | + Pr: FnOnce(&mut MockTransactionPruner), |
| 82 | + Ti: FnOnce(&mut MockTransactionImporterImpl), |
| 83 | + { |
| 84 | + let mut transaction_pruner = MockTransactionPruner::new(); |
| 85 | + transaction_pruner_mock_config(&mut transaction_pruner); |
| 86 | + let mut transaction_importer = MockTransactionImporterImpl::new(); |
| 87 | + importer_mock_config(&mut transaction_importer); |
| 88 | + |
| 89 | + Self::new( |
| 90 | + number_of_blocks_to_keep, |
| 91 | + Arc::new(transaction_pruner), |
| 92 | + Arc::new(transaction_importer), |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + #[tokio::test] |
| 98 | + async fn test_does_not_prune_if_none_is_configured() { |
| 99 | + let importer = TransactionsImporterWithPruneDecorator::new_with_mock( |
| 100 | + None, |
| 101 | + |mock| { |
| 102 | + mock.expect_prune().never(); |
| 103 | + }, |
| 104 | + |mock| { |
| 105 | + mock.expect_import().once().returning(|_| Ok(())); |
| 106 | + }, |
| 107 | + ); |
| 108 | + |
| 109 | + importer.import(10).await.expect("Import should not fail"); |
| 110 | + } |
| 111 | + |
| 112 | + #[tokio::test] |
| 113 | + async fn test_does_prune_if_a_block_number_is_configured() { |
| 114 | + let expected_block_number: BlockNumber = 5; |
| 115 | + let importer = TransactionsImporterWithPruneDecorator::new_with_mock( |
| 116 | + Some(expected_block_number), |
| 117 | + |mock| { |
| 118 | + mock.expect_prune() |
| 119 | + .with(eq(expected_block_number)) |
| 120 | + .once() |
| 121 | + .returning(|_| Ok(())); |
| 122 | + }, |
| 123 | + |mock| { |
| 124 | + mock.expect_import().once().returning(|_| Ok(())); |
| 125 | + }, |
| 126 | + ); |
| 127 | + |
| 128 | + importer.import(10).await.expect("Import should not fail"); |
| 129 | + } |
| 130 | +} |
0 commit comments