From 99b4c5e0e5b47d5641a2712853df6d202bec8b5c Mon Sep 17 00:00:00 2001 From: sfauvel Date: Fri, 31 Jan 2025 17:37:30 +0100 Subject: [PATCH 1/6] refacto: remove `entities` dependency to `messages` --- .../cardano_transactions_set_proof.rs | 29 ++----------------- .../cardano_transactions_set_proof.rs | 28 +++++++++++++++++- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/mithril-common/src/entities/cardano_transactions_set_proof.rs b/mithril-common/src/entities/cardano_transactions_set_proof.rs index 8314332fba1..47ae011a22d 100644 --- a/mithril-common/src/entities/cardano_transactions_set_proof.rs +++ b/mithril-common/src/entities/cardano_transactions_set_proof.rs @@ -1,7 +1,6 @@ use crate::crypto_helper::{MKMapProof, ProtocolMkProof}; use crate::entities::TransactionHash; -use crate::messages::CardanoTransactionsSetProofMessagePart; -use crate::{StdError, StdResult}; +use crate::StdResult; use super::BlockRange; @@ -15,10 +14,10 @@ cfg_test_tools! { #[derive(Clone, Debug, PartialEq)] pub struct CardanoTransactionsSetProof { /// Hashes of the certified transactions - transactions_hashes: Vec, + pub transactions_hashes: Vec, /// Proof of the transactions - transactions_proof: ProtocolMkProof, + pub transactions_proof: ProtocolMkProof, } impl CardanoTransactionsSetProof { @@ -104,28 +103,6 @@ impl CardanoTransactionsSetProof { } } -impl TryFrom for CardanoTransactionsSetProofMessagePart { - type Error = StdError; - - fn try_from(proof: CardanoTransactionsSetProof) -> Result { - Ok(Self { - transactions_hashes: proof.transactions_hashes, - proof: proof.transactions_proof.to_json_hex()?, - }) - } -} - -impl TryFrom for CardanoTransactionsSetProof { - type Error = StdError; - - fn try_from(proof: CardanoTransactionsSetProofMessagePart) -> Result { - Ok(Self { - transactions_hashes: proof.transactions_hashes, - transactions_proof: ProtocolMkProof::from_json_hex(&proof.proof)?, - }) - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/mithril-common/src/messages/message_parts/cardano_transactions_set_proof.rs b/mithril-common/src/messages/message_parts/cardano_transactions_set_proof.rs index a89e78a6eaf..379d00101e1 100644 --- a/mithril-common/src/messages/message_parts/cardano_transactions_set_proof.rs +++ b/mithril-common/src/messages/message_parts/cardano_transactions_set_proof.rs @@ -1,4 +1,8 @@ -use crate::entities::{HexEncodedKey, TransactionHash}; +use crate::{ + crypto_helper::ProtocolMkProof, + entities::{CardanoTransactionsSetProof, HexEncodedKey, TransactionHash}, + StdError, +}; use serde::{Deserialize, Serialize}; #[cfg(target_family = "wasm")] @@ -23,3 +27,25 @@ impl CardanoTransactionsSetProofMessagePart { } } } + +impl TryFrom for CardanoTransactionsSetProofMessagePart { + type Error = StdError; + + fn try_from(proof: CardanoTransactionsSetProof) -> Result { + Ok(Self { + transactions_hashes: proof.transactions_hashes, + proof: proof.transactions_proof.to_json_hex()?, + }) + } +} + +impl TryFrom for CardanoTransactionsSetProof { + type Error = StdError; + + fn try_from(proof: CardanoTransactionsSetProofMessagePart) -> Result { + Ok(Self { + transactions_hashes: proof.transactions_hashes, + transactions_proof: ProtocolMkProof::from_json_hex(&proof.proof)?, + }) + } +} From 2445ee9e28a85c66449a2e2fe6f7873bb04482c5 Mon Sep 17 00:00:00 2001 From: sfauvel Date: Fri, 31 Jan 2025 17:42:51 +0100 Subject: [PATCH 2/6] refacto: remove `entities` dependency to `protocol` --- mithril-common/src/entities/protocol_message.rs | 8 -------- mithril-common/src/protocol/mod.rs | 8 ++++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mithril-common/src/entities/protocol_message.rs b/mithril-common/src/entities/protocol_message.rs index fa61b879357..73374c02bd0 100644 --- a/mithril-common/src/entities/protocol_message.rs +++ b/mithril-common/src/entities/protocol_message.rs @@ -2,8 +2,6 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use std::{collections::BTreeMap, fmt::Display}; -use crate::protocol::ToMessage; - /// The key of a ProtocolMessage #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] pub enum ProtocolMessagePartKey { @@ -118,12 +116,6 @@ impl ProtocolMessage { } } -impl ToMessage for ProtocolMessage { - fn to_message(&self) -> String { - self.compute_hash() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/mithril-common/src/protocol/mod.rs b/mithril-common/src/protocol/mod.rs index bc99cbae067..cc3086aec58 100644 --- a/mithril-common/src/protocol/mod.rs +++ b/mithril-common/src/protocol/mod.rs @@ -12,6 +12,8 @@ pub use multi_signer::MultiSigner; pub use signer_builder::{SignerBuilder, SignerBuilderError}; pub use single_signer::SingleSigner; +use crate::entities::ProtocolMessage; + /// Trait to convert a type to a message that can be signed or verified by the Mithril protocol. pub trait ToMessage: Sync + Send { /// Return a String representation of the message. @@ -29,3 +31,9 @@ impl ToMessage for &str { self.to_string() } } + +impl ToMessage for ProtocolMessage { + fn to_message(&self) -> String { + self.compute_hash() + } +} From 7ae9a6cd9024bddc1b7802758f7677d33d0bec89 Mon Sep 17 00:00:00 2001 From: sfauvel Date: Mon, 3 Feb 2025 10:46:19 +0100 Subject: [PATCH 3/6] refacto: remove `entities` dependency to `signable_builder` Move `signed_entity` to `signable_builder` module. Move `Beacon` and `Artifact` implementations in `signable_builder` module. --- .../src/database/record/signed_entity.rs | 5 +- .../repository/signed_entity_store.rs | 3 +- .../http_server/routes/artifact_routes/mod.rs | 4 +- .../src/http_server/routes/proof_routes.rs | 10 ++-- .../to_cardano_transactions_proof_message.rs | 7 ++- .../src/services/signed_entity.rs | 4 +- .../test_extensions/aggregator_observer.rs | 7 ++- mithril-common/src/entities/block_number.rs | 3 -- .../src/entities/cardano_database.rs | 12 +---- .../src/entities/cardano_db_beacon.rs | 3 -- .../entities/cardano_stake_distribution.rs | 9 ---- .../entities/cardano_transactions_snapshot.rs | 9 ---- mithril-common/src/entities/epoch.rs | 3 -- .../entities/mithril_stake_distribution.rs | 12 +---- mithril-common/src/entities/mod.rs | 2 - mithril-common/src/entities/snapshot.rs | 12 +---- .../src/signable_builder/interface.rs | 47 ++++++++++++++++++- mithril-common/src/signable_builder/mod.rs | 2 + .../signed_entity.rs | 8 ++-- 19 files changed, 75 insertions(+), 87 deletions(-) rename mithril-common/src/{entities => signable_builder}/signed_entity.rs (94%) diff --git a/mithril-aggregator/src/database/record/signed_entity.rs b/mithril-aggregator/src/database/record/signed_entity.rs index 5966d7998db..e563a01f6ca 100644 --- a/mithril-aggregator/src/database/record/signed_entity.rs +++ b/mithril-aggregator/src/database/record/signed_entity.rs @@ -3,8 +3,7 @@ use serde::{Deserialize, Serialize}; use mithril_common::crypto_helper::ProtocolParameters; use mithril_common::entities::{ - BlockNumber, CardanoDatabaseSnapshot, Epoch, SignedEntity, SignedEntityType, Snapshot, - StakeDistribution, + BlockNumber, CardanoDatabaseSnapshot, Epoch, SignedEntityType, Snapshot, StakeDistribution, }; #[cfg(test)] use mithril_common::entities::{CardanoStakeDistribution, MithrilStakeDistribution}; @@ -15,7 +14,7 @@ use mithril_common::messages::{ MithrilStakeDistributionListItemMessage, MithrilStakeDistributionMessage, SignerWithStakeMessagePart, SnapshotListItemMessage, SnapshotMessage, }; -use mithril_common::signable_builder::Artifact; +use mithril_common::signable_builder::{Artifact, SignedEntity}; use mithril_common::StdError; use mithril_persistence::database::Hydrator; use mithril_persistence::sqlite::{HydrationError, Projection, SqLiteEntity}; diff --git a/mithril-aggregator/src/database/repository/signed_entity_store.rs b/mithril-aggregator/src/database/repository/signed_entity_store.rs index 90ccdc96da8..a29fc6f0d71 100644 --- a/mithril-aggregator/src/database/repository/signed_entity_store.rs +++ b/mithril-aggregator/src/database/repository/signed_entity_store.rs @@ -165,7 +165,8 @@ impl SignedEntityStorer for SignedEntityStore { #[cfg(test)] mod tests { use mithril_common::{ - entities::{Epoch, MithrilStakeDistribution, SignedEntity, Snapshot}, + entities::{Epoch, MithrilStakeDistribution, Snapshot}, + signable_builder::SignedEntity, test_utils::fake_data, }; diff --git a/mithril-aggregator/src/http_server/routes/artifact_routes/mod.rs b/mithril-aggregator/src/http_server/routes/artifact_routes/mod.rs index 8a20cec3274..33b60197a5a 100644 --- a/mithril-aggregator/src/http_server/routes/artifact_routes/mod.rs +++ b/mithril-aggregator/src/http_server/routes/artifact_routes/mod.rs @@ -7,8 +7,8 @@ pub mod snapshot; #[cfg(test)] pub mod test_utils { use chrono::{DateTime, Utc}; - use mithril_common::entities::{SignedEntity, SignedEntityType}; - use mithril_common::signable_builder::Artifact; + use mithril_common::entities::SignedEntityType; + use mithril_common::signable_builder::{Artifact, SignedEntity}; pub fn create_signed_entity( signed_entity_type: SignedEntityType, diff --git a/mithril-aggregator/src/http_server/routes/proof_routes.rs b/mithril-aggregator/src/http_server/routes/proof_routes.rs index e012ddb3bfe..52a094a84a5 100644 --- a/mithril-aggregator/src/http_server/routes/proof_routes.rs +++ b/mithril-aggregator/src/http_server/routes/proof_routes.rs @@ -48,9 +48,8 @@ fn proof_cardano_transaction( mod handlers { use mithril_common::{ - entities::{CardanoTransactionsSnapshot, SignedEntity}, - messages::CardanoTransactionsProofsMessage, - StdResult, + entities::CardanoTransactionsSnapshot, messages::CardanoTransactionsProofsMessage, + signable_builder::SignedEntity, StdResult, }; use slog::{debug, warn, Logger}; use std::{convert::Infallible, sync::Arc}; @@ -148,9 +147,8 @@ mod tests { }; use mithril_common::{ - entities::{ - BlockNumber, CardanoTransactionsSetProof, CardanoTransactionsSnapshot, SignedEntity, - }, + entities::{BlockNumber, CardanoTransactionsSetProof, CardanoTransactionsSnapshot}, + signable_builder::SignedEntity, test_utils::{apispec::APISpec, assert_equivalent, fake_data}, }; diff --git a/mithril-aggregator/src/message_adapters/to_cardano_transactions_proof_message.rs b/mithril-aggregator/src/message_adapters/to_cardano_transactions_proof_message.rs index 85e7611766d..d7003eea67f 100644 --- a/mithril-aggregator/src/message_adapters/to_cardano_transactions_proof_message.rs +++ b/mithril-aggregator/src/message_adapters/to_cardano_transactions_proof_message.rs @@ -1,8 +1,7 @@ -use mithril_common::entities::{CardanoTransactionsSnapshot, SignedEntity}; -use mithril_common::messages::CardanoTransactionsSetProofMessagePart; use mithril_common::{ - entities::{CardanoTransactionsSetProof, TransactionHash}, - messages::CardanoTransactionsProofsMessage, + entities::{CardanoTransactionsSetProof, CardanoTransactionsSnapshot, TransactionHash}, + messages::{CardanoTransactionsProofsMessage, CardanoTransactionsSetProofMessagePart}, + signable_builder::SignedEntity, StdResult, }; diff --git a/mithril-aggregator/src/services/signed_entity.rs b/mithril-aggregator/src/services/signed_entity.rs index f67db0f48e6..35b51e2fca3 100644 --- a/mithril-aggregator/src/services/signed_entity.rs +++ b/mithril-aggregator/src/services/signed_entity.rs @@ -12,11 +12,11 @@ use tokio::task::JoinHandle; use mithril_common::{ entities::{ BlockNumber, CardanoDatabaseSnapshot, CardanoDbBeacon, CardanoStakeDistribution, - CardanoTransactionsSnapshot, Certificate, Epoch, MithrilStakeDistribution, SignedEntity, + CardanoTransactionsSnapshot, Certificate, Epoch, MithrilStakeDistribution, SignedEntityType, SignedEntityTypeDiscriminants, Snapshot, }, logging::LoggerExtensions, - signable_builder::Artifact, + signable_builder::{Artifact, SignedEntity}, signed_entity_type_lock::SignedEntityTypeLock, StdResult, }; diff --git a/mithril-aggregator/tests/test_extensions/aggregator_observer.rs b/mithril-aggregator/tests/test_extensions/aggregator_observer.rs index eb924f293fb..d248d82e1b3 100644 --- a/mithril-aggregator/tests/test_extensions/aggregator_observer.rs +++ b/mithril-aggregator/tests/test_extensions/aggregator_observer.rs @@ -4,9 +4,12 @@ use mithril_aggregator::{ entities::OpenMessage, services::{CertifierService, SignedEntityService}, }; -use mithril_common::entities::{CardanoTransactionsSnapshot, Certificate, SignedEntity}; use mithril_common::{ - entities::{Epoch, SignedEntityType, SignedEntityTypeDiscriminants, TimePoint}, + entities::{ + CardanoTransactionsSnapshot, Certificate, Epoch, SignedEntityType, + SignedEntityTypeDiscriminants, TimePoint, + }, + signable_builder::SignedEntity, StdResult, TickerService, }; use std::sync::Arc; diff --git a/mithril-common/src/entities/block_number.rs b/mithril-common/src/entities/block_number.rs index 89d1e98c5cb..c81a47c2af4 100644 --- a/mithril-common/src/entities/block_number.rs +++ b/mithril-common/src/entities/block_number.rs @@ -8,7 +8,6 @@ use crate::entities::arithmetic_operation_wrapper::{ impl_add_to_wrapper, impl_div_to_wrapper, impl_mul_to_wrapper, impl_partial_eq_to_wrapper, impl_rem_to_wrapper, impl_sub_to_wrapper, }; -use crate::signable_builder::Beacon; #[cfg(target_family = "wasm")] use wasm_bindgen::prelude::*; @@ -20,8 +19,6 @@ use wasm_bindgen::prelude::*; #[cfg_attr(target_family = "wasm", wasm_bindgen)] pub struct BlockNumber(pub u64); -impl Beacon for BlockNumber {} - impl Display for BlockNumber { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) diff --git a/mithril-common/src/entities/cardano_database.rs b/mithril-common/src/entities/cardano_database.rs index 9c6fe2254a4..4c57e96f309 100644 --- a/mithril-common/src/entities/cardano_database.rs +++ b/mithril-common/src/entities/cardano_database.rs @@ -2,10 +2,7 @@ use semver::Version; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; -use crate::{ - entities::{CardanoDbBeacon, CompressionAlgorithm}, - signable_builder::Artifact, -}; +use crate::entities::{CardanoDbBeacon, CompressionAlgorithm}; use super::MultiFilesUri; @@ -120,13 +117,6 @@ pub struct ArtifactsLocations { pub ancillary: Vec, } -#[typetag::serde] -impl Artifact for CardanoDatabaseSnapshot { - fn get_id(&self) -> String { - self.hash.clone() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/mithril-common/src/entities/cardano_db_beacon.rs b/mithril-common/src/entities/cardano_db_beacon.rs index 5440ccc4cf9..59db2e7fce1 100644 --- a/mithril-common/src/entities/cardano_db_beacon.rs +++ b/mithril-common/src/entities/cardano_db_beacon.rs @@ -3,7 +3,6 @@ use sha2::{Digest, Sha256}; use std::fmt::{Display, Formatter}; use crate::entities::{Epoch, ImmutableFileNumber}; -use crate::signable_builder::Beacon; /// A point in the Cardano chain at which a Mithril certificate of the Cardano Database should be /// produced. @@ -16,8 +15,6 @@ pub struct CardanoDbBeacon { pub immutable_file_number: ImmutableFileNumber, } -impl Beacon for CardanoDbBeacon {} - impl Display for CardanoDbBeacon { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( diff --git a/mithril-common/src/entities/cardano_stake_distribution.rs b/mithril-common/src/entities/cardano_stake_distribution.rs index 13ea7391c53..89cc222b6e6 100644 --- a/mithril-common/src/entities/cardano_stake_distribution.rs +++ b/mithril-common/src/entities/cardano_stake_distribution.rs @@ -1,8 +1,6 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; -use crate::signable_builder::Artifact; - use super::{Epoch, StakeDistribution}; /// Cardano Stake Distribution @@ -44,13 +42,6 @@ impl CardanoStakeDistribution { } } -#[typetag::serde] -impl Artifact for CardanoStakeDistribution { - fn get_id(&self) -> String { - self.hash.clone() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/mithril-common/src/entities/cardano_transactions_snapshot.rs b/mithril-common/src/entities/cardano_transactions_snapshot.rs index a3dedd38018..883f2773f09 100644 --- a/mithril-common/src/entities/cardano_transactions_snapshot.rs +++ b/mithril-common/src/entities/cardano_transactions_snapshot.rs @@ -1,8 +1,6 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; -use crate::signable_builder::Artifact; - use super::BlockNumber; /// Snapshot of a set of Cardano transactions @@ -40,13 +38,6 @@ impl CardanoTransactionsSnapshot { } } -#[typetag::serde] -impl Artifact for CardanoTransactionsSnapshot { - fn get_id(&self) -> String { - self.hash.clone() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/mithril-common/src/entities/epoch.rs b/mithril-common/src/entities/epoch.rs index a2f15c7290c..2fd516692e4 100644 --- a/mithril-common/src/entities/epoch.rs +++ b/mithril-common/src/entities/epoch.rs @@ -8,7 +8,6 @@ use thiserror::Error; use crate::entities::arithmetic_operation_wrapper::{ impl_add_to_wrapper, impl_partial_eq_to_wrapper, impl_sub_to_wrapper, }; -use crate::signable_builder::Beacon as SignableBeacon; /// Epoch represents a Cardano epoch #[derive( @@ -119,8 +118,6 @@ impl Display for Epoch { } } -impl SignableBeacon for Epoch {} - impl TryInto for Epoch { type Error = TryFromIntError; diff --git a/mithril-common/src/entities/mithril_stake_distribution.rs b/mithril-common/src/entities/mithril_stake_distribution.rs index 47bf6f5ec0c..46ff7c1c6bb 100644 --- a/mithril-common/src/entities/mithril_stake_distribution.rs +++ b/mithril-common/src/entities/mithril_stake_distribution.rs @@ -1,10 +1,7 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; -use crate::{ - entities::{Epoch, SignerWithStake}, - signable_builder::Artifact, -}; +use crate::entities::{Epoch, SignerWithStake}; use super::ProtocolParameters; @@ -57,13 +54,6 @@ impl MithrilStakeDistribution { } } -#[typetag::serde] -impl Artifact for MithrilStakeDistribution { - fn get_id(&self) -> String { - self.hash.clone() - } -} - #[cfg(test)] mod tests { use crate::test_utils::{fake_data, MithrilFixtureBuilder}; diff --git a/mithril-common/src/entities/mod.rs b/mithril-common/src/entities/mod.rs index a7b5d7ae8d2..6607f0501cc 100644 --- a/mithril-common/src/entities/mod.rs +++ b/mithril-common/src/entities/mod.rs @@ -21,7 +21,6 @@ mod http_server_error; mod mithril_stake_distribution; mod protocol_message; mod protocol_parameters; -mod signed_entity; mod signed_entity_config; mod signed_entity_type; mod signer; @@ -54,7 +53,6 @@ pub use http_server_error::{ClientError, ServerError}; pub use mithril_stake_distribution::MithrilStakeDistribution; pub use protocol_message::{ProtocolMessage, ProtocolMessagePartKey, ProtocolMessagePartValue}; pub use protocol_parameters::ProtocolParameters; -pub use signed_entity::*; pub use signed_entity_config::*; pub use signed_entity_type::*; pub use signer::{Signer, SignerWithStake}; diff --git a/mithril-common/src/entities/snapshot.rs b/mithril-common/src/entities/snapshot.rs index 9a4f7ad91f4..6e4eb3ea90e 100644 --- a/mithril-common/src/entities/snapshot.rs +++ b/mithril-common/src/entities/snapshot.rs @@ -1,7 +1,4 @@ -use crate::{ - entities::{CardanoDbBeacon, CompressionAlgorithm}, - signable_builder::Artifact, -}; +use crate::entities::{CardanoDbBeacon, CompressionAlgorithm}; use semver::Version; use serde::{Deserialize, Serialize}; @@ -54,10 +51,3 @@ impl Snapshot { } } } - -#[typetag::serde] -impl Artifact for Snapshot { - fn get_id(&self) -> String { - self.digest.clone() - } -} diff --git a/mithril-common/src/signable_builder/interface.rs b/mithril-common/src/signable_builder/interface.rs index 2c3f76df85d..ac7b0ad75b0 100644 --- a/mithril-common/src/signable_builder/interface.rs +++ b/mithril-common/src/signable_builder/interface.rs @@ -2,7 +2,11 @@ use async_trait::async_trait; use std::fmt::Debug; use crate::{ - entities::{ProtocolMessage, ProtocolMessagePartValue}, + entities::{ + BlockNumber, CardanoDatabaseSnapshot, CardanoDbBeacon, CardanoStakeDistribution, + CardanoTransactionsSnapshot, Epoch, MithrilStakeDistribution, ProtocolMessage, + ProtocolMessagePartValue, Snapshot, + }, StdResult, }; @@ -43,3 +47,44 @@ pub trait SignableSeedBuilder: Send + Sync { /// Compute current epoch protocol message part value async fn compute_current_epoch(&self) -> StdResult; } + +impl Beacon for BlockNumber {} + +impl Beacon for CardanoDbBeacon {} + +impl Beacon for Epoch {} + +#[typetag::serde] +impl Artifact for CardanoDatabaseSnapshot { + fn get_id(&self) -> String { + self.hash.clone() + } +} + +#[typetag::serde] +impl Artifact for CardanoStakeDistribution { + fn get_id(&self) -> String { + self.hash.clone() + } +} + +#[typetag::serde] +impl Artifact for CardanoTransactionsSnapshot { + fn get_id(&self) -> String { + self.hash.clone() + } +} + +#[typetag::serde] +impl Artifact for MithrilStakeDistribution { + fn get_id(&self) -> String { + self.hash.clone() + } +} + +#[typetag::serde] +impl Artifact for Snapshot { + fn get_id(&self) -> String { + self.digest.clone() + } +} diff --git a/mithril-common/src/signable_builder/mod.rs b/mithril-common/src/signable_builder/mod.rs index 84bd6cda73d..db819949f97 100644 --- a/mithril-common/src/signable_builder/mod.rs +++ b/mithril-common/src/signable_builder/mod.rs @@ -4,11 +4,13 @@ mod cardano_stake_distribution; mod interface; mod mithril_stake_distribution; mod signable_builder_service; +mod signed_entity; pub use cardano_stake_distribution::*; pub use interface::*; pub use mithril_stake_distribution::*; pub use signable_builder_service::*; +pub use signed_entity::*; cfg_fs! { mod cardano_database; diff --git a/mithril-common/src/entities/signed_entity.rs b/mithril-common/src/signable_builder/signed_entity.rs similarity index 94% rename from mithril-common/src/entities/signed_entity.rs rename to mithril-common/src/signable_builder/signed_entity.rs index bcd96c83d48..2084f1a80ee 100644 --- a/mithril-common/src/entities/signed_entity.rs +++ b/mithril-common/src/signable_builder/signed_entity.rs @@ -1,15 +1,15 @@ use chrono::{DateTime, Utc}; +use super::Artifact; use crate::entities::{ - CardanoTransactionsSnapshot, MithrilStakeDistribution, SignedEntityType, Snapshot, + CardanoStakeDistribution, CardanoTransactionsSnapshot, MithrilStakeDistribution, + SignedEntityType, Snapshot, }; -use crate::signable_builder::Artifact; #[cfg(any(test, feature = "test_tools"))] use crate::test_utils::fake_data; -use super::CardanoStakeDistribution; #[cfg(any(test, feature = "test_tools"))] -use super::{CardanoDbBeacon, Epoch}; +use crate::entities::{CardanoDbBeacon, Epoch}; /// Aggregate for signed entity #[derive(Debug, Clone)] From dc2c10ac629582ce80b8c29e05831a6b1583b362 Mon Sep 17 00:00:00 2001 From: sfauvel Date: Mon, 3 Feb 2025 15:47:31 +0100 Subject: [PATCH 4/6] refacto: remove feature `random` --- mithril-client/Cargo.toml | 1 - mithril-common/Cargo.toml | 8 ++- mithril-common/src/chain_observer/mod.rs | 2 +- mithril-common/src/chain_observer/model.rs | 2 +- .../src/crypto_helper/cardano/mod.rs | 7 +-- mithril-common/src/crypto_helper/era.rs | 48 +++++++++--------- mithril-common/src/crypto_helper/genesis.rs | 40 +++++++-------- mithril-common/src/crypto_helper/mod.rs | 4 +- mithril-common/src/lib.rs | 20 -------- mithril-common/src/protocol/signer_builder.rs | 24 +++++---- mithril-common/src/test_utils/fake_data.rs | 49 +++++++++---------- .../mithril-aggregator-fake/Cargo.toml | 1 - 12 files changed, 82 insertions(+), 124 deletions(-) diff --git a/mithril-client/Cargo.toml b/mithril-client/Cargo.toml index 0fa43f120ca..7e5f7ac4e0c 100644 --- a/mithril-client/Cargo.toml +++ b/mithril-client/Cargo.toml @@ -69,7 +69,6 @@ reqwest = { version = "0.12.12", default-features = false, features = [ httpmock = "0.7.0" indicatif = { version = "0.17.9", features = ["tokio"] } mithril-common = { path = "../mithril-common", version = "=0.4", default-features = false, features = [ - "random", "test_tools", ] } mockall = "0.13.1" diff --git a/mithril-common/Cargo.toml b/mithril-common/Cargo.toml index b5bac28424f..b7beac137c7 100644 --- a/mithril-common/Cargo.toml +++ b/mithril-common/Cargo.toml @@ -56,7 +56,7 @@ pallas-network = { version = "0.32.0", optional = true } pallas-primitives = { version = "0.32.0", optional = true } pallas-traverse = { version = "0.32.0", optional = true } rand_chacha = "0.3.1" -rand_core = "0.6.4" +rand_core = { version = "0.6.4", features = ["getrandom"] } rayon = "1.10.0" reqwest = { version = "0.12.12", optional = true } semver = "1.0.24" @@ -81,7 +81,6 @@ wasm-bindgen = "0.2.99" criterion = { version = "0.5.1", features = ["html_reports", "async_tokio"] } mockall = "0.13.1" pallas-crypto = "0.32.0" -rand_core = { version = "0.6.4", features = ["getrandom"] } reqwest = { version = "0.12.12", features = ["json"] } slog-async = "2.8.0" slog-term = "2.9.1" @@ -94,8 +93,7 @@ mithril-build-script = { path = "../internal/mithril-build-script", version = "= default = ["rug-backend"] # Full feature set -full = ["random", "fs", "test_tools"] -random = ["rand_core/getrandom"] +full = ["fs", "test_tools"] fs = [ "tokio/fs", "tokio/process", @@ -116,7 +114,7 @@ num-integer-backend = ["mithril-stm/num-integer-backend"] # Disable signer certification, to be used only for tests allow_skip_signer_certification = [] # Enable all tests tools -test_tools = ["apispec", "test_http_server", "random"] +test_tools = ["apispec", "test_http_server"] # Enable tools to helps validate conformity to an OpenAPI specification apispec = ["dep:glob", "dep:jsonschema", "dep:warp", "dep:reqwest"] test_http_server = ["dep:warp"] diff --git a/mithril-common/src/chain_observer/mod.rs b/mithril-common/src/chain_observer/mod.rs index c891e9f55ee..3d96d745231 100644 --- a/mithril-common/src/chain_observer/mod.rs +++ b/mithril-common/src/chain_observer/mod.rs @@ -10,7 +10,7 @@ pub use model::{ ChainAddress, TxDatum, TxDatumBuilder, TxDatumError, TxDatumFieldTypeName, TxDatumFieldValue, }; -cfg_fs_random! { +cfg_fs! { mod builder; mod cli_observer; mod pallas_observer; diff --git a/mithril-common/src/chain_observer/model.rs b/mithril-common/src/chain_observer/model.rs index aae2f8d1f70..abd7b891d94 100644 --- a/mithril-common/src/chain_observer/model.rs +++ b/mithril-common/src/chain_observer/model.rs @@ -7,7 +7,7 @@ use thiserror::Error; use crate::{StdError, StdResult}; -cfg_fs_random! { +cfg_fs! { use serde::Deserialize; use anyhow::Context; use pallas_codec::minicbor::{Decode, Decoder, decode}; diff --git a/mithril-common/src/crypto_helper/cardano/mod.rs b/mithril-common/src/crypto_helper/cardano/mod.rs index 0d6d59317f5..c141296125a 100644 --- a/mithril-common/src/crypto_helper/cardano/mod.rs +++ b/mithril-common/src/crypto_helper/cardano/mod.rs @@ -1,4 +1,5 @@ mod codec; +mod cold_key; mod key_certification; mod opcert; @@ -6,8 +7,4 @@ pub use codec::*; pub use key_certification::*; pub use opcert::*; -cfg_random! { - mod cold_key; - - pub use cold_key::*; -} +pub use cold_key::*; diff --git a/mithril-common/src/crypto_helper/era.rs b/mithril-common/src/crypto_helper/era.rs index abff325b95b..d3dc2973d65 100644 --- a/mithril-common/src/crypto_helper/era.rs +++ b/mithril-common/src/crypto_helper/era.rs @@ -1,6 +1,4 @@ use ed25519_dalek::{Signer, SigningKey}; -#[cfg(feature = "random")] -use rand_chacha::rand_core; use rand_chacha::rand_core::{CryptoRng, RngCore, SeedableRng}; use rand_chacha::ChaCha20Rng; use serde::{Deserialize, Serialize}; @@ -49,8 +47,7 @@ impl EraMarkersSigner { Self::create_test_signer(rng) } - #[cfg(any(test, feature = "random"))] - #[cfg_attr(docsrs, doc(cfg(feature = "random")))] + #[cfg(test)] /// [EraMarkersSigner] non deterministic pub fn create_non_deterministic_signer() -> Self { let rng = rand_core::OsRng; @@ -118,29 +115,28 @@ mod tests { .expect("Decoding golden secret key should not fail"); } - cfg_random! { - #[test] - fn test_generate_test_deterministic_keypair() { - let signer = EraMarkersSigner::create_deterministic_signer(); - let verifier = signer.create_verifier(); - let signer_2 = EraMarkersSigner::create_deterministic_signer(); - let verifier_2 = signer.create_verifier(); - assert_eq!(signer.secret_key.to_bytes(), signer_2.secret_key.to_bytes()); - assert_eq!( - verifier.verification_key.as_bytes(), - verifier_2.verification_key.as_bytes() - ); - - println!( - "Deterministic Verification Key={}", - verifier.verification_key.to_json_hex().unwrap() - ); - println!( - "Deterministic Secret Key=={}", - signer.secret_key.to_json_hex().unwrap() - ); - } + #[test] + fn test_generate_test_deterministic_keypair() { + let signer = EraMarkersSigner::create_deterministic_signer(); + let verifier = signer.create_verifier(); + let signer_2 = EraMarkersSigner::create_deterministic_signer(); + let verifier_2 = signer.create_verifier(); + assert_eq!(signer.secret_key.to_bytes(), signer_2.secret_key.to_bytes()); + assert_eq!( + verifier.verification_key.as_bytes(), + verifier_2.verification_key.as_bytes() + ); + + println!( + "Deterministic Verification Key={}", + verifier.verification_key.to_json_hex().unwrap() + ); + println!( + "Deterministic Secret Key=={}", + signer.secret_key.to_json_hex().unwrap() + ); } + #[test] fn test_generate_test_non_deterministic_keypair() { let signer = EraMarkersSigner::create_non_deterministic_signer(); diff --git a/mithril-common/src/crypto_helper/genesis.rs b/mithril-common/src/crypto_helper/genesis.rs index 815ef48e770..9124cd8f233 100644 --- a/mithril-common/src/crypto_helper/genesis.rs +++ b/mithril-common/src/crypto_helper/genesis.rs @@ -1,8 +1,6 @@ use crate::{StdError, StdResult}; use anyhow::anyhow; use ed25519_dalek::{Signer, SigningKey}; -#[cfg(feature = "random")] -use rand_chacha::rand_core; use rand_chacha::rand_core::{CryptoRng, RngCore, SeedableRng}; use rand_chacha::ChaCha20Rng; use serde::{Deserialize, Serialize}; @@ -39,12 +37,10 @@ impl ProtocolGenesisSigner { Self::create_test_genesis_signer(rng) } - cfg_random! { - /// [ProtocolGenesisSigner] non deterministic - pub fn create_non_deterministic_genesis_signer() -> Self { - let rng = rand_core::OsRng; - Self::create_test_genesis_signer(rng) - } + /// [ProtocolGenesisSigner] non deterministic + pub fn create_non_deterministic_genesis_signer() -> Self { + let rng = rand_core::OsRng; + Self::create_test_genesis_signer(rng) } /// Get the [ProtocolGenesisSecretKey] @@ -135,21 +131,19 @@ mod tests { ); } - cfg_random! { - #[test] - fn test_generate_test_non_deterministic_genesis_keypair() { - let genesis_signer = ProtocolGenesisSigner::create_non_deterministic_genesis_signer(); - let genesis_verifier = genesis_signer.create_genesis_verifier(); - - println!( - "Non Deterministic Genesis Verification Key={}", - genesis_verifier.verification_key.to_json_hex().unwrap() - ); - println!( - "Non Deterministic Genesis Secret Key=={}", - genesis_signer.secret_key.to_json_hex().unwrap() - ); - } + #[test] + fn test_generate_test_non_deterministic_genesis_keypair() { + let genesis_signer = ProtocolGenesisSigner::create_non_deterministic_genesis_signer(); + let genesis_verifier = genesis_signer.create_genesis_verifier(); + + println!( + "Non Deterministic Genesis Verification Key={}", + genesis_verifier.verification_key.to_json_hex().unwrap() + ); + println!( + "Non Deterministic Genesis Secret Key=={}", + genesis_signer.secret_key.to_json_hex().unwrap() + ); } #[test] diff --git a/mithril-common/src/crypto_helper/mod.rs b/mithril-common/src/crypto_helper/mod.rs index aa0ba98548b..915d5a3c706 100644 --- a/mithril-common/src/crypto_helper/mod.rs +++ b/mithril-common/src/crypto_helper/mod.rs @@ -13,9 +13,7 @@ cfg_test_tools! { pub mod tests_setup; } -cfg_random! { - pub use cardano::ColdKeyGenerator; -} +pub use cardano::ColdKeyGenerator; pub use cardano::{ KESPeriod, OpCert, ProtocolInitializerErrorWrapper, ProtocolRegistrationErrorWrapper, diff --git a/mithril-common/src/lib.rs b/mithril-common/src/lib.rs index f59d286641c..e5b94fb1107 100644 --- a/mithril-common/src/lib.rs +++ b/mithril-common/src/lib.rs @@ -20,26 +20,6 @@ macro_rules! cfg_fs { } } -macro_rules! cfg_random { - ($($item:item)*) => { - $( - #[cfg(any(test, feature = "random"))] - #[cfg_attr(docsrs, doc(cfg(feature = "random")))] - $item - )* - } -} - -macro_rules! cfg_fs_random { - ($($item:item)*) => { - $( - #[cfg(all(feature = "fs", feature = "random"))] - #[cfg_attr(docsrs, doc(all(feature = "fs", feature = "random")))] - $item - )* - } -} - macro_rules! cfg_test_tools { ($($item:item)*) => { $( diff --git a/mithril-common/src/protocol/signer_builder.rs b/mithril-common/src/protocol/signer_builder.rs index 4ceeaa7f0f3..8bfeeb876e2 100644 --- a/mithril-common/src/protocol/signer_builder.rs +++ b/mithril-common/src/protocol/signer_builder.rs @@ -123,19 +123,17 @@ impl SignerBuilder { )) } - cfg_random! { - /// Build non deterministic [SingleSigner] and [ProtocolInitializer] based on the registered parties. - pub fn build_single_signer( - &self, - signer_with_stake: SignerWithStake, - kes_secret_key_path: Option<&Path>, - ) -> StdResult<(SingleSigner, ProtocolInitializer)> { - self.build_single_signer_with_rng( - signer_with_stake, - kes_secret_key_path, - &mut rand_core::OsRng, - ) - } + /// Build non deterministic [SingleSigner] and [ProtocolInitializer] based on the registered parties. + pub fn build_single_signer( + &self, + signer_with_stake: SignerWithStake, + kes_secret_key_path: Option<&Path>, + ) -> StdResult<(SingleSigner, ProtocolInitializer)> { + self.build_single_signer_with_rng( + signer_with_stake, + kes_secret_key_path, + &mut rand_core::OsRng, + ) } /// Build deterministic [SingleSigner] and [ProtocolInitializer] based on the registered parties. diff --git a/mithril-common/src/test_utils/fake_data.rs b/mithril-common/src/test_utils/fake_data.rs index a976b4e1bac..521cca8acd5 100644 --- a/mithril-common/src/test_utils/fake_data.rs +++ b/mithril-common/src/test_utils/fake_data.rs @@ -41,31 +41,30 @@ pub fn protocol_parameters() -> entities::ProtocolParameters { entities::ProtocolParameters::new(k, m, phi_f) } -cfg_random! { - /// Fake ProtocolInitializer - pub fn protocol_initializer>( - seed: S, - stake: entities::Stake, - ) -> crypto_helper::ProtocolInitializer { - use rand_chacha::ChaCha20Rng; - use rand_core::SeedableRng; - - let protocol_parameters = protocol_parameters(); - let seed: [u8; 32] = format!("{:<032}", seed.into()).as_bytes()[..32] - .try_into() - .unwrap(); - let mut rng = ChaCha20Rng::from_seed(seed); - let kes_secret_key_path: Option = None; - let kes_period = Some(0); - - crypto_helper::ProtocolInitializer::setup( - protocol_parameters.into(), - kes_secret_key_path, - kes_period, - stake, - &mut rng, - ).unwrap() - } +/// Fake ProtocolInitializer +pub fn protocol_initializer>( + seed: S, + stake: entities::Stake, +) -> crypto_helper::ProtocolInitializer { + use rand_chacha::ChaCha20Rng; + use rand_core::SeedableRng; + + let protocol_parameters = protocol_parameters(); + let seed: [u8; 32] = format!("{:<032}", seed.into()).as_bytes()[..32] + .try_into() + .unwrap(); + let mut rng = ChaCha20Rng::from_seed(seed); + let kes_secret_key_path: Option = None; + let kes_period = Some(0); + + crypto_helper::ProtocolInitializer::setup( + protocol_parameters.into(), + kes_secret_key_path, + kes_period, + stake, + &mut rng, + ) + .unwrap() } /// Fake CertificatePending diff --git a/mithril-test-lab/mithril-aggregator-fake/Cargo.toml b/mithril-test-lab/mithril-aggregator-fake/Cargo.toml index c50909b3483..00654834103 100644 --- a/mithril-test-lab/mithril-aggregator-fake/Cargo.toml +++ b/mithril-test-lab/mithril-aggregator-fake/Cargo.toml @@ -27,7 +27,6 @@ tracing-subscriber = "0.3.19" [dev-dependencies] mithril-common = { "path" = "../../mithril-common", features = [ "test_tools", - "random", ] } reqwest = "0.12.12" warp = "0.3.7" From 20dabc2c68a2b23314a6293ed50f36bc057d6ca1 Mon Sep 17 00:00:00 2001 From: sfauvel Date: Tue, 4 Feb 2025 17:31:22 +0100 Subject: [PATCH 5/6] refacto: reduce visibilty of `CardanoTransactionsSetProof`fields --- mithril-common/src/entities/cardano_transactions_set_proof.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mithril-common/src/entities/cardano_transactions_set_proof.rs b/mithril-common/src/entities/cardano_transactions_set_proof.rs index 47ae011a22d..12dfb5046cb 100644 --- a/mithril-common/src/entities/cardano_transactions_set_proof.rs +++ b/mithril-common/src/entities/cardano_transactions_set_proof.rs @@ -14,10 +14,10 @@ cfg_test_tools! { #[derive(Clone, Debug, PartialEq)] pub struct CardanoTransactionsSetProof { /// Hashes of the certified transactions - pub transactions_hashes: Vec, + pub(crate) transactions_hashes: Vec, /// Proof of the transactions - pub transactions_proof: ProtocolMkProof, + pub(crate) transactions_proof: ProtocolMkProof, } impl CardanoTransactionsSetProof { From 4c644ae1d900fb7dbbe9ce458f1e132b19845033 Mon Sep 17 00:00:00 2001 From: sfauvel Date: Tue, 4 Feb 2025 17:03:31 +0100 Subject: [PATCH 6/6] chore: upgrade crate versions * mithril-aggregator from `0.6.25` to `0.6.26` * mithril-client from `0.10.9` to `0.10.10` * mithril-common from `0.4.111` to `0.4.112` * mithril-aggregator-fake from `0.3.19` to `0.3.20` --- Cargo.lock | 8 ++++---- mithril-aggregator/Cargo.toml | 2 +- mithril-client/Cargo.toml | 2 +- mithril-common/Cargo.toml | 2 +- mithril-test-lab/mithril-aggregator-fake/Cargo.toml | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ec44ef571e8..b498cd6ae42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3578,7 +3578,7 @@ dependencies = [ [[package]] name = "mithril-aggregator" -version = "0.6.25" +version = "0.6.26" dependencies = [ "anyhow", "async-trait", @@ -3625,7 +3625,7 @@ dependencies = [ [[package]] name = "mithril-aggregator-fake" -version = "0.3.19" +version = "0.3.20" dependencies = [ "anyhow", "axum", @@ -3657,7 +3657,7 @@ dependencies = [ [[package]] name = "mithril-client" -version = "0.10.9" +version = "0.10.10" dependencies = [ "anyhow", "async-recursion", @@ -3735,7 +3735,7 @@ dependencies = [ [[package]] name = "mithril-common" -version = "0.4.111" +version = "0.4.112" dependencies = [ "anyhow", "async-trait", diff --git a/mithril-aggregator/Cargo.toml b/mithril-aggregator/Cargo.toml index 61f5c171810..07213b2ae63 100644 --- a/mithril-aggregator/Cargo.toml +++ b/mithril-aggregator/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mithril-aggregator" -version = "0.6.25" +version = "0.6.26" description = "A Mithril Aggregator server" authors = { workspace = true } edition = { workspace = true } diff --git a/mithril-client/Cargo.toml b/mithril-client/Cargo.toml index 7e5f7ac4e0c..d7e835355a1 100644 --- a/mithril-client/Cargo.toml +++ b/mithril-client/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mithril-client" -version = "0.10.9" +version = "0.10.10" description = "Mithril client library" authors = { workspace = true } edition = { workspace = true } diff --git a/mithril-common/Cargo.toml b/mithril-common/Cargo.toml index b7beac137c7..2e29f78f91d 100644 --- a/mithril-common/Cargo.toml +++ b/mithril-common/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mithril-common" -version = "0.4.111" +version = "0.4.112" description = "Common types, interfaces, and utilities for Mithril nodes." authors = { workspace = true } edition = { workspace = true } diff --git a/mithril-test-lab/mithril-aggregator-fake/Cargo.toml b/mithril-test-lab/mithril-aggregator-fake/Cargo.toml index 00654834103..8fd6421e2e0 100644 --- a/mithril-test-lab/mithril-aggregator-fake/Cargo.toml +++ b/mithril-test-lab/mithril-aggregator-fake/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mithril-aggregator-fake" -version = "0.3.19" +version = "0.3.20" description = "Mithril Fake Aggregator for client testing" authors = { workspace = true } documentation = { workspace = true }