cast/cmd/
run.rs

1use crate::{debug::handle_traces, utils::apply_chain_and_block_specific_env_changes};
2use alloy_consensus::Transaction;
3use alloy_network::{AnyNetwork, TransactionResponse};
4use alloy_primitives::{
5    Address, Bytes, U256,
6    map::{AddressSet, HashMap},
7};
8use alloy_provider::Provider;
9use alloy_rpc_types::BlockTransactions;
10use clap::Parser;
11use eyre::{Result, WrapErr};
12use foundry_cli::{
13    opts::{EtherscanOpts, RpcOpts},
14    utils::{TraceResult, init_progress},
15};
16use foundry_common::{SYSTEM_TRANSACTION_TYPE, is_impersonated_tx, is_known_system_sender, shell};
17use foundry_compilers::artifacts::EvmVersion;
18use foundry_config::{
19    Config,
20    figment::{
21        self, Metadata, Profile,
22        value::{Dict, Map},
23    },
24};
25use foundry_evm::{
26    Env,
27    core::env::AsEnvMut,
28    executors::{EvmError, Executor, TracingExecutor},
29    opts::EvmOpts,
30    traces::{InternalTraceMode, TraceMode, Traces},
31    utils::configure_tx_env,
32};
33use futures::TryFutureExt;
34use revm::DatabaseRef;
35
36/// CLI arguments for `cast run`.
37#[derive(Clone, Debug, Parser)]
38pub struct RunArgs {
39    /// The transaction hash.
40    tx_hash: String,
41
42    /// Opens the transaction in the debugger.
43    #[arg(long, short)]
44    debug: bool,
45
46    /// Whether to identify internal functions in traces.
47    #[arg(long)]
48    decode_internal: bool,
49
50    /// Defines the depth of a trace
51    #[arg(long)]
52    trace_depth: Option<usize>,
53
54    /// Print out opcode traces.
55    #[arg(long, short)]
56    trace_printer: bool,
57
58    /// Executes the transaction only with the state from the previous block.
59    ///
60    /// May result in different results than the live execution!
61    #[arg(long)]
62    quick: bool,
63
64    /// Whether to replay system transactions.
65    #[arg(long, alias = "sys")]
66    replay_system_txes: bool,
67
68    /// Disables the labels in the traces.
69    #[arg(long, default_value_t = false)]
70    disable_labels: bool,
71
72    /// Label addresses in the trace.
73    ///
74    /// Example: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045:vitalik.eth
75    #[arg(long, short)]
76    label: Vec<String>,
77
78    #[command(flatten)]
79    etherscan: EtherscanOpts,
80
81    #[command(flatten)]
82    rpc: RpcOpts,
83
84    /// The EVM version to use.
85    ///
86    /// Overrides the version specified in the config.
87    #[arg(long)]
88    evm_version: Option<EvmVersion>,
89
90    /// Sets the number of assumed available compute units per second for this provider
91    ///
92    /// default value: 330
93    ///
94    /// See also, <https://docs.alchemy.com/reference/compute-units#what-are-cups-compute-units-per-second>
95    #[arg(long, alias = "cups", value_name = "CUPS")]
96    pub compute_units_per_second: Option<u64>,
97
98    /// Disables rate limiting for this node's provider.
99    ///
100    /// default value: false
101    ///
102    /// See also, <https://docs.alchemy.com/reference/compute-units#what-are-cups-compute-units-per-second>
103    #[arg(long, value_name = "NO_RATE_LIMITS", visible_alias = "no-rpc-rate-limit")]
104    pub no_rate_limit: bool,
105
106    /// Use current project artifacts for trace decoding.
107    #[arg(long, visible_alias = "la")]
108    pub with_local_artifacts: bool,
109
110    /// Disable block gas limit check.
111    #[arg(long)]
112    pub disable_block_gas_limit: bool,
113
114    /// Enable the tx gas limit checks as imposed by Osaka (EIP-7825).
115    #[arg(long)]
116    pub enable_tx_gas_limit: bool,
117}
118
119impl RunArgs {
120    /// Executes the transaction by replaying it
121    ///
122    /// This replays the entire block the transaction was mined in unless `quick` is set to true
123    ///
124    /// Note: This executes the transaction(s) as is: Cheatcodes are disabled
125    pub async fn run(self) -> Result<()> {
126        let figment = self.rpc.clone().into_figment(self.with_local_artifacts).merge(&self);
127        let evm_opts = figment.extract::<EvmOpts>()?;
128        let mut config = Config::from_provider(figment)?.sanitized();
129
130        let label = self.label;
131        let with_local_artifacts = self.with_local_artifacts;
132        let debug = self.debug;
133        let decode_internal = self.decode_internal;
134        let disable_labels = self.disable_labels;
135        let compute_units_per_second =
136            if self.no_rate_limit { Some(u64::MAX) } else { self.compute_units_per_second };
137
138        let provider = foundry_cli::utils::get_provider_builder(&config)?
139            .compute_units_per_second_opt(compute_units_per_second)
140            .build()?;
141
142        let tx_hash = self.tx_hash.parse().wrap_err("invalid tx hash")?;
143        let tx = provider
144            .get_transaction_by_hash(tx_hash)
145            .await
146            .wrap_err_with(|| format!("tx not found: {tx_hash:?}"))?
147            .ok_or_else(|| eyre::eyre!("tx not found: {:?}", tx_hash))?;
148
149        // check if the tx is a system transaction
150        if !self.replay_system_txes
151            && (is_known_system_sender(tx.from())
152                || tx.transaction_type() == Some(SYSTEM_TRANSACTION_TYPE))
153        {
154            return Err(eyre::eyre!(
155                "{:?} is a system transaction.\nReplaying system transactions is currently not supported.",
156                tx.tx_hash()
157            ));
158        }
159
160        let tx_block_number =
161            tx.block_number.ok_or_else(|| eyre::eyre!("tx may still be pending: {:?}", tx_hash))?;
162
163        // we need to fork off the parent block
164        config.fork_block_number = Some(tx_block_number - 1);
165
166        let create2_deployer = evm_opts.create2_deployer;
167        let (block, (mut env, fork, chain, networks)) = tokio::try_join!(
168            // fetch the block the transaction was mined in
169            provider.get_block(tx_block_number.into()).full().into_future().map_err(Into::into),
170            TracingExecutor::get_fork_material(&mut config, evm_opts)
171        )?;
172
173        let mut evm_version = self.evm_version;
174
175        env.evm_env.cfg_env.disable_block_gas_limit = self.disable_block_gas_limit;
176
177        // By default do not enforce transaction gas limits imposed by Osaka (EIP-7825).
178        // Users can opt-in to enable these limits by setting `enable_tx_gas_limit` to true.
179        if !self.enable_tx_gas_limit {
180            env.evm_env.cfg_env.tx_gas_limit_cap = Some(u64::MAX);
181        }
182
183        env.evm_env.cfg_env.limit_contract_code_size = None;
184        env.evm_env.block_env.number = U256::from(tx_block_number);
185
186        if let Some(block) = &block {
187            env.evm_env.block_env.timestamp = U256::from(block.header.timestamp);
188            env.evm_env.block_env.beneficiary = block.header.beneficiary;
189            env.evm_env.block_env.difficulty = block.header.difficulty;
190            env.evm_env.block_env.prevrandao = Some(block.header.mix_hash.unwrap_or_default());
191            env.evm_env.block_env.basefee = block.header.base_fee_per_gas.unwrap_or_default();
192            env.evm_env.block_env.gas_limit = block.header.gas_limit;
193
194            // TODO: we need a smarter way to map the block to the corresponding evm_version for
195            // commonly used chains
196            if evm_version.is_none() {
197                // if the block has the excess_blob_gas field, we assume it's a Cancun block
198                if block.header.excess_blob_gas.is_some() {
199                    evm_version = Some(EvmVersion::Prague);
200                }
201            }
202            apply_chain_and_block_specific_env_changes::<AnyNetwork>(
203                env.as_env_mut(),
204                block,
205                config.networks,
206            );
207        }
208
209        let trace_mode = TraceMode::Call
210            .with_debug(self.debug)
211            .with_decode_internal(if self.decode_internal {
212                InternalTraceMode::Full
213            } else {
214                InternalTraceMode::None
215            })
216            .with_state_changes(shell::verbosity() > 4);
217        let mut executor = TracingExecutor::new(
218            env.clone(),
219            fork,
220            evm_version,
221            trace_mode,
222            networks,
223            create2_deployer,
224            None,
225        )?;
226        let mut env = Env::new_with_spec_id(
227            env.evm_env.cfg_env.clone(),
228            env.evm_env.block_env.clone(),
229            env.tx.clone(),
230            executor.spec_id(),
231        );
232
233        // Set the state to the moment right before the transaction
234        if !self.quick {
235            if !shell::is_json() {
236                sh_println!("Executing previous transactions from the block.")?;
237            }
238
239            if let Some(block) = block {
240                let pb = init_progress(block.transactions.len() as u64, "tx");
241                pb.set_position(0);
242
243                let BlockTransactions::Full(ref txs) = block.transactions else {
244                    return Err(eyre::eyre!("Could not get block txs"));
245                };
246
247                for (index, tx) in txs.iter().enumerate() {
248                    // Replay system transactions only if running with `sys` option.
249                    // System transactions such as on L2s don't contain any pricing info so it
250                    // could cause reverts.
251                    if !self.replay_system_txes
252                        && (is_known_system_sender(tx.from())
253                            || tx.transaction_type() == Some(SYSTEM_TRANSACTION_TYPE))
254                    {
255                        pb.set_position((index + 1) as u64);
256                        continue;
257                    }
258                    if tx.tx_hash() == tx_hash {
259                        break;
260                    }
261
262                    configure_tx_env(&mut env.as_env_mut(), &tx.inner);
263
264                    env.evm_env.cfg_env.disable_balance_check = true;
265
266                    if let Some(to) = Transaction::to(tx) {
267                        trace!(tx=?tx.tx_hash(),?to, "executing previous call transaction");
268                        executor.transact_with_env(env.clone()).wrap_err_with(|| {
269                            format!(
270                                "Failed to execute transaction: {:?} in block {}",
271                                tx.tx_hash(),
272                                env.evm_env.block_env.number
273                            )
274                        })?;
275                    } else {
276                        trace!(tx=?tx.tx_hash(), "executing previous create transaction");
277                        if let Err(error) = executor.deploy_with_env(env.clone(), None) {
278                            match error {
279                                // Reverted transactions should be skipped
280                                EvmError::Execution(_) => (),
281                                error => {
282                                    return Err(error).wrap_err_with(|| {
283                                        format!(
284                                            "Failed to deploy transaction: {:?} in block {}",
285                                            tx.tx_hash(),
286                                            env.evm_env.block_env.number
287                                        )
288                                    });
289                                }
290                            }
291                        }
292                    }
293
294                    pb.set_position((index + 1) as u64);
295                }
296            }
297        }
298
299        // Execute our transaction
300        let result = {
301            executor.set_trace_printer(self.trace_printer);
302
303            configure_tx_env(&mut env.as_env_mut(), &tx.inner);
304            if is_impersonated_tx(tx.inner.inner.inner()) {
305                env.evm_env.cfg_env.disable_balance_check = true;
306            }
307
308            if let Some(to) = Transaction::to(&tx) {
309                trace!(tx=?tx.tx_hash(), to=?to, "executing call transaction");
310                TraceResult::try_from(executor.transact_with_env(env))?
311            } else {
312                trace!(tx=?tx.tx_hash(), "executing create transaction");
313                TraceResult::try_from(executor.deploy_with_env(env, None))?
314            }
315        };
316
317        let contracts_bytecode = fetch_contracts_bytecode_from_trace(&executor, &result)?;
318        handle_traces(
319            result,
320            &config,
321            chain,
322            &contracts_bytecode,
323            label,
324            with_local_artifacts,
325            debug,
326            decode_internal,
327            disable_labels,
328            self.trace_depth,
329        )
330        .await?;
331
332        Ok(())
333    }
334}
335
336pub fn fetch_contracts_bytecode_from_trace(
337    executor: &Executor,
338    result: &TraceResult,
339) -> Result<HashMap<Address, Bytes>> {
340    let mut contracts_bytecode = HashMap::default();
341    if let Some(ref traces) = result.traces {
342        contracts_bytecode.extend(gather_trace_addresses(traces).filter_map(|addr| {
343            // All relevant bytecodes should already be cached in the executor.
344            let code = executor
345                .backend()
346                .basic_ref(addr)
347                .inspect_err(|e| _ = sh_warn!("Failed to fetch code for {addr}: {e}"))
348                .ok()??
349                .code?
350                .bytes();
351            if code.is_empty() {
352                return None;
353            }
354            Some((addr, code))
355        }));
356    }
357    Ok(contracts_bytecode)
358}
359
360fn gather_trace_addresses(traces: &Traces) -> impl Iterator<Item = Address> {
361    let mut addresses = AddressSet::default();
362    for (_, trace) in traces {
363        for node in trace.arena.nodes() {
364            if !node.trace.address.is_zero() {
365                addresses.insert(node.trace.address);
366            }
367            if !node.trace.caller.is_zero() {
368                addresses.insert(node.trace.caller);
369            }
370        }
371    }
372    addresses.into_iter()
373}
374
375impl figment::Provider for RunArgs {
376    fn metadata(&self) -> Metadata {
377        Metadata::named("RunArgs")
378    }
379
380    fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
381        let mut map = Map::new();
382
383        if let Some(api_key) = &self.etherscan.key {
384            map.insert("etherscan_api_key".into(), api_key.as_str().into());
385        }
386
387        if let Some(evm_version) = self.evm_version {
388            map.insert("evm_version".into(), figment::value::Value::serialize(evm_version)?);
389        }
390
391        Ok(Map::from([(Config::selected_profile(), map)]))
392    }
393}