Hi there, I can see how Initializable works, even without the second bool variable _initializing
.
I am just wondering why two bool variables _initializing
and _initialized
are used and what’s the functionality of _initializing
. Thanks.
Hi there, I can see how Initializable works, even without the second bool variable _initializing
.
I am just wondering why two bool variables _initializing
and _initialized
are used and what’s the functionality of _initializing
. Thanks.
One is to figure out if you are currently initializing (the state of the contract) and the other is to figure out if it’s initialized already or not. It is because technically you could enter into a situation where you try to initialize twice, I think at least. I don’t think you would ever intentionally do this, but it might be done unintentionally by deploying twice?
modifier initializer() {
require(_initializing || !_initialized, "Initializable: contract is already initialized");
bool isTopLevelCall = !_initializing;
if (isTopLevelCall) {
_initializing = true;
_initialized = true;
}
_;
if (isTopLevelCall) {
_initializing = false;
}
}
This modifier is called by your contract when deploying so you might do some code before or after it that checks these variables.
_initializing
is necessary when you have multiple inheritance:
contract A {
function _init_A() initializer internal {}
}
contract B {
function _init_B() initializer internal {}
}
contract C is A, B {
function initialize() initializer public {
_init_A();
_init_B();
}
}
Got it. Very clear in illustrating the necessity of having the _initializing
variable. Thanks.
You are right. It can be called multiple times given this _;
in the modifier.