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) _ }
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.

| improve this answer | |
  • 2
    throw is like ROLLBACK in SQL. – Xavier Leprêtre B9lab Aug 31 '16 at 12:42
  • Thank you! I guess the Emergency modifiers mentioned in the question are better to be used with throw as well. – takeshi Sep 2 '16 at 12:29

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.