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

feat: add util starknet-sierra-compile #35

Merged
merged 1 commit into from
May 29, 2024
Merged
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
13 changes: 13 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"crates/mempool_types",
"crates/mempool_node",
"crates/mempool_infra",
"crates/starknet_sierra_compile",
"crates/task_executor",
]
resolver = "2"
Expand Down Expand Up @@ -32,7 +33,9 @@ async-trait = "0.1.79"
axum = "0.6.12"
# TODO(YaelD, 1/5/2024): Use a fixed version once the StarkNet API is stable.
blockifier = { git = "https://github.com/starkware-libs/blockifier.git", rev = "fc62b8b8", features = ["testing"] }
cairo-lang-sierra = "2.6.0"
cairo-lang-starknet-classes = "2.6.0"
cairo-lang-utils = "2.6.0"
clap = "4.3.10"
derive_more = "0.99"
const_format = "0.2.30"
Expand Down
20 changes: 20 additions & 0 deletions crates/starknet_sierra_compile/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "starknet_sierra_compile"
version.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[lints]
workspace = true

[dependencies]
cairo-lang-sierra.workspace = true
cairo-lang-starknet-classes.workspace = true
cairo-lang-utils.workspace = true
serde_json.workspace = true
serde.workspace = true
thiserror.workspace = true

[dev-dependencies]
assert_matches.workspace = true
42 changes: 42 additions & 0 deletions crates/starknet_sierra_compile/src/compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use cairo_lang_starknet_classes::allowed_libfuncs::{AllowedLibfuncsError, ListSelector};
use cairo_lang_starknet_classes::casm_contract_class::{
CasmContractClass, StarknetSierraCompilationError,
};
use cairo_lang_starknet_classes::contract_class::ContractClass;
use thiserror::Error;

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

#[derive(Debug, Error)]
pub enum CompilationUtilError {
#[error(transparent)]
AllowedLibfuncsError(#[from] AllowedLibfuncsError),
#[error(transparent)]
StarknetSierraCompilationError(#[from] StarknetSierraCompilationError),
}

/// 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,
};

contract_class.validate_version_compatible(compilation_args.list_selector)?;

Ok(CasmContractClass::from_contract_class(
contract_class,
compilation_args.add_pythonic_hints,
compilation_args.max_bytecode_size,
)?)
}
38 changes: 38 additions & 0 deletions crates/starknet_sierra_compile/src/compile_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::path::Path;

use assert_matches::assert_matches;
use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError;

use crate::compile::{compile_sierra_to_casm, CompilationUtilError};
use crate::test_utils::contract_class_from_file;

const FAULTY_ACCOUNT_SIERRA_FILE: &str = "account_faulty.sierra.json";
const TEST_FILES_FOLDER: &str = "./tests/fixtures";

#[test]
fn test_compile_sierra_to_casm() {
let sierra_path = &Path::new(TEST_FILES_FOLDER).join(FAULTY_ACCOUNT_SIERRA_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 serialized_casm = serde_json::to_string_pretty(&casm_contract).unwrap().into_bytes();

assert_eq!(serialized_casm.len(), expected_casm_contract_length);
}

// TODO(Arni, 1/5/2024): Add a test for panic result test.
#[test]
fn test_negative_flow_compile_sierra_to_casm() {
let sierra_path = &Path::new(TEST_FILES_FOLDER).join(FAULTY_ACCOUNT_SIERRA_FILE);

let mut contract_class = contract_class_from_file(sierra_path);
// 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);
assert_matches!(
result,
Err(CompilationUtilError::AllowedLibfuncsError(AllowedLibfuncsError::SierraProgramError))
);
}
6 changes: 6 additions & 0 deletions crates/starknet_sierra_compile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! A lib for compiling Sierra into Casm.

pub mod compile;

#[cfg(any(feature = "testing", test))]
pub mod test_utils;
33 changes: 33 additions & 0 deletions crates/starknet_sierra_compile/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::fs;
use std::path::Path;

use cairo_lang_starknet_classes::contract_class::{ContractClass, ContractEntryPoints};
use cairo_lang_utils::bigint::BigUintAsHex;
use serde::Deserialize;

/// Same as `ContractClass` - but ignores unnecessary fields like `abi` in deserialization.
#[derive(Deserialize)]
struct DeserializedContractClass {
pub sierra_program: Vec<BigUintAsHex>,
pub sierra_program_debug_info: Option<cairo_lang_sierra::debug_info::DebugInfo>,
pub contract_class_version: String,
pub entry_points_by_type: ContractEntryPoints,
}

pub(crate) fn contract_class_from_file(file: &Path) -> ContractClass {
let DeserializedContractClass {
sierra_program,
sierra_program_debug_info,
contract_class_version,
entry_points_by_type,
} = serde_json::from_str(&fs::read_to_string(file).expect("Failed to read input file."))
.expect("deserialization Failed.");

ContractClass {
sierra_program,
sierra_program_debug_info,
contract_class_version,
entry_points_by_type,
abi: None,
}
}
Loading
Loading