From fc18ec8d61f406635f7ee7739cdcbb12ad9c7ea2 Mon Sep 17 00:00:00 2001 From: sfauvel Date: Mon, 3 Feb 2025 15:47:31 +0100 Subject: [PATCH] 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 | 43 ++++++++-------- mithril-common/src/crypto_helper/genesis.rs | 38 +++++++------- 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, 81 insertions(+), 118 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 5883447db92..dfb3301db27 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..b3d8b43a582 100644 --- a/mithril-common/src/crypto_helper/era.rs +++ b/mithril-common/src/crypto_helper/era.rs @@ -118,29 +118,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..1501d19e464 100644 --- a/mithril-common/src/crypto_helper/genesis.rs +++ b/mithril-common/src/crypto_helper/genesis.rs @@ -39,12 +39,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 +133,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"