Skip to content

Commit

Permalink
[wip] certifier service
Browse files Browse the repository at this point in the history
  • Loading branch information
ghubertpalo committed Apr 20, 2023
1 parent fefef9e commit 9c5c9eb
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 233 deletions.
16 changes: 10 additions & 6 deletions mithril-aggregator/src/certifier_service.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//! ## Certifier Service
//!
//! This service is responsible of [OpenMessage] cycle of life. It creates open
//! messages and turn them into [CertificateRecord]. To do so, it registers
//! single signatures and deal with the multisigner for aggregate signature
//! creation.
use std::sync::Arc;

use async_trait::async_trait;
Expand Down Expand Up @@ -269,7 +275,7 @@ impl CertifierService for MithrilCertifierService {
.get_master_certificate_for_epoch(open_message.epoch)
.await?
.map(|cert| cert.hash)
.ok_or_else(|| CertifierServiceError::NoParentCertificateFound)?;
.ok_or_else(|| Box::new(CertifierServiceError::NoParentCertificateFound))?;

let certificate = Certificate::new(
parent_certificate_hash,
Expand All @@ -296,10 +302,8 @@ impl CertifierService for MithrilCertifierService {

#[cfg(test)]
mod tests {
use std::sync::Mutex;

/*
use mithril_common::entities::Beacon;
use sqlite::Connection;
use crate::{dependency_injection::DependenciesBuilder, Configuration};
Expand All @@ -310,12 +314,11 @@ mod tests {
DependenciesBuilder::new(config)
}
/*
fn get_open_message(connection: Arc<Mutex<Connection>>, signed_entity_type: &SignedEntityType) -> StdResult<Option<OpenMessage>> {
let repository = OpenMessageRepository::new(connection);
let open_message = repository.get_open_message(epoch, signed_entity_type)
}
*/
#[tokio::test]
async fn create_open_message() {
let mut dependencies = setup_dependencies();
Expand All @@ -333,4 +336,5 @@ mod tests {
let connection = dependencies.get_sqlite_connection().await.unwrap();
}
*/
}
2 changes: 1 addition & 1 deletion mithril-aggregator/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Configuration {
run_interval: 5000,
db_directory: PathBuf::new(),
snapshot_directory: PathBuf::new(),
data_stores_directory: PathBuf::new(),
data_stores_directory: PathBuf::from(":memory:"),
genesis_verification_key: key_encode_hex(genesis_verification_key).unwrap(),
reset_digests_cache: false,
disable_digests_cache: false,
Expand Down
19 changes: 19 additions & 0 deletions mithril-aggregator/src/database/provider/open_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ pub struct OpenMessage {
pub created_at: NaiveDateTime,
}

impl OpenMessage {
#[cfg(test)]
/// Create a dumb OpenMessage instance mainly for test purposes
pub fn dummy() -> Self {
let beacon = mithril_common::test_utils::fake_data::beacon();
let epoch = beacon.epoch;
let signed_entity_type = SignedEntityType::CardanoImmutableFilesFull(beacon);

Self {
open_message_id: Uuid::parse_str("193d1442-e89b-43cf-9519-04d8db9a12ff").unwrap(),
epoch,
signed_entity_type,
protocol_message: ProtocolMessage::new(),
is_certified: false,
created_at: chrono::Local::now().naive_local(),
}
}
}

impl From<OpenMessageWithSingleSignatures> for OpenMessage {
fn from(value: OpenMessageWithSingleSignatures) -> Self {
Self {
Expand Down
99 changes: 12 additions & 87 deletions mithril-aggregator/src/dependency_injection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ use mithril_common::{
CardanoImmutableDigester, DumbImmutableFileObserver, ImmutableDigester,
ImmutableFileObserver, ImmutableFileSystemObserver,
},
entities::{
Beacon, Certificate, CertificatePending, Epoch, PartyId, ProtocolParameters,
SignerWithStake, SingleSignatures,
},
entities::{Beacon, CertificatePending, Epoch, PartyId, SingleSignatures},
era::{
adapters::{EraReaderAdapterBuilder, EraReaderDummyAdapter},
EraChecker, EraMarker, EraReader, EraReaderAdapter, SupportedEra,
Expand Down Expand Up @@ -228,7 +225,7 @@ impl DependenciesBuilder {
ExecutionEnvironment::Production => {
self.configuration.get_sqlite_dir().join(SQLITE_FILE)
}
_ => ":memory:".into(),
_ => self.configuration.data_stores_directory.clone(),
};
let connection = Connection::open(&path)
.map(|c| Arc::new(Mutex::new(c)))
Expand Down Expand Up @@ -290,27 +287,10 @@ impl DependenciesBuilder {
}

async fn build_snapshot_store(&mut self) -> Result<Arc<dyn SnapshotStore>> {
let adapter: Box<
dyn StoreAdapter<Key = String, Record = mithril_common::entities::Snapshot>,
> = match self.configuration.environment {
ExecutionEnvironment::Production => {
let adapter = SignedEntityStoreAdapter::new(self.get_sqlite_connection().await?);

Box::new(adapter)
}
_ => {
let adapter = MemoryAdapter::new(None).map_err(|e| {
DependenciesBuilderError::Initialization {
message: "Cannot create Memory adapter for Snapshot Store.".to_string(),
error: Some(e.into()),
}
})?;
Box::new(adapter)
}
};

Ok(Arc::new(LocalSnapshotStore::new(
adapter,
Box::new(SignedEntityStoreAdapter::new(
self.get_sqlite_connection().await?,
)),
LIST_SNAPSHOTS_MAX_ITEMS,
)))
}
Expand Down Expand Up @@ -423,26 +403,9 @@ impl DependenciesBuilder {
}

async fn build_certificate_store(&mut self) -> Result<Arc<CertificateStore>> {
let adapter: Box<dyn StoreAdapter<Key = String, Record = Certificate>> =
match self.configuration.environment {
ExecutionEnvironment::Production => {
let adapter = CertificateStoreAdapter::new(self.get_sqlite_connection().await?);

Box::new(adapter)
}
_ => {
let adapter = MemoryAdapter::new(None).map_err(|e| {
DependenciesBuilderError::Initialization {
message: "Cannot create Memory adapter for Certificate Store."
.to_string(),
error: Some(e.into()),
}
})?;
Box::new(adapter)
}
};

Ok(Arc::new(CertificateStore::new(adapter)))
Ok(Arc::new(CertificateStore::new(Box::new(
CertificateStoreAdapter::new(self.get_sqlite_connection().await?),
))))
}

/// Get a configured [CertificateStore].
Expand All @@ -455,29 +418,10 @@ impl DependenciesBuilder {
}

async fn build_verification_key_store(&mut self) -> Result<Arc<VerificationKeyStore>> {
let adapter: Box<
dyn StoreAdapter<Key = Epoch, Record = HashMap<PartyId, SignerWithStake>>,
> = match self.configuration.environment {
ExecutionEnvironment::Production => {
let adapter =
SignerRegistrationStoreAdapter::new(self.get_sqlite_connection().await?);

Box::new(adapter)
}
_ => {
let adapter = MemoryAdapter::new(None).map_err(|e| {
DependenciesBuilderError::Initialization {
message: "Cannot create Memory adapter for VerificationKeyStore."
.to_string(),
error: Some(e.into()),
}
})?;
Box::new(adapter)
}
};

Ok(Arc::new(VerificationKeyStore::new(
adapter,
Box::new(SignerRegistrationStoreAdapter::new(
self.get_sqlite_connection().await?,
)),
self.configuration.store_retention_limit,
)))
}
Expand Down Expand Up @@ -535,27 +479,8 @@ impl DependenciesBuilder {
}

async fn build_protocol_parameters_store(&mut self) -> Result<Arc<ProtocolParametersStore>> {
let adapter: Box<dyn StoreAdapter<Key = Epoch, Record = ProtocolParameters>> =
match self.configuration.environment {
ExecutionEnvironment::Production => {
let adapter = EpochSettingStore::new(self.get_sqlite_connection().await?);

Box::new(adapter)
}
_ => {
let adapter = MemoryAdapter::new(None).map_err(|e| {
DependenciesBuilderError::Initialization {
message: "Cannot create Memory adapter for ProtocolParametersStore."
.to_string(),
error: Some(e.into()),
}
})?;
Box::new(adapter)
}
};

Ok(Arc::new(ProtocolParametersStore::new(
adapter,
Box::new(EpochSettingStore::new(self.get_sqlite_connection().await?)),
self.configuration.store_retention_limit,
)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ mod tests {
let (dependency_manager, _) = initialize_dependencies().await;
dependency_manager
.certificate_store
.save(fake_data::certificate("{certificate_hash}".to_string()))
.save(fake_data::genesis_certificate(
"{certificate_hash}".to_string(),
))
.await
.expect("certificate store save should have succeeded");

Expand Down
Loading

0 comments on commit 9c5c9eb

Please sign in to comment.