Skip to content

Commit 5d7b6e1

Browse files
authored
refactor: move handle_traces into cast (#11775)
1 parent c19a479 commit 5d7b6e1

File tree

8 files changed

+101
-94
lines changed

8 files changed

+101
-94
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.

crates/cast/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ foundry-block-explorers.workspace = true
2626
foundry-common.workspace = true
2727
foundry-compilers.workspace = true
2828
foundry-config.workspace = true
29+
foundry-debugger.workspace = true
2930
foundry-evm-core.workspace = true
3031
foundry-evm.workspace = true
3132
foundry-wallets.workspace = true

crates/cast/src/cmd/call.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::run::fetch_contracts_bytecode_from_trace;
22
use crate::{
33
Cast,
4+
debug::handle_traces,
45
traces::TraceKind,
56
tx::{CastTxBuilder, SenderKind},
67
};
@@ -15,7 +16,7 @@ use clap::Parser;
1516
use eyre::Result;
1617
use foundry_cli::{
1718
opts::{EthereumOpts, TransactionOpts},
18-
utils::{self, TraceResult, handle_traces, parse_ether_value},
19+
utils::{self, TraceResult, parse_ether_value},
1920
};
2021
use foundry_common::shell;
2122
use foundry_compilers::artifacts::EvmVersion;

crates/cast/src/cmd/run.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::debug::handle_traces;
12
use alloy_consensus::Transaction;
23
use alloy_network::{AnyNetwork, TransactionResponse};
34
use alloy_primitives::{
@@ -10,7 +11,7 @@ use clap::Parser;
1011
use eyre::{Result, WrapErr};
1112
use foundry_cli::{
1213
opts::{EtherscanOpts, RpcOpts},
13-
utils::{TraceResult, handle_traces, init_progress},
14+
utils::{TraceResult, init_progress},
1415
};
1516
use foundry_common::{SYSTEM_TRANSACTION_TYPE, is_impersonated_tx, is_known_system_sender, shell};
1617
use foundry_compilers::artifacts::EvmVersion;

crates/cast/src/debug.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use std::str::FromStr;
2+
3+
use alloy_chains::Chain;
4+
use alloy_primitives::{Address, Bytes, map::HashMap};
5+
use foundry_cli::utils::{TraceResult, print_traces};
6+
use foundry_common::{ContractsByArtifact, compile::ProjectCompiler, shell};
7+
use foundry_config::Config;
8+
use foundry_debugger::Debugger;
9+
use foundry_evm::traces::{
10+
CallTraceDecoderBuilder, DebugTraceIdentifier,
11+
debug::ContractSources,
12+
identifier::{SignaturesIdentifier, TraceIdentifiers},
13+
};
14+
15+
/// labels the traces, conditionally prints them or opens the debugger
16+
#[expect(clippy::too_many_arguments)]
17+
pub(crate) async fn handle_traces(
18+
mut result: TraceResult,
19+
config: &Config,
20+
chain: Option<Chain>,
21+
contracts_bytecode: &HashMap<Address, Bytes>,
22+
labels: Vec<String>,
23+
with_local_artifacts: bool,
24+
debug: bool,
25+
decode_internal: bool,
26+
disable_label: bool,
27+
) -> eyre::Result<()> {
28+
let (known_contracts, mut sources) = if with_local_artifacts {
29+
let _ = sh_println!("Compiling project to generate artifacts");
30+
let project = config.project()?;
31+
let compiler = ProjectCompiler::new();
32+
let output = compiler.compile(&project)?;
33+
(
34+
Some(ContractsByArtifact::new(
35+
output.artifact_ids().map(|(id, artifact)| (id, artifact.clone().into())),
36+
)),
37+
ContractSources::from_project_output(&output, project.root(), None)?,
38+
)
39+
} else {
40+
(None, ContractSources::default())
41+
};
42+
43+
let labels = labels.iter().filter_map(|label_str| {
44+
let mut iter = label_str.split(':');
45+
46+
if let Some(addr) = iter.next()
47+
&& let (Ok(address), Some(label)) = (Address::from_str(addr), iter.next())
48+
{
49+
return Some((address, label.to_string()));
50+
}
51+
None
52+
});
53+
let config_labels = config.labels.clone().into_iter();
54+
55+
let mut builder = CallTraceDecoderBuilder::new()
56+
.with_labels(labels.chain(config_labels))
57+
.with_signature_identifier(SignaturesIdentifier::from_config(config)?)
58+
.with_label_disabled(disable_label);
59+
let mut identifier = TraceIdentifiers::new().with_etherscan(config, chain)?;
60+
if let Some(contracts) = &known_contracts {
61+
builder = builder.with_known_contracts(contracts);
62+
identifier = identifier.with_local_and_bytecodes(contracts, contracts_bytecode);
63+
}
64+
65+
let mut decoder = builder.build();
66+
67+
for (_, trace) in result.traces.as_deref_mut().unwrap_or_default() {
68+
decoder.identify(trace, &mut identifier);
69+
}
70+
71+
if decode_internal || debug {
72+
if let Some(ref etherscan_identifier) = identifier.etherscan {
73+
sources.merge(etherscan_identifier.get_compiled_contracts().await?);
74+
}
75+
76+
if debug {
77+
let mut debugger = Debugger::builder()
78+
.traces(result.traces.expect("missing traces"))
79+
.decoder(&decoder)
80+
.sources(sources)
81+
.build();
82+
debugger.try_run_tui()?;
83+
return Ok(());
84+
}
85+
86+
decoder.debug_identifier = Some(DebugTraceIdentifier::new(sources));
87+
}
88+
89+
print_traces(&mut result, &decoder, shell::verbosity() > 0, shell::verbosity() > 4).await?;
90+
91+
Ok(())
92+
}

crates/cast/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub mod cmd;
5858
pub mod opts;
5959

6060
pub mod base;
61+
pub(crate) mod debug;
6162
pub mod errors;
6263
mod rlp_converter;
6364
pub mod tx;

crates/cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ forge-fmt.workspace = true
1717
foundry-block-explorers.workspace = true
1818
foundry-common.workspace = true
1919
foundry-config.workspace = true
20-
foundry-debugger.workspace = true
2120
foundry-evm.workspace = true
2221
foundry-wallets.workspace = true
2322

crates/cli/src/utils/cmd.rs

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
use alloy_json_abi::JsonAbi;
2-
use alloy_primitives::{Address, Bytes, map::HashMap};
32
use eyre::{Result, WrapErr};
4-
use foundry_common::{
5-
ContractsByArtifact, TestFunctionExt, compile::ProjectCompiler, fs, selectors::SelectorKind,
6-
shell,
7-
};
3+
use foundry_common::{TestFunctionExt, fs, selectors::SelectorKind, shell};
84
use foundry_compilers::{
95
Artifact, ArtifactId, ProjectCompileOutput,
106
artifacts::{CompactBytecode, Settings},
117
cache::{CacheEntry, CompilerCache},
128
utils::read_json_file,
139
};
1410
use foundry_config::{Chain, Config, NamedChain, error::ExtractConfigError, figment::Figment};
15-
use foundry_debugger::Debugger;
1611
use foundry_evm::{
1712
executors::{DeployResult, EvmError, RawCallResult},
1813
opts::EvmOpts,
1914
traces::{
20-
CallTraceDecoder, CallTraceDecoderBuilder, TraceKind, Traces,
21-
debug::{ContractSources, DebugTraceIdentifier},
22-
decode_trace_arena,
23-
identifier::{SignaturesCache, SignaturesIdentifier, TraceIdentifiers},
15+
CallTraceDecoder, TraceKind, Traces, decode_trace_arena, identifier::SignaturesCache,
2416
render_trace_arena_inner,
2517
},
2618
};
2719
use std::{
2820
fmt::Write,
2921
path::{Path, PathBuf},
30-
str::FromStr,
3122
};
3223
use yansi::Paint;
3324

@@ -331,85 +322,6 @@ impl TryFrom<Result<RawCallResult>> for TraceResult {
331322
}
332323
}
333324

334-
/// labels the traces, conditionally prints them or opens the debugger
335-
#[expect(clippy::too_many_arguments)]
336-
pub async fn handle_traces(
337-
mut result: TraceResult,
338-
config: &Config,
339-
chain: Option<Chain>,
340-
contracts_bytecode: &HashMap<Address, Bytes>,
341-
labels: Vec<String>,
342-
with_local_artifacts: bool,
343-
debug: bool,
344-
decode_internal: bool,
345-
disable_label: bool,
346-
) -> Result<()> {
347-
let (known_contracts, mut sources) = if with_local_artifacts {
348-
let _ = sh_println!("Compiling project to generate artifacts");
349-
let project = config.project()?;
350-
let compiler = ProjectCompiler::new();
351-
let output = compiler.compile(&project)?;
352-
(
353-
Some(ContractsByArtifact::new(
354-
output.artifact_ids().map(|(id, artifact)| (id, artifact.clone().into())),
355-
)),
356-
ContractSources::from_project_output(&output, project.root(), None)?,
357-
)
358-
} else {
359-
(None, ContractSources::default())
360-
};
361-
362-
let labels = labels.iter().filter_map(|label_str| {
363-
let mut iter = label_str.split(':');
364-
365-
if let Some(addr) = iter.next()
366-
&& let (Ok(address), Some(label)) = (Address::from_str(addr), iter.next())
367-
{
368-
return Some((address, label.to_string()));
369-
}
370-
None
371-
});
372-
let config_labels = config.labels.clone().into_iter();
373-
374-
let mut builder = CallTraceDecoderBuilder::new()
375-
.with_labels(labels.chain(config_labels))
376-
.with_signature_identifier(SignaturesIdentifier::from_config(config)?)
377-
.with_label_disabled(disable_label);
378-
let mut identifier = TraceIdentifiers::new().with_etherscan(config, chain)?;
379-
if let Some(contracts) = &known_contracts {
380-
builder = builder.with_known_contracts(contracts);
381-
identifier = identifier.with_local_and_bytecodes(contracts, contracts_bytecode);
382-
}
383-
384-
let mut decoder = builder.build();
385-
386-
for (_, trace) in result.traces.as_deref_mut().unwrap_or_default() {
387-
decoder.identify(trace, &mut identifier);
388-
}
389-
390-
if decode_internal || debug {
391-
if let Some(ref etherscan_identifier) = identifier.etherscan {
392-
sources.merge(etherscan_identifier.get_compiled_contracts().await?);
393-
}
394-
395-
if debug {
396-
let mut debugger = Debugger::builder()
397-
.traces(result.traces.expect("missing traces"))
398-
.decoder(&decoder)
399-
.sources(sources)
400-
.build();
401-
debugger.try_run_tui()?;
402-
return Ok(());
403-
}
404-
405-
decoder.debug_identifier = Some(DebugTraceIdentifier::new(sources));
406-
}
407-
408-
print_traces(&mut result, &decoder, shell::verbosity() > 0, shell::verbosity() > 4).await?;
409-
410-
Ok(())
411-
}
412-
413325
pub async fn print_traces(
414326
result: &mut TraceResult,
415327
decoder: &CallTraceDecoder,

0 commit comments

Comments
 (0)