6

Almost every tutorial advises a code like this for a permission modifier.

modifier isAdmin() {
    if(msg.sender != owner) {
        throw;
    }
    _
}

But wouldn't it be simpler to use it without throw statement (AFAIU it would also save user's gas):

modifier isAdmin() {
    if(msg.sender == owner) _
}

As it is described by ConsenSys in Ethereum Contract Security Techniques and Tips but only for these modifiers:

modifier stopInEmergency { if (!stopped) _ }
modifier onlyInEmergency { if (stopped) _ }

1 Answer 1

6

If you send some ether in the transaction, it gets returned to the sender when there is a throw (see this). In your example, if you sent some funds (in msg.value), and the modifier condition is not met, your money is gone, but the code after the modifier is not run (and if the function was supposed to return something, it would return only the default values for parameters, so you might not even know something bad has happened).

You are correct about the gas, in case there is a throw, all gas is consumed.

2
  • 2
    throw is like ROLLBACK in SQL. Commented Aug 31, 2016 at 12:42
  • Thank you! I guess the Emergency modifiers mentioned in the question are better to be used with throw as well.
    – takeshi
    Commented Sep 2, 2016 at 12:29

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.