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.
rosul
  • 353
  • 1
  • 14

1 Answers1

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.

Ismael
  • 30,570
  • 21
  • 53
  • 96
  • Does the failure cause the gas fee to be used? – LeanMan Aug 22 '21 at 02:32
  • @LeanMan Yes, when executing a transaction gas usage has to be paid. – Ismael Aug 22 '21 at 04:05
  • a transaction or a function call? I just want to be a bit precise here what gas is used for. – LeanMan Aug 23 '21 at 01:37
  • 1
    @LeanMan Read this question https://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 Aug 23 '21 at 04:27
  • That was an awesome share, loads of information in that question. Thanks! – LeanMan Aug 23 '21 at 04:37