Skip to content

Commit

Permalink
Merge pull request #483 from input-output-hk/djo/clean_wrappers
Browse files Browse the repository at this point in the history
- Remove all Wrapper type that only wrap on one level, usually behind an `Arc` or a `Box` (meaning every wrapper except the one over `MultiSigner`)
- Simplify Client dependencies by making most of them mandatory.
- Fix some typos in the Client.
  • Loading branch information
Alenar authored Sep 7, 2022
2 parents 94a3de4 + a13ef68 commit b2704e1
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 263 deletions.
10 changes: 6 additions & 4 deletions mithril-aggregator/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use std::sync::Arc;

use mithril_common::{store::adapter::JsonFileStoreAdapter, CardanoNetwork};

use crate::dependency::{SnapshotStoreWrapper, SnapshotUploaderWrapper};
use crate::snapshot_stores::LocalSnapshotStore;
use crate::tools::GcpFileUploader;
use crate::{LocalSnapshotUploader, RemoteSnapshotStore, RemoteSnapshotUploader};
use crate::{
LocalSnapshotUploader, RemoteSnapshotStore, RemoteSnapshotUploader, SnapshotStore,
SnapshotUploader,
};

// TODO: 'LIST_SNAPSHOTS_MAX_ITEMS' keep as const or in config, or add a parameter to `list_snapshots`?
const LIST_SNAPSHOTS_MAX_ITEMS: usize = 20;
Expand Down Expand Up @@ -88,7 +90,7 @@ pub enum SnapshotUploaderType {

impl Configuration {
/// Create a snapshot store from the configuration settings.
pub fn build_snapshot_store(&self) -> Result<SnapshotStoreWrapper, Box<dyn Error>> {
pub fn build_snapshot_store(&self) -> Result<Arc<dyn SnapshotStore>, Box<dyn Error>> {
match self.snapshot_store_type {
SnapshotStoreType::Gcp => Ok(Arc::new(RemoteSnapshotStore::new(
Box::new(GcpFileUploader::default()),
Expand All @@ -104,7 +106,7 @@ impl Configuration {
}

/// Create a snapshot uploader from configuration settings.
pub fn build_snapshot_uploader(&self) -> SnapshotUploaderWrapper {
pub fn build_snapshot_uploader(&self) -> Arc<dyn SnapshotUploader> {
match self.snapshot_uploader_type {
SnapshotUploaderType::Gcp => Arc::new(RemoteSnapshotUploader::new(Box::new(
GcpFileUploader::default(),
Expand Down
75 changes: 15 additions & 60 deletions mithril-aggregator/src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,106 +20,61 @@ use crate::{
SingleSignatureStore, Snapshotter, VerificationKeyStore, VerificationKeyStorer,
};

/// SnapshotStoreWrapper wraps a SnapshotStore
pub type SnapshotStoreWrapper = Arc<dyn SnapshotStore>;

/// SnapshotUploaderWrapper wraps a SnapshotUploader
pub type SnapshotUploaderWrapper = Arc<dyn SnapshotUploader>;

/// MultiSignerWrapper wraps a MultiSigner
pub type MultiSignerWrapper = Arc<RwLock<dyn MultiSigner>>;

/// CertificatePendingStoreWrapper wraps a CertificatePendingStore
pub type CertificatePendingStoreWrapper = Arc<CertificatePendingStore>;

/// CertificateStoreWrapper wraps a CertificateStore
pub type CertificateStoreWrapper = Arc<CertificateStore>;

/// VerificationKeyStoreWrapper wraps a VerificationKeyStore
pub type VerificationKeyStoreWrapper = Arc<VerificationKeyStore>;

/// StakeStoreWrapper wraps a StakeStore
pub type StakeStoreWrapper = Arc<StakeStore>;

/// SingleSignatureStoreWrapper wraps a SingleSignatureStore
pub type SingleSignatureStoreWrapper = Arc<SingleSignatureStore>;

/// ProtocolParametersStoreWrapper wraps ProtocolParameters
pub type ProtocolParametersStoreWrapper = Arc<ProtocolParametersStore>;

/// ChainObserverWrapper wraps a ChainObserver
pub type ChainObserverWrapper = Arc<dyn ChainObserver>;

/// BeaconProviderWrapper wraps a BeaconProvider
pub type BeaconProviderWrapper = Arc<dyn BeaconProvider>;

/// BeaconProviderWrapper wraps a BeaconProvider
pub type ImmutableFileObserverWrapper = Arc<dyn ImmutableFileObserver>;

/// DigesterWrapper wraps a Digester
pub type DigesterWrapper = Arc<dyn ImmutableDigester>;

/// SnapshotterWrapper wraps a Snapshotter
pub type SnapshotterWrapper = Arc<dyn Snapshotter>;

/// CertificateVerifierWrapper wraps a CertificateVerifier
pub type CertificateVerifierWrapper = Arc<dyn CertificateVerifier>;

/// ProtocolGenesisVerifierWrapper wraps a ProtocolGenesisVerifier
pub type ProtocolGenesisVerifierWrapper = Arc<ProtocolGenesisVerifier>;

/// DependencyManager handles the dependencies
pub struct DependencyManager {
/// Configuration structure.
pub config: Configuration,

/// Snapshot store.
pub snapshot_store: SnapshotStoreWrapper,
pub snapshot_store: Arc<dyn SnapshotStore>,

/// Snapshot uploader service.
pub snapshot_uploader: SnapshotUploaderWrapper,
pub snapshot_uploader: Arc<dyn SnapshotUploader>,

/// Multisigner service.
pub multi_signer: MultiSignerWrapper,

/// Certificate pending store.
pub certificate_pending_store: CertificatePendingStoreWrapper,
pub certificate_pending_store: Arc<CertificatePendingStore>,

/// Certificate store.
pub certificate_store: CertificateStoreWrapper,
pub certificate_store: Arc<CertificateStore>,

/// Verification key store.
pub verification_key_store: VerificationKeyStoreWrapper,
pub verification_key_store: Arc<VerificationKeyStore>,

/// Stake store.
pub stake_store: StakeStoreWrapper,
pub stake_store: Arc<StakeStore>,

/// Signer single signature store.
pub single_signature_store: SingleSignatureStoreWrapper,
pub single_signature_store: Arc<SingleSignatureStore>,

/// Protocol parameter store.
pub protocol_parameters_store: ProtocolParametersStoreWrapper,
pub protocol_parameters_store: Arc<ProtocolParametersStore>,

/// Chain observer service.
pub chain_observer: ChainObserverWrapper,
pub chain_observer: Arc<dyn ChainObserver>,

/// Beacon provider service.
pub beacon_provider: BeaconProviderWrapper,
pub beacon_provider: Arc<dyn BeaconProvider>,

/// Immutable file observer service.
pub immutable_file_observer: ImmutableFileObserverWrapper,
pub immutable_file_observer: Arc<dyn ImmutableFileObserver>,

/// Digester service.
pub digester: DigesterWrapper,
pub digester: Arc<dyn ImmutableDigester>,

/// Snapshotter service.
pub snapshotter: SnapshotterWrapper,
pub snapshotter: Arc<dyn Snapshotter>,

/// Certificate verifier service.
pub certificate_verifier: CertificateVerifierWrapper,
pub certificate_verifier: Arc<dyn CertificateVerifier>,

/// Genesis signature verifier service.
pub genesis_verifier: ProtocolGenesisVerifierWrapper,
pub genesis_verifier: Arc<ProtocolGenesisVerifier>,
}

#[doc(hidden)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ fn certificate_certificate_hash(
}

mod handlers {
use crate::dependency::{CertificatePendingStoreWrapper, CertificateStoreWrapper};
use crate::http_server::routes::reply;
use crate::{CertificatePendingStore, CertificateStore};
use slog_scope::{debug, warn};
use std::convert::Infallible;
use std::sync::Arc;
use warp::http::StatusCode;

/// Certificate Pending
pub async fn certificate_pending(
certificate_pending_store: CertificatePendingStoreWrapper,
certificate_pending_store: Arc<CertificatePendingStore>,
) -> Result<impl warp::Reply, Infallible> {
debug!("certificate_pending");

Expand All @@ -58,7 +59,7 @@ mod handlers {
/// Certificate by certificate hash
pub async fn certificate_certificate_hash(
certificate_hash: String,
certificate_store: CertificateStoreWrapper,
certificate_store: Arc<CertificateStore>,
) -> Result<impl warp::Reply, Infallible> {
debug!("certificate_certificate_hash/{}", certificate_hash);

Expand Down
7 changes: 4 additions & 3 deletions mithril-aggregator/src/http_server/routes/epoch_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ fn epoch_settings(
}

mod handlers {
use crate::dependency::{MultiSignerWrapper, ProtocolParametersStoreWrapper};
use crate::dependency::MultiSignerWrapper;
use crate::http_server::routes::reply;
use crate::ProtocolParametersStorer;
use crate::{ProtocolParametersStore, ProtocolParametersStorer};
use mithril_common::entities::EpochSettings;
use slog_scope::{debug, warn};
use std::convert::Infallible;
use std::sync::Arc;
use warp::http::StatusCode;

/// Epoch Settings
pub async fn epoch_settings(
protocol_parameters_store: ProtocolParametersStoreWrapper,
protocol_parameters_store: Arc<ProtocolParametersStore>,
multi_signer: MultiSignerWrapper,
) -> Result<impl warp::Reply, Infallible> {
debug!("epoch_settings");
Expand Down
16 changes: 8 additions & 8 deletions mithril-aggregator/src/http_server/routes/middlewares.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
use crate::dependency::{
CertificatePendingStoreWrapper, CertificateStoreWrapper, MultiSignerWrapper,
ProtocolParametersStoreWrapper, SnapshotStoreWrapper,
use crate::dependency::MultiSignerWrapper;
use crate::{
CertificatePendingStore, CertificateStore, Configuration, DependencyManager,
ProtocolParametersStore, SnapshotStore,
};
use crate::{Configuration, DependencyManager};
use std::convert::Infallible;
use std::sync::Arc;
use warp::Filter;

/// With snapshot store middleware
pub fn with_snapshot_store(
dependency_manager: Arc<DependencyManager>,
) -> impl Filter<Extract = (SnapshotStoreWrapper,), Error = Infallible> + Clone {
) -> impl Filter<Extract = (Arc<dyn SnapshotStore>,), Error = Infallible> + Clone {
warp::any().map(move || dependency_manager.snapshot_store.clone())
}

/// With certificate store middleware
pub fn with_certificate_store(
dependency_manager: Arc<DependencyManager>,
) -> impl Filter<Extract = (CertificateStoreWrapper,), Error = Infallible> + Clone {
) -> impl Filter<Extract = (Arc<CertificateStore>,), Error = Infallible> + Clone {
warp::any().map(move || dependency_manager.certificate_store.clone())
}

/// With certificate pending store
pub(crate) fn with_certificate_pending_store(
dependency_manager: Arc<DependencyManager>,
) -> impl Filter<Extract = (CertificatePendingStoreWrapper,), Error = Infallible> + Clone {
) -> impl Filter<Extract = (Arc<CertificatePendingStore>,), Error = Infallible> + Clone {
warp::any().map(move || dependency_manager.certificate_pending_store.clone())
}

/// With protocol parameters store
pub(crate) fn with_protocol_parameters_store(
dependency_manager: Arc<DependencyManager>,
) -> impl Filter<Extract = (ProtocolParametersStoreWrapper,), Error = Infallible> + Clone {
) -> impl Filter<Extract = (Arc<ProtocolParametersStore>,), Error = Infallible> + Clone {
warp::any().map(move || dependency_manager.protocol_parameters_store.clone())
}

Expand Down
12 changes: 6 additions & 6 deletions mithril-aggregator/src/http_server/routes/snapshot_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ fn snapshot_digest(
}

mod handlers {
use crate::dependency::SnapshotStoreWrapper;
use crate::http_server::routes::reply;
use crate::http_server::SERVER_BASE_PATH;
use crate::Configuration;
use crate::{Configuration, SnapshotStore};
use slog_scope::{debug, warn};
use std::convert::Infallible;
use std::str::FromStr;
use std::sync::Arc;
use warp::http::{StatusCode, Uri};

/// Snapshots
pub async fn snapshots(
snapshot_store: SnapshotStoreWrapper,
snapshot_store: Arc<dyn SnapshotStore>,
) -> Result<impl warp::Reply, Infallible> {
debug!("snapshots");

Expand All @@ -82,7 +82,7 @@ mod handlers {
/// Download a file if and only if it's a snapshot archive
pub async fn ensure_downloaded_file_is_a_snapshot(
reply: warp::fs::File,
snapshot_store: SnapshotStoreWrapper,
snapshot_store: Arc<dyn SnapshotStore>,
) -> Result<impl warp::Reply, Infallible> {
let filepath = reply.path().to_path_buf();
debug!(
Expand Down Expand Up @@ -113,7 +113,7 @@ mod handlers {
pub async fn snapshot_download(
digest: String,
config: Configuration,
snapshot_store: SnapshotStoreWrapper,
snapshot_store: Arc<dyn SnapshotStore>,
) -> Result<impl warp::Reply, Infallible> {
debug!("snapshot_download/{}", digest);

Expand Down Expand Up @@ -148,7 +148,7 @@ mod handlers {
/// Snapshot by digest
pub async fn snapshot_digest(
digest: String,
snapshot_store: SnapshotStoreWrapper,
snapshot_store: Arc<dyn SnapshotStore>,
) -> Result<impl warp::Reply, Infallible> {
debug!("snapshot_digest/{}", digest);

Expand Down
27 changes: 13 additions & 14 deletions mithril-aggregator/src/multi_signer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use chrono::prelude::*;
Expand All @@ -14,18 +15,16 @@ use mithril_common::crypto_helper::{
PROTOCOL_VERSION,
};
use mithril_common::entities::{self, SignerWithStake};
use mithril_common::store::{StakeStorer, StoreError};
use mithril_common::store::{StakeStore, StakeStorer, StoreError};
use mithril_common::{
NEXT_SIGNER_EPOCH_RETRIEVAL_OFFSET, SIGNER_EPOCH_RECORDING_OFFSET,
SIGNER_EPOCH_RETRIEVAL_OFFSET,
};

use crate::dependency::{
ProtocolParametersStoreWrapper, SingleSignatureStoreWrapper, StakeStoreWrapper,
VerificationKeyStoreWrapper,
};
use crate::store::{SingleSignatureStorer, VerificationKeyStorer};
use crate::ProtocolParametersStorer;
use crate::{
ProtocolParametersStore, ProtocolParametersStorer, SingleSignatureStore, VerificationKeyStore,
};

#[cfg(test)]
use mockall::automock;
Expand Down Expand Up @@ -243,25 +242,25 @@ pub struct MultiSignerImpl {
avk: Option<ProtocolAggregateVerificationKey>,

/// Verification key store
verification_key_store: VerificationKeyStoreWrapper,
verification_key_store: Arc<VerificationKeyStore>,

/// Stake store
stake_store: StakeStoreWrapper,
stake_store: Arc<StakeStore>,

/// Single signature store
single_signature_store: SingleSignatureStoreWrapper,
single_signature_store: Arc<SingleSignatureStore>,

/// Protocol parameters store
protocol_parameters_store: ProtocolParametersStoreWrapper,
protocol_parameters_store: Arc<ProtocolParametersStore>,
}

impl MultiSignerImpl {
/// MultiSignerImpl factory
pub fn new(
verification_key_store: VerificationKeyStoreWrapper,
stake_store: StakeStoreWrapper,
single_signature_store: SingleSignatureStoreWrapper,
protocol_parameters_store: ProtocolParametersStoreWrapper,
verification_key_store: Arc<VerificationKeyStore>,
stake_store: Arc<StakeStore>,
single_signature_store: Arc<SingleSignatureStore>,
protocol_parameters_store: Arc<ProtocolParametersStore>,
) -> Self {
debug!("New MultiSignerImpl created");
Self {
Expand Down
2 changes: 1 addition & 1 deletion mithril-client/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub enum AggregatorHandlerError {
#[error("json parsing failed: '{0}'")]
JsonParseFailed(String),

/// Error raised when an IO error occured (ie: snapshot writting on disk fails).
/// Error raised when an IO error occured (ie: snapshot writing on disk fails).
#[error("io error: {0}")]
IOError(#[from] io::Error),

Expand Down
11 changes: 6 additions & 5 deletions mithril-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ async fn main() -> Result<(), String> {
let genesis_verifier = ProtocolGenesisVerifier::from_verification_key(genesis_verification_key);

// Init runtime
let mut runtime = Runtime::new(config.network.clone());
runtime
.with_aggregator_handler(aggregator_handler)
.with_certificate_verifier(certificate_verifier)
.with_genesis_verifier(genesis_verifier);
let mut runtime = Runtime::new(
config.network.clone(),
aggregator_handler,
certificate_verifier,
genesis_verifier,
);

// Execute commands
match &args.command {
Expand Down
Loading

0 comments on commit b2704e1

Please sign in to comment.