2020-05-29 16:45:37 +00:00
|
|
|
// SPDX-License-Identifier: agpl-3.0
|
2020-11-20 10:41:58 +00:00
|
|
|
pragma solidity 0.6.12;
|
2020-05-29 16:45:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @title VersionedInitializable
|
|
|
|
*
|
2020-11-25 17:33:49 +00:00
|
|
|
* @dev Helper contract to implement initializer functions. To use it, replace
|
2020-05-29 16:45:37 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @author Aave, inspired by the OpenZeppelin Initializable contract
|
|
|
|
*/
|
|
|
|
abstract contract VersionedInitializable {
|
2020-07-13 08:54:08 +00:00
|
|
|
/**
|
2020-05-29 16:45:37 +00:00
|
|
|
* @dev Indicates that the contract has been initialized.
|
|
|
|
*/
|
2020-07-13 08:54:08 +00:00
|
|
|
uint256 private lastInitializedRevision = 0;
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
/**
|
2020-05-29 16:45:37 +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-05-29 16:45:37 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
/**
|
2020-05-29 16:45:37 +00:00
|
|
|
* @dev Modifier to use in the initializer function of a contract.
|
|
|
|
*/
|
2020-07-13 08:54:08 +00:00
|
|
|
modifier initializer() {
|
|
|
|
uint256 revision = getRevision();
|
|
|
|
require(
|
|
|
|
initializing || isConstructor() || revision > lastInitializedRevision,
|
|
|
|
'Contract instance has already been initialized'
|
|
|
|
);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
bool isTopLevelCall = !initializing;
|
|
|
|
if (isTopLevelCall) {
|
|
|
|
initializing = true;
|
|
|
|
lastInitializedRevision = revision;
|
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
_;
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
if (isTopLevelCall) {
|
|
|
|
initializing = false;
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|
2020-07-13 08:54:08 +00:00
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-11-25 17:33:49 +00:00
|
|
|
/**
|
2021-01-27 14:43:34 +00:00
|
|
|
* @dev returns the revision number of the contract
|
|
|
|
* Needs to be defined in the inherited class as a constant.
|
|
|
|
**/
|
2020-11-23 10:28:57 +00:00
|
|
|
function getRevision() internal pure virtual returns (uint256);
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-11-25 17:33:49 +00:00
|
|
|
/**
|
2021-01-27 14:43:34 +00:00
|
|
|
* @dev Returns true if and only if the function is running in the constructor
|
|
|
|
**/
|
2020-07-13 08:54:08 +00:00
|
|
|
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-05-29 16:45:37 +00:00
|
|
|
}
|
2020-07-13 08:54:08 +00:00
|
|
|
return cs == 0;
|
|
|
|
}
|
2020-05-29 16:45:37 +00:00
|
|
|
|
2020-07-13 08:54:08 +00:00
|
|
|
// Reserved storage space to allow for layout changes in the future.
|
|
|
|
uint256[50] private ______gap;
|
2020-05-29 16:45:37 +00:00
|
|
|
}
|