Skip to content

Commit 4e5f1b4

Browse files
committed
Add casm debug info to ContractsDataStore
commit-id:6dc6b503
1 parent 384ba25 commit 4e5f1b4

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ cairo-lang-syntax = "2.12.0"
4343
cairo-lang-test-plugin = "2.12.0"
4444
cairo-lang-starknet-classes = "2.12.0"
4545
cairo-lang-parser = "2.12.0"
46+
cairo-lang-sierra-to-casm = "2.12.0"
4647
cairo-vm = "2.2.0"
4748
cairo-annotations = "0.5.0"
4849
dirs = "6.0.0"

crates/debugging/Cargo.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ version = "1.0.0"
44
edition.workspace = true
55

66
[dependencies]
7-
starknet_api.workspace = true
87
blockifier.workspace = true
9-
ptree.workspace = true
10-
paste.workspace = true
11-
console.workspace = true
12-
starknet-types-core.workspace = true
13-
starknet.workspace = true
148
cairo-lang-sierra.workspace = true
9+
cairo-lang-sierra-to-casm.workspace = true
1510
cairo-lang-starknet-classes.workspace = true
1611
cheatnet = { path = "../cheatnet" }
12+
console.workspace = true
1713
data-transformer = { path = "../data-transformer" }
18-
serde_json.workspace = true
14+
paste.workspace = true
15+
ptree.workspace = true
1916
rayon.workspace = true
17+
serde_json.workspace = true
18+
starknet-types-core.workspace = true
19+
starknet.workspace = true
20+
starknet_api.workspace = true
21+
thiserror.workspace = true

crates/debugging/src/contracts_data_store.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use crate::trace::types::{ContractName, Selector};
2-
use cairo_lang_sierra::program::ProgramArtifact;
2+
use cairo_lang_sierra::program::{Program, ProgramArtifact};
3+
use cairo_lang_sierra_to_casm::compiler::{
4+
CairoProgram, CairoProgramDebugInfo, SierraToCasmConfig,
5+
};
6+
use cairo_lang_sierra_to_casm::metadata::{MetadataComputationConfig, calc_metadata};
37
use cairo_lang_starknet_classes::contract_class::ContractClass;
48
use cheatnet::forking::data::ForkData;
59
use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::ContractsData;
@@ -16,6 +20,8 @@ pub struct ContractsDataStore {
1620
contract_names: HashMap<ClassHash, ContractName>,
1721
selectors: HashMap<EntryPointSelector, Selector>,
1822
programs: HashMap<ClassHash, ProgramArtifact>,
23+
// FIXME(https://github.com/software-mansion/universal-sierra-compiler/issues/98): Use CASM debug info from USC once it provides it.
24+
casm_debug_infos: HashMap<ClassHash, CairoProgramDebugInfo>,
1925
}
2026

2127
impl ContractsDataStore {
@@ -46,7 +52,7 @@ impl ContractsDataStore {
4652
.chain(fork_data.abi.clone())
4753
.collect();
4854

49-
let programs = contracts_data
55+
let programs: HashMap<_, _> = contracts_data
5056
.contracts
5157
.par_iter()
5258
.map(|(_, contract_data)| {
@@ -69,11 +75,20 @@ impl ContractsDataStore {
6975
})
7076
.collect();
7177

78+
let casm_debug_infos = programs
79+
.iter()
80+
.map(|(class_hash, program_artifact)| {
81+
let casm = compile(&program_artifact.program);
82+
(*class_hash, casm.debug_info)
83+
})
84+
.collect();
85+
7286
Self {
7387
abi,
7488
contract_names,
7589
selectors,
7690
programs,
91+
casm_debug_infos,
7792
}
7893
}
7994

@@ -99,4 +114,24 @@ impl ContractsDataStore {
99114
pub fn get_program_artifact(&self, class_hash: &ClassHash) -> Option<&ProgramArtifact> {
100115
self.programs.get(class_hash)
101116
}
117+
118+
/// Gets the [`CairoProgramDebugInfo`] for a given contract [`ClassHash`].
119+
#[must_use]
120+
pub fn get_casm_debug_info(&self, class_hash: &ClassHash) -> Option<&CairoProgramDebugInfo> {
121+
self.casm_debug_infos.get(class_hash)
122+
}
123+
}
124+
125+
/// Compile the given [`Program`] to `casm`.
126+
fn compile(program: &Program) -> CairoProgram {
127+
let metadata = calc_metadata(program, MetadataComputationConfig::default())
128+
.expect("metadata calculation should not fail");
129+
130+
let config = SierraToCasmConfig {
131+
gas_usage_check: false,
132+
max_bytecode_size: usize::MAX,
133+
};
134+
135+
cairo_lang_sierra_to_casm::compiler::compile(program, &metadata, config)
136+
.expect("compilation should not fail")
102137
}

0 commit comments

Comments
 (0)