aave-protocol-v2/contracts/dependencies/openzeppelin/upgradeability/Initializable.sol

67 lines
2.0 KiB
Solidity
Raw Normal View History

// SPDX-License-Identifier: agpl-3.0
pragma solidity >=0.4.24 <0.7.0;
/**
* @title Initializable
*
* @dev Helper contract to support initializer functions. To use it, replace
* the constructor with a function that has the `initializer` modifier.
* WARNING: Unlike constructors, initializer functions must be manually
* invoked. This applies both to deploying an Initializable contract, as well
* as extending an Initializable contract via inheritance.
* WARNING: When used with inheritance, manual care must be taken to not invoke
* a parent initializer twice, or ensure that all initializers are idempotent,
* because this is not dealt with automatically as with constructors.
*/
contract Initializable {
2020-07-13 08:54:08 +00:00
/**
* @dev Indicates that the contract has been initialized.
*/
2020-07-13 08:54:08 +00:00
bool private initialized;
2020-07-13 08:54:08 +00:00
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
2020-07-13 08:54:08 +00:00
bool private initializing;
2020-07-13 08:54:08 +00:00
/**
* @dev Modifier to use in the initializer function of a contract.
*/
2020-07-13 08:54:08 +00:00
modifier initializer() {
require(
initializing || isConstructor() || !initialized,
'Contract instance has already been initialized'
);
2020-07-13 08:54:08 +00:00
bool isTopLevelCall = !initializing;
if (isTopLevelCall) {
initializing = true;
initialized = true;
}
2020-07-13 08:54:08 +00:00
_;
2020-07-13 08:54:08 +00:00
if (isTopLevelCall) {
initializing = false;
}
2020-07-13 08:54:08 +00:00
}
2020-07-13 08:54:08 +00:00
/// @dev Returns true if and only if the function is running in the constructor
function isConstructor() private view returns (bool) {
// extcodesize checks the size of the code stored in an address, and
// address returns the current address. Since the code is still not
// deployed when running a constructor, any checks on its code size will
// yield zero, making it an effective way to detect if a contract is
// under construction or not.
uint256 cs;
//solium-disable-next-line
assembly {
cs := extcodesize(address())
}
2020-07-13 08:54:08 +00:00
return cs == 0;
}
2020-07-13 08:54:08 +00:00
// Reserved storage space to allow for layout changes in the future.
uint256[50] private ______gap;
}