Skip to content

Commit

Permalink
chore: expose max bytecode size arg for the compile sierra to casm util
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Aug 7, 2024
1 parent 92d201d commit 74dd64e
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 57 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 @@ -73,6 +73,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
5 changes: 5 additions & 0 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
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
8 changes: 3 additions & 5 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 Down Expand Up @@ -52,7 +50,7 @@ impl GatewayCompiler {
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
17 changes: 13 additions & 4 deletions crates/gateway/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ 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;

use crate::compiler_version::VersionId;

const MAX_BYTECODE_SIZE: usize = 81920;
const MAX_RAW_CLASS_SIZE: usize = 4089446;

#[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, PartialEq)]
pub struct GatewayConfig {
pub network_config: GatewayNetworkConfig,
Expand Down Expand Up @@ -88,8 +92,8 @@ 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,
max_bytecode_size: MAX_BYTECODE_SIZE,
max_raw_class_size: MAX_RAW_CLASS_SIZE,
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 @@ -233,10 +237,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",
)
}
}
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
2 changes: 2 additions & 0 deletions crates/gateway/src/stateless_transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ impl StatelessTransactionValidator {
&self,
contract_class: &starknet_api::rpc_transaction::ContractClass,
) -> StatelessTransactionValidatorResult<()> {
// This validation happens naturally when 'starknet-sierra-compile' is run. We still want to
// have this validation as early as possible as part of the stateless transaction validator.
let bytecode_size = contract_class.sierra_program.len();
if bytecode_size > self.config.max_bytecode_size {
return Err(StatelessTransactionValidatorError::BytecodeSizeTooLarge {
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
37 changes: 17 additions & 20 deletions crates/starknet_sierra_compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@ use cairo_lang_starknet_classes::allowed_libfuncs::ListSelector;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_lang_starknet_classes::contract_class::ContractClass;

use crate::config::SierraToCasmCompilationConfig;
use crate::errors::CompilationUtilError;

#[cfg(test)]
#[path = "compile_test.rs"]
pub mod compile_test;
pub struct SierraToCasmCompilationArgs {
list_selector: ListSelector,
add_pythonic_hints: bool,
max_bytecode_size: usize,
}

/// This function may panic.
pub fn compile_sierra_to_casm(
contract_class: ContractClass,
) -> Result<CasmContractClass, CompilationUtilError> {
let compilation_args = SierraToCasmCompilationArgs {
list_selector: ListSelector::DefaultList,
add_pythonic_hints: true,
max_bytecode_size: 1000000,
};
#[derive(Clone)]
pub struct SierraToCasmCompiler {
pub config: SierraToCasmCompilationConfig,
}

contract_class.validate_version_compatible(compilation_args.list_selector)?;
impl SierraToCasmCompiler {
pub fn compile(
&self,
contract_class: ContractClass,
) -> Result<CasmContractClass, CompilationUtilError> {
contract_class.validate_version_compatible(ListSelector::DefaultList)?;

Ok(CasmContractClass::from_contract_class(
contract_class,
compilation_args.add_pythonic_hints,
compilation_args.max_bytecode_size,
)?)
Ok(CasmContractClass::from_contract_class(
contract_class,
true,
self.config.max_bytecode_size,
)?)
}
}
Loading

0 comments on commit 74dd64e

Please sign in to comment.