Skip to content

Commit

Permalink
chore: compiler will raise error on casm bytecode size limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jul 23, 2024
1 parent 7289320 commit 5ab9f5e
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 32 deletions.
1 change: 1 addition & 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 @@ -37,6 +37,7 @@ axum = "0.6.12"
bincode = "1.3.3"
blockifier = { git = "https://github.com/starkware-libs/blockifier.git", rev = "32191d41" }
cairo-lang-sierra = "2.7.0-dev.0"
cairo-lang-sierra-to-casm = "2.7.0-dev.0"
cairo-lang-starknet-classes = "2.7.0-dev.0"
cairo-lang-utils = "2.7.0-dev.0"
cairo-vm = "1.0.0-rc3"
Expand Down
1 change: 1 addition & 0 deletions crates/gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ testing = []
async-trait.workspace = true
axum.workspace = true
blockifier= { workspace = true, features = ["testing"] }
cairo-lang-sierra-to-casm.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-vm.workspace = true
hyper.workspace = true
Expand Down
9 changes: 1 addition & 8 deletions crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,11 @@ impl GatewayCompiler {
// the validation of the raw class size. The validation should be linked to the way the class is
// saved in Papyrus etc.
/// Validates that the Casm class is within size limit. Specifically, this function validates
/// the size of the bytecode and the serialized class.
/// the size of the serialized class.
fn validate_casm_class_size(
&self,
casm_contract_class: &CasmContractClass,
) -> Result<(), GatewayError> {
let bytecode_size = casm_contract_class.bytecode.len();
if bytecode_size > self.config.max_casm_bytecode_size {
return Err(GatewayError::CasmBytecodeSizeTooLarge {
bytecode_size,
max_bytecode_size: self.config.max_casm_bytecode_size,
});
}
let contract_class_object_size = serde_json::to_string(&casm_contract_class)
.expect("Unexpected error serializing Casm contract class.")
.len();
Expand Down
10 changes: 9 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 Down Expand Up @@ -52,7 +54,13 @@ fn test_compile_contract_class_bytecode_size_validation(declare_tx: RPCDeclareTr
};

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

// TODO(Arni): Redesign this test once the compiler is passed with dependancy injection.
Expand Down
5 changes: 0 additions & 5 deletions crates/gateway/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ use crate::compiler_version::{VersionId, VersionIdError};
/// Errors directed towards the end-user, as a result of gateway requests.
#[derive(Debug, Error)]
pub enum GatewayError {
#[error(
"Cannot declare Casm contract class with bytecode size of {bytecode_size}; max allowed \
size: {max_bytecode_size}."
)]
CasmBytecodeSizeTooLarge { bytecode_size: usize, max_bytecode_size: usize },
#[error(
"Cannot declare Casm contract class with size of {contract_class_object_size}; max \
allowed size: {max_contract_class_object_size}."
Expand Down
20 changes: 2 additions & 18 deletions crates/starknet_sierra_compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,13 @@ use crate::errors::CompilationUtilError;
#[cfg(test)]
#[path = "compile_test.rs"]
pub mod compile_test;
struct SierraToCasmCompilationArgs {
list_selector: ListSelector,
add_pythonic_hints: bool,
}

impl Default for SierraToCasmCompilationArgs {
fn default() -> Self {
Self { list_selector: ListSelector::DefaultList, add_pythonic_hints: true }
}
}

/// This function may panic.
pub fn compile_sierra_to_casm(
contract_class: ContractClass,
max_bytecode_size: usize,
) -> Result<CasmContractClass, CompilationUtilError> {
let compilation_args = SierraToCasmCompilationArgs::default();

contract_class.validate_version_compatible(compilation_args.list_selector)?;
contract_class.validate_version_compatible(ListSelector::DefaultList)?;

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

0 comments on commit 5ab9f5e

Please sign in to comment.