5

For example, we have a contract:

contract A {

    modifier checkCaller() {
        require(msg.sender == 0x123);
        _;
    }

    function doSmth() checkCaller {}
}

We make child contract and override function:

contract B is A {
    function doSmth() {
        super.doSmth();
        // ... do more
    }
}

Do I need to explicitly use modifier checkCaller in the child contract B? As I understand super.doSmth() will throw properly. Correct?

9

Yes, you are correct.


Something I noticed which is worth keeping an eye on is that both modifiers of 'parent' contracts as well as any functions are overwritten by inheritance even when using 'super'.

For example, calling test(0) on B passes and returns 10 (not 5 as one may be led to believe), but fails on contract A.

pragma solidity ^0.4.15;

contract A {
    modifier only (uint test) {
        require(test > 5);
        _;
    }
    function test(uint test) public only(test) returns(uint) {
        return addSome(test);
    }
    function addSome(uint number) internal returns(uint) {
        return number + 5;
    }
}

contract B is A {
    modifier only (uint test) {
        require(test < 10);
        _;
    }
    function test(uint test) public only(test) returns(uint) {
        return super.test(test);
    }
    function addSome(uint number) internal returns(uint) {
        return number + 10;
    }
}
  • This example means modifiers are not overridable as methods, they are resolved in compile time. Parent behavior can’t be changed by only modifier overriding. – k06a Nov 9 '17 at 23:08
  • @k06a no. Actually, the sample means both modifiers and methods are not overridable. Both are resolved and overwritten at compile time. – Muhammad Altabba Mar 10 '18 at 8:16

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.