Skip to content

Commit 1efb3ca

Browse files
committed
Use context in debugging
commit-id:d194aae4
1 parent 28f57d1 commit 1efb3ca

File tree

6 files changed

+60
-36
lines changed

6 files changed

+60
-36
lines changed

crates/debugging/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ mod contracts_data_store;
77
mod trace;
88
mod tree;
99

10-
pub use contracts_data_store::ContractsDataStore;
1110
pub use trace::components::{Component, Components};
12-
pub use trace::types::Trace;
11+
pub use trace::{context::Context, types::Trace};

crates/debugging/src/trace/collect.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use crate::Trace;
21
use crate::contracts_data_store::ContractsDataStore;
3-
use crate::trace::components::Components;
42
use crate::trace::types::{
53
CallerAddress, ContractAddress, ContractName, ContractTrace, Selector, TestName, TraceInfo,
64
TransformedCallResult, TransformedCalldata,
75
};
6+
use crate::{Context, Trace};
87
use cheatnet::runtime_extensions::call_to_blockifier_runtime_extension::rpc::{
98
CallFailure, CallResult as CheatnetCallResult,
109
};
@@ -16,22 +15,16 @@ use starknet_api::execution_utils::format_panic_data;
1615

1716
pub struct Collector<'a> {
1817
call_trace: &'a CallTrace,
19-
contracts_data_store: &'a ContractsDataStore,
20-
components: &'a Components,
18+
context: &'a Context,
2119
}
2220

2321
impl<'a> Collector<'a> {
2422
/// Creates a new [`Collector`] from a given `cheatnet` [`CallTrace`], [`ContractsDataStore`] and [`Verbosity`].
2523
#[must_use]
26-
pub fn new(
27-
call_trace: &'a CallTrace,
28-
contracts_data_store: &'a ContractsDataStore,
29-
components: &'a Components,
30-
) -> Collector<'a> {
24+
pub fn new(call_trace: &'a CallTrace, context: &'a Context) -> Collector<'a> {
3125
Collector {
3226
call_trace,
33-
contracts_data_store,
34-
components,
27+
context,
3528
}
3629
}
3730

@@ -43,7 +36,7 @@ impl<'a> Collector<'a> {
4336
}
4437

4538
fn collect_contract_trace(&self) -> ContractTrace {
46-
let components = self.components;
39+
let components = self.context.components();
4740
let entry_point = &self.call_trace.entry_point;
4841
let nested_calls = self.collect_nested_calls();
4942
let contract_name = self.collect_contract_name();
@@ -75,29 +68,28 @@ impl<'a> Collector<'a> {
7568
.map(|call_trace| {
7669
Collector {
7770
call_trace: &call_trace.borrow(),
78-
contracts_data_store: self.contracts_data_store,
79-
components: self.components,
71+
context: self.context,
8072
}
8173
.collect_contract_trace()
8274
})
8375
.collect()
8476
}
8577

8678
fn collect_contract_name(&self) -> ContractName {
87-
self.contracts_data_store
79+
self.contracts_data_store()
8880
.get_contract_name(self.class_hash())
8981
.cloned()
9082
.unwrap_or_else(|| ContractName("forked contract".to_string()))
9183
}
9284

9385
fn collect_selector(&self) -> &Selector {
94-
self.contracts_data_store
86+
self.contracts_data_store()
9587
.get_selector(&self.call_trace.entry_point.entry_point_selector)
9688
.expect("`Selector` should be present")
9789
}
9890

9991
fn collect_abi(&self) -> &[AbiEntry] {
100-
self.contracts_data_store
92+
self.contracts_data_store()
10193
.get_abi(self.class_hash())
10294
.expect("`ABI` should be present")
10395
}
@@ -140,6 +132,10 @@ impl<'a> Collector<'a> {
140132
.as_ref()
141133
.expect("class_hash should be set in `fn execute_call_entry_point` in cheatnet")
142134
}
135+
136+
fn contracts_data_store(&self) -> &ContractsDataStore {
137+
self.context.contracts_data_store()
138+
}
143139
}
144140

145141
fn format_result_message(tag: &str, message: &str) -> String {

crates/debugging/src/trace/context.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::Components;
2+
use crate::contracts_data_store::ContractsDataStore;
3+
use cheatnet::forking::data::ForkData;
4+
use cheatnet::runtime_extensions::forge_runtime_extension::contracts_data::ContractsData;
5+
6+
/// Context is a structure that holds the necessary data for creating a [`Trace`](crate::Trace).
7+
pub struct Context {
8+
contracts_data_store: ContractsDataStore,
9+
components: Components,
10+
}
11+
12+
impl Context {
13+
/// Creates a new instance of [`Context`] from a given `cheatnet` [`ContractsData`], [`ForkData`] and [`Components`].
14+
#[must_use]
15+
pub fn new(
16+
contracts_data: &ContractsData,
17+
fork_data: &ForkData,
18+
components: Components,
19+
) -> Self {
20+
let contracts_data_store = ContractsDataStore::new(contracts_data, fork_data);
21+
Self {
22+
contracts_data_store,
23+
components,
24+
}
25+
}
26+
27+
/// Returns a reference to the [`ContractsDataStore`].
28+
#[must_use]
29+
pub fn contracts_data_store(&self) -> &ContractsDataStore {
30+
&self.contracts_data_store
31+
}
32+
33+
/// Returns a reference to the [`Components`].
34+
#[must_use]
35+
pub fn components(&self) -> &Components {
36+
&self.components
37+
}
38+
}

crates/debugging/src/trace/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod collect;
22
pub mod components;
3+
pub mod context;
34
pub mod types;

crates/debugging/src/trace/types.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::contracts_data_store::ContractsDataStore;
1+
use crate::Context;
22
use crate::trace::collect::Collector;
33
use crate::trace::components::{
4-
CallResultContainer, CallTypeContainer, CalldataContainer, CallerAddressContainer, Components,
4+
CallResultContainer, CallTypeContainer, CalldataContainer, CallerAddressContainer,
55
ContractAddressContainer, ContractNameContainer, EntryPointTypeContainer,
66
};
77
use crate::tree::TreeSerialize;
@@ -56,15 +56,10 @@ pub struct ContractAddress(pub ApiContractAddress);
5656
pub struct CallerAddress(pub ApiContractAddress);
5757

5858
impl Trace {
59-
/// Creates a new [`Trace`] from a given `cheatnet` [`CallTrace`], [`ContractsDataStore`], [`Verbosity`] and a test name.
59+
/// Creates a new [`Trace`] from a given [`Context`] and a test name.
6060
#[must_use]
61-
pub fn new(
62-
call_trace: &CallTrace,
63-
contracts_data_store: &ContractsDataStore,
64-
components: &Components,
65-
test_name: String,
66-
) -> Self {
67-
Collector::new(call_trace, contracts_data_store, components).collect_trace(test_name)
61+
pub fn new(call_trace: &CallTrace, context: &Context, test_name: String) -> Self {
62+
Collector::new(call_trace, context).collect_trace(test_name)
6863
}
6964
}
7065

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ pub fn build_debugging_trace(
1818
fork_data: &ForkData,
1919
) -> Option<debugging::Trace> {
2020
let components = trace_args.to_components()?;
21-
let contracts_data_store = debugging::ContractsDataStore::new(contracts_data, fork_data);
22-
Some(debugging::Trace::new(
23-
call_trace,
24-
&contracts_data_store,
25-
&components,
26-
test_name,
27-
))
21+
let context = debugging::Context::new(contracts_data, fork_data, components);
22+
Some(debugging::Trace::new(call_trace, &context, test_name))
2823
}

0 commit comments

Comments
 (0)