1

Function revert() is supposed to stop contract execution and rollback every change on blockchain except events reported. That's exactly what I need, but it seems to me it is not working as expected. In the following example, using remix.ethereum.org IDE and JS VM chain:

contract test {
    function a() public {
        L(1);
        revert();
    }

    event L(uint256 n);
}

When a() function is called on JS VM the following error appears:

... test.a errored: VM error: revert.
revert  The transaction has been reverted to the initial state.

And no event has been recorded.

Have tested assert(false), require(false) and throw(), same result.

Any idea how to stop the contract without losing event records? Thx!

1

1 Answer 1

4

Revert, require, assert; they will all undo all changes to state, including events.

If you wanted to halt execution of the function while still being able to log an event you would have to use if-else with a return.

For example:

contract test {
    function a(uint someNumber) public returns(bool) {
        if(someNumber > 5){
          L(someNumber);
          return false; // if(someNumber is > 5 it will "halt" execution"
        }

        someNumber ++;
        // continue execution and do something with the number
        L(someNumber);
        return true;
    }

    event L(uint256 n);
}

Your Answer

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

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