Skip to content

feat(cast): Add --bypass-sender-recovery flag to cast run for compatibility with hardhat impersonated transactions #10795

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions crates/cast/src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use foundry_evm::{
executors::{EvmError, TracingExecutor},
opts::EvmOpts,
traces::{InternalTraceMode, TraceMode},
utils::configure_tx_env,
utils::configure_tx_env_assume_impersonation,
Env,
};
use foundry_evm_core::env::AsEnvMut;
Expand Down Expand Up @@ -98,6 +98,11 @@ pub struct RunArgs {
/// Disable block gas limit check.
#[arg(long)]
pub disable_block_gas_limit: bool,

/// Pass through 'from' address returned by rpc transaction response instead of recovering
/// address from signature.
#[arg(long)]
pub bypass_sender_recovery: bool,
}

impl RunArgs {
Expand Down Expand Up @@ -222,7 +227,11 @@ impl RunArgs {
break;
}

configure_tx_env(&mut env.as_env_mut(), &tx.inner);
configure_tx_env_assume_impersonation(
&mut env.as_env_mut(),
&tx.inner,
self.bypass_sender_recovery,
);

if let Some(to) = Transaction::to(tx) {
trace!(tx=?tx.tx_hash(),?to, "executing previous call transaction");
Expand Down Expand Up @@ -261,7 +270,11 @@ impl RunArgs {
let result = {
executor.set_trace_printer(self.trace_printer);

configure_tx_env(&mut env.as_env_mut(), &tx.inner);
configure_tx_env_assume_impersonation(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that naming is not entirely consistent here. Elsewhere i call it "bypass sender recovery" whereas here i call it "assume impersonation", happy to change it to either one, or any other naming that improves readability / clairity.

&mut env.as_env_mut(),
&tx.inner,
self.bypass_sender_recovery,
);

if let Some(to) = Transaction::to(&tx) {
trace!(tx=?tx.tx_hash(), to=?to, "executing call transaction");
Expand Down
20 changes: 20 additions & 0 deletions crates/evm/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ pub fn get_function<'a>(
/// Accounts for an impersonated transaction by resetting the `env.tx.caller` field to `tx.from`.
pub fn configure_tx_env(env: &mut EnvMut<'_>, tx: &Transaction<AnyTxEnvelope>) {
let impersonated_from = is_impersonated_tx(&tx.inner).then_some(tx.from());
configure_tx_env_inner(env, &tx.clone(), impersonated_from);
}

/// Configures the env for the given RPC transaction, using the from field of the transaction
/// directly if the assume_impersonation flag is set to true.
pub fn configure_tx_env_assume_impersonation(
env: &mut EnvMut<'_>,
tx: &Transaction<AnyTxEnvelope>,
assume_impersonation: bool,
) {
let impersonated_from =
(assume_impersonation || is_impersonated_tx(&tx.inner)).then_some(tx.from());
configure_tx_env_inner(env, &tx.clone(), impersonated_from);
}

fn configure_tx_env_inner(
env: &mut EnvMut<'_>,
tx: &Transaction<AnyTxEnvelope>,
impersonated_from: Option<Address>,
) {
if let AnyTxEnvelope::Ethereum(tx) = &tx.inner.inner() {
configure_tx_req_env(env, &tx.clone().into(), impersonated_from).expect("cannot fail");
}
Expand Down