Skip to content

[Bug]: Reverts in Nested Contract Calls Should Be Handled Gracefully #116

@0xNeshi

Description

@0xNeshi

What happened?

Currently the panic unwinds. This causes problems in unit tests for SafeErc20, copy/paste the following code into contracts/src/token/erc20/utils/safe_erc20.rs > tests:

    // Mock ERC20-like contract that panics on `allowance` calls
    #[storage]
    struct PanickingAllowanceToken;

    unsafe impl TopLevelStorage for PanickingAllowanceToken {}

    #[public]
    impl PanickingAllowanceToken {
        // External signature matches IERC20.allowance(owner, spender) ->
        // uint256 Panicking causes a revert so the RawCall in
        // SafeErc20::allowance fails.
        fn allowance(&self, _owner: Address, _spender: Address) -> U256 {
            panic!("revert");
        }
    }

    // When the token's allowance call reverts, SafeErc20::allowance should map
    // it to Error::SafeErc20FailedOperation and bubble up through callers
    // like safe_increase_allowance.
    #[motsu::test]
    fn safe_increase_allowance_reverts_on_allowance_call_panic(
        contract: Contract<SafeErc20Example>,
        bad_token: Contract<PanickingAllowanceToken>,
        alice: Address,
    ) {
        let token = bad_token.address();
        let err = contract
            .sender(alice)
            .safe_increase_allowance(token, alice, U256::from(1))
            .unwrap_err();
        assert!(
            matches!(err, Error::SafeErc20FailedOperation(SafeErc20FailedOperation { token }) if token == bad_token.address())
        );
    }

Running this tests results in:

thread 'token::erc20::utils::safe_erc20::tests::safe_increase_allowance_reverts_on_allowance_call_error' panicked at library/core/src/panicking.rs:225:5:
panic in a function that cannot unwind
stack backtrace:
   0:     0x62314773ef82 - std::backtrace_rs::backtrace::libunwind::trace::hf0b950c5fb8c6b8c
                               at /rustc/5d707b07e42766c080c5012869c9988a18dcbb83/library/std/src/../../backtrace/src/backtrace/libunwind.rs:117:9
   1:     0x62314773ef82 - std::backtrace_rs::backtrace::trace_unsynchronized::hbababaa4234eb23f
   ...

platform

  • linux
  • windows
  • macos

Expected behavior

The test case above should successfully pass without unwinding panics.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions