Am I doing multiple function modifiers correctly?

ytriusytrius Member Posts: 26
I want a function to both be only runnable by owner, and also use some common code at the end:

function contractConstructor(){
owner = msg.sender;
}

modifier onlyOwner{
if(msg.sender != owner) throw;
-
}

modifier extraStuff{
-
a++; // whatever, some extra code
}

function theFunction() onlyOwner extraStuff{
//some code
}

Will theFunction be only runnable by owner and also increment a after the main code? Also, what happens if the order of the modifiers on theFunction are reversed?

Thanks.

Comments

  • o0ragman0oo0ragman0o Member, Moderator Posts: 1,291 mod
    Chaining is fine. Reversing it in this instance will increment 'a' but still throw afterwards if it wasn't called by the owner. If you need to privilege an owner action in code that is allowed to be run by others also, just wrap it in an if (msg.sender == owner){}, which could be in the modifier also.
  • ethereumfanaticethereumfanatic Member Posts: 27
    I wish I could understand this lol
  • ytriusytrius Member Posts: 26

    Chaining is fine. Reversing it in this instance will increment 'a' but still throw afterwards if it wasn't called by the owner. If you need to privilege an owner action in code that is allowed to be run by others also, just wrap it in an if (msg.sender == owner){}, which could be in the modifier also.

    Okay I see, thanks. I have another question. What is the purpose of function modifiers in the first place? Why not just call another function:

    function onlyOwner(){
    if(msg.sender != owner) throw;
    }

    function theFunction(){
    onlyOwner();
    // other code
    }

    ?
  • o0ragman0oo0ragman0o Member, Moderator Posts: 1,291 mod
    @plorph, My understanding is that it inlines the bytecode, like a macro, and so there isn't the gas overhead of a function call and return.
Sign In or Register to comment.