Update initializer modifier to prevent reentrancy during initialization (#3006)
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
(cherry picked from commit 08840b9f8c)
This commit is contained in:
@ -22,6 +22,16 @@ contract SampleHuman is Initializable {
|
||||
bool public isHuman;
|
||||
|
||||
function initialize() public initializer {
|
||||
__SampleHuman_init();
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleHuman_init() internal onlyInitializing {
|
||||
__SampleHuman_init_unchained();
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleHuman_init_unchained() internal onlyInitializing {
|
||||
isHuman = true;
|
||||
}
|
||||
}
|
||||
@ -33,7 +43,17 @@ contract SampleMother is Initializable, SampleHuman {
|
||||
uint256 public mother;
|
||||
|
||||
function initialize(uint256 value) public virtual initializer {
|
||||
SampleHuman.initialize();
|
||||
__SampleMother_init(value);
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleMother_init(uint256 value) internal onlyInitializing {
|
||||
__SampleHuman_init();
|
||||
__SampleMother_init_unchained(value);
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleMother_init_unchained(uint256 value) internal onlyInitializing {
|
||||
mother = value;
|
||||
}
|
||||
}
|
||||
@ -45,7 +65,17 @@ contract SampleGramps is Initializable, SampleHuman {
|
||||
string public gramps;
|
||||
|
||||
function initialize(string memory value) public virtual initializer {
|
||||
SampleHuman.initialize();
|
||||
__SampleGramps_init(value);
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleGramps_init(string memory value) internal onlyInitializing {
|
||||
__SampleHuman_init();
|
||||
__SampleGramps_init_unchained(value);
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleGramps_init_unchained(string memory value) internal onlyInitializing {
|
||||
gramps = value;
|
||||
}
|
||||
}
|
||||
@ -57,7 +87,17 @@ contract SampleFather is Initializable, SampleGramps {
|
||||
uint256 public father;
|
||||
|
||||
function initialize(string memory _gramps, uint256 _father) public initializer {
|
||||
SampleGramps.initialize(_gramps);
|
||||
__SampleFather_init(_gramps, _father);
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleFather_init(string memory _gramps, uint256 _father) internal onlyInitializing {
|
||||
__SampleGramps_init(_gramps);
|
||||
__SampleFather_init_unchained(_father);
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleFather_init_unchained(uint256 _father) internal onlyInitializing {
|
||||
father = _father;
|
||||
}
|
||||
}
|
||||
@ -74,8 +114,23 @@ contract SampleChild is Initializable, SampleMother, SampleFather {
|
||||
uint256 _father,
|
||||
uint256 _child
|
||||
) public initializer {
|
||||
SampleMother.initialize(_mother);
|
||||
SampleFather.initialize(_gramps, _father);
|
||||
__SampleChild_init(_mother, _gramps, _father, _child);
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleChild_init(
|
||||
uint256 _mother,
|
||||
string memory _gramps,
|
||||
uint256 _father,
|
||||
uint256 _child
|
||||
) internal onlyInitializing {
|
||||
__SampleMother_init(_mother);
|
||||
__SampleFather_init(_gramps, _father);
|
||||
__SampleChild_init_unchained(_child);
|
||||
}
|
||||
|
||||
// solhint-disable-next-line func-name-mixedcase
|
||||
function __SampleChild_init_unchained(uint256 _child) internal onlyInitializing {
|
||||
child = _child;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user