0

I am not familiar with Solidity inheritance.

Will there be multiple _owner variables defined when the Core is inheriting multiple parents that are both inheriting OwnableUpgradeable?

Which one will be the inheritance graph?

  1. Core > Parent A > OwnableUpgradeable > Parent B > OwnableUpgradeable
  2. Core > Parent A > Parent B > OwnableUpgradeable

How this inheritance affect the code size ?

Any best practice I should follow such that I can separate fn1 and fn2 into different Solidity files for Separation of Concern ?

Code:

contract ParentA is OwnableUpgradeable  {
    function __ParentA_init() internal onlyInitializing {
        __Ownable_init();
        __ParentA_init_unchained();
    }

    function __ParentA_init_unchained() internal onlyInitializing {}

    function fn1() public onlyOwner {}
}


contract ParentB is OwnableUpgradeable  {
    function __ParentB_init() internal onlyInitializing {
        __Ownable_init();
        __ParentB_init_unchained();
    }

    function __ParentB_init_unchained() internal onlyInitializing {}

    function fn2() public onlyOwner {}
}

contract Core is initializable, ParentA, ParentB  {
    function initialize() public initializer {
        __ParentA_init();
        __ParentB_init();
    }
}

1 Answer 1

2

The example has too many issues to resolve. Many have nothing to do with the question.

Here's an example that focuses on inheritance.

// SPDX-License-Identifier: UNLICENSED

pragma solidity 0.8.14;

contract CommonAncestor {
    uint public foo;
}

contract ParentA is CommonAncestor {
    function setFoo(uint val) public virtual {
        foo = val;
    }

    function getFoo() public view virtual returns(uint) {
        return foo;
    }
}

contract ParentB is CommonAncestor {
    function setFoo(uint val) public virtual {
        foo = val;
    }

    function getFoo() public view virtual returns(uint) {
        return foo;
    }    
}

contract Descendant is ParentA, ParentB {
    function setFoo(uint val) public override(ParentA, ParentB) {
        foo = val;
    }

    function getFoo() public view override(ParentA, ParentB) returns(uint) {
        return foo;
    }

    function ASetFoo(uint val) public {
        ParentA.setFoo(val);
    }    

    function BSetFoo(uint val) public {
        ParentB.setFoo(val);
    }

    function AGetFoo() public view returns(uint) {
        return ParentA.getFoo();
    }

    function BGetFoo() public view returns(uint) {
        return ParentB.getFoo();
    }
}

Notice virtual and override. virtual indicates that the function can be overridden, and override replaces the code in the parent. When two or more inherited contracts contain the same functions, they are specified explicitly to for readability/comprehension and to help catch developer error. In other words, the developer guides the compiler to resolve the resolution of the duplication.

Although the variable foo exists in the original contract and two "parents" that inherit it and and contract that inherits the two parents, it occupies the same storage slot in all cases, i.e. there is only one instance of this variable.

You can explore that explanation with Remix and see that when Descendent is deployed, there is agreement about the value of foo from every angle.

Hope it helps.

2
  • Thank you for your help. I understand more about the inheritance now. The reason why I included the upgradeable contracts is because, I want to know whether upgradeable contract share the same idea on inheritance. As upgradeable contract replace constructor with initialize, should I just put the override after the initialize function ?
    – ykn121
    Jul 24 at 3:02
  • They add another layer of complexity. Short answer is yes. Better answer: Constructor-like concerns migrate to initializers so they can be executed in the context of the proxy instead of the context of the implementation. The rules of inheritance are indifferent to that objective, so resolve what's overriding what and the storage as you would for any other function. Jul 24 at 17:40

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.