2

In this little sample I try to play with modifiers, master1 creates the contrat and pass the master2 address as a parameter in the constructor.

contract testOnlyBy {
    address master1;
    address master2;

    modifier onlyByMaster1 { if (msg.sender != master1) throw; _ }
    modifier onlyByMaster2 { if (msg.sender != master2) throw; _ }
    modifier onlyByMaster1or2 { if (msg.sender != master1 || msg.sender != master2) throw; _ }

    // "0x6eec66e0133feb5e432584515880afad4be047f0"

    function testOnlyBy(address _master2) {
        master1 = msg.sender;
        master2 = _master2;
    }
    function get() external constant returns(address ret1, address ret2) {
        if (msg.sender == master1 || msg.sender == master2){
             return (master1, master2);
        }
    }
    function getonlyByMaster1() onlyByMaster1 external constant returns(address ret1, address ret2) {
        return (master1, master2);
    }
    function getonlyByMaster2() onlyByMaster2 external constant returns(address ret1, address ret2) {
         return (master1, master2);
    }

    function getonlyByMaster1or2() onlyByMaster1or2 external constant returns(address ret1, address ret2) {
         return (master1, master2);           
    }
}

the get function works as intended, but the idea is to use modifiers instead.

I don't get why getonlyByMaster1or2 returns 0x0, I though the two functions would be equivalent. Am I missing something ?

enter image description here

2

Try changing

modifier onlyByMaster1or2 { 
  if (msg.sender != master1 || msg.sender != master2) throw;
  _ 
}

to

modifier onlyByMaster1or2 { 
  if (msg.sender != master1 && msg.sender != master2) throw;
  _ 
}

The _ part of the modifier will be run if the following expression is false:

(msg.sender != master1 && msg.sender != master2)

This is equivalent to the following expression being true:

(msg.sender == master1 || msg.sender == master2)

which is the logic in get().

| improve this answer | |
  • omfg, logic 101....thanks, was banging my head around – euri10 May 12 '16 at 14:58

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.