The Initializable.sol contract includes a modifier initializer()
that shows:
modifier initializer() {
require(initializing || isConstructor() || !initialized, "Contract instance has already been initialized");
bool wasInitializing = initializing;
initializing = true;
initialized = true;
_;
initializing = wasInitializing;
}
- Why is the point of introducing
initializing
?
- Why is
initialized
placed before the body of the inserted code? Shouldn’t it be only set to true
at the very last step?
initializing
is necessary because contracts that have multiple inheritance will do something like this:
contract A is Initializable, B, C {
function initializer() public initializer {
B_initialize();
C_initialize();
}
}
If the modifier just sets initialized = true
and has require(!initialized)
at the top, it doesn’t support this scenario.
The other part of your question doesn’t really make a difference! I don’t remember if there is a particular reason why it was placed there.
Thanks for your reply @frangio! So for the above case, would Contract A’s initializer()
function be reverted as C_initialize()
will not be able to get through the require(!initialized)
statement?
And also would the only way for this initializer()
function to execute be to only have a single aParentContract_initialize()
function within it?
Yes exactly what you said.
1 Like
Cool! Thanks so much for your help.