Skip to content

Commit ea8f43a

Browse files
committed
Make self-review
1 parent 33f7243 commit ea8f43a

6 files changed

Lines changed: 78 additions & 33 deletions

File tree

crates/cheatnet/src/predeployment/contracts_data.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
predeployment::erc20::strk::{STRK_CONTRACT_CLASS_HASH, STRK_CONTRACT_NAME},
55
runtime_extensions::forge_runtime_extension::contracts_data::ContractsData,
66
};
7-
use anyhow::Result;
7+
use anyhow::{Result, anyhow};
88
use conversions::string::TryFromHexStr;
99

1010
pub fn load_predeployed_contracts() -> Result<ContractsData> {
@@ -27,12 +27,11 @@ pub fn load_predeployed_contracts() -> Result<ContractsData> {
2727
.class_hashes
2828
.insert(contract_name.clone(), class_hash);
2929

30-
// update contract data class hash
31-
contracts_data
30+
let contract_data = contracts_data
3231
.contracts
3332
.get_mut(&contract_name)
34-
.expect("contract data for {contract_name} should exist")
35-
.class_hash = class_hash;
33+
.ok_or_else(|| anyhow!("contract data for {contract_name} should exist"))?;
34+
contract_data.class_hash = class_hash;
3635
}
3736

3837
Ok(contracts_data)
Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,73 @@
11
use std::{collections::HashMap, fs};
22

33
use crate::predeployment::erc20::{eth::ETH_CONTRACT_NAME, strk::STRK_CONTRACT_NAME};
4-
use anyhow::Result;
4+
use anyhow::{Context, Result, anyhow};
55
use camino::Utf8PathBuf;
66
use scarb_api::StarknetContractArtifacts;
77

88
pub fn load_erc20_predeployed_contracts()
99
-> Result<HashMap<String, (StarknetContractArtifacts, Utf8PathBuf)>> {
10+
let strk_sierra = include_str!("../../data/predeployed_contracts/STRK/sierra.json");
11+
let eth_sierra = include_str!("../../data/predeployed_contracts/ETH/sierra.json");
12+
1013
Ok(HashMap::from([
1114
(
1215
STRK_CONTRACT_NAME.to_string(),
1316
(
1417
StarknetContractArtifacts {
15-
sierra: fs::read_to_string(concat!(
16-
env!("CARGO_MANIFEST_DIR"),
17-
"/src/data/predeployed_contracts/strk/sierra.json"
18+
sierra: strk_sierra.to_string(),
19+
casm: serde_json::from_str(include_str!(
20+
"../../data/predeployed_contracts/STRK/casm.json"
1821
))?,
19-
casm: serde_json::from_str(&fs::read_to_string(concat!(
20-
env!("CARGO_MANIFEST_DIR"),
21-
"/src/data/predeployed_contracts/strk/casm.json"
22-
))?)?,
2322
#[cfg(feature = "cairo-native")]
2423
executor: None,
2524
},
26-
Utf8PathBuf::from(concat!(
27-
env!("CARGO_MANIFEST_DIR"),
28-
"/src/data/predeployed_contracts/strk/sierra.json"
29-
)),
25+
persist_embedded_sierra("STRK", strk_sierra)?,
3026
),
3127
),
3228
(
3329
ETH_CONTRACT_NAME.to_string(),
3430
(
3531
StarknetContractArtifacts {
36-
sierra: fs::read_to_string(concat!(
37-
env!("CARGO_MANIFEST_DIR"),
38-
"/src/data/predeployed_contracts/eth/sierra.json"
32+
sierra: eth_sierra.to_string(),
33+
casm: serde_json::from_str(include_str!(
34+
"../../data/predeployed_contracts/ETH/casm.json"
3935
))?,
40-
casm: serde_json::from_str(&fs::read_to_string(concat!(
41-
env!("CARGO_MANIFEST_DIR"),
42-
"/src/data/predeployed_contracts/eth/casm.json"
43-
))?)?,
4436
#[cfg(feature = "cairo-native")]
4537
executor: None,
4638
},
47-
Utf8PathBuf::from(concat!(
48-
env!("CARGO_MANIFEST_DIR"),
49-
"/src/data/predeployed_contracts/eth/sierra.json"
50-
)),
39+
persist_embedded_sierra("ETH", eth_sierra)?,
5140
),
5241
),
5342
]))
5443
}
44+
45+
fn persist_embedded_sierra(contract_name: &str, sierra: &str) -> Result<Utf8PathBuf> {
46+
let path = std::env::temp_dir()
47+
.join("snfoundry-predeployed-contracts")
48+
.join(env!("CARGO_PKG_VERSION"))
49+
.join(format!("{contract_name}.sierra.json"));
50+
51+
let parent = path
52+
.parent()
53+
.with_context(|| format!("Failed to get parent directory for {}", path.display()))?;
54+
fs::create_dir_all(parent).with_context(|| {
55+
format!(
56+
"Failed to create directory for predeployed Sierra at {}",
57+
parent.display()
58+
)
59+
})?;
60+
fs::write(&path, sierra).with_context(|| {
61+
format!(
62+
"Failed to materialize embedded Sierra for {contract_name} at {}",
63+
path.display()
64+
)
65+
})?;
66+
67+
Utf8PathBuf::from_path_buf(path).map_err(|path| {
68+
anyhow!(
69+
"Materialized Sierra path is not valid UTF-8: {}",
70+
path.display()
71+
)
72+
})
73+
}

crates/cheatnet/src/runtime_extensions/forge_runtime_extension/contracts_data.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,22 @@ impl ContractsData {
7575
}
7676

7777
pub fn extend(&mut self, other: Self) -> Result<()> {
78-
for (name, contract_data) in other.contracts {
79-
if self.contracts.contains_key(&name) {
78+
for (name, contract_data) in &other.contracts {
79+
if self.contracts.contains_key(name) {
8080
bail!("duplicate contract name: {name}");
8181
}
82+
83+
if let Some(existing_name) = self.class_hashes.get_by_right(&contract_data.class_hash) {
84+
bail!(
85+
"duplicate contract class hash for `{}` and `{}`: {:?}",
86+
existing_name,
87+
name,
88+
contract_data.class_hash
89+
);
90+
}
91+
}
92+
93+
for (name, contract_data) in other.contracts {
8294
self.class_hashes
8395
.insert(name.clone(), contract_data.class_hash);
8496
self.contracts.insert(name, contract_data);

crates/forge-runner/src/debugging/args.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ impl TraceArgs {
2828
}
2929
}
3030
}
31+
32+
/// Returns `true` if no trace-related arguments were provided.
33+
pub fn is_empty(&self) -> bool {
34+
self.trace_verbosity.is_none() && self.trace_components.is_none()
35+
}
3136
}
3237

3338
fn build_components<'a>(iter: impl IntoIterator<Item = &'a Component>) -> Components {

crates/forge/src/run_tests/package.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl RunForPackageArgs {
8383
cache_dir: &Utf8PathBuf,
8484
artifacts_dir: &Utf8Path,
8585
partitioning_config: PartitionConfig,
86-
predeployed_contracts: ContractsData,
86+
predeployed_contracts: Option<ContractsData>,
8787
ui: &UI,
8888
) -> Result<RunForPackageArgs> {
8989
let raw_test_targets = load_test_artifacts(artifacts_dir, &package)?;
@@ -99,7 +99,9 @@ impl RunForPackageArgs {
9999
},
100100
)?;
101101
let mut contracts_data = ContractsData::try_from(contracts)?;
102-
contracts_data.extend(predeployed_contracts)?;
102+
if let Some(predeployed_contracts) = predeployed_contracts {
103+
contracts_data.extend(predeployed_contracts)?;
104+
}
103105

104106
let forge_config_from_scarb =
105107
load_package_config::<ForgeConfigFromScarb>(scarb_metadata, &package.id)?;

crates/forge/src/run_tests/workspace.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::{
1414
};
1515
use anyhow::Result;
1616
use cheatnet::predeployment::contracts_data::load_predeployed_contracts;
17+
use forge_runner::backtrace::is_backtrace_enabled;
18+
use forge_runner::debugging::TraceArgs;
1719
use forge_runner::partition::PartitionConfig;
1820
use forge_runner::test_case_summary::AnyTestCaseSummary;
1921
use forge_runner::{CACHE_DIR, test_target_summary::TestTargetSummary};
@@ -108,7 +110,9 @@ pub async fn execute_workspace(
108110

109111
let partitioning_config = get_partitioning_config(args, &ui, &packages, &artifacts_dir_path)?;
110112

111-
let predeployed_contracts = load_predeployed_contracts()?;
113+
let predeployed_contracts = should_load_predeployed_contracts_sierra(&args.trace_args)
114+
.then(|| load_predeployed_contracts())
115+
.transpose()?;
112116

113117
for package in packages {
114118
let cwd = env::current_dir()?;
@@ -237,3 +241,7 @@ fn unset_forge_test_filter() {
237241
env::remove_var(SNFORGE_TEST_FILTER);
238242
};
239243
}
244+
245+
fn should_load_predeployed_contracts_sierra(trace_args: &TraceArgs) -> bool {
246+
is_backtrace_enabled() || !trace_args.is_empty()
247+
}

0 commit comments

Comments
 (0)