From 869ee69a538cdd12087fe4d9038d672c5cf60cd9 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Mon, 22 Jan 2024 18:00:56 +0100 Subject: [PATCH 1/5] Enable features in mithril-common doc by adding the doc_cfg attribute --- mithril-common/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/mithril-common/src/lib.rs b/mithril-common/src/lib.rs index cbd802d5b8..13f5852566 100644 --- a/mithril-common/src/lib.rs +++ b/mithril-common/src/lib.rs @@ -1,4 +1,5 @@ #![warn(missing_docs)] +#![cfg_attr(docsrs, feature(doc_cfg))] //! Shared datatypes and traits used by Mithril rust projects //! From b2f444c8600809c1909f8f533df5811cdee9a517 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Mon, 22 Jan 2024 17:58:46 +0100 Subject: [PATCH 2/5] Fix mithril-common build when using individual flag --- mithril-common/Cargo.toml | 3 ++- mithril-common/src/chain_observer/builder.rs | 9 ++++++--- mithril-common/src/chain_observer/mod.rs | 14 +++++++------- .../src/crypto_helper/cardano/cold_key.rs | 5 ++++- mithril-common/src/crypto_helper/cardano/mod.rs | 4 ++-- mithril-common/src/crypto_helper/era.rs | 12 ++++++------ mithril-common/src/crypto_helper/mod.rs | 4 ++-- mithril-common/src/entities/signed_entity.rs | 4 ++-- mithril-common/src/lib.rs | 7 ++++--- mithril-common/src/messages/certificate.rs | 5 +++-- .../src/messages/message_parts/signer.rs | 2 +- .../src/messages/mithril_stake_distribution.rs | 2 +- mithril-common/src/messages/register_signature.rs | 2 +- mithril-common/src/messages/register_signer.rs | 2 +- 14 files changed, 42 insertions(+), 33 deletions(-) diff --git a/mithril-common/Cargo.toml b/mithril-common/Cargo.toml index ea2c175430..15df94284b 100644 --- a/mithril-common/Cargo.toml +++ b/mithril-common/Cargo.toml @@ -77,6 +77,7 @@ mithril-stm = { path = "../mithril-stm", version = "0.3", default-features = fal criterion = { version = "0.5.1", features = ["html_reports", "async_tokio"] } mockall = "0.12.0" pallas-codec = "0.21.0" +rand_core = { version = "0.6.4", features = ["getrandom"] } reqwest = { version = "0.11.22", features = ["json"] } slog-async = "2.8.0" slog-scope = "4.4.0" @@ -104,7 +105,7 @@ portable = ["mithril-stm/portable"] # Disable signer certification, to be used only for tests allow_skip_signer_certification = [] # Enable all tests tools -test_tools = ["apispec", "test_http_server"] +test_tools = ["apispec", "test_http_server", "random"] # Enable tools to helps validate conformity to an OpenAPI specification apispec = ["glob", "http", "jsonschema", "warp"] test_http_server = ["warp"] diff --git a/mithril-common/src/chain_observer/builder.rs b/mithril-common/src/chain_observer/builder.rs index 0bd286a090..339faf68f4 100644 --- a/mithril-common/src/chain_observer/builder.rs +++ b/mithril-common/src/chain_observer/builder.rs @@ -4,7 +4,9 @@ use thiserror::Error; use crate::{chain_observer::ChainObserver, CardanoNetwork, StdResult}; -use super::{CardanoCliChainObserver, CardanoCliRunner, FakeObserver, PallasChainObserver}; +#[cfg(any(test, feature = "test_tools"))] +use super::FakeObserver; +use super::{CardanoCliChainObserver, CardanoCliRunner, PallasChainObserver}; /// Type of chain observers available #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] @@ -16,7 +18,7 @@ pub enum ChainObserverType { /// Pallas chain observer. Pallas, /// Fake chain observer. - #[cfg(feature = "test_tools")] + #[cfg(any(test, feature = "test_tools"))] Fake, } @@ -25,6 +27,7 @@ impl Display for ChainObserverType { match self { Self::CardanoCli => write!(f, "cardano-cli"), Self::Pallas => write!(f, "pallas"), + #[cfg(any(test, feature = "test_tools"))] Self::Fake => write!(f, "fake"), } } @@ -85,7 +88,7 @@ impl ChainObserverBuilder { ); Ok(Arc::new(observer)) } - #[cfg(feature = "test_tools")] + #[cfg(any(test, feature = "test_tools"))] ChainObserverType::Fake => Ok(Arc::new(FakeObserver::default())), } } diff --git a/mithril-common/src/chain_observer/mod.rs b/mithril-common/src/chain_observer/mod.rs index 45fdd887f8..f17c4bde5e 100644 --- a/mithril-common/src/chain_observer/mod.rs +++ b/mithril-common/src/chain_observer/mod.rs @@ -4,25 +4,25 @@ mod builder; #[cfg(all(feature = "fs", feature = "random"))] mod cli_observer; -#[cfg(feature = "test_tools")] +#[cfg(any(test, feature = "test_tools"))] mod fake_observer; mod interface; mod model; #[cfg(all(feature = "fs", feature = "random"))] mod pallas_observer; -#[cfg(test)] +#[cfg(all(test, feature = "fs", feature = "random"))] mod test_cli_runner; -#[cfg(test)] -pub use cli_observer::CliRunner; - #[cfg(all(feature = "fs", feature = "random"))] pub use builder::{ChainObserverBuilder, ChainObserverType}; #[cfg(all(feature = "fs", feature = "random"))] +pub use cli_observer::CliRunner; +#[cfg(all(feature = "fs", feature = "random"))] pub use cli_observer::{CardanoCliChainObserver, CardanoCliRunner}; -#[cfg(feature = "test_tools")] -pub use fake_observer::FakeObserver; +cfg_test_tools! { + pub use fake_observer::FakeObserver; +} #[cfg(test)] pub use interface::MockChainObserver; pub use interface::{ChainObserver, ChainObserverError}; diff --git a/mithril-common/src/crypto_helper/cardano/cold_key.rs b/mithril-common/src/crypto_helper/cardano/cold_key.rs index 67c5e0caa6..bc6d4f1252 100644 --- a/mithril-common/src/crypto_helper/cardano/cold_key.rs +++ b/mithril-common/src/crypto_helper/cardano/cold_key.rs @@ -3,11 +3,14 @@ use rand_chacha::ChaCha20Rng; use rand_core::SeedableRng; /// A cold key generator / test only +#[doc(hidden)] #[derive(Debug)] pub struct ColdKeyGenerator(); impl ColdKeyGenerator { - pub(crate) fn create_deterministic_keypair(seed: [u8; 32]) -> ColdSecretKey { + #[doc(hidden)] + /// Create a deterministic secret key using the given seed. (test only) + pub fn create_deterministic_keypair(seed: [u8; 32]) -> ColdSecretKey { let mut rng = ChaCha20Rng::from_seed(seed); ColdSecretKey::generate(&mut rng) } diff --git a/mithril-common/src/crypto_helper/cardano/mod.rs b/mithril-common/src/crypto_helper/cardano/mod.rs index 4c89902a20..9926e657f6 100644 --- a/mithril-common/src/crypto_helper/cardano/mod.rs +++ b/mithril-common/src/crypto_helper/cardano/mod.rs @@ -1,11 +1,11 @@ mod codec; -#[cfg(feature = "random")] +#[cfg(any(test, feature = "random"))] mod cold_key; mod key_certification; mod opcert; pub use codec::*; -#[cfg(feature = "random")] +#[cfg(any(test, feature = "random"))] pub use cold_key::*; pub use key_certification::*; pub use opcert::*; diff --git a/mithril-common/src/crypto_helper/era.rs b/mithril-common/src/crypto_helper/era.rs index e119a34916..abff325b95 100644 --- a/mithril-common/src/crypto_helper/era.rs +++ b/mithril-common/src/crypto_helper/era.rs @@ -49,12 +49,12 @@ impl EraMarkersSigner { Self::create_test_signer(rng) } - cfg_random! { - /// [EraMarkersSigner] non deterministic - pub fn create_non_deterministic_signer() -> Self { - let rng = rand_core::OsRng; - Self::create_test_signer(rng) - } + #[cfg(any(test, feature = "random"))] + #[cfg_attr(docsrs, doc(cfg(feature = "random")))] + /// [EraMarkersSigner] non deterministic + pub fn create_non_deterministic_signer() -> Self { + let rng = rand_core::OsRng; + Self::create_test_signer(rng) } /// [EraMarkersSigner] from [EraMarkersVerifierSecretKey] diff --git a/mithril-common/src/crypto_helper/mod.rs b/mithril-common/src/crypto_helper/mod.rs index de9b72086b..0698b7c6be 100644 --- a/mithril-common/src/crypto_helper/mod.rs +++ b/mithril-common/src/crypto_helper/mod.rs @@ -6,11 +6,11 @@ mod conversions; mod era; mod genesis; mod merkle_tree; -#[cfg(feature = "test_tools")] +#[cfg(any(test, feature = "test_tools"))] pub mod tests_setup; mod types; -#[cfg(feature = "random")] +#[cfg(any(test, feature = "random"))] pub use cardano::ColdKeyGenerator; pub use cardano::{ diff --git a/mithril-common/src/entities/signed_entity.rs b/mithril-common/src/entities/signed_entity.rs index c8a20a6f89..12842a4eeb 100644 --- a/mithril-common/src/entities/signed_entity.rs +++ b/mithril-common/src/entities/signed_entity.rs @@ -1,8 +1,8 @@ -#[cfg(feature = "test_tools")] +#[cfg(any(test, feature = "test_tools"))] use super::{Beacon, Epoch}; use super::{CardanoTransactionsCommitment, MithrilStakeDistribution, SignedEntityType, Snapshot}; use crate::signable_builder::Artifact; -#[cfg(feature = "test_tools")] +#[cfg(any(test, feature = "test_tools"))] use crate::test_utils::fake_data; use chrono::{DateTime, Utc}; diff --git a/mithril-common/src/lib.rs b/mithril-common/src/lib.rs index 13f5852566..1f402e5d31 100644 --- a/mithril-common/src/lib.rs +++ b/mithril-common/src/lib.rs @@ -34,7 +34,7 @@ macro_rules! cfg_random { macro_rules! cfg_test_tools { ($($item:item)*) => { $( - #[cfg(feature = "test_tools")] + #[cfg(any(test, feature = "test_tools"))] #[cfg_attr(docsrs, doc(cfg(feature = "test_tools")))] $item )* @@ -67,8 +67,9 @@ pub mod sqlite; #[cfg(feature = "database")] pub mod store; -#[cfg(feature = "test_tools")] -pub mod test_utils; +cfg_test_tools! { + pub mod test_utils; +} #[cfg(feature = "fs")] pub use beacon_provider::{BeaconProvider, BeaconProviderImpl}; diff --git a/mithril-common/src/messages/certificate.rs b/mithril-common/src/messages/certificate.rs index 9701564290..d03a7e220c 100644 --- a/mithril-common/src/messages/certificate.rs +++ b/mithril-common/src/messages/certificate.rs @@ -2,13 +2,14 @@ use anyhow::Context; use serde::{Deserialize, Serialize}; use std::fmt::{Debug, Formatter}; -#[cfg(feature = "test_tools")] +#[cfg(any(test, feature = "test_tools"))] use crate::entities::ProtocolMessagePartKey; use crate::entities::{ Beacon, Certificate, CertificateMetadata, CertificateSignature, ProtocolMessage, }; use crate::messages::CertificateMetadataMessagePart; -#[cfg(feature = "test_tools")] + +#[cfg(any(test, feature = "test_tools"))] use crate::test_utils::fake_keys; use crate::StdError; diff --git a/mithril-common/src/messages/message_parts/signer.rs b/mithril-common/src/messages/message_parts/signer.rs index 35a012a245..43a466f698 100644 --- a/mithril-common/src/messages/message_parts/signer.rs +++ b/mithril-common/src/messages/message_parts/signer.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "test_tools")] +#[cfg(any(test, feature = "test_tools"))] use crate::test_utils::fake_keys; use crate::{ crypto_helper::{KESPeriod, ProtocolOpCert, ProtocolSignerVerificationKeySignature}, diff --git a/mithril-common/src/messages/mithril_stake_distribution.rs b/mithril-common/src/messages/mithril_stake_distribution.rs index e78836d191..f616f5e840 100644 --- a/mithril-common/src/messages/mithril_stake_distribution.rs +++ b/mithril-common/src/messages/mithril_stake_distribution.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::entities::Epoch; use crate::entities::ProtocolParameters; -#[cfg(feature = "test_tools")] +#[cfg(any(test, feature = "test_tools"))] use crate::test_utils::fake_data; use super::SignerWithStakeMessagePart; diff --git a/mithril-common/src/messages/register_signature.rs b/mithril-common/src/messages/register_signature.rs index 4c066d92d9..7d18722d9f 100644 --- a/mithril-common/src/messages/register_signature.rs +++ b/mithril-common/src/messages/register_signature.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use std::fmt::{Debug, Formatter}; use crate::entities::{HexEncodedSingleSignature, LotteryIndex, PartyId, SignedEntityType}; -#[cfg(feature = "test_tools")] +#[cfg(any(test, feature = "test_tools"))] use crate::test_utils::fake_keys; era_deprecate!("make signed_entity_type of RegisterSignatureMessage not optional"); diff --git a/mithril-common/src/messages/register_signer.rs b/mithril-common/src/messages/register_signer.rs index c6d8de51d4..d1aebe132f 100644 --- a/mithril-common/src/messages/register_signer.rs +++ b/mithril-common/src/messages/register_signer.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use std::fmt::{Debug, Formatter}; -#[cfg(feature = "test_tools")] +#[cfg(any(test, feature = "test_tools"))] use crate::test_utils::fake_keys; use crate::{ crypto_helper::KESPeriod, From fafa570ef2981a3354bfc387381c59e7d3b61ecf Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 25 Jan 2024 17:13:12 +0100 Subject: [PATCH 3/5] Disable implicitly defined features in mithril-common By using the `dep:` prefix when specifying the features (see the [rustbook](https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies) to know how this work). This means that all the features of the crates are manually defined by us and can be easilly retrieved using `cargo metadata`. --- mithril-common/Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mithril-common/Cargo.toml b/mithril-common/Cargo.toml index 15df94284b..9cdfebf7ea 100644 --- a/mithril-common/Cargo.toml +++ b/mithril-common/Cargo.toml @@ -96,8 +96,8 @@ default = [] # Full feature set full = ["random", "database", "fs", "test_tools"] random = ["rand_core/getrandom"] -database = ["sqlite"] -fs = ["tokio/fs", "tokio/process", "pallas-network", "pallas-traverse"] +database = ["dep:sqlite"] +fs = ["tokio/fs", "tokio/process", "dep:pallas-network", "dep:pallas-traverse"] # Portable feature avoids SIGILL crashes on CPUs not supporting Intel ADX instruction set when built on CPUs that support it portable = ["mithril-stm/portable"] @@ -107,8 +107,8 @@ allow_skip_signer_certification = [] # Enable all tests tools test_tools = ["apispec", "test_http_server", "random"] # Enable tools to helps validate conformity to an OpenAPI specification -apispec = ["glob", "http", "jsonschema", "warp"] -test_http_server = ["warp"] +apispec = ["dep:glob", "dep:http", "dep:jsonschema", "dep:warp"] +test_http_server = ["dep:warp"] [package.metadata.docs.rs] all-features = true From 0be63c5ac7639571510aab702506a6f638e5f3ea Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Thu, 25 Jan 2024 18:36:40 +0100 Subject: [PATCH 4/5] Add target to mithril common makefile that check its powerset of features --- mithril-common/Makefile | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/mithril-common/Makefile b/mithril-common/Makefile index 275775be04..2e44dcfb46 100644 --- a/mithril-common/Makefile +++ b/mithril-common/Makefile @@ -1,6 +1,11 @@ -.PHONY: all build test check debug run help doc +.PHONY: all build test check doc check-all-features-set CARGO = cargo +# All the crates features excluding the two that have little to no impact to the code +FEATURES := $(shell ${CARGO} metadata --quiet --no-deps \ + | jq -r '.packages[] | select(.name=="mithril-common") | .features \ + | del(.allow_skip_signer_certification) | del(.portable) \ + | keys | join(" ")') all: test build @@ -18,3 +23,17 @@ check: doc: ${CARGO} doc --no-deps --open --features full + +# Compute the powerset of all the given features and save it to a file +.feature-sets: + powerset() { [ $$# -eq 0 ] && echo || (shift; powerset "$$@") | while read r ; do echo "$$1 $$r"; echo "$$r"; done };\ + powerset $$(echo "$(FEATURES)") > .features-sets + +check-all-features-set: .feature-sets + # Read the file to run cargo clippy on all those features sets + cat .features-sets | while read features_set; do \ + echo "Clippy common with feature '$$features_set''"; \ + ${CARGO} clippy -p mithril-common --features "$$features_set"; \ + done + + rm .features-sets From 079d148546d7efbeae18275ba65fafb832a33b03 Mon Sep 17 00:00:00 2001 From: DJO <790521+Alenar@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:23:02 +0100 Subject: [PATCH 5/5] Update mithril-common crate version --- Cargo.lock | 2 +- mithril-common/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 84d080208b..031f2b302e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3426,7 +3426,7 @@ dependencies = [ [[package]] name = "mithril-common" -version = "0.2.154" +version = "0.2.155" dependencies = [ "anyhow", "async-trait", diff --git a/mithril-common/Cargo.toml b/mithril-common/Cargo.toml index 9cdfebf7ea..130dac6382 100644 --- a/mithril-common/Cargo.toml +++ b/mithril-common/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mithril-common" -version = "0.2.154" +version = "0.2.155" description = "Common types, interfaces, and utilities for Mithril nodes." authors = { workspace = true } edition = { workspace = true }