From e10b23799c57c79abaf20352cea9308fdaf63175 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Tue, 25 Jun 2024 15:16:52 +0300 Subject: [PATCH] feat: simplify max_sierra_version enforcment --- config/default_config.json | 2 +- crates/gateway/src/compiler_version.rs | 2 +- crates/gateway/src/config.rs | 19 +------------------ crates/gateway/src/config_test.rs | 17 ----------------- .../src/stateless_transaction_validator.rs | 12 +++++------- .../stateless_transaction_validator_test.rs | 5 +++-- 6 files changed, 11 insertions(+), 46 deletions(-) delete mode 100644 crates/gateway/src/config_test.rs diff --git a/config/default_config.json b/config/default_config.json index e458a30c2..22889fb14 100644 --- a/config/default_config.json +++ b/config/default_config.json @@ -67,7 +67,7 @@ "gateway_config.stateless_tx_validator_config.max_sierra_version.patch": { "description": "The patch version of the configuration.", "privacy": "Public", - "value": 0 + "value": 18446744073709551615 }, "gateway_config.stateless_tx_validator_config.max_signature_length": { "description": "Validates that a transaction has signature length less than or equal to this value.", diff --git a/crates/gateway/src/compiler_version.rs b/crates/gateway/src/compiler_version.rs index b42b459dd..6317c9914 100644 --- a/crates/gateway/src/compiler_version.rs +++ b/crates/gateway/src/compiler_version.rs @@ -25,7 +25,7 @@ pub struct VersionId { impl VersionId { pub const MIN: Self = Self { major: 0, minor: 0, patch: 0 }; - pub const MAX: Self = Self { major: usize::MAX, minor: usize::MAX, patch: 0 }; + pub const MAX: Self = Self { major: usize::MAX, minor: usize::MAX, patch: usize::MAX }; } impl From<&VersionId> for CairoLangVersionId { diff --git a/crates/gateway/src/config.rs b/crates/gateway/src/config.rs index d01e54e68..9add4d86e 100644 --- a/crates/gateway/src/config.rs +++ b/crates/gateway/src/config.rs @@ -6,14 +6,10 @@ use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam}; use serde::{Deserialize, Serialize}; use starknet_api::core::{ChainId, ContractAddress, Nonce}; -use validator::{Validate, ValidationError}; +use validator::Validate; use crate::compiler_version::VersionId; -#[cfg(test)] -#[path = "config_test.rs"] -mod config_test; - #[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, PartialEq)] pub struct GatewayConfig { pub network_config: GatewayNetworkConfig, @@ -68,7 +64,6 @@ impl Default for GatewayNetworkConfig { } #[derive(Clone, Debug, Serialize, Deserialize, Validate, PartialEq)] -#[validate(schema(function = "validate_stateless_transaction_validator_config"))] pub struct StatelessTransactionValidatorConfig { // If true, validates that the resource bounds are not zero. pub validate_non_zero_l1_gas_fee: bool, @@ -139,18 +134,6 @@ impl SerializeConfig for StatelessTransactionValidatorConfig { } } -pub fn validate_stateless_transaction_validator_config( - config: &StatelessTransactionValidatorConfig, -) -> Result<(), ValidationError> { - if config.max_sierra_version.patch != 0 { - let mut error = ValidationError::new("Invalid max_sierra_version."); - error.message = Some("The patch version should be 0.".into()); - return Err(error); - } - - Ok(()) -} - #[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, PartialEq)] pub struct RpcStateReaderConfig { pub url: String, diff --git a/crates/gateway/src/config_test.rs b/crates/gateway/src/config_test.rs deleted file mode 100644 index 670ab0d8e..000000000 --- a/crates/gateway/src/config_test.rs +++ /dev/null @@ -1,17 +0,0 @@ -use assert_matches::assert_matches; -use validator::Validate; - -use super::StatelessTransactionValidatorConfig; -use crate::compiler_version::VersionId; - -#[test] -fn test_stateless_transaction_validator_config_validation() { - let mut config = StatelessTransactionValidatorConfig { - max_sierra_version: VersionId { major: 1, minor: 2, patch: 0 }, - ..Default::default() - }; - assert_matches!(config.validate(), Ok(())); - - config.max_sierra_version.patch = 1; - assert!(config.validate().is_err()); -} diff --git a/crates/gateway/src/stateless_transaction_validator.rs b/crates/gateway/src/stateless_transaction_validator.rs index 3095b81dc..97a47dd47 100644 --- a/crates/gateway/src/stateless_transaction_validator.rs +++ b/crates/gateway/src/stateless_transaction_validator.rs @@ -113,14 +113,12 @@ impl StatelessTransactionValidator { &self, sierra_program: &[StarkFelt], ) -> StatelessTransactionValidatorResult<()> { - let sierra_version = VersionId::from_sierra_program(sierra_program)?; - - // Check that the version is lower than the latest version allowing higher patch versions - // (i.e. we ignore the Z part in a version X.Y.Z). - let minor_sierra_version = VersionId { patch: 0, ..sierra_version }; + // Any patch version is valid. (i.e. when check version for upper bound, we ignore the Z + // part in a version X.Y.Z). + let max_sierra_version = VersionId { patch: usize::MAX, ..self.config.max_sierra_version }; - if self.config.min_sierra_version <= sierra_version - && minor_sierra_version <= self.config.max_sierra_version + let sierra_version = VersionId::from_sierra_program(sierra_program)?; + if self.config.min_sierra_version <= sierra_version && sierra_version <= max_sierra_version { return Ok(()); } diff --git a/crates/gateway/src/stateless_transaction_validator_test.rs b/crates/gateway/src/stateless_transaction_validator_test.rs index 993806fd2..495f5437d 100644 --- a/crates/gateway/src/stateless_transaction_validator_test.rs +++ b/crates/gateway/src/stateless_transaction_validator_test.rs @@ -18,7 +18,7 @@ use crate::stateless_transaction_validator::{ use crate::test_utils::create_sierra_program; const MIN_SIERRA_VERSION: VersionId = VersionId { major: 1, minor: 1, patch: 0 }; -const MAX_SIERRA_VERSION: VersionId = VersionId { major: 1, minor: 5, patch: 0 }; +const MAX_SIERRA_VERSION: VersionId = VersionId { major: 1, minor: 5, patch: usize::MAX }; const DEFAULT_VALIDATOR_CONFIG_FOR_TESTING: StatelessTransactionValidatorConfig = StatelessTransactionValidatorConfig { @@ -265,8 +265,9 @@ fn test_declare_sierra_version_failure( #[rstest] #[case::min_sierra_version(create_sierra_program(&MIN_SIERRA_VERSION))] #[case::valid_sierra_version(create_sierra_program(&VersionId { major: 1, minor: 3, patch: 0 }))] +#[case::max_sierra_version_patch_zero(create_sierra_program(&VersionId { patch: 0, ..MAX_SIERRA_VERSION }))] +#[case::max_sierra_version_patch_non_trivial(create_sierra_program(&VersionId { patch: 1, ..MAX_SIERRA_VERSION }))] #[case::max_sierra_version(create_sierra_program(&MAX_SIERRA_VERSION))] -#[case::max_sierra_version_ignore_patch(create_sierra_program(&VersionId { patch: 1, ..MAX_SIERRA_VERSION }))] fn test_declare_sierra_version_sucsses(#[case] sierra_program: Vec) { let tx_validator = StatelessTransactionValidator { config: DEFAULT_VALIDATOR_CONFIG_FOR_TESTING };