diff --git a/Cargo.lock b/Cargo.lock index 3a53aecbdd..acc9342a15 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 3b8d4a7cd8..00783c2007 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 } @@ -87,6 +87,7 @@ criterion = { version = "0.5.1", features = ["html_reports", "async_tokio"] } mockall = "0.12.0" # pallas-crypto = "0.21.0" pallas-crypto = { git = "https://github.com/falcucci/pallas.git", branch = "fix/legacy-transaction-utxo-by-address" } +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" @@ -105,16 +106,16 @@ default = [] # Full feature set full = ["random", "database", "fs", "test_tools"] random = ["rand_core/getrandom"] -database = ["sqlite"] +database = ["dep:sqlite"] fs = [ "tokio/fs", "tokio/process", - "minicbor", - "pallas-addresses", - "pallas-codec", - "pallas-network", - "pallas-primitives", - "pallas-traverse", + "dep:minicbor", + "dep:pallas-addresses", + "dep:pallas-codec", + "dep:pallas-network", + "dep:pallas-primitives", + "dep:pallas-traverse", ] # Portable feature avoids SIGILL crashes on CPUs not supporting Intel ADX instruction set when built on CPUs that support it @@ -123,10 +124,10 @@ 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"] +apispec = ["dep:glob", "dep:http", "dep:jsonschema", "dep:warp"] +test_http_server = ["dep:warp"] [package.metadata.docs.rs] all-features = true 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 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 a72039f742..2925b8839d 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 //! @@ -43,7 +44,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 )* @@ -76,8 +77,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,