Skip to content

Commit d88921d

Browse files
committed
Add casm debug info to ContractsDataStore
commit-id:6dc6b503
1 parent 1efb3ca commit d88921d

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
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: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::trace::types::{ContractName, Selector};
22
use cairo_lang_sierra::program::ProgramArtifact;
3+
use cairo_lang_sierra_to_casm::compiler::CairoProgramDebugInfo;
4+
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
35
use cairo_lang_starknet_classes::contract_class::ContractClass;
46
use cheatnet::forking::data::ForkData;
57
use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::ContractsData;
@@ -8,6 +10,7 @@ use rayon::iter::ParallelIterator;
810
use starknet::core::types::contract::{AbiEntry, SierraClass};
911
use starknet_api::core::{ClassHash, EntryPointSelector};
1012
use std::collections::HashMap;
13+
use std::hash::Hash;
1114

1215
/// Data structure containing information about contracts,
1316
/// including their ABI, names, selectors and programs that will be used to create a [`Trace`](crate::Trace).
@@ -16,6 +19,8 @@ pub struct ContractsDataStore {
1619
contract_names: HashMap<ClassHash, ContractName>,
1720
selectors: HashMap<EntryPointSelector, Selector>,
1821
programs: HashMap<ClassHash, ProgramArtifact>,
22+
// FIXME(https://github.com/software-mansion/universal-sierra-compiler/issues/98): Use CASM debug info from USC once it provides it.
23+
casm_debug_infos: HashMap<ClassHash, CairoProgramDebugInfo>,
1924
}
2025

2126
impl ContractsDataStore {
@@ -46,7 +51,7 @@ impl ContractsDataStore {
4651
.chain(fork_data.abi.clone())
4752
.collect();
4853

49-
let programs = contracts_data
54+
let program_data = contracts_data
5055
.contracts
5156
.par_iter()
5257
.map(|(_, contract_data)| {
@@ -65,15 +70,27 @@ impl ContractsDataStore {
6570
debug_info,
6671
};
6772

68-
(contract_data.class_hash, program_artifact)
73+
let casm_debug_info = compile(ContractClass {
74+
// Debug info is unused in the compilation. This saves us a costly clone.
75+
sierra_program_debug_info: None,
76+
..contract_class
77+
});
78+
79+
(
80+
contract_data.class_hash,
81+
(program_artifact, casm_debug_info),
82+
)
6983
})
70-
.collect();
84+
.collect::<Vec<_>>();
85+
86+
let (programs, casm_debug_infos) = split_maps(program_data);
7187

7288
Self {
7389
abi,
7490
contract_names,
7591
selectors,
7692
programs,
93+
casm_debug_infos,
7794
}
7895
}
7996

@@ -99,4 +116,36 @@ impl ContractsDataStore {
99116
pub fn get_program_artifact(&self, class_hash: &ClassHash) -> Option<&ProgramArtifact> {
100117
self.programs.get(class_hash)
101118
}
119+
120+
/// Gets the [`CairoProgramDebugInfo`] for a given contract [`ClassHash`].
121+
#[must_use]
122+
pub fn get_casm_debug_info(&self, class_hash: &ClassHash) -> Option<&CairoProgramDebugInfo> {
123+
self.casm_debug_infos.get(class_hash)
124+
}
125+
}
126+
127+
/// Compile the given [`ContractClass`] to `casm` and return [`CairoProgramDebugInfo`]
128+
fn compile(contract_class: ContractClass) -> CairoProgramDebugInfo {
129+
let (_, casm_debug_info) =
130+
CasmContractClass::from_contract_class_with_debug_info(contract_class, false, usize::MAX)
131+
.expect("compilation should succeed");
132+
casm_debug_info
133+
}
134+
135+
/// Splits an iterator of `(K, (V1, V2))` into two `HashMaps`:
136+
/// `HashMap<K, V1>` and `HashMap<K, V2>`
137+
fn split_maps<K, V1, V2, I>(iter: I) -> (HashMap<K, V1>, HashMap<K, V2>)
138+
where
139+
K: Copy + Eq + Hash,
140+
I: IntoIterator<Item = (K, (V1, V2))>,
141+
{
142+
let mut map1 = HashMap::new();
143+
let mut map2 = HashMap::new();
144+
145+
for (key, (v1, v2)) in iter {
146+
map1.insert(key, v1);
147+
map2.insert(key, v2);
148+
}
149+
150+
(map1, map2)
102151
}

0 commit comments

Comments
 (0)