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

[Bug] alloy_sol_types::decode_revert_reason panics when called #382

Closed
ufoscout opened this issue Oct 24, 2023 · 2 comments · Fixed by #383
Closed

[Bug] alloy_sol_types::decode_revert_reason panics when called #382

ufoscout opened this issue Oct 24, 2023 · 2 comments · Fixed by #383
Labels
bug Something isn't working

Comments

@ufoscout
Copy link

ufoscout commented Oct 24, 2023

Component

sol-types

What version of Alloy are you on?

├── alloy-sol-types v0.4.2 │ ├── alloy-primitives v0.4.2 │ │ ├── alloy-rlp v0.3.3 │ │ │ ├── alloy-rlp-derive v0.3.3 (proc-macro) │ │ │ ├── alloy-rlp v0.3.3 () │ ├── alloy-sol-macro v0.4.2 (proc-macro) │ ├── alloy-primitives v0.4.2 () │ │ ├── alloy-primitives v0.4.2 () │ │ ├── alloy-rlp v0.3.3 ()

Operating System

Linux

Describe the bug

Given this simple solidity contract:

// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.17;
contract TestRevertMessage {

    error SenderAddressError(address sender);

    function revertWithSenderAddressError() public view {
        revert SenderAddressError(msg.sender);
    }
}

When the revertWithSenderAddressError() function is called, the transaction is correctly reverted. Nevertheless, trying to decode the revert message through decode_revert_reason causes a panic:

thread 'mycrate::test_decode_revert_reason' panicked at /home/myhome/.cargo/registry/src/index.crates.io-6f17d22bba15001f/alloy-sol-types-0.4.2/src/types/interface.rs:89:25:
attempt to add with overflow
stack backtrace:
   0: rust_begin_unwind
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/panicking.rs:595:5
   1: core::panicking::panic_fmt
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/panicking.rs:67:14
   2: core::panicking::panic
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/panicking.rs:117:5
   3: alloy_sol_types::types::error::decode_revert_reason
   4: mycrate::test_decode_revert_reason

Here is a small reproducer:

#[test]
fn test_decode_revert_reason() {

    // This is the hex-encoded output returned by RETH when the revertWithSenderAddressError() function is called.
    // RETH returns an `ExecutionResult::Revert { output, gas_used }` but `decode_revert_reason` panics when trying to
    // decode the output
    let revert_output_bytes_hex = "8758782b000000000000000000000000a48388222c7ee7daefde5d0b9c99319995c4a990";
    let revert_output_bytes = hex::decode(revert_output_bytes_hex).unwrap();

    let revert_message = alloy_sol_types::decode_revert_reason(revert_output_bytes.as_ref());

    println!("revert_message: {}", revert_message.unwrap_or_default())
}
@ufoscout
Copy link
Author

@DaniPopes thanks for fixing it.
I noticed that with your fix the decode_revert_reason returns None while hardhat decodes that revert message to reverted with custom error 'SenderAddressResult("0x6D91e68e0adA18325a12Bd647A13342c141D0648")', is there a way to achieve a similar result with alloy?

@DaniPopes
Copy link
Member

Hardhat is not comparable to Ethers. Hardhat will compile all your contracts and keep their ABI in memory to use to decode errors and such. See the test I added on how to decode custom errors

// https://github.com/alloy-rs/core/issues/382
#[test]
fn decode_solidity_no_interface() {
sol! {
interface C {
#[derive(Debug, PartialEq)]
error SenderAddressError(address);
}
}
let data = hex!("8758782b000000000000000000000000a48388222c7ee7daefde5d0b9c99319995c4a990");
assert_eq!(decode_revert_reason(&data), None);
let C::CErrors::SenderAddressError(decoded) = C::CErrors::abi_decode(&data, true).unwrap();
assert_eq!(
decoded,
C::SenderAddressError {
_0: address!("a48388222c7ee7daefde5d0b9c99319995c4a990")
}
);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants