Skip to content

Commit

Permalink
feat: simplify max_sierra_version enforcment
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jun 25, 2024
1 parent 5512c32 commit e10b237
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 46 deletions.
2 changes: 1 addition & 1 deletion config/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion crates/gateway/src/compiler_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 1 addition & 18 deletions crates/gateway/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 0 additions & 17 deletions crates/gateway/src/config_test.rs

This file was deleted.

12 changes: 5 additions & 7 deletions crates/gateway/src/stateless_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
Expand Down
5 changes: 3 additions & 2 deletions crates/gateway/src/stateless_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<StarkFelt>) {
let tx_validator =
StatelessTransactionValidator { config: DEFAULT_VALIDATOR_CONFIG_FOR_TESTING };
Expand Down

0 comments on commit e10b237

Please sign in to comment.