Skip to content

Commit 87f53c0

Browse files
committed
Update sncast crate so it depends on cairo-lang-runner and cairo-lang-runnable-utils directly
commit-id:2b801af1
1 parent 5cdb51b commit 87f53c0

File tree

5 files changed

+266
-138
lines changed

5 files changed

+266
-138
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 135 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(unused_imports, dead_code)]
21
use crate::starknet_commands::declare::Declare;
32
use crate::starknet_commands::{call, declare, deploy, invoke, tx_status};
43
use crate::{WaitForTx, get_account};
@@ -15,7 +14,6 @@ use cairo_lang_runner::casm_run::hint_to_hint_params;
1514
use cairo_lang_runner::short_string::as_cairo_short_string;
1615
use cairo_lang_runner::{Arg, RunResultValue, SierraCasmRunner};
1716
use cairo_lang_sierra::program::VersionedProgram;
18-
use cairo_lang_sierra_to_casm::metadata::MetadataComputationConfig;
1917
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
2018
use cairo_vm::serde::deserialize_program::HintParams;
2119
use cairo_vm::types::relocatable::Relocatable;
@@ -34,6 +32,7 @@ use runtime::{
3432
};
3533
use scarb_api::{StarknetContractArtifacts, package_matches_version_requirement};
3634
use scarb_metadata::{Metadata, PackageMetadata};
35+
use script_runtime::CastScriptRuntime;
3736
use semver::{Comparator, Op, Version, VersionReq};
3837
use shared::print::print_as_warning;
3938
use shared::utils::build_readable_text;
@@ -57,6 +56,8 @@ use std::collections::HashMap;
5756
use std::fs;
5857
use tokio::runtime::Runtime;
5958

59+
mod script_runtime;
60+
6061
type ScriptStarknetContractArtifacts = StarknetContractArtifacts;
6162

6263
#[derive(Args, Debug)]
@@ -95,7 +96,7 @@ impl CastScriptExtension<'_> {
9596
}
9697

9798
impl<'a> ExtensionLogic for CastScriptExtension<'a> {
98-
type Runtime = StarknetRuntime<'a>;
99+
type Runtime = CastScriptRuntime<'a>;
99100

100101
#[expect(clippy::too_many_lines)]
101102
fn handle_cheatcode(
@@ -273,136 +274,139 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
273274
}
274275
}
275276

276-
#[expect(clippy::too_many_arguments)]
277+
#[expect(clippy::too_many_arguments, clippy::too_many_lines)]
277278
pub fn run(
278-
_module_name: &str,
279-
_metadata: &Metadata,
280-
_package_metadata: &PackageMetadata,
281-
_artifacts: &mut HashMap<String, StarknetContractArtifacts>,
282-
_provider: &JsonRpcClient<HttpTransport>,
283-
_tokio_runtime: Runtime,
284-
_config: &CastConfig,
285-
_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>,
286287
) -> Result<ScriptRunResponse> {
287-
todo!("Use blockifier to run");
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-
// Some(MetadataComputationConfig::default()),
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 = RunnableBuilder::new(sierra_program, Some(MetadataComputationConfig::default()))
312-
// .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: StarknetRuntime {
383-
// hint_handler: syscall_handler,
384-
// },
385-
// };
386-
//
387-
// match runner.run_function(
388-
// func,
389-
// &mut cast_runtime,
390-
// hints_dict,
391-
// assembled_program.bytecode.iter(),
392-
// builtins,
393-
// ) {
394-
// Ok(result) => match result.value {
395-
// RunResultValue::Success(data) => Ok(ScriptRunResponse {
396-
// status: "success".to_string(),
397-
// message: build_readable_text(&data),
398-
// }),
399-
// RunResultValue::Panic(panic_data) => Ok(ScriptRunResponse {
400-
// status: "script panicked".to_string(),
401-
// message: build_readable_text(&panic_data),
402-
// }),
403-
// },
404-
// Err(err) => Err(err.into()),
405-
// }
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+
}
406410
}
407411

408412
fn sncast_std_version_requirement() -> VersionReq {

0 commit comments

Comments
 (0)