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

Remove unnecessary reserialization #21

Merged
merged 1 commit into from
Feb 2, 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
16 changes: 2 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use anyhow::{Context, Result};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::Value;

use cairo_lang_starknet::casm_contract_class::CasmContractClass;
Expand All @@ -11,7 +9,7 @@ use cairo_lang_starknet_sierra_1_0_0::casm_contract_class::CasmContractClass as
use cairo_lang_starknet_sierra_1_0_0::contract_class::ContractClass as ContractClassSierraV1;

/// `sierra_json` should be a json containing `sierra_program` and `entry_points_by_type`
pub fn compile(mut sierra_json: Value) -> Result<CasmContractClass> {
pub fn compile(mut sierra_json: Value) -> Result<Value> {
sierra_json["abi"] = Value::Null;
sierra_json["sierra_program_debug_info"] = Value::Null;
sierra_json["contract_class_version"] = Value::String(String::new());
Expand All @@ -20,7 +18,7 @@ pub fn compile(mut sierra_json: Value) -> Result<CasmContractClass> {
($sierra_type:ty, $casm_type:ty) => {{
let sierra_class = serde_json::from_value::<$sierra_type>(sierra_json.clone()).unwrap();
let casm_class = <$casm_type>::from_contract_class(sierra_class, true).unwrap();
return Ok(old_casm_to_newest_casm::<$casm_type>(&casm_class));
return Ok(serde_json::to_value(&casm_class)?);
}};
}

Expand All @@ -38,16 +36,6 @@ pub fn compile(mut sierra_json: Value) -> Result<CasmContractClass> {
}
}

/// Converts `CasmContractClass` from the old `cairo_lang_starknet` library version
/// to the `CasmContractClass` from the newest version
fn old_casm_to_newest_casm<T>(value: &T) -> CasmContractClass
where
T: Serialize + DeserializeOwned,
{
let serialized = serde_json::to_value(value).unwrap();
serde_json::from_value::<CasmContractClass>(serialized).unwrap()
}

/// Extracts sierra version from the program
/// It will not be possible to convert sierra 0.1.0 version because it keeps its version only in the first felt252
/// (as a shortstring) while other versions keep it on the first 3 (major, minor, patch)
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ fn main_execution() -> Result<bool> {
let sierra_json =
serde_json::from_reader(sierra_file).context("Unable to read sierra json file")?;

let casm = compile(sierra_json)?;
let casm_json = serde_json::to_value(casm)?;
let casm_json = compile(sierra_json)?;

match args.casm_output_path {
Some(output_path) => {
Expand Down
Loading