Skip to content

Commit 73b6968

Browse files
committed
Update sncast crate so it depends on cairo-lang-runner and cairo-lang-runnable-utils directly
commit-id:2b801af1
1 parent a2007b2 commit 73b6968

File tree

5 files changed

+266
-137
lines changed

5 files changed

+266
-137
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ license-file = "LICENSE"
3030
blockifier = { git = "https://github.com/software-mansion-labs/sequencer.git", branch = "main-v0.13.5-modified-sierra-dep", default-features = false, features = ["testing", "tracing"] }
3131
starknet_api = { git = "https://github.com/software-mansion-labs/sequencer.git", branch = "main-v0.13.5-modified-sierra-dep" }
3232
cairo-lang-casm = { version = "2.11.2", features = ["serde"] }
33-
cairo-lang-runner = "2.11.2"
34-
cairo-lang-sierra-to-casm = "2.11.2"
3533
cairo-lang-sierra = "2.11.2"
3634
cairo-lang-utils = "2.11.2"
3735
cairo-lang-starknet = "2.11.2"
38-
cairo-lang-runnable-utils = "2.11.2"
3936
cairo-lang-filesystem = "2.11.2"
4037
cairo-lang-diagnostics = "2.11.2"
4138
cairo-lang-sierra-type-size = "2.11.2"

crates/sncast/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ data-transformer = { path = "../data-transformer" }
3232
configuration = { path = "../configuration" }
3333
shared = { path = "../shared" }
3434
forge_runner = { path = "../forge-runner" }
35-
cairo-lang-runner.workspace = true
36-
cairo-lang-sierra-to-casm.workspace = true
37-
cairo-lang-runnable-utils.workspace = true
35+
cairo-lang-runner = "2.11.2"
36+
cairo-lang-runnable-utils = "2.11.2"
3837
cairo-lang-utils.workspace = true
3938
cairo-lang-sierra.workspace = true
4039
cairo-lang-casm.workspace = true
@@ -54,6 +53,7 @@ regex.workspace = true
5453
dirs.workspace = true
5554
dialoguer.workspace = true
5655
toml_edit.workspace = true
56+
num-traits.workspace = true
5757

5858
[dev-dependencies]
5959
ctor.workspace = true

crates/sncast/src/starknet_commands/script/run.rs

+135-130
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use cairo_lang_runner::casm_run::hint_to_hint_params;
1414
use cairo_lang_runner::short_string::as_cairo_short_string;
1515
use cairo_lang_runner::{Arg, RunResultValue, SierraCasmRunner};
1616
use cairo_lang_sierra::program::VersionedProgram;
17-
use cairo_lang_sierra_to_casm::metadata::MetadataComputationConfig;
1817
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
1918
use cairo_vm::serde::deserialize_program::HintParams;
2019
use cairo_vm::types::relocatable::Relocatable;
@@ -33,6 +32,7 @@ use runtime::{
3332
};
3433
use scarb_api::{StarknetContractArtifacts, package_matches_version_requirement};
3534
use scarb_metadata::{Metadata, PackageMetadata};
35+
use script_runtime::CastScriptRuntime;
3636
use semver::{Comparator, Op, Version, VersionReq};
3737
use shared::print::print_as_warning;
3838
use shared::utils::build_readable_text;
@@ -56,6 +56,8 @@ use std::collections::HashMap;
5656
use std::fs;
5757
use tokio::runtime::Runtime;
5858

59+
mod script_runtime;
60+
5961
type ScriptStarknetContractArtifacts = StarknetContractArtifacts;
6062

6163
#[derive(Args, Debug)]
@@ -94,7 +96,7 @@ impl CastScriptExtension<'_> {
9496
}
9597

9698
impl<'a> ExtensionLogic for CastScriptExtension<'a> {
97-
type Runtime = StarknetRuntime<'a>;
99+
type Runtime = CastScriptRuntime<'a>;
98100

99101
#[expect(clippy::too_many_lines)]
100102
fn handle_cheatcode(
@@ -272,136 +274,139 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
272274
}
273275
}
274276

275-
#[expect(clippy::too_many_arguments)]
277+
#[expect(clippy::too_many_arguments, clippy::too_many_lines)]
276278
pub fn run(
277-
_module_name: &str,
278-
_metadata: &Metadata,
279-
_package_metadata: &PackageMetadata,
280-
_artifacts: &mut HashMap<String, StarknetContractArtifacts>,
281-
_provider: &JsonRpcClient<HttpTransport>,
282-
_tokio_runtime: Runtime,
283-
_config: &CastConfig,
284-
_state_file_path: Option<Utf8PathBuf>,
279+
module_name: &str,
280+
metadata: &Metadata,
281+
package_metadata: &PackageMetadata,
282+
artifacts: &mut HashMap<String, StarknetContractArtifacts>,
283+
provider: &JsonRpcClient<HttpTransport>,
284+
tokio_runtime: Runtime,
285+
config: &CastConfig,
286+
state_file_path: Option<Utf8PathBuf>,
285287
) -> Result<ScriptRunResponse> {
286-
todo!("Use blockifier to run");
287-
// warn_if_sncast_std_not_compatible(metadata)?;
288-
// let artifacts = inject_lib_artifact(metadata, package_metadata, artifacts)?;
289-
//
290-
// let artifact = artifacts
291-
// .get(SCRIPT_LIB_ARTIFACT_NAME)
292-
// .ok_or(anyhow!("Failed to find script artifact"))?;
293-
//
294-
// let sierra_program = serde_json::from_str::<VersionedProgram>(&artifact.sierra)
295-
// .with_context(|| "Failed to deserialize Sierra program")?
296-
// .into_v1()
297-
// .with_context(|| "Failed to load Sierra program")?
298-
// .program;
299-
//
300-
// let runner = SierraCasmRunner::new(
301-
// sierra_program.clone(),
302-
// Some(MetadataComputationConfig::default()),
303-
// OrderedHashMap::default(),
304-
// None,
305-
// )
306-
// .with_context(|| "Failed to set up runner")?;
307-
//
308-
// // `builder` field in `SierraCasmRunner` is private, hence the need to create a new `RunnableBuilder`
309-
// // https://github.com/starkware-libs/cairo/blob/66f5c7223f7a6c27c5f800816dba05df9b60674e/crates/cairo-lang-runner/src/lib.rs#L184
310-
// let builder = RunnableBuilder::new(sierra_program, Some(MetadataComputationConfig::default()))
311-
// .with_context(|| "Failed to create builder")?;
312-
//
313-
// let name_suffix = module_name.to_string() + "::main";
314-
// let func = runner.find_function(name_suffix.as_str())
315-
// .context("Failed to find main function in script - please make sure `sierra-replace-ids` is not set to `false` for `dev` profile in script's Scarb.toml")?;
316-
//
317-
// let entry_code_config = EntryCodeConfig::testing();
318-
// let casm_program_wrapper_info = builder.create_wrapper_info(func, entry_code_config)?;
319-
// let entry_code = casm_program_wrapper_info.header;
320-
// let builtins = casm_program_wrapper_info.builtins;
321-
// let footer = create_code_footer();
322-
//
323-
// // import from cairo-lang-runner
324-
// let assembled_program = builder
325-
// .casm_program()
326-
// .clone()
327-
// .assemble_ex(&entry_code, &footer);
328-
// let (hints_dict, string_to_hint) = hints_to_params(assembled_program.hints);
329-
//
330-
// // hint processor
331-
// let mut context = build_context(
332-
// &SerializableBlockInfo::default().into(),
333-
// None,
334-
// &TrackedResource::CairoSteps,
335-
// );
336-
//
337-
// let mut blockifier_state = CachedState::new(DictStateReader::default());
338-
//
339-
// // TODO(#2954)
340-
// let param_types = builder.generic_id_and_size_from_concrete(&func.signature.param_types);
341-
//
342-
// let segment_index = syscall_handler_offset(builtins.len(), has_segment_arena(&param_types));
343-
// let syscall_handler = SyscallHintProcessor::new(
344-
// &mut blockifier_state,
345-
// &mut context,
346-
// // This segment is created by SierraCasmRunner
347-
// Relocatable {
348-
// segment_index: segment_index
349-
// .try_into()
350-
// .expect("Failed to convert index to isize"),
351-
// offset: 0,
352-
// },
353-
// ExecutableCallEntryPoint::default(),
354-
// &string_to_hint,
355-
// ReadOnlySegments::default(),
356-
// );
357-
//
358-
// let account = if config.account.is_empty() {
359-
// None
360-
// } else {
361-
// Some(tokio_runtime.block_on(get_account(
362-
// &config.account,
363-
// &config.accounts_file,
364-
// provider,
365-
// config.keystore.clone(),
366-
// ))?)
367-
// };
368-
// let state = StateManager::from(_state_file_path)?;
369-
//
370-
// let cast_extension = CastScriptExtension {
371-
// provider,
372-
// tokio_runtime,
373-
// config,
374-
// artifacts: &artifacts,
375-
// account: account.as_ref(),
376-
// state,
377-
// };
378-
//
379-
// let mut cast_runtime = ExtendedRuntime {
380-
// extension: cast_extension,
381-
// extended_runtime: StarknetRuntime {
382-
// hint_handler: syscall_handler,
383-
// },
384-
// };
385-
//
386-
// match runner.run_function(
387-
// func,
388-
// &mut cast_runtime,
389-
// hints_dict,
390-
// assembled_program.bytecode.iter(),
391-
// builtins,
392-
// ) {
393-
// Ok(result) => match result.value {
394-
// RunResultValue::Success(data) => Ok(ScriptRunResponse {
395-
// status: "success".to_string(),
396-
// message: build_readable_text(&data),
397-
// }),
398-
// RunResultValue::Panic(panic_data) => Ok(ScriptRunResponse {
399-
// status: "script panicked".to_string(),
400-
// message: build_readable_text(&panic_data),
401-
// }),
402-
// },
403-
// Err(err) => Err(err.into()),
404-
// }
288+
warn_if_sncast_std_not_compatible(metadata)?;
289+
let artifacts = inject_lib_artifact(metadata, package_metadata, artifacts)?;
290+
291+
let artifact = artifacts
292+
.get(SCRIPT_LIB_ARTIFACT_NAME)
293+
.ok_or(anyhow!("Failed to find script artifact"))?;
294+
295+
let sierra_program = serde_json::from_str::<VersionedProgram>(&artifact.sierra)
296+
.with_context(|| "Failed to deserialize Sierra program")?
297+
.into_v1()
298+
.with_context(|| "Failed to load Sierra program")?
299+
.program;
300+
301+
let runner = SierraCasmRunner::new(
302+
sierra_program.clone(),
303+
None,
304+
OrderedHashMap::default(),
305+
None,
306+
)
307+
.with_context(|| "Failed to set up runner")?;
308+
309+
// `builder` field in `SierraCasmRunner` is private, hence the need to create a new `RunnableBuilder`
310+
// https://github.com/starkware-libs/cairo/blob/66f5c7223f7a6c27c5f800816dba05df9b60674e/crates/cairo-lang-runner/src/lib.rs#L184
311+
let builder =
312+
RunnableBuilder::new(sierra_program, None).with_context(|| "Failed to create builder")?;
313+
314+
let name_suffix = module_name.to_string() + "::main";
315+
let func = runner.find_function(name_suffix.as_str())
316+
.context("Failed to find main function in script - please make sure `sierra-replace-ids` is not set to `false` for `dev` profile in script's Scarb.toml")?;
317+
318+
let entry_code_config = EntryCodeConfig::testing();
319+
let casm_program_wrapper_info = builder.create_wrapper_info(func, entry_code_config)?;
320+
let entry_code = casm_program_wrapper_info.header;
321+
let builtins = casm_program_wrapper_info.builtins;
322+
let footer = create_code_footer();
323+
324+
// import from cairo-lang-runner
325+
let assembled_program = builder
326+
.casm_program()
327+
.clone()
328+
.assemble_ex(&entry_code, &footer);
329+
let (hints_dict, string_to_hint) = hints_to_params(assembled_program.hints);
330+
331+
// hint processor
332+
let mut context = build_context(
333+
&SerializableBlockInfo::default().into(),
334+
None,
335+
&TrackedResource::CairoSteps,
336+
);
337+
338+
let mut blockifier_state = CachedState::new(DictStateReader::default());
339+
340+
// TODO(#2954)
341+
let param_types = builder.generic_id_and_size_from_concrete(&func.signature.param_types);
342+
343+
let segment_index = syscall_handler_offset(builtins.len(), has_segment_arena(&param_types));
344+
let syscall_handler = SyscallHintProcessor::new(
345+
&mut blockifier_state,
346+
&mut context,
347+
// This segment is created by SierraCasmRunner
348+
Relocatable {
349+
segment_index: segment_index
350+
.try_into()
351+
.expect("Failed to convert index to isize"),
352+
offset: 0,
353+
},
354+
ExecutableCallEntryPoint::default(),
355+
&string_to_hint,
356+
ReadOnlySegments::default(),
357+
);
358+
359+
let account = if config.account.is_empty() {
360+
None
361+
} else {
362+
Some(tokio_runtime.block_on(get_account(
363+
&config.account,
364+
&config.accounts_file,
365+
provider,
366+
config.keystore.clone(),
367+
))?)
368+
};
369+
let state = StateManager::from(state_file_path)?;
370+
371+
let cast_extension = CastScriptExtension {
372+
provider,
373+
tokio_runtime,
374+
config,
375+
artifacts: &artifacts,
376+
account: account.as_ref(),
377+
state,
378+
};
379+
380+
let mut cast_runtime = ExtendedRuntime {
381+
extension: cast_extension,
382+
extended_runtime: CastScriptRuntime {
383+
starknet_runtime: StarknetRuntime {
384+
hint_handler: syscall_handler,
385+
panic_traceback: None,
386+
},
387+
user_args: vec![vec![Arg::Value(Felt::from(i64::MAX))]],
388+
},
389+
};
390+
391+
match runner.run_function(
392+
func,
393+
&mut cast_runtime,
394+
hints_dict,
395+
assembled_program.bytecode.iter(),
396+
builtins,
397+
) {
398+
Ok(result) => match result.value {
399+
RunResultValue::Success(data) => Ok(ScriptRunResponse {
400+
status: "success".to_string(),
401+
message: build_readable_text(&data),
402+
}),
403+
RunResultValue::Panic(panic_data) => Ok(ScriptRunResponse {
404+
status: "script panicked".to_string(),
405+
message: build_readable_text(&panic_data),
406+
}),
407+
},
408+
Err(err) => Err(err.into()),
409+
}
405410
}
406411

407412
fn sncast_std_version_requirement() -> VersionReq {

0 commit comments

Comments
 (0)