From f43cf9c18a3cd4998731bb0085b337ed31a9ebe5 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 4 Feb 2025 18:40:42 +0100 Subject: [PATCH] fix: rm redundant revert in error message (#14215) --- crates/rpc/rpc-eth-types/src/error/mod.rs | 24 ++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/crates/rpc/rpc-eth-types/src/error/mod.rs b/crates/rpc/rpc-eth-types/src/error/mod.rs index d798002f3..7a00852dd 100644 --- a/crates/rpc/rpc-eth-types/src/error/mod.rs +++ b/crates/rpc/rpc-eth-types/src/error/mod.rs @@ -8,7 +8,7 @@ use core::time::Duration; use alloy_eips::BlockId; use alloy_primitives::{Address, Bytes, U256}; use alloy_rpc_types_eth::{error::EthRpcErrorCode, request::TransactionInputError, BlockError}; -use alloy_sol_types::decode_revert_reason; +use alloy_sol_types::{ContractError, RevertReason}; use reth_errors::RethError; use reth_primitives_traits::transaction::signed::RecoveryError; use reth_rpc_server_types::result::{ @@ -615,8 +615,14 @@ impl RevertError { impl std::fmt::Display for RevertError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("execution reverted")?; - if let Some(reason) = self.output.as_ref().and_then(|bytes| decode_revert_reason(bytes)) { - write!(f, ": {reason}")?; + if let Some(reason) = self.output.as_ref().and_then(|out| RevertReason::decode(out)) { + let error = reason.to_string(); + let mut error = error.as_str(); + if matches!(reason, RevertReason::ContractError(ContractError::Revert(_))) { + // we strip redundant `revert: ` prefix from the revert reason + error = error.trim_start_matches("revert: "); + } + write!(f, ": {error}")?; } Ok(()) } @@ -768,9 +774,9 @@ pub fn ensure_success(result: ExecutionResult) -> EthResult { #[cfg(test)] mod tests { - use revm_primitives::b256; - use super::*; + use alloy_sol_types::{Revert, SolError}; + use revm_primitives::b256; #[test] fn timed_out_error() { @@ -805,4 +811,12 @@ mod tests { EthApiError::HeaderNotFound(BlockId::finalized()).into(); assert_eq!(err.message(), "block not found: finalized"); } + + #[test] + fn revert_err_display() { + let revert = Revert::from("test_revert_reason"); + let err = RevertError::new(revert.abi_encode().into()); + let msg = err.to_string(); + assert_eq!(msg, "execution reverted: test_revert_reason"); + } }