Skip to content

Commit

Permalink
fix(forge): decode traces when an onchain simulation fails (foundry-r…
Browse files Browse the repository at this point in the history
…s#2875)

* decode err on onchain simulation failures

* change message

* add deployment fail reason
  • Loading branch information
joshieDo authored and iFrostizz committed Nov 9, 2022
1 parent 2fb5710 commit 881a3a2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
3 changes: 1 addition & 2 deletions cli/src/cmd/forge/script/broadcast.rs
Expand Up @@ -220,8 +220,7 @@ impl ScriptArgs {
.await
.map_err(|_| {
eyre::eyre!(
"One or more transactions failed when simulating the
on-chain version. Check the trace by re-running with `-vvv`"
"Transaction failed when running the on-chain simulation. Check the trace above for more information."
)
})?
};
Expand Down
29 changes: 12 additions & 17 deletions cli/src/cmd/forge/script/executor.rs
Expand Up @@ -88,7 +88,6 @@ impl ScriptArgs {
let mut runner = self
.prepare_runner(script_config, script_config.evm_opts.sender, SimulationStage::OnChain)
.await;
let mut failed = false;

if script_config.evm_opts.verbosity > 3 {
println!("==========================");
Expand Down Expand Up @@ -130,6 +129,17 @@ impl ScriptArgs {
)
}

if !result.success || script_config.evm_opts.verbosity > 3 {
for (_kind, trace) in &mut result.traces {
decoder.decode(trace).await;
println!("{}", trace);
}
}

if !result.success {
eyre::bail!("Simulated execution failed");
}

let created_contracts = result
.traces
.iter()
Expand All @@ -155,17 +165,6 @@ impl ScriptArgs {
// We inflate the gas used by the user specified percentage
tx.gas = Some(U256::from(result.gas * self.gas_estimate_multiplier / 100));

if !result.success {
failed = true;
}

if script_config.evm_opts.verbosity > 3 {
for (_kind, trace) in &mut result.traces {
decoder.decode(trace).await;
println!("{}", trace);
}
}

final_txs.push_back(TransactionWithMetadata::new(
tx.into(),
&result,
Expand All @@ -178,11 +177,7 @@ impl ScriptArgs {
}
}

if failed {
eyre::bail!("Simulated execution failed")
} else {
Ok(final_txs)
}
Ok(final_txs)
}

/// Creates the Runner that drives script execution
Expand Down
16 changes: 13 additions & 3 deletions cli/src/cmd/forge/script/runner.rs
Expand Up @@ -161,16 +161,26 @@ impl ScriptRunner {
if let Some(NameOrAddress::Address(to)) = to {
self.call(from, to, calldata.unwrap_or_default(), value.unwrap_or(U256::zero()), true)
} else if to.is_none() {
let DeployResult { address, gas, logs, traces, debug } = self.executor.deploy(
let (address, gas, logs, traces, debug) = match self.executor.deploy(
from,
calldata.expect("No data for create transaction").0,
value.unwrap_or(U256::zero()),
None,
)?;
) {
Ok(DeployResult { address, gas, logs, traces, debug }) => {
(address, gas, logs, traces, debug)
}
Err(EvmError::Execution { reason, traces, gas, logs, debug, .. }) => {
println!("{}", Paint::red(format!("\nFailed with `{reason}`:\n")));

(Address::zero(), gas, logs, traces, debug)
}
e => eyre::bail!("Unrecoverable error: {:?}", e),
};

Ok(ScriptResult {
returned: bytes::Bytes::new(),
success: true,
success: address != Address::zero(),
gas,
logs,
traces: traces
Expand Down

0 comments on commit 881a3a2

Please sign in to comment.