Skip to content

Commit 8439227

Browse files
authored
feat(anvil): use signatures identifier for --print-traces (#11070)
1 parent 56b806a commit 8439227

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

crates/anvil/src/config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use tokio::sync::RwLock as TokioRwLock;
6565
use yansi::Paint;
6666

6767
pub use foundry_common::version::SHORT_VERSION as VERSION_MESSAGE;
68+
use foundry_evm::traces::{CallTraceDecoderBuilder, identifier::SignaturesIdentifier};
6869

6970
/// Default port the rpc will open
7071
pub const NODE_PORT: u16 = 8545;
@@ -1102,6 +1103,15 @@ impl NodeConfig {
11021103
genesis_init: self.genesis.clone(),
11031104
};
11041105

1106+
let mut decoder_builder = CallTraceDecoderBuilder::new();
1107+
if self.print_traces {
1108+
// if traces should get printed we configure the decoder with the signatures cache
1109+
if let Ok(identifier) = SignaturesIdentifier::new(false) {
1110+
debug!(target: "node", "using signature identifier");
1111+
decoder_builder = decoder_builder.with_signature_identifier(identifier);
1112+
}
1113+
}
1114+
11051115
// only memory based backend for now
11061116
let backend = mem::Backend::with_genesis(
11071117
db,
@@ -1112,6 +1122,7 @@ impl NodeConfig {
11121122
self.enable_steps_tracing,
11131123
self.print_logs,
11141124
self.print_traces,
1125+
Arc::new(decoder_builder.build()),
11151126
self.odyssey,
11161127
self.prune_history,
11171128
self.max_persisted_states,

crates/anvil/src/eth/backend/executor.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ use anvil_core::eth::{
2424
DepositReceipt, PendingTransaction, TransactionInfo, TypedReceipt, TypedTransaction,
2525
},
2626
};
27-
use foundry_evm::{backend::DatabaseError, traces::CallTraceNode};
27+
use foundry_evm::{
28+
backend::DatabaseError,
29+
traces::{CallTraceDecoder, CallTraceNode},
30+
};
2831
use foundry_evm_core::either_evm::EitherEvm;
2932
use op_revm::{L1BlockInfo, OpContext, precompiles::OpPrecompiles};
3033
use revm::{
@@ -119,6 +122,8 @@ pub struct TransactionExecutor<'a, Db: ?Sized, V: TransactionValidator> {
119122
pub optimism: bool,
120123
pub print_logs: bool,
121124
pub print_traces: bool,
125+
/// Recorder used for decoding traces, used together with print_traces
126+
pub call_trace_decoder: Arc<CallTraceDecoder>,
122127
/// Precompiles to inject to the EVM.
123128
pub precompile_factory: Option<Arc<dyn PrecompileFactory>>,
124129
pub blob_params: BlobParams,
@@ -367,7 +372,7 @@ impl<DB: Db + ?Sized, V: TransactionValidator> Iterator for &mut TransactionExec
367372
};
368373

369374
if self.print_traces {
370-
inspector.print_traces();
375+
inspector.print_traces(self.call_trace_decoder.clone());
371376
}
372377
inspector.print_logs();
373378

crates/anvil/src/eth/backend/mem/inspector.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use revm::{
1919
interpreter::EthInterpreter,
2020
},
2121
};
22+
use std::sync::Arc;
2223

2324
/// The [`revm::Inspector`] used when transacting in the evm
2425
#[derive(Clone, Debug, Default)]
@@ -40,17 +41,17 @@ impl AnvilInspector {
4041
}
4142

4243
/// Consumes the type and prints the traces.
43-
pub fn into_print_traces(mut self) {
44+
pub fn into_print_traces(mut self, decoder: Arc<CallTraceDecoder>) {
4445
if let Some(a) = self.tracer.take() {
45-
print_traces(a)
46+
print_traces(a, decoder);
4647
}
4748
}
4849

4950
/// Called after the inspecting the evm
5051
/// This will log all traces
51-
pub fn print_traces(&self) {
52+
pub fn print_traces(&self, decoder: Arc<CallTraceDecoder>) {
5253
if let Some(a) = self.tracer.clone() {
53-
print_traces(a)
54+
print_traces(a, decoder);
5455
}
5556
}
5657

@@ -60,6 +61,7 @@ impl AnvilInspector {
6061
self
6162
}
6263

64+
/// Configures the `TracingInspector` [`revm::Inspector`]
6365
pub fn with_tracing_config(mut self, config: TracingInspectorConfig) -> Self {
6466
self.tracer = Some(TracingInspector::new(config));
6567
self
@@ -91,11 +93,10 @@ impl AnvilInspector {
9193
/// # Panics
9294
///
9395
/// If called outside tokio runtime
94-
fn print_traces(tracer: TracingInspector) {
96+
fn print_traces(tracer: TracingInspector, decoder: Arc<CallTraceDecoder>) {
9597
let arena = tokio::task::block_in_place(move || {
9698
tokio::runtime::Handle::current().block_on(async move {
9799
let mut arena = tracer.into_traces();
98-
let decoder = CallTraceDecoder::new();
99100
decoder.populate_traces(arena.nodes_mut()).await;
100101
arena
101102
})

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! In-memory blockchain backend.
22
33
use self::state::trie_storage;
4+
use super::executor::new_evm_with_inspector_ref;
45
use crate::{
56
ForkChoice, NodeConfig, PrecompileFactory,
67
config::PruneStateHistoryConfig,
@@ -89,7 +90,7 @@ use foundry_evm::{
8990
constants::DEFAULT_CREATE2_DEPLOYER_RUNTIME_CODE,
9091
decode::RevertDecoder,
9192
inspectors::AccessListInspector,
92-
traces::TracingInspectorConfig,
93+
traces::{CallTraceDecoder, TracingInspectorConfig},
9394
utils::{get_blob_base_fee_update_fraction, get_blob_base_fee_update_fraction_by_spec_id},
9495
};
9596
use foundry_evm_core::either_evm::EitherEvm;
@@ -125,8 +126,6 @@ use std::{
125126
use storage::{Blockchain, DEFAULT_HISTORY_LIMIT, MinedTransaction};
126127
use tokio::sync::RwLock as AsyncRwLock;
127128

128-
use super::executor::new_evm_with_inspector_ref;
129-
130129
pub mod cache;
131130
pub mod fork_db;
132131
pub mod in_memory_db;
@@ -226,6 +225,8 @@ pub struct Backend {
226225
enable_steps_tracing: bool,
227226
print_logs: bool,
228227
print_traces: bool,
228+
/// Recorder used for decoding traces, used together with print_traces
229+
call_trace_decoder: Arc<CallTraceDecoder>,
229230
odyssey: bool,
230231
/// How to keep history state
231232
prune_state_history_config: PruneStateHistoryConfig,
@@ -255,6 +256,7 @@ impl Backend {
255256
enable_steps_tracing: bool,
256257
print_logs: bool,
257258
print_traces: bool,
259+
call_trace_decoder: Arc<CallTraceDecoder>,
258260
odyssey: bool,
259261
prune_state_history_config: PruneStateHistoryConfig,
260262
max_persisted_states: Option<usize>,
@@ -360,6 +362,7 @@ impl Backend {
360362
enable_steps_tracing,
361363
print_logs,
362364
print_traces,
365+
call_trace_decoder,
363366
odyssey,
364367
prune_state_history_config,
365368
transaction_block_keeper,
@@ -1211,7 +1214,7 @@ impl Backend {
12111214
inspector.print_logs();
12121215

12131216
if self.print_traces {
1214-
inspector.print_traces();
1217+
inspector.print_traces(self.call_trace_decoder.clone());
12151218
}
12161219

12171220
Ok((exit_reason, out, gas_used, state, logs.unwrap_or_default()))
@@ -1254,6 +1257,7 @@ impl Backend {
12541257
enable_steps_tracing: self.enable_steps_tracing,
12551258
print_logs: self.print_logs,
12561259
print_traces: self.print_traces,
1260+
call_trace_decoder: self.call_trace_decoder.clone(),
12571261
precompile_factory: self.precompile_factory.clone(),
12581262
odyssey: self.odyssey,
12591263
optimism: self.is_optimism(),
@@ -1340,6 +1344,7 @@ impl Backend {
13401344
enable_steps_tracing: self.enable_steps_tracing,
13411345
print_logs: self.print_logs,
13421346
print_traces: self.print_traces,
1347+
call_trace_decoder: self.call_trace_decoder.clone(),
13431348
odyssey: self.odyssey,
13441349
precompile_factory: self.precompile_factory.clone(),
13451350
optimism: self.is_optimism(),
@@ -1862,7 +1867,7 @@ impl Backend {
18621867
inspector.print_logs();
18631868

18641869
if self.print_traces {
1865-
inspector.into_print_traces();
1870+
inspector.into_print_traces(self.call_trace_decoder.clone());
18661871
}
18671872

18681873
Ok((exit_reason, out, gas_used as u128, state))

crates/evm/traces/src/decoder/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,18 @@ impl CallTraceDecoder {
410410
None
411411
};
412412

413-
if let Some(func) = functions.first() {
414-
return DecodedCallTrace {
413+
return if let Some(func) = functions.first() {
414+
DecodedCallTrace {
415415
label,
416416
call_data: Some(self.decode_function_input(trace, func)),
417417
return_data,
418-
};
418+
}
419419
} else {
420-
return DecodedCallTrace {
420+
DecodedCallTrace {
421421
label,
422422
call_data: self.fallback_call_data(trace),
423423
return_data,
424-
};
424+
}
425425
};
426426
}
427427

0 commit comments

Comments
 (0)