Skip to content

Commit

Permalink
chore: expose max bytecode size arg for the compile sierra to casm ut…
Browse files Browse the repository at this point in the history
…il (#283)
  • Loading branch information
ArniStarkware authored Aug 8, 2024
1 parent 41b496e commit 07571a1
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 108 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ cairo-felt = "0.9.1"
cairo-lang-casm = "2.7.0"
cairo-lang-runner = "2.7.0"
cairo-lang-sierra = "=2.7.0"
cairo-lang-sierra-to-casm = "2.7.0"
cairo-lang-starknet-classes = "2.7.0"
cairo-lang-utils = "2.7.0"
cairo-vm = "=1.0.0-rc6"
Expand Down
10 changes: 5 additions & 5 deletions config/mempool/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"privacy": "Public",
"value": true
},
"gateway_config.compiler_config.sierra_to_casm_compiler_config.max_bytecode_size": {
"description": "Limitation of contract bytecode size.",
"privacy": "Public",
"value": 81920
},
"gateway_config.network_config.ip": {
"description": "The gateway server ip.",
"privacy": "Public",
Expand Down Expand Up @@ -49,11 +54,6 @@
"privacy": "Public",
"value": 1000000
},
"gateway_config.stateless_tx_validator_config.max_bytecode_size": {
"description": "Limitation of contract bytecode size.",
"privacy": "Public",
"value": 81920
},
"gateway_config.stateless_tx_validator_config.max_calldata_length": {
"description": "Limitation of calldata length.",
"privacy": "Public",
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ num-integer.workspace = true
num-rational.workspace = true
num-traits.workspace = true
once_cell.workspace = true
papyrus_config = { path = "../papyrus_config", version = "0.4.0-rc.0" }
papyrus_config.workspace = true
paste.workspace = true
phf.workspace = true
rand = { workspace = true, optional = true }
Expand Down
1 change: 1 addition & 0 deletions crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ validator.workspace = true

[dev-dependencies]
assert_matches.workspace = true
cairo-lang-sierra-to-casm.workspace = true
mockall.workspace = true
mockito = "1.4.0"
num-bigint.workspace = true
Expand Down
9 changes: 3 additions & 6 deletions crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::contract_class::ContractClass as CairoLangContractClass;
use starknet_api::core::CompiledClassHash;
use starknet_api::rpc_transaction::RpcDeclareTransaction;
use starknet_sierra_compile::compile::compile_sierra_to_casm;
use starknet_sierra_compile::compile::SierraToCasmCompiler;
use starknet_sierra_compile::errors::CompilationUtilError;
use starknet_sierra_compile::utils::into_contract_class_for_compilation;

use crate::config::GatewayCompilerConfig;
use crate::errors::{GatewayError, GatewayResult};

#[cfg(test)]
Expand All @@ -19,8 +18,7 @@ mod compilation_test;
// TODO(Arni): Pass the compiler with dependancy injection.
#[derive(Clone)]
pub struct GatewayCompiler {
#[allow(dead_code)]
pub config: GatewayCompilerConfig,
pub sierra_to_casm_compiler: SierraToCasmCompiler,
}

impl GatewayCompiler {
Expand All @@ -46,13 +44,12 @@ impl GatewayCompiler {
)?)
}

// TODO(Arni): Pass the compilation args from the config.
fn compile(
&self,
cairo_lang_contract_class: CairoLangContractClass,
) -> Result<CasmContractClass, GatewayError> {
let catch_unwind_result =
panic::catch_unwind(|| compile_sierra_to_casm(cairo_lang_contract_class));
panic::catch_unwind(|| self.sierra_to_casm_compiler.compile(cairo_lang_contract_class));
let casm_contract_class =
catch_unwind_result.map_err(|_| CompilationUtilError::CompilationPanic)??;

Expand Down
25 changes: 24 additions & 1 deletion crates/gateway/src/compilation_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use assert_matches::assert_matches;
use blockifier::execution::contract_class::ContractClass;
use cairo_lang_sierra_to_casm::compiler::CompilationError;
use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError;
use cairo_lang_starknet_classes::casm_contract_class::StarknetSierraCompilationError;
use mempool_test_utils::starknet_api_test_utils::declare_tx as rpc_declare_tx;
use rstest::{fixture, rstest};
use starknet_api::core::CompiledClassHash;
Expand All @@ -9,14 +11,16 @@ use starknet_api::rpc_transaction::{
RpcDeclareTransactionV3,
RpcTransaction,
};
use starknet_sierra_compile::compile::SierraToCasmCompiler;
use starknet_sierra_compile::config::SierraToCasmCompilationConfig;
use starknet_sierra_compile::errors::CompilationUtilError;

use crate::compilation::GatewayCompiler;
use crate::errors::GatewayError;

#[fixture]
fn gateway_compiler() -> GatewayCompiler {
GatewayCompiler { config: Default::default() }
GatewayCompiler { sierra_to_casm_compiler: SierraToCasmCompiler { config: Default::default() } }
}

#[fixture]
Expand Down Expand Up @@ -46,6 +50,25 @@ fn test_compile_contract_class_compiled_class_hash_mismatch(
);
}

// TODO(Arni): Redesign this test once the compiler is passed with dependancy injection.
#[rstest]
fn test_compile_contract_class_bytecode_size_validation(declare_tx_v3: RpcDeclareTransactionV3) {
let gateway_compiler = GatewayCompiler {
sierra_to_casm_compiler: SierraToCasmCompiler {
config: SierraToCasmCompilationConfig { max_bytecode_size: 1 },
},
};

let result = gateway_compiler.process_declare_tx(&RpcDeclareTransaction::V3(declare_tx_v3));
assert_matches!(
result.unwrap_err(),
GatewayError::CompilationError(CompilationUtilError::StarknetSierraCompilationError(
StarknetSierraCompilationError::CompilationError(err)
))
if matches!(err.as_ref(), CompilationError::CodeSizeLimitExceeded)
)
}

#[rstest]
fn test_compile_contract_class_bad_sierra(
gateway_compiler: GatewayCompiler,
Expand Down
18 changes: 8 additions & 10 deletions crates/gateway/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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::Nonce;
use starknet_sierra_compile::config::SierraToCasmCompilationConfig;
use starknet_types_core::felt::Felt;
use validator::Validate;

Expand Down Expand Up @@ -75,7 +76,6 @@ pub struct StatelessTransactionValidatorConfig {
pub max_signature_length: usize,

// Declare txs specific config.
pub max_bytecode_size: usize,
pub max_raw_class_size: usize,
pub min_sierra_version: VersionId,
pub max_sierra_version: VersionId,
Expand All @@ -88,7 +88,6 @@ impl Default for StatelessTransactionValidatorConfig {
validate_non_zero_l2_gas_fee: false,
max_calldata_length: 4000,
max_signature_length: 4000,
max_bytecode_size: 81920,
max_raw_class_size: 4089446,
min_sierra_version: VersionId { major: 1, minor: 1, patch: 0 },
max_sierra_version: VersionId { major: 1, minor: 5, patch: usize::MAX },
Expand Down Expand Up @@ -123,12 +122,6 @@ impl SerializeConfig for StatelessTransactionValidatorConfig {
"Limitation of calldata length.",
ParamPrivacyInput::Public,
),
ser_param(
"max_bytecode_size",
&self.max_bytecode_size,
"Limitation of contract bytecode size.",
ParamPrivacyInput::Public,
),
ser_param(
"max_raw_class_size",
&self.max_raw_class_size,
Expand Down Expand Up @@ -233,10 +226,15 @@ impl StatefulTransactionValidatorConfig {
}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, Validate, PartialEq)]
pub struct GatewayCompilerConfig {}
pub struct GatewayCompilerConfig {
pub sierra_to_casm_compiler_config: SierraToCasmCompilationConfig,
}

impl SerializeConfig for GatewayCompilerConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
BTreeMap::new()
append_sub_config_name(
self.sierra_to_casm_compiler_config.dump(),
"sierra_to_casm_compiler_config",
)
}
}
5 changes: 0 additions & 5 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ pub enum StatelessTransactionValidatorError {
The Sierra version of the declared contract is {version}."
)]
UnsupportedSierraVersion { version: VersionId, min_version: VersionId, max_version: VersionId },
#[error(
"Cannot declare contract class with bytecode size of {bytecode_size}; max allowed size: \
{max_bytecode_size}."
)]
BytecodeSizeTooLarge { bytecode_size: usize, max_bytecode_size: usize },
#[error(
"Cannot declare contract class with size of {contract_class_object_size}; max allowed \
size: {max_contract_class_object_size}."
Expand Down
7 changes: 6 additions & 1 deletion crates/gateway/src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starknet_api::transaction::TransactionHash;
use starknet_mempool_infra::component_runner::{ComponentStartError, ComponentStarter};
use starknet_mempool_types::communication::SharedMempoolClient;
use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput};
use starknet_sierra_compile::compile::SierraToCasmCompiler;
use tracing::{info, instrument};

use crate::compilation::GatewayCompiler;
Expand Down Expand Up @@ -154,7 +155,11 @@ pub fn create_gateway(
mempool_client: SharedMempoolClient,
) -> Gateway {
let state_reader_factory = Arc::new(RpcStateReaderFactory { config: rpc_state_reader_config });
let gateway_compiler = GatewayCompiler { config: config.compiler_config };
let gateway_compiler = GatewayCompiler {
sierra_to_casm_compiler: SierraToCasmCompiler {
config: config.compiler_config.sierra_to_casm_compiler_config,
},
};
Gateway::new(config, state_reader_factory, gateway_compiler, mempool_client)
}

Expand Down
23 changes: 9 additions & 14 deletions crates/gateway/src/gateway_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_mempool_types::communication::MockMempoolClient;
use starknet_mempool_types::mempool_types::{Account, AccountState, MempoolInput, ThinTransaction};
use starknet_sierra_compile::compile::SierraToCasmCompiler;
use starknet_sierra_compile::config::SierraToCasmCompilationConfig;

use crate::compilation::GatewayCompiler;
use crate::config::{
GatewayCompilerConfig,
StatefulTransactionValidatorConfig,
StatelessTransactionValidatorConfig,
};
use crate::config::{StatefulTransactionValidatorConfig, StatelessTransactionValidatorConfig};
use crate::gateway::{add_tx, AppState, SharedMempoolClient};
use crate::state_reader_test_utils::{local_test_state_reader_factory, TestStateReaderFactory};
use crate::stateful_transaction_validator::StatefulTransactionValidator;
Expand All @@ -32,19 +30,16 @@ pub fn app_state(
) -> AppState {
AppState {
stateless_tx_validator: StatelessTransactionValidator {
config: StatelessTransactionValidatorConfig {
validate_non_zero_l1_gas_fee: true,
max_calldata_length: 10,
max_signature_length: 2,
max_bytecode_size: 10000,
max_raw_class_size: 1000000,
..Default::default()
},
config: StatelessTransactionValidatorConfig::default(),
},
stateful_tx_validator: Arc::new(StatefulTransactionValidator {
config: StatefulTransactionValidatorConfig::create_for_testing(),
}),
gateway_compiler: GatewayCompiler { config: GatewayCompilerConfig {} },
gateway_compiler: GatewayCompiler {
sierra_to_casm_compiler: SierraToCasmCompiler {
config: SierraToCasmCompilationConfig::default(),
},
},
state_reader_factory: Arc::new(state_reader_factory),
mempool_client,
}
Expand Down
11 changes: 7 additions & 4 deletions crates/gateway/src/stateful_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::felt;
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::transaction::TransactionHash;
use starknet_sierra_compile::compile::SierraToCasmCompiler;
use starknet_types_core::felt::Felt;

use crate::compilation::GatewayCompiler;
use crate::config::{GatewayCompilerConfig, StatefulTransactionValidatorConfig};
use crate::config::StatefulTransactionValidatorConfig;
use crate::errors::{StatefulTransactionValidatorError, StatefulTransactionValidatorResult};
use crate::state_reader::{MockStateReaderFactory, StateReaderFactory};
use crate::state_reader_test_utils::local_test_state_reader_factory;
Expand Down Expand Up @@ -83,9 +84,11 @@ fn test_stateful_tx_validator(
) {
let optional_class_info = match &external_tx {
RpcTransaction::Declare(declare_tx) => Some(
GatewayCompiler { config: GatewayCompilerConfig {} }
.process_declare_tx(declare_tx)
.unwrap(),
GatewayCompiler {
sierra_to_casm_compiler: SierraToCasmCompiler { config: Default::default() },
}
.process_declare_tx(declare_tx)
.unwrap(),
),
_ => None,
};
Expand Down
8 changes: 0 additions & 8 deletions crates/gateway/src/stateless_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ impl StatelessTransactionValidator {
&self,
contract_class: &starknet_api::rpc_transaction::ContractClass,
) -> StatelessTransactionValidatorResult<()> {
let bytecode_size = contract_class.sierra_program.len();
if bytecode_size > self.config.max_bytecode_size {
return Err(StatelessTransactionValidatorError::BytecodeSizeTooLarge {
bytecode_size,
max_bytecode_size: self.config.max_bytecode_size,
});
}

let contract_class_object_size = serde_json::to_string(&contract_class)
.expect("Unexpected error serializing contract class.")
.len();
Expand Down
26 changes: 0 additions & 26 deletions crates/gateway/src/stateless_transaction_validator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const DEFAULT_VALIDATOR_CONFIG_FOR_TESTING: StatelessTransactionValidatorConfig
validate_non_zero_l2_gas_fee: false,
max_calldata_length: 1,
max_signature_length: 1,
max_bytecode_size: 10000,
max_raw_class_size: 100000,
min_sierra_version: MIN_SIERRA_VERSION,
max_sierra_version: MAX_SIERRA_VERSION,
Expand Down Expand Up @@ -288,31 +287,6 @@ fn test_declare_sierra_version_sucsses(#[case] sierra_program: Vec<Felt>) {
assert_matches!(tx_validator.validate(&tx), Ok(()));
}

#[test]
fn test_declare_bytecode_size_too_long() {
let config_max_bytecode_size = 10;
let tx_validator = StatelessTransactionValidator {
config: StatelessTransactionValidatorConfig {
max_bytecode_size: config_max_bytecode_size,
..DEFAULT_VALIDATOR_CONFIG_FOR_TESTING
},
};
let sierra_program_length = config_max_bytecode_size + 1;
let sierra_program = vec![felt!(1_u128); sierra_program_length];
let contract_class = ContractClass { sierra_program, ..Default::default() };
let tx = external_declare_tx(declare_tx_args!(contract_class));

assert_matches!(
tx_validator.validate(&tx).unwrap_err(),
StatelessTransactionValidatorError::BytecodeSizeTooLarge {
bytecode_size,
max_bytecode_size
} if (
bytecode_size, max_bytecode_size
) == (sierra_program_length, config_max_bytecode_size)
)
}

#[test]
fn test_declare_contract_class_size_too_long() {
let config_max_raw_class_size = 100; // Some arbitrary value, which will fail the test.
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_sierra_compile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ workspace = true
cairo-lang-sierra.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-lang-utils.workspace = true
papyrus_config.workspace = true
serde.workspace = true
serde_json.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
thiserror.workspace = true
validator.workspace = true

[dev-dependencies]
assert_matches.workspace = true
mempool_test_utils.workspace = true
rstest.workspace = true
Loading

0 comments on commit 07571a1

Please sign in to comment.