2

I am mostly interested in the following aspects:

  1. does the entire stack get unwind?
  2. if the entire stack gets unwind does this lead to all the changes made to the state by the contract get reverted?
  3. do events get reverted? I imagine that event are stored in the (global) state and reverting the state would also remove the events.

1 Answer 1

1

It depends how the call was made.

// Interface to contract A
contract A { 
    function foo() public; 
}

contract B {
    uint public status = 0;
    event logNewStatus(uint status);

    function bar(address a) public {
        status = 1;
        logNewStatus(status);

        // Call using A's interface
        A contractA = A(a);
        contractA.foo();

        status = 2;
        logNewStatus(status);
    }
}

If we use the interface to call functions from contract A, and foo() fails it will undo all operations, status will remain 0, and none of the events will be recorded in the blockchain.

We can use the low level function call to have more flexibility

function bar(address a) public {
    status = 1;
    logNewStatus(status);

    // Call using low level function
    // It returns true when call have succeeded and false otherwise
    bool result = a.call(bytes4(keccak256("foo()"));
    if (result) {
        status = 2;
        logNewStatus(status);
    } else {
        status = 3;
        logNewStatus(status);
    }
}

Using call it will return true when it have succeeded, and false otherwise. So you can decide to revert changes, or to continue.

In the example there's no revert, so if the transaction fails it will set status to 3, and generate an event with the new status.

5
  • Does the failure cause the gas fee to be used?
    – LeanMan
    Commented Aug 22, 2021 at 2:32
  • @LeanMan Yes, when executing a transaction gas usage has to be paid.
    – Ismael
    Commented Aug 22, 2021 at 4:05
  • a transaction or a function call? I just want to be a bit precise here what gas is used for.
    – LeanMan
    Commented Aug 23, 2021 at 1:37
  • 1
    @LeanMan Read this question ethereum.stackexchange.com/q/765 for the difference between transaction and call. Transaction always pays gas usage even when executing a view function. If you have more doubts it is better to create a question by clicking on Ask Question.
    – Ismael
    Commented Aug 23, 2021 at 4:27
  • That was an awesome share, loads of information in that question. Thanks!
    – LeanMan
    Commented Aug 23, 2021 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.