Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

use alloy GenericContractError in reth_primitives::abi::decode_revert_reason #4759

Merged
merged 4 commits into from
Sep 26, 2023
Merged
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
62 changes: 59 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ revm-primitives = { git = "https://github.com/Evalir/revm/", branch = "reintrodu
## eth
alloy-primitives = "0.3"
alloy-rlp = "0.3"
alloy-sol-types = "0.3"
ethers-core = { version = "2.0", default-features = false }
ethers-providers = { version = "2.0", default-features = false }
ethers-signers = { version = "2.0", default-features = false }
Expand Down Expand Up @@ -180,3 +181,4 @@ serial_test = "2"
# TODO
[patch.crates-io]
alloy-primitives = { git = "https://github.com/alloy-rs/core" }
alloy-sol-types = { git = "https://github.com/alloy-rs/core", rev = "5733dc1b3987b0d2f2677d055de85fd5f721b2fd" }
1 change: 1 addition & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ revm-primitives = { workspace = true, features = ["serde"] }
# ethereum
alloy-primitives = { workspace = true, features = ["rand", "rlp"] }
alloy-rlp = { workspace = true, features = ["arrayvec"] }
alloy-sol-types.workspace = true
ethers-core = { workspace = true, default-features = false }
ruint = { version = "1.9.0", features = ["primitive-types", "rlp"] }

Expand Down
22 changes: 16 additions & 6 deletions crates/primitives/src/abi.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
//! Eth ABI helpers.

// TODO: Use `alloy_sol_types::ContractError`

use crate::constants::SELECTOR_LEN;
use ethers_core::abi::AbiDecode;
use alloy_sol_types::{GenericContractError, SolInterface};

/// Returns the revert reason from the given output data, if it's an abi encoded String. Returns
/// `None` if the output is not long enough to contain a function selector or the content is not a
/// valid abi encoded String.
///
/// **Note:** it's assumed the `out` buffer starts with the call's signature
pub fn decode_revert_reason(out: impl AsRef<[u8]>) -> Option<String> {
let out = out.as_ref();
pub fn decode_revert_reason(out: &[u8]) -> Option<String> {
// Ensure the output data is long enough to contain a function selector.
if out.len() < SELECTOR_LEN {
return None
}
String::decode(&out[SELECTOR_LEN..]).ok()

// Try to decode as a generic contract error.
if let Ok(error) = GenericContractError::decode(out, true) {
return Some(error.to_string())
}

// If that fails, try to decode as a regular string.
if let Ok(decoded_string) = std::str::from_utf8(out) {
return Some(decoded_string.to_string())
}

// If both attempts fail, return None.
None
}
2 changes: 1 addition & 1 deletion crates/revm/revm-inspectors/src/tracing/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ impl CallTraceNode {

// we need to populate error and revert reason
if !self.trace.success {
call_frame.revert_reason = decode_revert_reason(self.trace.output.clone());
call_frame.revert_reason = decode_revert_reason(self.trace.output.as_ref());
// Note: the call tracer mimics parity's trace transaction and geth maps errors to parity style error messages, <https://github.com/ethereum/go-ethereum/blob/34d507215951fb3f4a5983b65e127577989a6db8/eth/tracers/native/call_flat.go#L39-L55>
call_frame.error = self.trace.as_error(TraceStyle::Parity);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc/src/eth/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ 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(decode_revert_reason) {
if let Some(reason) = self.output.as_ref().and_then(|bytes| decode_revert_reason(bytes)) {
write!(f, ": {reason}")?;
}
Ok(())
Expand Down
Loading