From e2153ad47ecfcbe37756ad8a736083fa700a4952 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Tue, 19 Dec 2023 20:27:53 +0100 Subject: [PATCH] refactor: define conflicting features in a more general way --- filecoin-proofs/src/types/porep_config.rs | 18 ++++++++---------- storage-proofs-core/src/api_version.rs | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/filecoin-proofs/src/types/porep_config.rs b/filecoin-proofs/src/types/porep_config.rs index e255e8387..2eff1b324 100644 --- a/filecoin-proofs/src/types/porep_config.rs +++ b/filecoin-proofs/src/types/porep_config.rs @@ -100,13 +100,16 @@ impl PoRepConfig { #[inline] pub fn enable_feature(&mut self, feat: ApiFeature) -> Result<()> { + for conflict in feat.conflicting_features() { + if self.feature_enabled(*conflict) { + return Err(anyhow!( + "Cannot enable feature `{feat}` when `{conflict}` is already enabled" + )); + } + } + match feat { ApiFeature::SyntheticPoRep => { - if self.feature_enabled(ApiFeature::NonInteractivePoRep) { - return Err(anyhow!( - "Cannot enable Synthetic PoRep when Non-interactive PoRep is already enabled")); - } - self.partitions = PoRepProofPartitions( *POREP_PARTITIONS .read() @@ -116,11 +119,6 @@ impl PoRepConfig { ); } ApiFeature::NonInteractivePoRep => { - if self.feature_enabled(ApiFeature::SyntheticPoRep) { - return Err(anyhow!( - "Cannot enable Non-interactive PoRep when Synthetic PoRep is already enabled")); - } - self.partitions = PoRepProofPartitions( constants::get_porep_non_interactive_partitions(self.sector_size.into()), ); diff --git a/storage-proofs-core/src/api_version.rs b/storage-proofs-core/src/api_version.rs index 8dacfd745..a0966b54f 100644 --- a/storage-proofs-core/src/api_version.rs +++ b/storage-proofs-core/src/api_version.rs @@ -112,6 +112,24 @@ impl ApiFeature { ApiFeature::SyntheticPoRep | ApiFeature::NonInteractivePoRep => None, } } + + /// Return the features that are in conflict with the current one. + pub fn conflicting_features(&self) -> &[ApiFeature] { + match self { + ApiFeature::SyntheticPoRep => &[ApiFeature::NonInteractivePoRep], + ApiFeature::NonInteractivePoRep => &[ApiFeature::SyntheticPoRep], + } + } +} + +impl Display for ApiFeature { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let api_feature_str = match self { + Self::SyntheticPoRep => "synthetic-porep", + Self::NonInteractivePoRep => "non-interactive-porep", + }; + write!(f, "{}", api_feature_str) + } } #[test]