From 40a8a865f12e7096f618c8316edd99a051ca0910 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:05:35 +0200 Subject: [PATCH 01/14] Scaffold a `dependency_injection` module in Signer Based on the aggregator equivalent module. --- .../src/dependency_injection/builder.rs | 0 .../src/dependency_injection/containers.rs | 0 .../src/dependency_injection/mod.rs | 20 +++++++++++++++++++ mithril-signer/src/lib.rs | 1 + 4 files changed, 21 insertions(+) create mode 100644 mithril-signer/src/dependency_injection/builder.rs create mode 100644 mithril-signer/src/dependency_injection/containers.rs create mode 100644 mithril-signer/src/dependency_injection/mod.rs diff --git a/mithril-signer/src/dependency_injection/builder.rs b/mithril-signer/src/dependency_injection/builder.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/mithril-signer/src/dependency_injection/containers.rs b/mithril-signer/src/dependency_injection/containers.rs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/mithril-signer/src/dependency_injection/mod.rs b/mithril-signer/src/dependency_injection/mod.rs new file mode 100644 index 00000000000..8153527e641 --- /dev/null +++ b/mithril-signer/src/dependency_injection/mod.rs @@ -0,0 +1,20 @@ +//! Dependency injection module. +//! +//! This module provides tools to initialize and share resources and services +//! amongst different threads. +//! +//! It takes all its inputs from the configuration which should combine inputs from: +//! +//! * environment +//! * command line +//! * configuration files +//! * default values +//! +//! The Builder ensure every service has required dependencies to build and +//! provide services containers for each sub process. + +mod builder; +mod containers; + +pub use builder::*; +pub use containers::*; diff --git a/mithril-signer/src/lib.rs b/mithril-signer/src/lib.rs index f77311c7e7b..05b61a367e7 100644 --- a/mithril-signer/src/lib.rs +++ b/mithril-signer/src/lib.rs @@ -11,6 +11,7 @@ mod cardano_transactions_importer; mod cardano_transactions_preloader_checker; mod configuration; pub mod database; +pub mod dependency_injection; mod message_adapters; pub mod metrics; mod mktree_store_sqlite; From c28cd91d578ea7e1993fafd4aeabe6c3f9a7618f Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:20:34 +0200 Subject: [PATCH 02/14] Align Signer dependency injection components with aggregator naming * `**ServiceBuilder` -> `**DependenciesBuilder` * `SignerService` -> `SignerDependencyContainer` --- mithril-signer/src/main.rs | 6 ++-- mithril-signer/src/runtime/runner.rs | 12 +++---- mithril-signer/src/runtime/signer_services.rs | 33 ++++++++++--------- .../test_extensions/state_machine_tester.rs | 12 +++---- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/mithril-signer/src/main.rs b/mithril-signer/src/main.rs index 79b7f41485b..536de1f3737 100644 --- a/mithril-signer/src/main.rs +++ b/mithril-signer/src/main.rs @@ -16,8 +16,8 @@ use tokio::{ use mithril_common::StdResult; use mithril_doc::{Documenter, DocumenterDefault, GenerateDocCommands, StructDoc}; use mithril_signer::{ - Configuration, DefaultConfiguration, MetricsServer, ProductionServiceBuilder, ServiceBuilder, - SignerRunner, SignerState, StateMachine, + Configuration, DefaultConfiguration, DependenciesBuilder, MetricsServer, + ProductionDependenciesBuilder, SignerRunner, SignerState, StateMachine, }; /// CLI args @@ -158,7 +158,7 @@ async fn main() -> StdResult<()> { .try_deserialize() .with_context(|| "configuration deserialize error")?; - let services = ProductionServiceBuilder::new(&config) + let services = ProductionDependenciesBuilder::new(&config) .build() .await .with_context(|| "services initialization error")?; diff --git a/mithril-signer/src/runtime/runner.rs b/mithril-signer/src/runtime/runner.rs index 5ee99d16986..ae4e6c59126 100644 --- a/mithril-signer/src/runtime/runner.rs +++ b/mithril-signer/src/runtime/runner.rs @@ -16,7 +16,7 @@ use mithril_persistence::store::StakeStorer; use crate::{Configuration, MithrilProtocolInitializerBuilder}; -use super::signer_services::SignerServices; +use super::signer_services::SignerDependencyContainer; /// This trait is mainly intended for mocking. #[async_trait] @@ -100,12 +100,12 @@ pub enum RunnerError { /// Controller methods for the Signer's state machine. pub struct SignerRunner { config: Configuration, - services: SignerServices, + services: SignerDependencyContainer, } impl SignerRunner { /// Create a new Runner instance. - pub fn new(config: Configuration, services: SignerServices) -> Self { + pub fn new(config: Configuration, services: SignerDependencyContainer) -> Self { Self { services, config } } } @@ -528,7 +528,7 @@ mod tests { } } - async fn init_services() -> SignerServices { + async fn init_services() -> SignerDependencyContainer { let adapter: MemoryAdapter = MemoryAdapter::new(None).unwrap(); let stake_distribution_signers = fake_data::signers_with_stakes(2); let party_id = stake_distribution_signers[1].party_id.clone(); @@ -596,7 +596,7 @@ mod tests { )); let upkeep_service = Arc::new(MockUpkeepService::new()); - SignerServices { + SignerDependencyContainer { stake_store, certificate_handler: Arc::new(DumbAggregatorClient::default()), chain_observer, @@ -619,7 +619,7 @@ mod tests { } async fn init_runner( - maybe_services: Option, + maybe_services: Option, maybe_config: Option, ) -> SignerRunner { SignerRunner::new( diff --git a/mithril-signer/src/runtime/signer_services.rs b/mithril-signer/src/runtime/signer_services.rs index 90ac1dbee0d..a40ab7bc3c2 100644 --- a/mithril-signer/src/runtime/signer_services.rs +++ b/mithril-signer/src/runtime/signer_services.rs @@ -47,24 +47,25 @@ type SingleSignerService = Arc; type TimePointProviderService = Arc; type ProtocolInitializerStoreService = Arc; -/// The ServiceBuilder is intended to manage Services instance creation. +/// The `DependenciesBuilder` is intended to manage Services instance creation. +/// /// The goal of this is to put all this code out of the way of business code. #[async_trait] -pub trait ServiceBuilder { +pub trait DependenciesBuilder { /// Create a SignerService instance. - async fn build(&self) -> StdResult; + async fn build(&self) -> StdResult; } -/// Create a SignerService instance for Production environment. -pub struct ProductionServiceBuilder<'a> { +/// A `DependenciesBuilder` for Production environment. +pub struct ProductionDependenciesBuilder<'a> { config: &'a Configuration, chain_observer_builder: fn(&Configuration) -> StdResult, immutable_file_observer_builder: fn(&Configuration) -> StdResult>, } -impl<'a> ProductionServiceBuilder<'a> { - /// Create a new production service builder. +impl<'a> ProductionDependenciesBuilder<'a> { + /// Create a new `ProductionDependenciesBuilder`. pub fn new(config: &'a Configuration) -> Self { let chain_observer_builder: fn(&Configuration) -> StdResult = |config: &Configuration| { @@ -72,7 +73,7 @@ impl<'a> ProductionServiceBuilder<'a> { let cardano_cli_path = &config.cardano_cli_path; let cardano_node_socket_path = &config.cardano_node_socket_path; let cardano_network = &config.get_network().with_context(|| { - "Production Service Builder can not get Cardano network while building the chain observer" + "Production Dependencies Builder can not get Cardano network while building the chain observer" })?; let cardano_cli_runner = &CardanoCliRunner::new( cardano_cli_path.to_owned(), @@ -185,9 +186,9 @@ impl<'a> ProductionServiceBuilder<'a> { } #[async_trait] -impl<'a> ServiceBuilder for ProductionServiceBuilder<'a> { - /// Build a Services for the Production environment. - async fn build(&self) -> StdResult { +impl<'a> DependenciesBuilder for ProductionDependenciesBuilder<'a> { + /// Build dependencies for the Production environment. + async fn build(&self) -> StdResult { if !self.config.data_stores_directory.exists() { fs::create_dir_all(self.config.data_stores_directory.clone()).with_context(|| { format!( @@ -349,7 +350,7 @@ impl<'a> ServiceBuilder for ProductionServiceBuilder<'a> { slog_scope::logger(), )); - let services = SignerServices { + let services = SignerDependencyContainer { ticker_service, certificate_handler: aggregator_client, chain_observer, @@ -371,8 +372,8 @@ impl<'a> ServiceBuilder for ProductionServiceBuilder<'a> { } } -/// This structure groups all the services required by the state machine. -pub struct SignerServices { +/// This structure groups all the dependencies required by the state machine. +pub struct SignerDependencyContainer { /// Time point provider service pub ticker_service: TimePointProviderService, @@ -451,8 +452,8 @@ mod tests { -> StdResult> = |_config: &Configuration| Ok(Arc::new(DumbImmutableFileObserver::default())); - let mut service_builder = ProductionServiceBuilder::new(&config); - service_builder + let mut dependencies_builder = ProductionDependenciesBuilder::new(&config); + dependencies_builder .override_chain_observer_builder(chain_observer_builder) .override_immutable_file_observer_builder(immutable_file_observer_builder) .build() diff --git a/mithril-signer/tests/test_extensions/state_machine_tester.rs b/mithril-signer/tests/test_extensions/state_machine_tester.rs index c5bc6f40d84..def7859d0d4 100644 --- a/mithril-signer/tests/test_extensions/state_machine_tester.rs +++ b/mithril-signer/tests/test_extensions/state_machine_tester.rs @@ -35,8 +35,8 @@ use mithril_persistence::{ }; use mithril_signer::{ metrics::*, AggregatorClient, CardanoTransactionsImporter, Configuration, MKTreeStoreSqlite, - MetricsService, MithrilSingleSigner, ProductionServiceBuilder, ProtocolInitializerStore, - ProtocolInitializerStorer, RuntimeError, SignerRunner, SignerServices, SignerState, + MetricsService, MithrilSingleSigner, ProductionDependenciesBuilder, ProtocolInitializerStore, + ProtocolInitializerStorer, RuntimeError, SignerDependencyContainer, SignerRunner, SignerState, SignerUpkeepService, StateMachine, }; @@ -94,9 +94,9 @@ impl StateMachineTester { let selected_signer_party_id = selected_signer_with_stake.party_id.clone(); let config = Configuration::new_sample(&selected_signer_party_id); - let production_service_builder = ProductionServiceBuilder::new(&config); + let production_dependencies_builder = ProductionDependenciesBuilder::new(&config); let sqlite_connection = Arc::new( - production_service_builder + production_dependencies_builder .build_sqlite_connection( ":memory:", mithril_signer::database::migration::get_migrations(), @@ -104,7 +104,7 @@ impl StateMachineTester { .await .unwrap(), ); - let transaction_sqlite_connection = production_service_builder + let transaction_sqlite_connection = production_dependencies_builder .build_sqlite_connection( ":memory:", mithril_persistence::database::cardano_transaction_migration::get_migrations(), @@ -226,7 +226,7 @@ impl StateMachineTester { slog_scope::logger(), )); - let services = SignerServices { + let services = SignerDependencyContainer { certificate_handler: certificate_handler.clone(), ticker_service: ticker_service.clone(), chain_observer: chain_observer.clone(), From 44bd0bfba3963a0436c9dab1d7e4f95e7ec3a2e9 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:41:32 +0200 Subject: [PATCH 03/14] Move Signer Dependencies builder & container to `dependency_injection` module And remove `signer_services.rs` from the `runtime` module. --- .../src/dependency_injection/builder.rs | 413 ++++++++++++++++ .../src/dependency_injection/containers.rs | 71 +++ mithril-signer/src/main.rs | 4 +- mithril-signer/src/runtime/mod.rs | 2 - mithril-signer/src/runtime/runner.rs | 3 +- mithril-signer/src/runtime/signer_services.rs | 464 ------------------ .../test_extensions/state_machine_tester.rs | 9 +- 7 files changed, 492 insertions(+), 474 deletions(-) delete mode 100644 mithril-signer/src/runtime/signer_services.rs diff --git a/mithril-signer/src/dependency_injection/builder.rs b/mithril-signer/src/dependency_injection/builder.rs index e69de29bb2d..edb1256b7eb 100644 --- a/mithril-signer/src/dependency_injection/builder.rs +++ b/mithril-signer/src/dependency_injection/builder.rs @@ -0,0 +1,413 @@ +use std::fs; +use std::sync::Arc; +use std::time::Duration; + +use anyhow::{anyhow, Context}; +use async_trait::async_trait; +use tokio::sync::Mutex; + +use mithril_common::api_version::APIVersionProvider; +use mithril_common::cardano_block_scanner::CardanoBlockScanner; +use mithril_common::cardano_transactions_preloader::CardanoTransactionsPreloader; +use mithril_common::chain_observer::{ + CardanoCliRunner, ChainObserver, ChainObserverBuilder, ChainObserverType, +}; +use mithril_common::chain_reader::PallasChainReader; +use mithril_common::crypto_helper::{OpCert, ProtocolPartyId, SerDeShelleyFileFormat}; +use mithril_common::digesters::cache::{ + ImmutableFileDigestCacheProvider, JsonImmutableFileDigestCacheProviderBuilder, +}; +use mithril_common::digesters::{ + CardanoImmutableDigester, ImmutableFileObserver, ImmutableFileSystemObserver, +}; +use mithril_common::era::{EraChecker, EraReader}; +use mithril_common::signable_builder::{ + CardanoImmutableFilesFullSignableBuilder, CardanoStakeDistributionSignableBuilder, + CardanoTransactionsSignableBuilder, MithrilSignableBuilderService, + MithrilStakeDistributionSignableBuilder, +}; +use mithril_common::signed_entity_type_lock::SignedEntityTypeLock; +use mithril_common::{MithrilTickerService, StdResult, TickerService}; + +use mithril_persistence::database::repository::CardanoTransactionRepository; +use mithril_persistence::database::{ApplicationNodeType, SqlMigration}; +use mithril_persistence::sqlite::{ConnectionBuilder, SqliteConnection, SqliteConnectionPool}; +use mithril_persistence::store::adapter::SQLiteAdapter; +use mithril_persistence::store::StakeStore; + +use crate::dependency_injection::SignerDependencyContainer; +use crate::{ + AggregatorHTTPClient, CardanoTransactionsImporter, + CardanoTransactionsPreloaderActivationSigner, Configuration, MKTreeStoreSqlite, MetricsService, + MithrilSingleSigner, ProtocolInitializerStore, SignerUpkeepService, + TransactionsImporterByChunk, TransactionsImporterWithPruner, TransactionsImporterWithVacuum, + HTTP_REQUEST_TIMEOUT_DURATION, SQLITE_FILE, SQLITE_FILE_CARDANO_TRANSACTION, +}; + +/// The `DependenciesBuilder` is intended to manage Services instance creation. +/// +/// The goal of this is to put all this code out of the way of business code. +#[async_trait] +pub trait DependenciesBuilder { + /// Create a SignerService instance. + async fn build(&self) -> StdResult; +} + +/// A `DependenciesBuilder` for Production environment. +pub struct ProductionDependenciesBuilder<'a> { + config: &'a Configuration, + chain_observer_builder: fn(&Configuration) -> StdResult>, + immutable_file_observer_builder: + fn(&Configuration) -> StdResult>, +} + +impl<'a> ProductionDependenciesBuilder<'a> { + /// Create a new `ProductionDependenciesBuilder`. + pub fn new(config: &'a Configuration) -> Self { + let chain_observer_builder: fn(&Configuration) -> StdResult> = + |config: &Configuration| { + let chain_observer_type = ChainObserverType::Pallas; + let cardano_cli_path = &config.cardano_cli_path; + let cardano_node_socket_path = &config.cardano_node_socket_path; + let cardano_network = &config.get_network().with_context(|| { + "Production Dependencies Builder can not get Cardano network while building the chain observer" + })?; + let cardano_cli_runner = &CardanoCliRunner::new( + cardano_cli_path.to_owned(), + cardano_node_socket_path.to_owned(), + cardano_network.to_owned(), + ); + + let chain_observer_builder = ChainObserverBuilder::new( + &chain_observer_type, + cardano_node_socket_path, + cardano_network, + Some(cardano_cli_runner), + ); + + chain_observer_builder + .build() + .with_context(|| "Dependencies Builder can not build chain observer") + }; + + let immutable_file_observer_builder: fn( + &Configuration, + ) + -> StdResult> = |config: &Configuration| { + Ok(Arc::new(ImmutableFileSystemObserver::new( + &config.db_directory, + ))) + }; + + Self { + config, + chain_observer_builder, + immutable_file_observer_builder, + } + } + + /// Override immutable file observer builder. + pub fn override_immutable_file_observer_builder( + &mut self, + builder: fn(&Configuration) -> StdResult>, + ) -> &mut Self { + self.immutable_file_observer_builder = builder; + + self + } + + /// Override default chain observer builder. + pub fn override_chain_observer_builder( + &mut self, + builder: fn(&Configuration) -> StdResult>, + ) -> &mut Self { + self.chain_observer_builder = builder; + + self + } + + /// Compute protocol party id + fn compute_protocol_party_id(&self) -> StdResult { + match &self.config.operational_certificate_path { + Some(operational_certificate_path) => { + let opcert: OpCert = OpCert::from_file(operational_certificate_path) + .with_context(|| "Could not decode operational certificate")?; + Ok(opcert + .compute_protocol_party_id() + .with_context(|| "Could not compute party_id from operational certificate")?) + } + _ => Ok(self + .config + .party_id + .to_owned() + .ok_or(anyhow!("A party_id should at least be provided"))?), + } + } + + async fn build_digester_cache_provider( + &self, + ) -> StdResult>> { + if self.config.disable_digests_cache { + return Ok(None); + } + + let cache_provider = JsonImmutableFileDigestCacheProviderBuilder::new( + &self.config.data_stores_directory, + &format!("immutables_digests_{}.json", self.config.network), + ) + .should_reset_digests_cache(self.config.reset_digests_cache) + .with_logger(slog_scope::logger()) + .build() + .await?; + + Ok(Some(Arc::new(cache_provider))) + } + + /// Build a SQLite connection. + pub async fn build_sqlite_connection( + &self, + sqlite_file_name: &str, + migrations: Vec, + ) -> StdResult { + let sqlite_db_path = self.config.get_sqlite_file(sqlite_file_name)?; + let logger = slog_scope::logger(); + let connection = ConnectionBuilder::open_file(&sqlite_db_path) + .with_node_type(ApplicationNodeType::Signer) + .with_migrations(migrations) + .with_logger(logger.clone()) + .build() + .with_context(|| "Database connection initialisation error")?; + + Ok(connection) + } +} + +#[async_trait] +impl<'a> DependenciesBuilder for ProductionDependenciesBuilder<'a> { + /// Build dependencies for the Production environment. + async fn build(&self) -> StdResult { + if !self.config.data_stores_directory.exists() { + fs::create_dir_all(self.config.data_stores_directory.clone()).with_context(|| { + format!( + "Could not create data stores directory: `{}`", + self.config.data_stores_directory.display() + ) + })?; + } + + let network = self.config.get_network()?; + let sqlite_connection = Arc::new( + self.build_sqlite_connection(SQLITE_FILE, crate::database::migration::get_migrations()) + .await?, + ); + let transaction_sqlite_connection = self + .build_sqlite_connection( + SQLITE_FILE_CARDANO_TRANSACTION, + mithril_persistence::database::cardano_transaction_migration::get_migrations(), + ) + .await?; + let sqlite_connection_cardano_transaction_pool = Arc::new( + SqliteConnectionPool::build_from_connection(transaction_sqlite_connection), + ); + + let signed_entity_type_lock = Arc::new(SignedEntityTypeLock::default()); + let protocol_initializer_store = Arc::new(ProtocolInitializerStore::new( + Box::new(SQLiteAdapter::new( + "protocol_initializer", + sqlite_connection.clone(), + )?), + self.config.store_retention_limit, + )); + let single_signer = Arc::new(MithrilSingleSigner::new(self.compute_protocol_party_id()?)); + let digester = Arc::new(CardanoImmutableDigester::new( + self.build_digester_cache_provider().await?, + slog_scope::logger(), + )); + let stake_store = Arc::new(StakeStore::new( + Box::new(SQLiteAdapter::new("stake", sqlite_connection.clone())?), + self.config.store_retention_limit, + )); + let chain_observer = { + let builder = self.chain_observer_builder; + builder(self.config)? + }; + let ticker_service = { + let builder = self.immutable_file_observer_builder; + Arc::new(MithrilTickerService::new( + chain_observer.clone(), + builder(self.config)?, + )) + }; + + let era_reader = Arc::new(EraReader::new( + self.config + .build_era_reader_adapter(chain_observer.clone())?, + )); + let era_epoch_token = era_reader + .read_era_epoch_token(ticker_service.get_current_epoch().await?) + .await?; + let era_checker = Arc::new(EraChecker::new( + era_epoch_token.get_current_supported_era()?, + era_epoch_token.get_current_epoch(), + )); + + let api_version_provider = Arc::new(APIVersionProvider::new(era_checker.clone())); + let aggregator_client = Arc::new(AggregatorHTTPClient::new( + self.config.aggregator_endpoint.clone(), + self.config.relay_endpoint.clone(), + api_version_provider.clone(), + Some(Duration::from_millis(HTTP_REQUEST_TIMEOUT_DURATION)), + )); + + let cardano_immutable_snapshot_builder = + Arc::new(CardanoImmutableFilesFullSignableBuilder::new( + digester.clone(), + &self.config.db_directory, + slog_scope::logger(), + )); + let mithril_stake_distribution_signable_builder = + Arc::new(MithrilStakeDistributionSignableBuilder::default()); + let transaction_store = Arc::new(CardanoTransactionRepository::new( + sqlite_connection_cardano_transaction_pool.clone(), + )); + let chain_block_reader = + PallasChainReader::new(&self.config.cardano_node_socket_path, network); + let block_scanner = Arc::new(CardanoBlockScanner::new( + Arc::new(Mutex::new(chain_block_reader)), + self.config + .cardano_transactions_block_streamer_max_roll_forwards_per_poll, + slog_scope::logger(), + )); + let transactions_importer = Arc::new(CardanoTransactionsImporter::new( + block_scanner, + transaction_store.clone(), + slog_scope::logger(), + )); + // Wrap the transaction importer with decorator to prune the transactions after import + let transactions_importer = Arc::new(TransactionsImporterWithPruner::new( + self.config + .enable_transaction_pruning + .then_some(self.config.network_security_parameter), + transaction_store.clone(), + transactions_importer, + slog_scope::logger(), + )); + // Wrap the transaction importer with decorator to chunk its workload, so it prunes + // transactions after each chunk, reducing the storage footprint + let state_machine_transactions_importer = Arc::new(TransactionsImporterByChunk::new( + transaction_store.clone(), + transactions_importer.clone(), + self.config.transactions_import_block_chunk_size, + slog_scope::logger(), + )); + // For the preloader, we want to vacuum the database after each chunk, to reclaim disk space + // earlier than with just auto_vacuum (that execute only after the end of all import). + let preloader_transactions_importer = Arc::new(TransactionsImporterByChunk::new( + transaction_store.clone(), + Arc::new(TransactionsImporterWithVacuum::new( + sqlite_connection_cardano_transaction_pool.clone(), + transactions_importer.clone(), + slog_scope::logger(), + )), + self.config.transactions_import_block_chunk_size, + slog_scope::logger(), + )); + let block_range_root_retriever = transaction_store.clone(); + let cardano_transactions_builder = Arc::new(CardanoTransactionsSignableBuilder::< + MKTreeStoreSqlite, + >::new( + state_machine_transactions_importer, + block_range_root_retriever, + slog_scope::logger(), + )); + let cardano_stake_distribution_signable_builder = Arc::new( + CardanoStakeDistributionSignableBuilder::new(stake_store.clone()), + ); + let signable_builder_service = Arc::new(MithrilSignableBuilderService::new( + mithril_stake_distribution_signable_builder, + cardano_immutable_snapshot_builder, + cardano_transactions_builder, + cardano_stake_distribution_signable_builder, + )); + let metrics_service = Arc::new(MetricsService::new()?); + let preloader_activation = + CardanoTransactionsPreloaderActivationSigner::new(aggregator_client.clone()); + let cardano_transactions_preloader = Arc::new(CardanoTransactionsPreloader::new( + signed_entity_type_lock.clone(), + preloader_transactions_importer, + self.config.preload_security_parameter, + chain_observer.clone(), + slog_scope::logger(), + Arc::new(preloader_activation), + )); + let upkeep_service = Arc::new(SignerUpkeepService::new( + sqlite_connection.clone(), + sqlite_connection_cardano_transaction_pool, + signed_entity_type_lock.clone(), + slog_scope::logger(), + )); + + let services = SignerDependencyContainer { + ticker_service, + certificate_handler: aggregator_client, + chain_observer, + digester, + single_signer, + stake_store, + protocol_initializer_store, + era_checker, + era_reader, + api_version_provider, + signable_builder_service, + metrics_service, + signed_entity_type_lock, + cardano_transactions_preloader, + upkeep_service, + }; + + Ok(services) + } +} + +#[cfg(test)] +mod tests { + use std::path::PathBuf; + + use mithril_common::{ + chain_observer::FakeObserver, digesters::DumbImmutableFileObserver, entities::TimePoint, + test_utils::TempDir, + }; + + use super::*; + + fn get_test_dir(test_name: &str) -> PathBuf { + TempDir::create("signer_service", test_name) + } + + #[tokio::test] + async fn test_auto_create_stores_directory() { + let stores_dir = get_test_dir("test_auto_create_stores_directory").join("stores"); + let config = Configuration { + data_stores_directory: stores_dir.clone(), + ..Configuration::new_sample("party-123456") + }; + + assert!(!stores_dir.exists()); + let chain_observer_builder: fn(&Configuration) -> StdResult> = + |_config| Ok(Arc::new(FakeObserver::new(Some(TimePoint::dummy())))); + let immutable_file_observer_builder: fn( + &Configuration, + ) + -> StdResult> = + |_config: &Configuration| Ok(Arc::new(DumbImmutableFileObserver::default())); + + let mut dependencies_builder = ProductionDependenciesBuilder::new(&config); + dependencies_builder + .override_chain_observer_builder(chain_observer_builder) + .override_immutable_file_observer_builder(immutable_file_observer_builder) + .build() + .await + .expect("service builder build should not fail"); + assert!(stores_dir.exists()); + } +} diff --git a/mithril-signer/src/dependency_injection/containers.rs b/mithril-signer/src/dependency_injection/containers.rs index e69de29bb2d..9e53dc7dc8d 100644 --- a/mithril-signer/src/dependency_injection/containers.rs +++ b/mithril-signer/src/dependency_injection/containers.rs @@ -0,0 +1,71 @@ +use std::sync::Arc; + +use mithril_common::api_version::APIVersionProvider; +use mithril_common::cardano_transactions_preloader::CardanoTransactionsPreloader; +use mithril_common::chain_observer::ChainObserver; +use mithril_common::digesters::ImmutableDigester; +use mithril_common::era::{EraChecker, EraReader}; +use mithril_common::signable_builder::SignableBuilderService; +use mithril_common::signed_entity_type_lock::SignedEntityTypeLock; +use mithril_common::TickerService; +use mithril_persistence::store::StakeStore; + +use crate::{ + AggregatorClient, MetricsService, ProtocolInitializerStorer, SingleSigner, UpkeepService, +}; + +type StakeStoreService = Arc; +type CertificateHandlerService = Arc; +type ChainObserverService = Arc; +type DigesterService = Arc; +type SingleSignerService = Arc; +type TimePointProviderService = Arc; +type ProtocolInitializerStoreService = Arc; + +/// This structure groups all the dependencies required by the state machine. +pub struct SignerDependencyContainer { + /// Time point provider service + pub ticker_service: TimePointProviderService, + + /// Stake store service + pub stake_store: StakeStoreService, + + /// Certificate handler service + pub certificate_handler: CertificateHandlerService, + + /// Chain Observer service + pub chain_observer: ChainObserverService, + + /// Digester service + pub digester: DigesterService, + + /// SingleSigner service + pub single_signer: SingleSignerService, + + /// ProtocolInitializer store + pub protocol_initializer_store: ProtocolInitializerStoreService, + + /// Era checker service + pub era_checker: Arc, + + /// Era reader service + pub era_reader: Arc, + + /// API version provider + pub api_version_provider: Arc, + + /// Signable Builder Service + pub signable_builder_service: Arc, + + /// Metrics service + pub metrics_service: Arc, + + /// Signed entity type lock + pub signed_entity_type_lock: Arc, + + /// Cardano transactions preloader + pub cardano_transactions_preloader: Arc, + + /// Upkeep service + pub upkeep_service: Arc, +} diff --git a/mithril-signer/src/main.rs b/mithril-signer/src/main.rs index 536de1f3737..9652b5efe55 100644 --- a/mithril-signer/src/main.rs +++ b/mithril-signer/src/main.rs @@ -15,9 +15,9 @@ use tokio::{ use mithril_common::StdResult; use mithril_doc::{Documenter, DocumenterDefault, GenerateDocCommands, StructDoc}; +use mithril_signer::dependency_injection::{DependenciesBuilder, ProductionDependenciesBuilder}; use mithril_signer::{ - Configuration, DefaultConfiguration, DependenciesBuilder, MetricsServer, - ProductionDependenciesBuilder, SignerRunner, SignerState, StateMachine, + Configuration, DefaultConfiguration, MetricsServer, SignerRunner, SignerState, StateMachine, }; /// CLI args diff --git a/mithril-signer/src/runtime/mod.rs b/mithril-signer/src/runtime/mod.rs index 288886248bb..a5e87f07ee1 100644 --- a/mithril-signer/src/runtime/mod.rs +++ b/mithril-signer/src/runtime/mod.rs @@ -1,9 +1,7 @@ mod error; mod runner; -mod signer_services; mod state_machine; pub use error::*; pub use runner::*; -pub use signer_services::*; pub use state_machine::*; diff --git a/mithril-signer/src/runtime/runner.rs b/mithril-signer/src/runtime/runner.rs index ae4e6c59126..1de8fe9b285 100644 --- a/mithril-signer/src/runtime/runner.rs +++ b/mithril-signer/src/runtime/runner.rs @@ -14,10 +14,9 @@ use mithril_common::entities::{ use mithril_common::StdResult; use mithril_persistence::store::StakeStorer; +use crate::dependency_injection::SignerDependencyContainer; use crate::{Configuration, MithrilProtocolInitializerBuilder}; -use super::signer_services::SignerDependencyContainer; - /// This trait is mainly intended for mocking. #[async_trait] pub trait Runner: Send + Sync { diff --git a/mithril-signer/src/runtime/signer_services.rs b/mithril-signer/src/runtime/signer_services.rs deleted file mode 100644 index a40ab7bc3c2..00000000000 --- a/mithril-signer/src/runtime/signer_services.rs +++ /dev/null @@ -1,464 +0,0 @@ -use anyhow::{anyhow, Context}; -use async_trait::async_trait; -use std::{fs, sync::Arc, time::Duration}; -use tokio::sync::Mutex; - -use mithril_common::{ - api_version::APIVersionProvider, - cardano_block_scanner::CardanoBlockScanner, - cardano_transactions_preloader::CardanoTransactionsPreloader, - chain_observer::{CardanoCliRunner, ChainObserver, ChainObserverBuilder, ChainObserverType}, - chain_reader::PallasChainReader, - crypto_helper::{OpCert, ProtocolPartyId, SerDeShelleyFileFormat}, - digesters::{ - cache::{ImmutableFileDigestCacheProvider, JsonImmutableFileDigestCacheProviderBuilder}, - CardanoImmutableDigester, ImmutableDigester, ImmutableFileObserver, - ImmutableFileSystemObserver, - }, - era::{EraChecker, EraReader}, - signable_builder::{ - CardanoImmutableFilesFullSignableBuilder, CardanoStakeDistributionSignableBuilder, - CardanoTransactionsSignableBuilder, MithrilSignableBuilderService, - MithrilStakeDistributionSignableBuilder, SignableBuilderService, - }, - signed_entity_type_lock::SignedEntityTypeLock, - MithrilTickerService, StdResult, TickerService, -}; -use mithril_persistence::{ - database::{repository::CardanoTransactionRepository, ApplicationNodeType, SqlMigration}, - sqlite::{ConnectionBuilder, SqliteConnection, SqliteConnectionPool}, - store::{adapter::SQLiteAdapter, StakeStore}, -}; - -use crate::{ - aggregator_client::AggregatorClient, metrics::MetricsService, single_signer::SingleSigner, - AggregatorHTTPClient, CardanoTransactionsImporter, - CardanoTransactionsPreloaderActivationSigner, Configuration, MKTreeStoreSqlite, - MithrilSingleSigner, ProtocolInitializerStore, ProtocolInitializerStorer, SignerUpkeepService, - TransactionsImporterByChunk, TransactionsImporterWithPruner, TransactionsImporterWithVacuum, - UpkeepService, HTTP_REQUEST_TIMEOUT_DURATION, SQLITE_FILE, SQLITE_FILE_CARDANO_TRANSACTION, -}; - -type StakeStoreService = Arc; -type CertificateHandlerService = Arc; -type ChainObserverService = Arc; -type DigesterService = Arc; -type SingleSignerService = Arc; -type TimePointProviderService = Arc; -type ProtocolInitializerStoreService = Arc; - -/// The `DependenciesBuilder` is intended to manage Services instance creation. -/// -/// The goal of this is to put all this code out of the way of business code. -#[async_trait] -pub trait DependenciesBuilder { - /// Create a SignerService instance. - async fn build(&self) -> StdResult; -} - -/// A `DependenciesBuilder` for Production environment. -pub struct ProductionDependenciesBuilder<'a> { - config: &'a Configuration, - chain_observer_builder: fn(&Configuration) -> StdResult, - immutable_file_observer_builder: - fn(&Configuration) -> StdResult>, -} - -impl<'a> ProductionDependenciesBuilder<'a> { - /// Create a new `ProductionDependenciesBuilder`. - pub fn new(config: &'a Configuration) -> Self { - let chain_observer_builder: fn(&Configuration) -> StdResult = - |config: &Configuration| { - let chain_observer_type = ChainObserverType::Pallas; - let cardano_cli_path = &config.cardano_cli_path; - let cardano_node_socket_path = &config.cardano_node_socket_path; - let cardano_network = &config.get_network().with_context(|| { - "Production Dependencies Builder can not get Cardano network while building the chain observer" - })?; - let cardano_cli_runner = &CardanoCliRunner::new( - cardano_cli_path.to_owned(), - cardano_node_socket_path.to_owned(), - cardano_network.to_owned(), - ); - - let chain_observer_builder = ChainObserverBuilder::new( - &chain_observer_type, - cardano_node_socket_path, - cardano_network, - Some(cardano_cli_runner), - ); - - chain_observer_builder - .build() - .with_context(|| "Dependencies Builder can not build chain observer") - }; - - let immutable_file_observer_builder: fn( - &Configuration, - ) - -> StdResult> = |config: &Configuration| { - Ok(Arc::new(ImmutableFileSystemObserver::new( - &config.db_directory, - ))) - }; - - Self { - config, - chain_observer_builder, - immutable_file_observer_builder, - } - } - - /// Override immutable file observer builder. - pub fn override_immutable_file_observer_builder( - &mut self, - builder: fn(&Configuration) -> StdResult>, - ) -> &mut Self { - self.immutable_file_observer_builder = builder; - - self - } - - /// Override default chain observer builder. - pub fn override_chain_observer_builder( - &mut self, - builder: fn(&Configuration) -> StdResult, - ) -> &mut Self { - self.chain_observer_builder = builder; - - self - } - - /// Compute protocol party id - fn compute_protocol_party_id(&self) -> StdResult { - match &self.config.operational_certificate_path { - Some(operational_certificate_path) => { - let opcert: OpCert = OpCert::from_file(operational_certificate_path) - .with_context(|| "Could not decode operational certificate")?; - Ok(opcert - .compute_protocol_party_id() - .with_context(|| "Could not compute party_id from operational certificate")?) - } - _ => Ok(self - .config - .party_id - .to_owned() - .ok_or(anyhow!("A party_id should at least be provided"))?), - } - } - - async fn build_digester_cache_provider( - &self, - ) -> StdResult>> { - if self.config.disable_digests_cache { - return Ok(None); - } - - let cache_provider = JsonImmutableFileDigestCacheProviderBuilder::new( - &self.config.data_stores_directory, - &format!("immutables_digests_{}.json", self.config.network), - ) - .should_reset_digests_cache(self.config.reset_digests_cache) - .with_logger(slog_scope::logger()) - .build() - .await?; - - Ok(Some(Arc::new(cache_provider))) - } - - /// Build a SQLite connection. - pub async fn build_sqlite_connection( - &self, - sqlite_file_name: &str, - migrations: Vec, - ) -> StdResult { - let sqlite_db_path = self.config.get_sqlite_file(sqlite_file_name)?; - let logger = slog_scope::logger(); - let connection = ConnectionBuilder::open_file(&sqlite_db_path) - .with_node_type(ApplicationNodeType::Signer) - .with_migrations(migrations) - .with_logger(logger.clone()) - .build() - .with_context(|| "Database connection initialisation error")?; - - Ok(connection) - } -} - -#[async_trait] -impl<'a> DependenciesBuilder for ProductionDependenciesBuilder<'a> { - /// Build dependencies for the Production environment. - async fn build(&self) -> StdResult { - if !self.config.data_stores_directory.exists() { - fs::create_dir_all(self.config.data_stores_directory.clone()).with_context(|| { - format!( - "Could not create data stores directory: `{}`", - self.config.data_stores_directory.display() - ) - })?; - } - - let network = self.config.get_network()?; - let sqlite_connection = Arc::new( - self.build_sqlite_connection(SQLITE_FILE, crate::database::migration::get_migrations()) - .await?, - ); - let transaction_sqlite_connection = self - .build_sqlite_connection( - SQLITE_FILE_CARDANO_TRANSACTION, - mithril_persistence::database::cardano_transaction_migration::get_migrations(), - ) - .await?; - let sqlite_connection_cardano_transaction_pool = Arc::new( - SqliteConnectionPool::build_from_connection(transaction_sqlite_connection), - ); - - let signed_entity_type_lock = Arc::new(SignedEntityTypeLock::default()); - let protocol_initializer_store = Arc::new(ProtocolInitializerStore::new( - Box::new(SQLiteAdapter::new( - "protocol_initializer", - sqlite_connection.clone(), - )?), - self.config.store_retention_limit, - )); - let single_signer = Arc::new(MithrilSingleSigner::new(self.compute_protocol_party_id()?)); - let digester = Arc::new(CardanoImmutableDigester::new( - self.build_digester_cache_provider().await?, - slog_scope::logger(), - )); - let stake_store = Arc::new(StakeStore::new( - Box::new(SQLiteAdapter::new("stake", sqlite_connection.clone())?), - self.config.store_retention_limit, - )); - let chain_observer = { - let builder = self.chain_observer_builder; - builder(self.config)? - }; - let ticker_service = { - let builder = self.immutable_file_observer_builder; - Arc::new(MithrilTickerService::new( - chain_observer.clone(), - builder(self.config)?, - )) - }; - - let era_reader = Arc::new(EraReader::new( - self.config - .build_era_reader_adapter(chain_observer.clone())?, - )); - let era_epoch_token = era_reader - .read_era_epoch_token(ticker_service.get_current_epoch().await?) - .await?; - let era_checker = Arc::new(EraChecker::new( - era_epoch_token.get_current_supported_era()?, - era_epoch_token.get_current_epoch(), - )); - - let api_version_provider = Arc::new(APIVersionProvider::new(era_checker.clone())); - let aggregator_client = Arc::new(AggregatorHTTPClient::new( - self.config.aggregator_endpoint.clone(), - self.config.relay_endpoint.clone(), - api_version_provider.clone(), - Some(Duration::from_millis(HTTP_REQUEST_TIMEOUT_DURATION)), - )); - - let cardano_immutable_snapshot_builder = - Arc::new(CardanoImmutableFilesFullSignableBuilder::new( - digester.clone(), - &self.config.db_directory, - slog_scope::logger(), - )); - let mithril_stake_distribution_signable_builder = - Arc::new(MithrilStakeDistributionSignableBuilder::default()); - let transaction_store = Arc::new(CardanoTransactionRepository::new( - sqlite_connection_cardano_transaction_pool.clone(), - )); - let chain_block_reader = - PallasChainReader::new(&self.config.cardano_node_socket_path, network); - let block_scanner = Arc::new(CardanoBlockScanner::new( - Arc::new(Mutex::new(chain_block_reader)), - self.config - .cardano_transactions_block_streamer_max_roll_forwards_per_poll, - slog_scope::logger(), - )); - let transactions_importer = Arc::new(CardanoTransactionsImporter::new( - block_scanner, - transaction_store.clone(), - slog_scope::logger(), - )); - // Wrap the transaction importer with decorator to prune the transactions after import - let transactions_importer = Arc::new(TransactionsImporterWithPruner::new( - self.config - .enable_transaction_pruning - .then_some(self.config.network_security_parameter), - transaction_store.clone(), - transactions_importer, - slog_scope::logger(), - )); - // Wrap the transaction importer with decorator to chunk its workload, so it prunes - // transactions after each chunk, reducing the storage footprint - let state_machine_transactions_importer = Arc::new(TransactionsImporterByChunk::new( - transaction_store.clone(), - transactions_importer.clone(), - self.config.transactions_import_block_chunk_size, - slog_scope::logger(), - )); - // For the preloader, we want to vacuum the database after each chunk, to reclaim disk space - // earlier than with just auto_vacuum (that execute only after the end of all import). - let preloader_transactions_importer = Arc::new(TransactionsImporterByChunk::new( - transaction_store.clone(), - Arc::new(TransactionsImporterWithVacuum::new( - sqlite_connection_cardano_transaction_pool.clone(), - transactions_importer.clone(), - slog_scope::logger(), - )), - self.config.transactions_import_block_chunk_size, - slog_scope::logger(), - )); - let block_range_root_retriever = transaction_store.clone(); - let cardano_transactions_builder = Arc::new(CardanoTransactionsSignableBuilder::< - MKTreeStoreSqlite, - >::new( - state_machine_transactions_importer, - block_range_root_retriever, - slog_scope::logger(), - )); - let cardano_stake_distribution_signable_builder = Arc::new( - CardanoStakeDistributionSignableBuilder::new(stake_store.clone()), - ); - let signable_builder_service = Arc::new(MithrilSignableBuilderService::new( - mithril_stake_distribution_signable_builder, - cardano_immutable_snapshot_builder, - cardano_transactions_builder, - cardano_stake_distribution_signable_builder, - )); - let metrics_service = Arc::new(MetricsService::new().unwrap()); - let preloader_activation = - CardanoTransactionsPreloaderActivationSigner::new(aggregator_client.clone()); - let cardano_transactions_preloader = Arc::new(CardanoTransactionsPreloader::new( - signed_entity_type_lock.clone(), - preloader_transactions_importer, - self.config.preload_security_parameter, - chain_observer.clone(), - slog_scope::logger(), - Arc::new(preloader_activation), - )); - let upkeep_service = Arc::new(SignerUpkeepService::new( - sqlite_connection.clone(), - sqlite_connection_cardano_transaction_pool, - signed_entity_type_lock.clone(), - slog_scope::logger(), - )); - - let services = SignerDependencyContainer { - ticker_service, - certificate_handler: aggregator_client, - chain_observer, - digester, - single_signer, - stake_store, - protocol_initializer_store, - era_checker, - era_reader, - api_version_provider, - signable_builder_service, - metrics_service, - signed_entity_type_lock, - cardano_transactions_preloader, - upkeep_service, - }; - - Ok(services) - } -} - -/// This structure groups all the dependencies required by the state machine. -pub struct SignerDependencyContainer { - /// Time point provider service - pub ticker_service: TimePointProviderService, - - /// Stake store service - pub stake_store: StakeStoreService, - - /// Certificate handler service - pub certificate_handler: CertificateHandlerService, - - /// Chain Observer service - pub chain_observer: ChainObserverService, - - /// Digester service - pub digester: DigesterService, - - /// SingleSigner service - pub single_signer: SingleSignerService, - - /// ProtocolInitializer store - pub protocol_initializer_store: ProtocolInitializerStoreService, - - /// Era checker service - pub era_checker: Arc, - - /// Era reader service - pub era_reader: Arc, - - /// API version provider - pub api_version_provider: Arc, - - /// Signable Builder Service - pub signable_builder_service: Arc, - - /// Metrics service - pub metrics_service: Arc, - - /// Signed entity type lock - pub signed_entity_type_lock: Arc, - - /// Cardano transactions preloader - pub cardano_transactions_preloader: Arc, - - /// Upkeep service - pub upkeep_service: Arc, -} - -#[cfg(test)] -mod tests { - use std::path::PathBuf; - - use mithril_common::{ - chain_observer::FakeObserver, digesters::DumbImmutableFileObserver, entities::TimePoint, - test_utils::TempDir, - }; - - use super::*; - - fn get_test_dir(test_name: &str) -> PathBuf { - TempDir::create("signer_service", test_name) - } - - #[tokio::test] - async fn test_auto_create_stores_directory() { - let stores_dir = get_test_dir("test_auto_create_stores_directory").join("stores"); - let config = Configuration { - data_stores_directory: stores_dir.clone(), - ..Configuration::new_sample("party-123456") - }; - - assert!(!stores_dir.exists()); - let chain_observer_builder: fn(&Configuration) -> StdResult = - |_config| Ok(Arc::new(FakeObserver::new(Some(TimePoint::dummy())))); - let immutable_file_observer_builder: fn( - &Configuration, - ) - -> StdResult> = - |_config: &Configuration| Ok(Arc::new(DumbImmutableFileObserver::default())); - - let mut dependencies_builder = ProductionDependenciesBuilder::new(&config); - dependencies_builder - .override_chain_observer_builder(chain_observer_builder) - .override_immutable_file_observer_builder(immutable_file_observer_builder) - .build() - .await - .expect("service builder build should not fail"); - assert!(stores_dir.exists()); - } -} diff --git a/mithril-signer/tests/test_extensions/state_machine_tester.rs b/mithril-signer/tests/test_extensions/state_machine_tester.rs index def7859d0d4..6f232f75c81 100644 --- a/mithril-signer/tests/test_extensions/state_machine_tester.rs +++ b/mithril-signer/tests/test_extensions/state_machine_tester.rs @@ -34,10 +34,11 @@ use mithril_persistence::{ store::{StakeStore, StakeStorer}, }; use mithril_signer::{ - metrics::*, AggregatorClient, CardanoTransactionsImporter, Configuration, MKTreeStoreSqlite, - MetricsService, MithrilSingleSigner, ProductionDependenciesBuilder, ProtocolInitializerStore, - ProtocolInitializerStorer, RuntimeError, SignerDependencyContainer, SignerRunner, SignerState, - SignerUpkeepService, StateMachine, + dependency_injection::{ProductionDependenciesBuilder, SignerDependencyContainer}, + metrics::*, + AggregatorClient, CardanoTransactionsImporter, Configuration, MKTreeStoreSqlite, + MetricsService, MithrilSingleSigner, ProtocolInitializerStore, ProtocolInitializerStorer, + RuntimeError, SignerRunner, SignerState, SignerUpkeepService, StateMachine, }; use super::FakeAggregator; From 9c7c3496b57f2d9a23798d2ce624c11fe03a19b6 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:50:33 +0200 Subject: [PATCH 04/14] Scaffold a `services` module in Signer --- mithril-signer/src/lib.rs | 2 ++ mithril-signer/src/services/mod.rs | 0 2 files changed, 2 insertions(+) create mode 100644 mithril-signer/src/services/mod.rs diff --git a/mithril-signer/src/lib.rs b/mithril-signer/src/lib.rs index 05b61a367e7..cffc1fd29f4 100644 --- a/mithril-signer/src/lib.rs +++ b/mithril-signer/src/lib.rs @@ -17,6 +17,7 @@ pub mod metrics; mod mktree_store_sqlite; mod protocol_initializer_store; mod runtime; +mod services; mod single_signer; mod transactions_importer_by_chunk; mod transactions_importer_with_pruner; @@ -36,6 +37,7 @@ pub use metrics::*; pub use mktree_store_sqlite::*; pub use protocol_initializer_store::{ProtocolInitializerStore, ProtocolInitializerStorer}; pub use runtime::*; +pub use services::*; pub use single_signer::*; pub use transactions_importer_by_chunk::*; pub use transactions_importer_with_pruner::*; diff --git a/mithril-signer/src/services/mod.rs b/mithril-signer/src/services/mod.rs new file mode 100644 index 00000000000..e69de29bb2d From 23791da5dd1a61542992a6aee3401650acd5a3c6 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:53:44 +0200 Subject: [PATCH 05/14] Move several Signer services to `services` mod While keeping import paths identical for now. --- mithril-signer/src/lib.rs | 18 ------------------ .../src/{ => services}/aggregator_client.rs | 0 .../cardano_transactions_importer.rs | 0 .../cardano_transactions_preloader_checker.rs | 0 mithril-signer/src/services/mod.rs | 19 +++++++++++++++++++ .../src/{ => services}/single_signer.rs | 0 .../transactions_importer_by_chunk.rs | 0 .../transactions_importer_with_pruner.rs | 0 .../transactions_importer_with_vacuum.rs | 0 .../src/{ => services}/upkeep_service.rs | 0 10 files changed, 19 insertions(+), 18 deletions(-) rename mithril-signer/src/{ => services}/aggregator_client.rs (100%) rename mithril-signer/src/{ => services}/cardano_transactions_importer.rs (100%) rename mithril-signer/src/{ => services}/cardano_transactions_preloader_checker.rs (100%) rename mithril-signer/src/{ => services}/single_signer.rs (100%) rename mithril-signer/src/{ => services}/transactions_importer_by_chunk.rs (100%) rename mithril-signer/src/{ => services}/transactions_importer_with_pruner.rs (100%) rename mithril-signer/src/{ => services}/transactions_importer_with_vacuum.rs (100%) rename mithril-signer/src/{ => services}/upkeep_service.rs (100%) diff --git a/mithril-signer/src/lib.rs b/mithril-signer/src/lib.rs index cffc1fd29f4..d69dcd30c9c 100644 --- a/mithril-signer/src/lib.rs +++ b/mithril-signer/src/lib.rs @@ -6,9 +6,6 @@ //! See the [Mithril documentation](https://mithril.network/doc/manual/developer-docs/nodes/mithril-signer) //! for more information on how it works. -mod aggregator_client; -mod cardano_transactions_importer; -mod cardano_transactions_preloader_checker; mod configuration; pub mod database; pub mod dependency_injection; @@ -18,17 +15,7 @@ mod mktree_store_sqlite; mod protocol_initializer_store; mod runtime; mod services; -mod single_signer; -mod transactions_importer_by_chunk; -mod transactions_importer_with_pruner; -mod transactions_importer_with_vacuum; -mod upkeep_service; -#[cfg(test)] -pub use aggregator_client::dumb::DumbAggregatorClient; -pub use aggregator_client::*; -pub use cardano_transactions_importer::*; -pub use cardano_transactions_preloader_checker::*; pub use configuration::{Configuration, DefaultConfiguration}; pub use message_adapters::{ FromEpochSettingsAdapter, FromPendingCertificateMessageAdapter, ToRegisterSignerMessageAdapter, @@ -38,11 +25,6 @@ pub use mktree_store_sqlite::*; pub use protocol_initializer_store::{ProtocolInitializerStore, ProtocolInitializerStorer}; pub use runtime::*; pub use services::*; -pub use single_signer::*; -pub use transactions_importer_by_chunk::*; -pub use transactions_importer_with_pruner::*; -pub use transactions_importer_with_vacuum::*; -pub use upkeep_service::*; /// HTTP request timeout duration in milliseconds const HTTP_REQUEST_TIMEOUT_DURATION: u64 = 30000; diff --git a/mithril-signer/src/aggregator_client.rs b/mithril-signer/src/services/aggregator_client.rs similarity index 100% rename from mithril-signer/src/aggregator_client.rs rename to mithril-signer/src/services/aggregator_client.rs diff --git a/mithril-signer/src/cardano_transactions_importer.rs b/mithril-signer/src/services/cardano_transactions_importer.rs similarity index 100% rename from mithril-signer/src/cardano_transactions_importer.rs rename to mithril-signer/src/services/cardano_transactions_importer.rs diff --git a/mithril-signer/src/cardano_transactions_preloader_checker.rs b/mithril-signer/src/services/cardano_transactions_preloader_checker.rs similarity index 100% rename from mithril-signer/src/cardano_transactions_preloader_checker.rs rename to mithril-signer/src/services/cardano_transactions_preloader_checker.rs diff --git a/mithril-signer/src/services/mod.rs b/mithril-signer/src/services/mod.rs index e69de29bb2d..5c618779467 100644 --- a/mithril-signer/src/services/mod.rs +++ b/mithril-signer/src/services/mod.rs @@ -0,0 +1,19 @@ +mod aggregator_client; +mod cardano_transactions_importer; +mod cardano_transactions_preloader_checker; +mod single_signer; +mod transactions_importer_by_chunk; +mod transactions_importer_with_pruner; +mod transactions_importer_with_vacuum; +mod upkeep_service; + +#[cfg(test)] +pub use aggregator_client::dumb::DumbAggregatorClient; +pub use aggregator_client::*; +pub use cardano_transactions_importer::*; +pub use cardano_transactions_preloader_checker::*; +pub use single_signer::*; +pub use transactions_importer_by_chunk::*; +pub use transactions_importer_with_pruner::*; +pub use transactions_importer_with_vacuum::*; +pub use upkeep_service::*; diff --git a/mithril-signer/src/single_signer.rs b/mithril-signer/src/services/single_signer.rs similarity index 100% rename from mithril-signer/src/single_signer.rs rename to mithril-signer/src/services/single_signer.rs diff --git a/mithril-signer/src/transactions_importer_by_chunk.rs b/mithril-signer/src/services/transactions_importer_by_chunk.rs similarity index 100% rename from mithril-signer/src/transactions_importer_by_chunk.rs rename to mithril-signer/src/services/transactions_importer_by_chunk.rs diff --git a/mithril-signer/src/transactions_importer_with_pruner.rs b/mithril-signer/src/services/transactions_importer_with_pruner.rs similarity index 100% rename from mithril-signer/src/transactions_importer_with_pruner.rs rename to mithril-signer/src/services/transactions_importer_with_pruner.rs diff --git a/mithril-signer/src/transactions_importer_with_vacuum.rs b/mithril-signer/src/services/transactions_importer_with_vacuum.rs similarity index 100% rename from mithril-signer/src/transactions_importer_with_vacuum.rs rename to mithril-signer/src/services/transactions_importer_with_vacuum.rs diff --git a/mithril-signer/src/upkeep_service.rs b/mithril-signer/src/services/upkeep_service.rs similarity index 100% rename from mithril-signer/src/upkeep_service.rs rename to mithril-signer/src/services/upkeep_service.rs From 556acef2e9e37e45276b31023a12f67b155be0d5 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:04:32 +0200 Subject: [PATCH 06/14] Scope signer services import to the `services` module Instead of reexporting them directly at the lib root. --- .../repository/cardano_transaction_repository.rs | 2 +- mithril-signer/src/dependency_injection/builder.rs | 8 +++++--- mithril-signer/src/dependency_injection/containers.rs | 5 ++--- mithril-signer/src/lib.rs | 3 +-- mithril-signer/src/runtime/runner.rs | 11 ++++++----- .../cardano_transactions_preloader_checker.rs | 4 ++-- mithril-signer/src/services/mod.rs | 11 +++++++++++ .../tests/test_extensions/certificate_handler.rs | 2 +- .../tests/test_extensions/state_machine_tester.rs | 8 +++++--- 9 files changed, 34 insertions(+), 20 deletions(-) diff --git a/mithril-signer/src/database/repository/cardano_transaction_repository.rs b/mithril-signer/src/database/repository/cardano_transaction_repository.rs index 9d2c039c255..9119e0711d1 100644 --- a/mithril-signer/src/database/repository/cardano_transaction_repository.rs +++ b/mithril-signer/src/database/repository/cardano_transaction_repository.rs @@ -9,7 +9,7 @@ use mithril_common::entities::{ use mithril_common::StdResult; use mithril_persistence::database::repository::CardanoTransactionRepository; -use crate::{HighestTransactionBlockNumberGetter, TransactionPruner, TransactionStore}; +use crate::services::{HighestTransactionBlockNumberGetter, TransactionPruner, TransactionStore}; #[async_trait] impl TransactionStore for CardanoTransactionRepository { diff --git a/mithril-signer/src/dependency_injection/builder.rs b/mithril-signer/src/dependency_injection/builder.rs index edb1256b7eb..3b98d6befdc 100644 --- a/mithril-signer/src/dependency_injection/builder.rs +++ b/mithril-signer/src/dependency_injection/builder.rs @@ -36,11 +36,13 @@ use mithril_persistence::store::adapter::SQLiteAdapter; use mithril_persistence::store::StakeStore; use crate::dependency_injection::SignerDependencyContainer; -use crate::{ +use crate::services::{ AggregatorHTTPClient, CardanoTransactionsImporter, - CardanoTransactionsPreloaderActivationSigner, Configuration, MKTreeStoreSqlite, MetricsService, - MithrilSingleSigner, ProtocolInitializerStore, SignerUpkeepService, + CardanoTransactionsPreloaderActivationSigner, MithrilSingleSigner, SignerUpkeepService, TransactionsImporterByChunk, TransactionsImporterWithPruner, TransactionsImporterWithVacuum, +}; +use crate::{ + Configuration, MKTreeStoreSqlite, MetricsService, ProtocolInitializerStore, HTTP_REQUEST_TIMEOUT_DURATION, SQLITE_FILE, SQLITE_FILE_CARDANO_TRANSACTION, }; diff --git a/mithril-signer/src/dependency_injection/containers.rs b/mithril-signer/src/dependency_injection/containers.rs index 9e53dc7dc8d..1c20d6410c9 100644 --- a/mithril-signer/src/dependency_injection/containers.rs +++ b/mithril-signer/src/dependency_injection/containers.rs @@ -10,9 +10,8 @@ use mithril_common::signed_entity_type_lock::SignedEntityTypeLock; use mithril_common::TickerService; use mithril_persistence::store::StakeStore; -use crate::{ - AggregatorClient, MetricsService, ProtocolInitializerStorer, SingleSigner, UpkeepService, -}; +use crate::services::{AggregatorClient, SingleSigner, UpkeepService}; +use crate::{MetricsService, ProtocolInitializerStorer}; type StakeStoreService = Arc; type CertificateHandlerService = Arc; diff --git a/mithril-signer/src/lib.rs b/mithril-signer/src/lib.rs index d69dcd30c9c..90318600e92 100644 --- a/mithril-signer/src/lib.rs +++ b/mithril-signer/src/lib.rs @@ -14,7 +14,7 @@ pub mod metrics; mod mktree_store_sqlite; mod protocol_initializer_store; mod runtime; -mod services; +pub mod services; pub use configuration::{Configuration, DefaultConfiguration}; pub use message_adapters::{ @@ -24,7 +24,6 @@ pub use metrics::*; pub use mktree_store_sqlite::*; pub use protocol_initializer_store::{ProtocolInitializerStore, ProtocolInitializerStorer}; pub use runtime::*; -pub use services::*; /// HTTP request timeout duration in milliseconds const HTTP_REQUEST_TIMEOUT_DURATION: u64 = 30000; diff --git a/mithril-signer/src/runtime/runner.rs b/mithril-signer/src/runtime/runner.rs index 1de8fe9b285..b289e3c7f9b 100644 --- a/mithril-signer/src/runtime/runner.rs +++ b/mithril-signer/src/runtime/runner.rs @@ -15,7 +15,8 @@ use mithril_common::StdResult; use mithril_persistence::store::StakeStorer; use crate::dependency_injection::SignerDependencyContainer; -use crate::{Configuration, MithrilProtocolInitializerBuilder}; +use crate::services::MithrilProtocolInitializerBuilder; +use crate::Configuration; /// This trait is mainly intended for mocking. #[async_trait] @@ -491,11 +492,11 @@ mod tests { use mockall::mock; use std::{path::Path, sync::Arc}; - use crate::{ - metrics::MetricsService, AggregatorClient, CardanoTransactionsImporter, - DumbAggregatorClient, MithrilSingleSigner, MockAggregatorClient, MockTransactionStore, - MockUpkeepService, ProtocolInitializerStore, SingleSigner, + use crate::services::{ + AggregatorClient, CardanoTransactionsImporter, DumbAggregatorClient, MithrilSingleSigner, + MockAggregatorClient, MockTransactionStore, MockUpkeepService, SingleSigner, }; + use crate::{metrics::MetricsService, ProtocolInitializerStore}; use super::*; diff --git a/mithril-signer/src/services/cardano_transactions_preloader_checker.rs b/mithril-signer/src/services/cardano_transactions_preloader_checker.rs index c26111728b5..74508423ff6 100644 --- a/mithril-signer/src/services/cardano_transactions_preloader_checker.rs +++ b/mithril-signer/src/services/cardano_transactions_preloader_checker.rs @@ -8,7 +8,7 @@ use mithril_common::{ entities::SignedEntityTypeDiscriminants, StdResult, }; -use crate::AggregatorClient; +use crate::services::AggregatorClient; /// CardanoTransactionsPreloaderActivationSigner pub struct CardanoTransactionsPreloaderActivationSigner { @@ -47,7 +47,7 @@ mod tests { entities::SignedEntityTypeDiscriminants, messages::AggregatorFeaturesMessage, }; - use crate::{AggregatorClientError, MockAggregatorClient}; + use crate::services::{AggregatorClientError, MockAggregatorClient}; use super::*; diff --git a/mithril-signer/src/services/mod.rs b/mithril-signer/src/services/mod.rs index 5c618779467..4e751656720 100644 --- a/mithril-signer/src/services/mod.rs +++ b/mithril-signer/src/services/mod.rs @@ -1,3 +1,14 @@ +//! # Services +//! +//! This module regroups services. Services are adapters in charge of the different bounded contexts of the application: +//! +//! * Aggregator Client: communicate with the Aggregator +//! * Cardano Transaction Importer: Import transactions from the Cardano chain +//! * Single Signer: create single signatures +//! * Upkeep: perform maintenance tasks +//! +//! Each service is defined by a public API (a trait) that is used in the controllers (runtimes). + mod aggregator_client; mod cardano_transactions_importer; mod cardano_transactions_preloader_checker; diff --git a/mithril-signer/tests/test_extensions/certificate_handler.rs b/mithril-signer/tests/test_extensions/certificate_handler.rs index 7e24411c400..6a073a132db 100644 --- a/mithril-signer/tests/test_extensions/certificate_handler.rs +++ b/mithril-signer/tests/test_extensions/certificate_handler.rs @@ -11,7 +11,7 @@ use mithril_common::{ test_utils::fake_data, MithrilTickerService, TickerService, }; -use mithril_signer::{AggregatorClient, AggregatorClientError}; +use mithril_signer::services::{AggregatorClient, AggregatorClientError}; use tokio::sync::RwLock; pub struct FakeAggregator { diff --git a/mithril-signer/tests/test_extensions/state_machine_tester.rs b/mithril-signer/tests/test_extensions/state_machine_tester.rs index 6f232f75c81..7e743c2c3e9 100644 --- a/mithril-signer/tests/test_extensions/state_machine_tester.rs +++ b/mithril-signer/tests/test_extensions/state_machine_tester.rs @@ -36,9 +36,11 @@ use mithril_persistence::{ use mithril_signer::{ dependency_injection::{ProductionDependenciesBuilder, SignerDependencyContainer}, metrics::*, - AggregatorClient, CardanoTransactionsImporter, Configuration, MKTreeStoreSqlite, - MetricsService, MithrilSingleSigner, ProtocolInitializerStore, ProtocolInitializerStorer, - RuntimeError, SignerRunner, SignerState, SignerUpkeepService, StateMachine, + services::{ + AggregatorClient, CardanoTransactionsImporter, MithrilSingleSigner, SignerUpkeepService, + }, + Configuration, MKTreeStoreSqlite, MetricsService, ProtocolInitializerStore, + ProtocolInitializerStorer, RuntimeError, SignerRunner, SignerState, StateMachine, }; use super::FakeAggregator; From 141d68c4d8602f9647db53edc03a52f8d75af3f3 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:05:54 +0200 Subject: [PATCH 07/14] Promote Signer `services::cardano_transactions_importer` mod to a directory And move existing code to a `service.rs` mod to avoid putting everything in the `mod.rs`. This is a ground work to move all importers to this directory --- .../src/services/cardano_transactions_importer/mod.rs | 3 +++ .../service.rs} | 0 2 files changed, 3 insertions(+) create mode 100644 mithril-signer/src/services/cardano_transactions_importer/mod.rs rename mithril-signer/src/services/{cardano_transactions_importer.rs => cardano_transactions_importer/service.rs} (100%) diff --git a/mithril-signer/src/services/cardano_transactions_importer/mod.rs b/mithril-signer/src/services/cardano_transactions_importer/mod.rs new file mode 100644 index 00000000000..9d0e40166e1 --- /dev/null +++ b/mithril-signer/src/services/cardano_transactions_importer/mod.rs @@ -0,0 +1,3 @@ +mod service; + +pub use service::*; diff --git a/mithril-signer/src/services/cardano_transactions_importer.rs b/mithril-signer/src/services/cardano_transactions_importer/service.rs similarity index 100% rename from mithril-signer/src/services/cardano_transactions_importer.rs rename to mithril-signer/src/services/cardano_transactions_importer/service.rs From 8c0797366331c3c3af1a2470a4605b3e9275b2ba Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:13:54 +0200 Subject: [PATCH 08/14] Move tx importer decorators to the new module --- .../importer_by_chunk.rs} | 0 .../importer_with_pruner.rs} | 0 .../importer_with_vacuum.rs} | 0 .../src/services/cardano_transactions_importer/mod.rs | 6 ++++++ mithril-signer/src/services/mod.rs | 6 ------ 5 files changed, 6 insertions(+), 6 deletions(-) rename mithril-signer/src/services/{transactions_importer_by_chunk.rs => cardano_transactions_importer/importer_by_chunk.rs} (100%) rename mithril-signer/src/services/{transactions_importer_with_pruner.rs => cardano_transactions_importer/importer_with_pruner.rs} (100%) rename mithril-signer/src/services/{transactions_importer_with_vacuum.rs => cardano_transactions_importer/importer_with_vacuum.rs} (100%) diff --git a/mithril-signer/src/services/transactions_importer_by_chunk.rs b/mithril-signer/src/services/cardano_transactions_importer/importer_by_chunk.rs similarity index 100% rename from mithril-signer/src/services/transactions_importer_by_chunk.rs rename to mithril-signer/src/services/cardano_transactions_importer/importer_by_chunk.rs diff --git a/mithril-signer/src/services/transactions_importer_with_pruner.rs b/mithril-signer/src/services/cardano_transactions_importer/importer_with_pruner.rs similarity index 100% rename from mithril-signer/src/services/transactions_importer_with_pruner.rs rename to mithril-signer/src/services/cardano_transactions_importer/importer_with_pruner.rs diff --git a/mithril-signer/src/services/transactions_importer_with_vacuum.rs b/mithril-signer/src/services/cardano_transactions_importer/importer_with_vacuum.rs similarity index 100% rename from mithril-signer/src/services/transactions_importer_with_vacuum.rs rename to mithril-signer/src/services/cardano_transactions_importer/importer_with_vacuum.rs diff --git a/mithril-signer/src/services/cardano_transactions_importer/mod.rs b/mithril-signer/src/services/cardano_transactions_importer/mod.rs index 9d0e40166e1..8abf337edff 100644 --- a/mithril-signer/src/services/cardano_transactions_importer/mod.rs +++ b/mithril-signer/src/services/cardano_transactions_importer/mod.rs @@ -1,3 +1,9 @@ +mod importer_by_chunk; +mod importer_with_pruner; +mod importer_with_vacuum; mod service; +pub use importer_by_chunk::*; +pub use importer_with_pruner::*; +pub use importer_with_vacuum::*; pub use service::*; diff --git a/mithril-signer/src/services/mod.rs b/mithril-signer/src/services/mod.rs index 4e751656720..931b00751de 100644 --- a/mithril-signer/src/services/mod.rs +++ b/mithril-signer/src/services/mod.rs @@ -13,9 +13,6 @@ mod aggregator_client; mod cardano_transactions_importer; mod cardano_transactions_preloader_checker; mod single_signer; -mod transactions_importer_by_chunk; -mod transactions_importer_with_pruner; -mod transactions_importer_with_vacuum; mod upkeep_service; #[cfg(test)] @@ -24,7 +21,4 @@ pub use aggregator_client::*; pub use cardano_transactions_importer::*; pub use cardano_transactions_preloader_checker::*; pub use single_signer::*; -pub use transactions_importer_by_chunk::*; -pub use transactions_importer_with_pruner::*; -pub use transactions_importer_with_vacuum::*; pub use upkeep_service::*; From 01e9188e721c832db7938d3de94c3974979a2522 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:16:50 +0200 Subject: [PATCH 09/14] Scaffold a `store` module in Signer --- mithril-signer/src/store/mod.rs | 1 + 1 file changed, 1 insertion(+) create mode 100644 mithril-signer/src/store/mod.rs diff --git a/mithril-signer/src/store/mod.rs b/mithril-signer/src/store/mod.rs new file mode 100644 index 00000000000..821942eaccb --- /dev/null +++ b/mithril-signer/src/store/mod.rs @@ -0,0 +1 @@ +//! Alternative storage backends when relational database capabilities are not needed. From 8b6511d698c9a69a14b7d1d34157e5d4b316dc1e Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:40:45 +0200 Subject: [PATCH 10/14] Move Signer stores to their new dedicated mod --- mithril-signer/src/lib.rs | 6 ++---- mithril-signer/src/{ => store}/mktree_store_sqlite.rs | 0 mithril-signer/src/store/mod.rs | 6 ++++++ .../src/{ => store}/protocol_initializer_store.rs | 0 4 files changed, 8 insertions(+), 4 deletions(-) rename mithril-signer/src/{ => store}/mktree_store_sqlite.rs (100%) rename mithril-signer/src/{ => store}/protocol_initializer_store.rs (100%) diff --git a/mithril-signer/src/lib.rs b/mithril-signer/src/lib.rs index 90318600e92..5f1316c52ad 100644 --- a/mithril-signer/src/lib.rs +++ b/mithril-signer/src/lib.rs @@ -11,19 +11,17 @@ pub mod database; pub mod dependency_injection; mod message_adapters; pub mod metrics; -mod mktree_store_sqlite; -mod protocol_initializer_store; mod runtime; pub mod services; +mod store; pub use configuration::{Configuration, DefaultConfiguration}; pub use message_adapters::{ FromEpochSettingsAdapter, FromPendingCertificateMessageAdapter, ToRegisterSignerMessageAdapter, }; pub use metrics::*; -pub use mktree_store_sqlite::*; -pub use protocol_initializer_store::{ProtocolInitializerStore, ProtocolInitializerStorer}; pub use runtime::*; +pub use store::*; /// HTTP request timeout duration in milliseconds const HTTP_REQUEST_TIMEOUT_DURATION: u64 = 30000; diff --git a/mithril-signer/src/mktree_store_sqlite.rs b/mithril-signer/src/store/mktree_store_sqlite.rs similarity index 100% rename from mithril-signer/src/mktree_store_sqlite.rs rename to mithril-signer/src/store/mktree_store_sqlite.rs diff --git a/mithril-signer/src/store/mod.rs b/mithril-signer/src/store/mod.rs index 821942eaccb..ebcd46647c9 100644 --- a/mithril-signer/src/store/mod.rs +++ b/mithril-signer/src/store/mod.rs @@ -1 +1,7 @@ //! Alternative storage backends when relational database capabilities are not needed. + +mod mktree_store_sqlite; +mod protocol_initializer_store; + +pub use mktree_store_sqlite::*; +pub use protocol_initializer_store::*; diff --git a/mithril-signer/src/protocol_initializer_store.rs b/mithril-signer/src/store/protocol_initializer_store.rs similarity index 100% rename from mithril-signer/src/protocol_initializer_store.rs rename to mithril-signer/src/store/protocol_initializer_store.rs From 6fe022054d90377cf35582cfe60b042bf46c1cc4 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 12:44:45 +0200 Subject: [PATCH 11/14] Scope signer store import to the `store` module --- mithril-signer/benches/mktree_store_sqlite.rs | 2 +- mithril-signer/src/dependency_injection/builder.rs | 5 +++-- mithril-signer/src/dependency_injection/containers.rs | 3 ++- mithril-signer/src/lib.rs | 3 +-- mithril-signer/src/runtime/runner.rs | 3 ++- mithril-signer/tests/test_extensions/state_machine_tester.rs | 4 ++-- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/mithril-signer/benches/mktree_store_sqlite.rs b/mithril-signer/benches/mktree_store_sqlite.rs index 3c848199a0d..804e882f08f 100644 --- a/mithril-signer/benches/mktree_store_sqlite.rs +++ b/mithril-signer/benches/mktree_store_sqlite.rs @@ -1,6 +1,6 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use mithril_common::crypto_helper::{MKTree, MKTreeNode}; -use mithril_signer::MKTreeStoreSqlite; +use mithril_signer::store::MKTreeStoreSqlite; // Shortcuts for magnitudes: K for thousand, M for million const K: usize = 1_000; diff --git a/mithril-signer/src/dependency_injection/builder.rs b/mithril-signer/src/dependency_injection/builder.rs index 3b98d6befdc..110bbc2e7fa 100644 --- a/mithril-signer/src/dependency_injection/builder.rs +++ b/mithril-signer/src/dependency_injection/builder.rs @@ -41,9 +41,10 @@ use crate::services::{ CardanoTransactionsPreloaderActivationSigner, MithrilSingleSigner, SignerUpkeepService, TransactionsImporterByChunk, TransactionsImporterWithPruner, TransactionsImporterWithVacuum, }; +use crate::store::{MKTreeStoreSqlite, ProtocolInitializerStore}; use crate::{ - Configuration, MKTreeStoreSqlite, MetricsService, ProtocolInitializerStore, - HTTP_REQUEST_TIMEOUT_DURATION, SQLITE_FILE, SQLITE_FILE_CARDANO_TRANSACTION, + Configuration, MetricsService, HTTP_REQUEST_TIMEOUT_DURATION, SQLITE_FILE, + SQLITE_FILE_CARDANO_TRANSACTION, }; /// The `DependenciesBuilder` is intended to manage Services instance creation. diff --git a/mithril-signer/src/dependency_injection/containers.rs b/mithril-signer/src/dependency_injection/containers.rs index 1c20d6410c9..c5d25df41e0 100644 --- a/mithril-signer/src/dependency_injection/containers.rs +++ b/mithril-signer/src/dependency_injection/containers.rs @@ -11,7 +11,8 @@ use mithril_common::TickerService; use mithril_persistence::store::StakeStore; use crate::services::{AggregatorClient, SingleSigner, UpkeepService}; -use crate::{MetricsService, ProtocolInitializerStorer}; +use crate::store::ProtocolInitializerStorer; +use crate::MetricsService; type StakeStoreService = Arc; type CertificateHandlerService = Arc; diff --git a/mithril-signer/src/lib.rs b/mithril-signer/src/lib.rs index 5f1316c52ad..f68b90916f5 100644 --- a/mithril-signer/src/lib.rs +++ b/mithril-signer/src/lib.rs @@ -13,7 +13,7 @@ mod message_adapters; pub mod metrics; mod runtime; pub mod services; -mod store; +pub mod store; pub use configuration::{Configuration, DefaultConfiguration}; pub use message_adapters::{ @@ -21,7 +21,6 @@ pub use message_adapters::{ }; pub use metrics::*; pub use runtime::*; -pub use store::*; /// HTTP request timeout duration in milliseconds const HTTP_REQUEST_TIMEOUT_DURATION: u64 = 30000; diff --git a/mithril-signer/src/runtime/runner.rs b/mithril-signer/src/runtime/runner.rs index b289e3c7f9b..a1a77b6d879 100644 --- a/mithril-signer/src/runtime/runner.rs +++ b/mithril-signer/src/runtime/runner.rs @@ -492,11 +492,12 @@ mod tests { use mockall::mock; use std::{path::Path, sync::Arc}; + use crate::metrics::MetricsService; use crate::services::{ AggregatorClient, CardanoTransactionsImporter, DumbAggregatorClient, MithrilSingleSigner, MockAggregatorClient, MockTransactionStore, MockUpkeepService, SingleSigner, }; - use crate::{metrics::MetricsService, ProtocolInitializerStore}; + use crate::store::ProtocolInitializerStore; use super::*; diff --git a/mithril-signer/tests/test_extensions/state_machine_tester.rs b/mithril-signer/tests/test_extensions/state_machine_tester.rs index 7e743c2c3e9..b52bf0eb96d 100644 --- a/mithril-signer/tests/test_extensions/state_machine_tester.rs +++ b/mithril-signer/tests/test_extensions/state_machine_tester.rs @@ -39,8 +39,8 @@ use mithril_signer::{ services::{ AggregatorClient, CardanoTransactionsImporter, MithrilSingleSigner, SignerUpkeepService, }, - Configuration, MKTreeStoreSqlite, MetricsService, ProtocolInitializerStore, - ProtocolInitializerStorer, RuntimeError, SignerRunner, SignerState, StateMachine, + store::{MKTreeStoreSqlite, ProtocolInitializerStore, ProtocolInitializerStorer}, + Configuration, MetricsService, RuntimeError, SignerRunner, SignerState, StateMachine, }; use super::FakeAggregator; From a96b9507be9c38c6a262707de462592f30e3a69d Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:22:00 +0200 Subject: [PATCH 12/14] Futher reorganize cardano transactions services inside a single mod --- .../importer}/importer_by_chunk.rs | 0 .../importer}/importer_with_pruner.rs | 0 .../importer}/importer_with_vacuum.rs | 0 .../importer}/mod.rs | 0 .../importer}/service.rs | 0 mithril-signer/src/services/cardano_transactions/mod.rs | 5 +++++ .../preloader_checker.rs} | 0 mithril-signer/src/services/mod.rs | 8 +++----- 8 files changed, 8 insertions(+), 5 deletions(-) rename mithril-signer/src/services/{cardano_transactions_importer => cardano_transactions/importer}/importer_by_chunk.rs (100%) rename mithril-signer/src/services/{cardano_transactions_importer => cardano_transactions/importer}/importer_with_pruner.rs (100%) rename mithril-signer/src/services/{cardano_transactions_importer => cardano_transactions/importer}/importer_with_vacuum.rs (100%) rename mithril-signer/src/services/{cardano_transactions_importer => cardano_transactions/importer}/mod.rs (100%) rename mithril-signer/src/services/{cardano_transactions_importer => cardano_transactions/importer}/service.rs (100%) create mode 100644 mithril-signer/src/services/cardano_transactions/mod.rs rename mithril-signer/src/services/{cardano_transactions_preloader_checker.rs => cardano_transactions/preloader_checker.rs} (100%) diff --git a/mithril-signer/src/services/cardano_transactions_importer/importer_by_chunk.rs b/mithril-signer/src/services/cardano_transactions/importer/importer_by_chunk.rs similarity index 100% rename from mithril-signer/src/services/cardano_transactions_importer/importer_by_chunk.rs rename to mithril-signer/src/services/cardano_transactions/importer/importer_by_chunk.rs diff --git a/mithril-signer/src/services/cardano_transactions_importer/importer_with_pruner.rs b/mithril-signer/src/services/cardano_transactions/importer/importer_with_pruner.rs similarity index 100% rename from mithril-signer/src/services/cardano_transactions_importer/importer_with_pruner.rs rename to mithril-signer/src/services/cardano_transactions/importer/importer_with_pruner.rs diff --git a/mithril-signer/src/services/cardano_transactions_importer/importer_with_vacuum.rs b/mithril-signer/src/services/cardano_transactions/importer/importer_with_vacuum.rs similarity index 100% rename from mithril-signer/src/services/cardano_transactions_importer/importer_with_vacuum.rs rename to mithril-signer/src/services/cardano_transactions/importer/importer_with_vacuum.rs diff --git a/mithril-signer/src/services/cardano_transactions_importer/mod.rs b/mithril-signer/src/services/cardano_transactions/importer/mod.rs similarity index 100% rename from mithril-signer/src/services/cardano_transactions_importer/mod.rs rename to mithril-signer/src/services/cardano_transactions/importer/mod.rs diff --git a/mithril-signer/src/services/cardano_transactions_importer/service.rs b/mithril-signer/src/services/cardano_transactions/importer/service.rs similarity index 100% rename from mithril-signer/src/services/cardano_transactions_importer/service.rs rename to mithril-signer/src/services/cardano_transactions/importer/service.rs diff --git a/mithril-signer/src/services/cardano_transactions/mod.rs b/mithril-signer/src/services/cardano_transactions/mod.rs new file mode 100644 index 00000000000..233c280c562 --- /dev/null +++ b/mithril-signer/src/services/cardano_transactions/mod.rs @@ -0,0 +1,5 @@ +mod importer; +mod preloader_checker; + +pub use importer::*; +pub use preloader_checker::*; diff --git a/mithril-signer/src/services/cardano_transactions_preloader_checker.rs b/mithril-signer/src/services/cardano_transactions/preloader_checker.rs similarity index 100% rename from mithril-signer/src/services/cardano_transactions_preloader_checker.rs rename to mithril-signer/src/services/cardano_transactions/preloader_checker.rs diff --git a/mithril-signer/src/services/mod.rs b/mithril-signer/src/services/mod.rs index 931b00751de..c4a3fdc36ad 100644 --- a/mithril-signer/src/services/mod.rs +++ b/mithril-signer/src/services/mod.rs @@ -3,22 +3,20 @@ //! This module regroups services. Services are adapters in charge of the different bounded contexts of the application: //! //! * Aggregator Client: communicate with the Aggregator -//! * Cardano Transaction Importer: Import transactions from the Cardano chain +//! * Cardano Transactions: handle Cardano transactions (import, preload, etc.) //! * Single Signer: create single signatures //! * Upkeep: perform maintenance tasks //! //! Each service is defined by a public API (a trait) that is used in the controllers (runtimes). mod aggregator_client; -mod cardano_transactions_importer; -mod cardano_transactions_preloader_checker; +mod cardano_transactions; mod single_signer; mod upkeep_service; #[cfg(test)] pub use aggregator_client::dumb::DumbAggregatorClient; pub use aggregator_client::*; -pub use cardano_transactions_importer::*; -pub use cardano_transactions_preloader_checker::*; +pub use cardano_transactions::*; pub use single_signer::*; pub use upkeep_service::*; From afd886529abbb042f6f788917bcec7b52ae9586b Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:26:26 +0200 Subject: [PATCH 13/14] Rename `ProductionDependenciesBuilder` to `DependenciesBuilder` And remove the trait of the same name. The idea behind the `DependenciesBuilder` trait was to define a common api to build dependencies, but two years after its introduction it only have one implementor. --- .../src/dependency_injection/builder.rs | 23 +++++-------------- mithril-signer/src/main.rs | 4 ++-- .../test_extensions/state_machine_tester.rs | 8 +++---- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/mithril-signer/src/dependency_injection/builder.rs b/mithril-signer/src/dependency_injection/builder.rs index 110bbc2e7fa..d806df4b1c5 100644 --- a/mithril-signer/src/dependency_injection/builder.rs +++ b/mithril-signer/src/dependency_injection/builder.rs @@ -3,7 +3,6 @@ use std::sync::Arc; use std::time::Duration; use anyhow::{anyhow, Context}; -use async_trait::async_trait; use tokio::sync::Mutex; use mithril_common::api_version::APIVersionProvider; @@ -50,22 +49,15 @@ use crate::{ /// The `DependenciesBuilder` is intended to manage Services instance creation. /// /// The goal of this is to put all this code out of the way of business code. -#[async_trait] -pub trait DependenciesBuilder { - /// Create a SignerService instance. - async fn build(&self) -> StdResult; -} - -/// A `DependenciesBuilder` for Production environment. -pub struct ProductionDependenciesBuilder<'a> { +pub struct DependenciesBuilder<'a> { config: &'a Configuration, chain_observer_builder: fn(&Configuration) -> StdResult>, immutable_file_observer_builder: fn(&Configuration) -> StdResult>, } -impl<'a> ProductionDependenciesBuilder<'a> { - /// Create a new `ProductionDependenciesBuilder`. +impl<'a> DependenciesBuilder<'a> { + /// Create a new `DependenciesBuilder`. pub fn new(config: &'a Configuration) -> Self { let chain_observer_builder: fn(&Configuration) -> StdResult> = |config: &Configuration| { @@ -73,7 +65,7 @@ impl<'a> ProductionDependenciesBuilder<'a> { let cardano_cli_path = &config.cardano_cli_path; let cardano_node_socket_path = &config.cardano_node_socket_path; let cardano_network = &config.get_network().with_context(|| { - "Production Dependencies Builder can not get Cardano network while building the chain observer" + "Dependencies Builder can not get Cardano network while building the chain observer" })?; let cardano_cli_runner = &CardanoCliRunner::new( cardano_cli_path.to_owned(), @@ -183,12 +175,9 @@ impl<'a> ProductionDependenciesBuilder<'a> { Ok(connection) } -} -#[async_trait] -impl<'a> DependenciesBuilder for ProductionDependenciesBuilder<'a> { /// Build dependencies for the Production environment. - async fn build(&self) -> StdResult { + pub async fn build(&self) -> StdResult { if !self.config.data_stores_directory.exists() { fs::create_dir_all(self.config.data_stores_directory.clone()).with_context(|| { format!( @@ -404,7 +393,7 @@ mod tests { -> StdResult> = |_config: &Configuration| Ok(Arc::new(DumbImmutableFileObserver::default())); - let mut dependencies_builder = ProductionDependenciesBuilder::new(&config); + let mut dependencies_builder = DependenciesBuilder::new(&config); dependencies_builder .override_chain_observer_builder(chain_observer_builder) .override_immutable_file_observer_builder(immutable_file_observer_builder) diff --git a/mithril-signer/src/main.rs b/mithril-signer/src/main.rs index 9652b5efe55..cdd0b151ba4 100644 --- a/mithril-signer/src/main.rs +++ b/mithril-signer/src/main.rs @@ -15,7 +15,7 @@ use tokio::{ use mithril_common::StdResult; use mithril_doc::{Documenter, DocumenterDefault, GenerateDocCommands, StructDoc}; -use mithril_signer::dependency_injection::{DependenciesBuilder, ProductionDependenciesBuilder}; +use mithril_signer::dependency_injection::DependenciesBuilder; use mithril_signer::{ Configuration, DefaultConfiguration, MetricsServer, SignerRunner, SignerState, StateMachine, }; @@ -158,7 +158,7 @@ async fn main() -> StdResult<()> { .try_deserialize() .with_context(|| "configuration deserialize error")?; - let services = ProductionDependenciesBuilder::new(&config) + let services = DependenciesBuilder::new(&config) .build() .await .with_context(|| "services initialization error")?; diff --git a/mithril-signer/tests/test_extensions/state_machine_tester.rs b/mithril-signer/tests/test_extensions/state_machine_tester.rs index b52bf0eb96d..54d8a05cef3 100644 --- a/mithril-signer/tests/test_extensions/state_machine_tester.rs +++ b/mithril-signer/tests/test_extensions/state_machine_tester.rs @@ -34,7 +34,7 @@ use mithril_persistence::{ store::{StakeStore, StakeStorer}, }; use mithril_signer::{ - dependency_injection::{ProductionDependenciesBuilder, SignerDependencyContainer}, + dependency_injection::{DependenciesBuilder, SignerDependencyContainer}, metrics::*, services::{ AggregatorClient, CardanoTransactionsImporter, MithrilSingleSigner, SignerUpkeepService, @@ -97,9 +97,9 @@ impl StateMachineTester { let selected_signer_party_id = selected_signer_with_stake.party_id.clone(); let config = Configuration::new_sample(&selected_signer_party_id); - let production_dependencies_builder = ProductionDependenciesBuilder::new(&config); + let dependencies_builder = DependenciesBuilder::new(&config); let sqlite_connection = Arc::new( - production_dependencies_builder + dependencies_builder .build_sqlite_connection( ":memory:", mithril_signer::database::migration::get_migrations(), @@ -107,7 +107,7 @@ impl StateMachineTester { .await .unwrap(), ); - let transaction_sqlite_connection = production_dependencies_builder + let transaction_sqlite_connection = dependencies_builder .build_sqlite_connection( ":memory:", mithril_persistence::database::cardano_transaction_migration::get_migrations(), From 8362c9bd20d8edc78c6077e77528b27bd3a3cd1b Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:48:47 +0200 Subject: [PATCH 14/14] Upgrade `mithril-signer` version: from `0.2.174` to `0.2.175` --- Cargo.lock | 2 +- mithril-signer/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4b3025d12af..ea77e0c8c85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3853,7 +3853,7 @@ dependencies = [ [[package]] name = "mithril-signer" -version = "0.2.174" +version = "0.2.175" dependencies = [ "anyhow", "async-trait", diff --git a/mithril-signer/Cargo.toml b/mithril-signer/Cargo.toml index cfea892d5f3..fbc4723ff89 100644 --- a/mithril-signer/Cargo.toml +++ b/mithril-signer/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mithril-signer" -version = "0.2.174" +version = "0.2.175" description = "A Mithril Signer" authors = { workspace = true } edition = { workspace = true }