0

Two modifiers on a contract throw an "Exception during execution. (invalid opcode)." error when not being satisfied as expected. Both of them are calling an event when the condition they check is not met. So I guess the exception comes from calling the event on the modifier. Is that correct?

The event log is this one:

event Error(string error);

And the modifiers are the following:

modifier onlyOwner() {
    if (msg.sender != owner) {
    Error('Mortal: onlyOwner function called by user that is not owner'); throw;
    }
    _;
}

modifier onlyIssuer() {
    if (!issuers[phoneNumbers[msg.sender]]) {
    Error('Mobile: onlyIssuer function called by user that is not an authorized issuer'); throw;
    }
    _;
}

The full source code of the contract is available at https://gist.github.com/computerphysicslab/f362383f9d3fed26becba48b934bbcfc

So, if modifiers cannot call events, is not possible to log somehow a function that fails because its modifiers weren't met?

Thanks!

1

You can use events in modifiers. In you example you have a throw after calling the event. throw is also rewinding events, so you do not see events when you throw. Simply remove that and all works well.

Here a complete minimal example:

contract c {
    address owner;

    event Error(string error);

    modifier onlyOwner() {
        if (msg.sender != owner) {
            Error('Mortal: onlyOwner function called by user that is not owner');
        }
        _;
    }

    function c() {
        // constructor sets owner
        owner = msg.sender;
    }

    function f() onlyOwner {

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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