Skip to content

Commit

Permalink
Merge pull request #670 from input-output-hk/ensemble/663/refactor_cr…
Browse files Browse the repository at this point in the history
…ypto_helpers

Add MithrilFixture builder
  • Loading branch information
Alenar authored Dec 22, 2022
2 parents 07b5e8b + aecd616 commit a43b1b8
Show file tree
Hide file tree
Showing 52 changed files with 752 additions and 513 deletions.
4 changes: 2 additions & 2 deletions mithril-aggregator/src/certificate_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub enum CertificateCreationError {
Codec(String),
}

/// Define a way to create a [Certificate]
/// Define a way to create a [Certificate][mithril_common::entities::Certificate]
pub trait CertificateCreator {
/// Create a [Certificate]
/// Create a [Certificate][mithril_common::entities::Certificate]
fn create_certificate(
working: &WorkingCertificate,
signatures_party_ids: &[PartyId],
Expand Down
10 changes: 5 additions & 5 deletions mithril-aggregator/src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl DependencyManager {
/// * Fill the [VerificationKeyStore] with the given signers keys.
/// * Fill the [StakeStore] with the given signers stakes.
/// * Fill the [ProtocolParametersStore] with the given parameters.
pub async fn simulate_genesis(
pub async fn prepare_for_genesis(
&self,
genesis_signers: Vec<SignerWithStake>,
second_epoch_signers: Vec<SignerWithStake>,
Expand Down Expand Up @@ -255,13 +255,13 @@ pub mod tests {
MithrilSignerRegisterer, MultiSignerImpl, ProtocolParametersStore, SingleSignatureStore,
SnapshotStoreType, SnapshotUploaderType, VerificationKeyStore,
};
use mithril_common::certificate_chain::MithrilCertificateVerifier;
use mithril_common::crypto_helper::{key_encode_hex, ProtocolGenesisSigner};
use mithril_common::digesters::{DumbImmutableDigester, DumbImmutableFileObserver};
use mithril_common::{
certificate_chain::MithrilCertificateVerifier,
chain_observer::FakeObserver,
fake_data,
crypto_helper::{key_encode_hex, ProtocolGenesisSigner},
digesters::{DumbImmutableDigester, DumbImmutableFileObserver},
store::{adapter::MemoryAdapter, StakeStore},
test_utils::fake_data,
BeaconProviderImpl, CardanoNetwork,
};
use std::{path::PathBuf, sync::Arc};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ mod tests {
const API_SPEC_FILE: &str = "../openapi.yaml";

use crate::http_server::SERVER_BASE_PATH;
use mithril_common::apispec::APISpec;
use mithril_common::store::adapter::FailStoreAdapter;
use mithril_common::{entities, fake_data};
use mithril_common::test_utils::apispec::APISpec;
use mithril_common::{entities, test_utils::fake_data};
use serde_json::Value::Null;
use warp::http::Method;
use warp::test::request;
Expand Down
2 changes: 1 addition & 1 deletion mithril-aggregator/src/http_server/routes/epoch_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod tests {
const API_SPEC_FILE: &str = "../openapi.yaml";

use crate::http_server::SERVER_BASE_PATH;
use mithril_common::apispec::APISpec;
use mithril_common::test_utils::apispec::APISpec;
use serde_json::Value::Null;
use warp::http::Method;
use warp::test::request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ mod tests {
const API_SPEC_FILE: &str = "../openapi.yaml";

use crate::http_server::SERVER_BASE_PATH;
use mithril_common::apispec::APISpec;
use mithril_common::fake_data;
use mithril_common::test_utils::apispec::APISpec;
use mithril_common::test_utils::fake_data;
use tokio::sync::RwLock;
use warp::http::Method;
use warp::test::request;
Expand Down
4 changes: 2 additions & 2 deletions mithril-aggregator/src/http_server/routes/signer_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ mod handlers {
mod tests {
const API_SPEC_FILE: &str = "../openapi.yaml";

use mithril_common::apispec::APISpec;
use mithril_common::crypto_helper::ProtocolRegistrationError;
use mithril_common::fake_data;
use mithril_common::test_utils::apispec::APISpec;
use mithril_common::test_utils::fake_data;
use warp::http::Method;
use warp::test::request;

Expand Down
4 changes: 2 additions & 2 deletions mithril-aggregator/src/http_server/routes/snapshot_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ mod tests {
const API_SPEC_FILE: &str = "../openapi.yaml";

use crate::http_server::SERVER_BASE_PATH;
use mithril_common::apispec::APISpec;
use mithril_common::fake_data;
use mithril_common::test_utils::apispec::APISpec;
use mithril_common::test_utils::fake_data;
use serde_json::Value::Null;

use crate::initialize_dependencies;
Expand Down
42 changes: 15 additions & 27 deletions mithril-aggregator/src/multi_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ mod tests {
};
use mithril_common::{
crypto_helper::tests_setup::*,
fake_data,
store::{adapter::MemoryAdapter, StakeStore},
test_utils::{fake_data, MithrilFixtureBuilder},
};
use std::{collections::HashMap, sync::Arc};

Expand Down Expand Up @@ -861,17 +861,9 @@ mod tests {
#[tokio::test]
async fn test_multi_signer_stake_distribution_ok() {
let mut multi_signer = setup_multi_signer().await;
let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
let mut stake_distribution_expected = fixture.protocol_stake_distribution();

let mut stake_distribution_expected: ProtocolStakeDistribution =
setup_signers(5, &setup_protocol_parameters())
.iter()
.map(|(signer_with_stake, _, _)| {
(
signer_with_stake.party_id.to_owned(),
signer_with_stake.stake,
)
})
.collect::<_>();
stake_distribution_expected.sort_by_key(|k| k.0.clone());
multi_signer
.update_stake_distribution(&stake_distribution_expected)
Expand Down Expand Up @@ -918,22 +910,15 @@ mod tests {
.await
.expect("update protocol parameters failed");

let signers = setup_signers(5, &protocol_parameters);
let fixture = MithrilFixtureBuilder::default().with_signers(5).build();

let stake_distribution = fixture.protocol_stake_distribution();

let stake_distribution = &signers
.iter()
.map(|(signer_with_stake, _, _)| {
(
signer_with_stake.party_id.to_owned(),
signer_with_stake.stake,
)
})
.collect::<_>();
multi_signer
.update_stake_distribution(stake_distribution)
.update_stake_distribution(&stake_distribution)
.await
.expect("update stake distribution failed");
for (signer_with_stake, _, _) in &signers {
for signer_with_stake in &fixture.signers_with_stake() {
verification_key_store
.save_verification_key(
start_epoch.offset_to_recording_epoch(),
Expand All @@ -959,17 +944,20 @@ mod tests {
let mut signatures = Vec::new();

let mut expected_certificate_signers: Vec<SignerWithStake> = Vec::new();
for (signer_with_stake, protocol_signer, _protocol_initializer) in &signers {
if let Some(signature) = protocol_signer.sign(message.compute_hash().as_bytes()) {
for signer_fixture in fixture.signers_fixture() {
if let Some(signature) = signer_fixture
.protocol_signer
.sign(message.compute_hash().as_bytes())
{
let won_indexes = signature.indexes.clone();

signatures.push(entities::SingleSignatures::new(
signer_with_stake.party_id.to_owned(),
signer_fixture.signer_with_stake.party_id.to_owned(),
key_encode_hex(signature).unwrap(),
won_indexes,
));

expected_certificate_signers.push(signer_with_stake.to_owned())
expected_certificate_signers.push(signer_fixture.signer_with_stake.to_owned())
}
}

Expand Down
92 changes: 36 additions & 56 deletions mithril-aggregator/src/runtime/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,16 +615,16 @@ pub mod tests {
use crate::{MithrilSignerRegisterer, ProtocolParametersStorer, SignerRegistrationRound};
use mithril_common::chain_observer::FakeObserver;
use mithril_common::crypto_helper::{
key_decode_hex,
tests_setup::{setup_certificate_chain, setup_signers},
ProtocolMultiSignature,
key_decode_hex, tests_setup::setup_certificate_chain, ProtocolMultiSignature,
};
use mithril_common::digesters::DumbImmutableFileObserver;
use mithril_common::entities::{
Beacon, CertificatePending, Epoch, HexEncodedKey, ProtocolMessage, SignerWithStake,
StakeDistribution,
Beacon, CertificatePending, Epoch, HexEncodedKey, ProtocolMessage, StakeDistribution,
};
use mithril_common::test_utils::MithrilFixtureBuilder;
use mithril_common::{
entities::ProtocolMessagePartKey, store::StakeStorer, test_utils::fake_data,
};
use mithril_common::{entities::ProtocolMessagePartKey, fake_data, store::StakeStorer};
use mithril_common::{BeaconProviderImpl, CardanoNetwork};
use std::collections::HashMap;
use std::path::Path;
Expand Down Expand Up @@ -696,10 +696,18 @@ pub mod tests {

#[tokio::test]
async fn test_update_stake_distribution() {
let (deps, config) = initialize_dependencies().await;
let (mut deps, config) = initialize_dependencies().await;
let chain_observer = Arc::new(FakeObserver::default());
deps.chain_observer = chain_observer.clone();
let deps = Arc::new(deps);
let runner = AggregatorRunner::new(config, deps.clone());
let beacon = runner.get_beacon_from_chain().await.unwrap();
let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
let expected = fixture.stake_distribution();

chain_observer
.set_signers(fixture.signers_with_stake())
.await;
runner
.update_beacon(&beacon)
.await
Expand All @@ -709,13 +717,6 @@ pub mod tests {
.await
.expect("updating stake distribution should not return an error");

let current_stake_distribution = deps
.chain_observer
.get_current_stake_distribution()
.await
.unwrap()
.expect("The stake distribution should not be None.");

let saved_stake_distribution = deps
.stake_store
.get_stakes(beacon.epoch.offset_to_recording_epoch())
Expand All @@ -728,13 +729,7 @@ pub mod tests {
)
});

assert_eq!(
current_stake_distribution.len(),
saved_stake_distribution.len()
);
for (party_id, stake) in current_stake_distribution.iter() {
assert_eq!(stake, saved_stake_distribution.get(party_id).unwrap());
}
assert_eq!(expected, saved_stake_distribution);
}

#[tokio::test]
Expand Down Expand Up @@ -815,11 +810,11 @@ pub mod tests {
let beacon = runner.get_beacon_from_chain().await.unwrap();
runner.update_beacon(&beacon).await.unwrap();

let signers = fake_data::signers_with_stakes(5);
let current_signers = signers[1..3].to_vec();
let next_signers = signers[2..5].to_vec();
let fixture = MithrilFixtureBuilder::default().with_signers(5).build();
let current_signers = fixture.signers_with_stake()[1..3].to_vec();
let next_signers = fixture.signers_with_stake()[2..5].to_vec();
let protocol_parameters = fake_data::protocol_parameters();
deps.simulate_genesis(
deps.prepare_for_genesis(
current_signers.clone(),
next_signers.clone(),
&protocol_parameters.clone(),
Expand Down Expand Up @@ -853,17 +848,12 @@ pub mod tests {
let beacon = runner.get_beacon_from_chain().await.unwrap();
runner.update_beacon(&beacon).await.unwrap();

let protocol_parameters = fake_data::protocol_parameters();
let signers = setup_signers(5, &protocol_parameters.clone().into())
.into_iter()
.map(|s| s.0)
.collect::<Vec<SignerWithStake>>();
let current_signers = signers[1..3].to_vec();
let next_signers = signers[2..5].to_vec();
deps.simulate_genesis(
current_signers.clone(),
next_signers.clone(),
&protocol_parameters.clone(),
let fixture = MithrilFixtureBuilder::default().with_signers(5).build();

deps.prepare_for_genesis(
fixture.signers_with_stake(),
fixture.signers_with_stake(),
&fixture.protocol_parameters(),
)
.await;

Expand Down Expand Up @@ -902,7 +892,7 @@ pub mod tests {

let mut expected = WorkingCertificate::from_pending_certificate(
&certificate_pending,
&current_signers,
&fixture.signers_with_stake(),
&message,
&aggregate_verification_key,
&sut.initiated_at,
Expand Down Expand Up @@ -956,18 +946,15 @@ pub mod tests {
let (deps, config) = initialize_dependencies().await;
let deps = Arc::new(deps);
let runner = AggregatorRunner::new(config, deps.clone());
let beacon = runner.get_beacon_from_chain().await.unwrap();
let digest = "1+2+3+4=10".to_string();
runner.update_beacon(&beacon).await.unwrap();
for epoch in [
beacon.epoch.offset_to_signer_retrieval_epoch().unwrap(),
beacon.epoch.offset_to_next_signer_retrieval_epoch(),
] {
deps.protocol_parameters_store
.save_protocol_parameters(epoch, fake_data::protocol_parameters())
.await
.unwrap();
}
runner.update_beacon(&fake_data::beacon()).await.unwrap();
let fixture = MithrilFixtureBuilder::default().build();
deps.prepare_for_genesis(
fixture.signers_with_stake(),
fixture.signers_with_stake(),
&fixture.protocol_parameters(),
)
.await;

runner
.update_message_in_multisigner(digest)
Expand Down Expand Up @@ -1061,15 +1048,8 @@ pub mod tests {
.expect_get_multi_signature()
.return_once(move || Ok(Some(multi_signature)));
deps.multi_signer = Arc::new(RwLock::new(mock_multi_signer));
let certificate_store = deps.certificate_store.clone();
deps.init_state_from_chain(&certificate_chain, vec![]).await;
let runner = AggregatorRunner::new(config, Arc::new(deps));
for certificate in certificate_chain[1..].iter().rev() {
certificate_store
.as_ref()
.save(certificate.to_owned())
.await
.expect("save certificate to store should not fail");
}

let certificate = runner
.create_and_save_certificate(&working_certificate)
Expand Down
2 changes: 1 addition & 1 deletion mithril-aggregator/src/runtime/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ mod tests {

use super::super::runner::MockAggregatorRunner;
use super::*;
use mithril_common::fake_data;
use mithril_common::test_utils::fake_data;
use mockall::predicate;

async fn init_runtime(
Expand Down
2 changes: 1 addition & 1 deletion mithril-aggregator/src/runtime/working_certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl WorkingCertificate {

#[cfg(test)]
pub fn fake() -> Self {
use mithril_common::fake_data;
use mithril_common::test_utils::fake_data;

Self {
beacon: fake_data::beacon(),
Expand Down
Loading

0 comments on commit a43b1b8

Please sign in to comment.