Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: expose compilation args for the compile sierra to casm util #348

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 4 additions & 10 deletions crates/gateway/src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ impl GatewayCompiler {
&self,
cairo_lang_contract_class: CairoLangContractClass,
) -> Result<CasmContractClass, GatewayError> {
let catch_unwind_result =
panic::catch_unwind(|| compile_sierra_to_casm(cairo_lang_contract_class));
let catch_unwind_result = panic::catch_unwind(|| {
compile_sierra_to_casm(cairo_lang_contract_class, self.config.max_casm_bytecode_size)
});
let casm_contract_class =
catch_unwind_result.map_err(|_| CompilationUtilError::CompilationPanic)??;

Expand All @@ -69,18 +70,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: 3 additions & 17 deletions crates/starknet_sierra_compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,13 @@ 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,
max_bytecode_size: usize,
) -> Result<CasmContractClass, CompilationUtilError> {
let compilation_args = SierraToCasmCompilationArgs {
list_selector: ListSelector::DefaultList,
add_pythonic_hints: true,
max_bytecode_size: 1000000,
};

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,
compilation_args.max_bytecode_size,
)?)
Ok(CasmContractClass::from_contract_class(contract_class, true, max_bytecode_size)?)
}
6 changes: 4 additions & 2 deletions crates/starknet_sierra_compile/src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ use mempool_test_utils::{get_absolute_path, FAULTY_ACCOUNT_CLASS_FILE, TEST_FILE
use crate::compile::{compile_sierra_to_casm, CompilationUtilError};
use crate::test_utils::contract_class_from_file;

const MAX_BYTECODE_SIZE: usize = 81920;

#[test]
fn test_compile_sierra_to_casm() {
env::set_current_dir(get_absolute_path(TEST_FILES_FOLDER)).expect("Failed to set current dir.");
let sierra_path = Path::new(FAULTY_ACCOUNT_CLASS_FILE);
let expected_casm_contract_length = 72304;

let contract_class = contract_class_from_file(sierra_path);
let casm_contract = compile_sierra_to_casm(contract_class).unwrap();
let casm_contract = compile_sierra_to_casm(contract_class, MAX_BYTECODE_SIZE).unwrap();
let serialized_casm = serde_json::to_string_pretty(&casm_contract).unwrap().into_bytes();

assert_eq!(serialized_casm.len(), expected_casm_contract_length);
Expand All @@ -31,7 +33,7 @@ fn test_negative_flow_compile_sierra_to_casm() {
// Truncate the sierra program to trigger an error.
contract_class.sierra_program = contract_class.sierra_program[..100].to_vec();

let result = compile_sierra_to_casm(contract_class);
let result = compile_sierra_to_casm(contract_class, MAX_BYTECODE_SIZE);
assert_matches!(
result,
Err(CompilationUtilError::AllowedLibfuncsError(AllowedLibfuncsError::SierraProgramError))
Expand Down
Loading